0

I've been trying to get this to work on a few occasion but I seem to be having a bit of trouble.

asterisk -rx "core show calls" | grep "active" | cut -d' ' -f1

The asterisk command runs from /usr/sbin/asterisk Launching it like so, executes the asterisk executable and pipes the output, grep for active calls. I usually get an Integer value which I would like to store in a variable and keep working the script from there.

I'm having a rough time passing arguments to the command.

I'm basically trying to make that command into a SHELL SCRIPT and store the value.

Can you guys, point me towards the right direction ?

hayonj
  • 1,429
  • 3
  • 21
  • 28
  • Arguments to what command? What arguments are you trying to pass? What have you tried? What isn't working exactly? – Etan Reisner Mar 16 '15 at 14:54
  • I tried something like this : `#!/bin/sh echo -e "/usr/sbin/asterisk $1";` – hayonj Mar 16 '15 at 14:56
  • You tried that where? Called how? What about that didn't work? Where are you expecting the script when run by cron to get arguments from (assuming that's the question here)? – Etan Reisner Mar 16 '15 at 14:58
  • To be honnest I haven't even setup the cron job yet, I just want to be able to write a script that can pass arguments to asterisk. Doing that command above outputs a value, I want to use that value as variable and work from there. – hayonj Mar 16 '15 at 14:59
  • I'm entirely unclear at this point what your actual question is. Is it "how do I store the output of a command in a variable"? Is it "how do I pass arguments to a cron job"? Something else? – Etan Reisner Mar 16 '15 at 15:01
  • Fine, I'll modify the title and try to be a little more concise, using this command above (which uses the asterisk binary file I believe written in C ) I should pass argument to it like you see above : `-rx "core show calls" | grep "active" | cut -d' ' -f1` that command should execute and I'm trung to store a the value, so I can work on it. – hayonj Mar 16 '15 at 15:04
  • 1
    Only `-rx "core show calls"` are arguments to `asterisk` and you know how to pass them to it (your example command in the post is correct). That whole pipeline is valid (assuming that `active` is the keyword you need and that `cut` finds the correct field on each line. If the question is then how do you store that in a variable and use it in your calculation of count later that's a *wildly* different question then this started with. – Etan Reisner Mar 16 '15 at 15:11

3 Answers3

0

What about:

active=$(asterisk -rx "core show calls" | awk '$2=="active"{n=$1} END{printf("%d",n)}')

This uses awk's printf to make sure that if the active calls line is skipped in your output, your variable is assigned a 0 instead of being blank.

Or alternately, if your shell is bash, you could use this to avoid all the pipes:

while read active what _; do
  if [ "$what" = "active" ]; then
    break
  fi
done < <(asterisk -rx "core show calls")

At the end of this loop, if no "active calls" lines were seen, $active will be blank, so if you want, you could add:

active=${active:-0}

to assign a zero value if no active calls are reported.

(In my tests, 0 active calls does get reported with each run, so all this blank handling is just insurance.)

ghoti
  • 45,319
  • 8
  • 65
  • 104
  • I like this, but the value if no calls is ALWAYS 0. i'm thinking of setting up a cron job that will alert me if calls go over 10 for example. However I'm looking for something much more simple since this is one of my first bash scripts. – hayonj Mar 16 '15 at 15:30
  • Sounds like what you really want is a monitoring system. I use [Nagios](http://www.nagios.org/) which has [plugins for Asterisk](http://exchange.nagios.org/directory/Plugins/Telephony/Asterisk) but you might find [Zabbix](http://zabbix.com/) easier to manage. You might also find [Munin](http://munin-monitoring.org/) or some other [RRDtool](http://oss.oetiker.ch/rrdtool/)-backed system useful. Have a look at the [Asterisk monitoring page at voip-info.org](http://www.voip-info.org/wiki/view/Asterisk+monitoring). – ghoti Mar 16 '15 at 17:40
  • Thanks @ghoti I will into it, I haven't had luck configuring Nagios properly, the other one I never heard about. I did play around with bash scripting this morning and found a solution. I will post it. – hayonj Mar 16 '15 at 18:20
0

I found this example 7.2.2.2 , i had modified it to work around what I was trying to accomplish, So if I have more than let's say 5 calls running everytime the program runs, then alert me by email.

#!/bin/sh

rast=`asterisk -rx 'core show calls' | grep 'active' | cut -d' ' -f1`
maxvalue="5"

echo "Program started"

if [ "$rast" -ge "$maxvalue" ]; then
  echo "More than one calls processed" | mail -s 'Verify Calls from '`uname -n` hayonj@example.com
else
  echo "No active calls" | mail -s 'Crontab executed from '`uname -n` hayonj@example.com
fi

echo "Program Terminated"

saved the file under verify.sh I placed it where the binary for asterisk is located in /sbin/ and used chmod a+x verify.sh to set execute permissions to the script.

Afterwards, i simply set it up like so with a crontab, crontab -e and * * * * * /sbin/verify.sh

This little example, was my first bash script to automate my daily tasks.

hayonj
  • 1,429
  • 3
  • 21
  • 28
0

All other examples will not work if your asterisk have single peer with name "active"

Note, for this task simple do

asterisk -rx 'core show calls' |tail -n 2 | head -n 1|cut -f 1 -d\ 
arheops
  • 15,544
  • 1
  • 21
  • 27