Capture numeric result in Linux

Posted on 2011-09-26
Last Modified: 2012-05-12
I am running a few Asterisk servers and I would like the run a daily cron job to check and see how many sip peers are resisted on my servers.  Currently we are only able to do this manually which can be a lot of work.  Below is the command we run which shows the result.

COMMAND: /usr/sbin/asterisk -rx "sip show peers" | grep "sip peers"
RESULT: 901 sip peers [Monitored: 22 online, 4 offline Unmonitored: 771 online, 104 offline]

I need some help with a script that will run the above command, read the first number into a numeric variable (example: 901 above.  This will always be between 0 and 1,500), and then process an IF statement to send me an email alert once the value is above 1,300.

Question by:ipconfig610

Expert Comment

ID: 36599102
This should work with integer strings


if [ $this -gt $that ]
LVL 68

Accepted Solution

woolmilkporc earned 500 total points
ID: 36599146
if [[ $( /usr/sbin/asterisk -rx "sip show peers" | grep "sip peers" |awk '{print $2}') -gt 1300 ]]; then
 date > mail -s "No. of SIP Peers above 1.300" ipconfig610@his.domain.tld

LVL 68

Expert Comment

ID: 36599166
Sorry, typo

date | mail -s "No. of SIP Peers above 1.300" ipconfig610@his.domain.tld
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.


Author Comment

ID: 36599539
when I run:
sudo /usr/sbin/asterisk -rx "sip show peers" | grep "sip peers" |awk '{print $2}'

the result only returns:

LVL 19

Expert Comment

ID: 36599588
Change the $2 to $1 in the command, and it will work.

Your original post made it look as if the word "RESULT:" was part of the result, which is why the suggestions above are grabbing the 2nd field instead of the first.

Author Comment

ID: 36599723
my mistake.  I have never used the  'awk' command before.  That changed worked perfectly.


Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Linux Mint 18 31 89
What language/protocol is the Angular Chat? 2 73
Check if a filw is immutable for a certain amount of time 6 61
how to configure linux OS using Ubuntu 7 39
Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question