Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need some help with a script and im not yet too good with bash.
i need a script which would execute hourly on the Cron Job.
the command would be: asterisk -rx 'show channels'
which would then give an output of:
2 active channels
1 active call
i need the script to grep the x amount of calls and when there are 200 or more calls to send me an email saying 'you've got 200 or more call in system x.x.x.x.
#/bin/bash
# the following line will grep the number of "active call"
ActiveCall=$( asterik -rx 'show channels' | tail -1 | sed -e 's/[a-z]//g' )
# "-ge" meen greater or equal. Use "-gt" for strictly greater or type "man test" for more details
If [ ${ActiveCall} -ge 200 ]; then
*******************
send your email here (I didn't have time to check how to do it)
*******************
done
else
*******************
do some thing else
*******************
fi
I didn't try it but it should work. I didn't have time to check how to send notification email.
I need some help with a script and im not yet too good with bash.
Can anyone help me creating this script.
You can at least show what you've tried so far! All of us were bad at the very first attempts in shell programming! Anyway, for the notification part you can use sendmail, doing some tests to be sure there are no limitations from the mail server. In a script you can put something like:
Code:
/usr/sbin/sendmail -f "sender@domain.it" "recipient@domain.it" << EOM
From: Me <sender@domain.it>
To: Me Too <recipient@domain.it>
Subject: number of calls
(date "+%A %e %b %Y %T")
The number of active calls exceed 200. Actual number is $ActiveCall
EOM
The construct
Code:
<< EOM
EOM
is a so-called "here document", look at man bash for details or a bash programming guide. The important thing is that the ending delimiter (EOM in this case) must be at the beginning of the line, otherwise it is unnoticed. In the here document, that is in the body of the mail you can put command substitutions, shell variables and so on: they all will be expanded/executed, so you will have a flexible and useful notification tool!
# the following line will grep the number of "active call"
ActiveCall=$( asterisk -rx 'show channels' | tail -1 | sed -e 's/[a-z]//g' )
# "-ge" meen greater or equal. Use "-gt" for strictly greater or type "man test" for more details
If [ ${ActiveCall} -ge 200 ];Then
/usr/sbin/sendmail -f "xtremeclones@hotmail.com" "xtremeclones@hotmail.com" << EOM
From: Me <xtremeclones@hotmail.com>
Subject: number of calls
(date "+%A %e %b %Y %T")
The number of active calls exceed 200. Actual number is $ActiveCall
EOM
done
fi
It gives me the following:
[root@1]# ./channels.sh
./channels.sh: line 7: If: command not found
./channels.sh: line 7: Then: command not found
./channels.sh: line 16: syntax error near unexpected token `done'
./channels.sh: line 16: ` done'
I do however get an email saying:
(date "+%A %e %b %Y %T")
The number of active calls exceed 200. Actual number is V 13
[root@1]# ./channels.sh
./channels.sh: line 7: If: command not found
./channels.sh: line 7: Then: command not found
./channels.sh: line 16: syntax error near unexpected token `done'
./channels.sh: line 16: ` done'
I did not check the syntax of code from previous posts, but here the errors are pretty clear. The correct syntax of an if/then control statement is
Code:
if <condition>
then
<commands>
fi
that is lower case. The "done" statement is out of place, or better it is not required at all, since it terminates a "for... do" or a "while... do" loop.
Quote:
I do however get an email saying:
(date "+%A %e %b %Y %T")
The number of active calls exceed 200. Actual number is V 13
I expected that the date command would have been executed. You can try the following syntax with back quotes
Code:
`date "+%A %e %b %Y %T"`
this works for me. For any other doubt about syntax I'd suggest to keep at hand a good bash programming guide (some of them are available at www.tldp.org) or simply check
Code:
man bash
the rest is a matter of experience. Anyway, feel free to ask!
#/bin/bash
# Grab the Date and time here and put it in the variable "mytime"
mytime=$( date "+%A %e %b %Y %T" )
# the following line will grep the number of "active call"
ActiveCall=$( asterisk -rx 'show channels' | tail -1 | sed -e 's/[a-z]//g' )
# "-ge" meen greater or equal. Use "-gt" for strictly greater or type "man test" for more details
if [ ${ActiveCall} -ge 200 ];then
/usr/sbin/sendmail -f "xtremeclones@hotmail.com" "xtremeclones@hotmail.com" << EOM
From: Me <xtremeclones@hotmail.com>
Subject: number of calls
${mytime}
The number of active calls exceed 200. Actual number is ${ActiveCall}
EOM
fi
#/bin/bash
# Grab the Date and time here and put it in the variable "mytime"
mytime=$( date "+%A %e %b %Y %T" )
# the following line will grep the number of "active call"
ActiveCall=$( asterisk -rx 'show channels' | tail -1 | sed -e 's/[a-z]//g' )
# "-ge" meen greater or equal. Use "-gt" for strictly greater or type "man test" for more details
if [ ${ActiveCall} -ge 200 ];then
/usr/sbin/sendmail -f "xtremeclones@hotmail.com" "xtremeclones@hotmail.com" << EOM
From: Me <xtremeclones@hotmail.com>
Subject: number of calls
${mytime}
The number of active calls exceed 200. Actual number is ${ActiveCall}
? I suspect it is not a single number, but a string with two or more fields. First you can try to echo the value of $ActiveCall before the if/then statement, second you can try to launch the script by means of
Code:
bash -x channels.sh
this will show the execution flow of the entire script and help in debugging.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.