LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 09-11-2007, 10:09 AM   #1
xtremeclones
Member
 
Registered: Jan 2006
Posts: 70

Rep: Reputation: 15
Need help in with creating a script.


Hello all.

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.

Can anyone help me creating this script.

thanks in advanced!!!
 
Old 09-11-2007, 01:02 PM   #2
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
Hello xtremeclones,

For your script you can try the following:
Code:
#/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.

Maybe some one else could help for that part

PS: Let me know if that doesn't work.

Best regards,
Angel.

Last edited by angel115; 09-11-2007 at 01:07 PM.
 
Old 09-11-2007, 01:24 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by xtremeclones View Post
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!
 
Old 09-12-2007, 09:53 AM   #4
xtremeclones
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Thank you so much guys, i will give it a try today and post the results.

again thanks for the help!!
 
Old 09-12-2007, 01:57 PM   #5
xtremeclones
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
ok this is what i have:

#/bin/bash

# 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

this is all it says in the Body.

What am i doing wrong??

thanks for the help
 
Old 09-13-2007, 02:24 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
[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!
 
Old 09-13-2007, 05:43 AM   #7
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
So here is a script which should work

Code:
#/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

Last edited by angel115; 09-13-2007 at 05:44 AM.
 
Old 09-13-2007, 11:20 PM   #8
xtremeclones
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Thank you for all the responses.

This is what i have:

#/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


I get this error:

./channels.sh: line 9: [: too many arguments

has anyone else tries this on their system?

thanks!!!
 
Old 09-17-2007, 03:39 PM   #9
xtremeclones
Member
 
Registered: Jan 2006
Posts: 70

Original Poster
Rep: Reputation: 15
Bump! Anyone??
 
Old 09-17-2007, 04:24 PM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The error message is quite clear
Code:
./channels.sh: line 9: [: too many arguments
too many arguments in expression
Code:
if [ ${ActiveCall} -ge 200 ];then
I never used asterisk (nor I know what it is) so I cannot assist you for this part, but what is the output from
Code:
asterisk -rx 'show channels' | tail -1 | sed -e 's/[a-z]//g'
? 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.
 
Old 10-01-2007, 05:34 AM   #11
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 542

Rep: Reputation: 79
Hello xtremeclones,

You probably have some space at the end which might create some issues.
Can you try that and tell me what does it give you?
Code:
asterisk -rx 'show channels' | tail -1 | sed -e 's/[a-z]//g' | sed -e 's/\ *$//g'
Regards,
Angel.

Last edited by angel115; 10-01-2007 at 05:39 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help in creating an .sh script wild_oscar Programming 5 06-04-2007 06:17 AM
help creating a script... minm Linux - Newbie 5 01-09-2005 12:27 AM
Creating a script thomas289 Linux - Networking 1 12-17-2004 11:59 AM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM
Creating a Script? BajaNick Linux - Software 1 07-26-2003 07:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration