Welcome to It-Slav.Net blog
Peter Andersson
peter@it-slav.net

I've already got a female to worry about. Her name is the Enterprise.
-- Kirk, "The Corbomite Maneuver", stardate 1514.0

This article describe howto monitor an Asterisk server with Nagios or op5 Monitor. Pre-req to get it running is a working Nagios or op5 Monitor installation and an Asterisk 🙂

Theory

In my implementation of Asterisk I have a couple of softphones, 2 hardphones and connections to two SIP providers. I want to monitor the following:

  • Possibility for a phone to be able to register at the Asterisk server
  • The registration at the SIP providers are OK
  • The Operating system is not overloaded
  • The server where Asterisk is running is up

Implementation

Sip check

I found the following after Googling: http://bashton.com/osprojects/nagiosplugins/ Define it in commands.cfg

# command 'check_sip'
define command{
    command_name                   check_sip
    command_line                   $USER1$/custom/nagios-check_sip-1.2/check_sip -u "$ARG1$"
    }

Define the sip check in services.cfg, I also created a service group called ip_telephony

# service 'Asterisk Check SIP'
define service{
    use                            default-service
    host_name                      dull
    service_description            Asterisk Check SIP
    check_command                  check_sip!sip:XXXXX@dull.mynet
    servicegroups                  ip_telephony
    contact_groups                 it-slav_msn,it-slav_mail,call_it-slav
    }

Monitor the Peers

First I tried to find a suitable plugin at Nagiosexchange, Google and other sites if there was anybody that has created and published a Nagios plugin to monitor the sip peers. I could not find any. With the asterisk command "sip show peers", information about the connected sip peers can be found:

[root@dull custom]# asterisk -rx "sip show peers"
Name/username              Host            Dyn Nat ACL Port     Status
pulver                     69.90.155.70                5060     OK (154 ms)
digisip/XXXXX              82.209.165.194              5060     OK (44 ms)
6016                       (Unspecified)    D          0        UNKNOWN
6005                       (Unspecified)    D          0        UNKNOWN
6004/6004                  10.1.1.168       D          5060     OK (139 ms)
6003/6003                  10.1.1.168       D          5060     OK (136 ms)
6002/6002                  10.1.1.152       D          5060     OK (8 ms)
6011                       (Unspecified)    D          0        UNKNOWN
6010                       (Unspecified)    D          0        UNKNOWN
6000                       (Unspecified)    D          0        UNKNOWN
6001                       (Unspecified)    D          0        UNKNOWN
11 sip peers [Monitored: 5 online, 6 offline Unmonitored: 0 online, 0 offline]

I discussed this issue with Marcus Rejås when we meet and he told me that he has written a Nagios plugin to monitor the sip peers. I got the script and modified to fit my needs, i.e. get performance data for graphing: /opt/plugins/custom/check_asterisk_sip_peers.sh

#!/bin/bash
#
# Simple Asterisk Peer Check.
# Copyright (C) 2008 Marcus Rejås / Rejås Datakonsult
#
# Modified with perfdata by Peter Andersson
# http://www.it-slav.net/blogs/?p=123
# peter@it-slav.net
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
#
# Very simple plugin that checks if a peer is ok. The peers needs "qualify=yes"
# in its configuration.
#
# A peer that is not registered or non-existent will result in error. If the
# peer is OK a short statusline (from Asterisk) is written. There is timing
# information suitable for graphing as well.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see .
#
# Example use of this script:
#
# sip:~# ./sip_check_peer mysecretary-100
# mysecretary-100/461762501 62.80.200.53 5060 OK (10 ms)
# sip:~#
#
#

if [ $# == 0 -o "$1" == "-h" -o  $# -gt 1 ]; then
	echo "Usage: $0"
	exit 3
fi

LINE=`asterisk -r -x "sip show peers" | grep $1 | grep "OK ("`

#
# This is a uggly. Just to check that the expression above does not match more
# then one line.
#
HITS=`asterisk -r -x "sip show peers" | grep $1 | grep "OK (" | wc -l`

if [ $HITS -gt 1 ]; then
	echo "ERROR: Multiple match, tweak your arguments or fix $0 :-)"
	exit 3
fi

if [ "$LINE" ]; then
	echo -n "OK: "
	echo -n $LINE
	#Create perdata
	echo -n "|time="
	echo $LINE | awk '{gsub(/\(/,"")};{gsub(/\)/,"")};{print $(NF-1)$NF}'
	exit 0
elif [ -z "$LINE" ]; then
	echo "CRITICAL: Something is wrong with $1";
	exit 2
else
	echo $LINE
	exit 2
fi

The command run by hand looks like this:

[root@dull /]# /opt/plugins/custom/check_asterisk_sip_peers.sh pulver
OK: pulver 69.90.155.70 5060 OK (166 ms)|time=166ms

The plugin is started by nrpe at the asterisk server and configured in /etc/nrpe.d/mycommands.cfg

command[asterisk_peer_digisip]=sudo /opt/plugins/custom/check_asterisk_sip_peers.sh digisip
command[asterisk_peer_pulver]=sudo /opt/plugins/custom/check_asterisk_sip_peers.sh pulver
command[asterisk_peer_6002]=sudo /opt/plugins/custom/check_asterisk_sip_peers.sh 6002
command[asterisk_peer_6003]=sudo /opt/plugins/custom/check_asterisk_sip_peers.sh 6003
command[asterisk_peer_6004]=sudo /opt/plugins/custom/check_asterisk_sip_peers.sh 6004

/opt/plugins/custom/check_asterisk_sip_peers.sh must run as a high privileged user so I’m using sudo, modify /etc/sudoers with visudo:

# visudo -f /etc/sudoers
--snip--
nobody ALL= (root) NOPASSWD: /opt/plugins/custom/check_asterisk_sip_peers.sh
--snip--
Turn of requiretty, because it will run without a console
--snip--
#Defaults    requiretty
--snip--

The service checks are defined in services.cfg and also put into the same servicegroup

# service 'Asterisk Peer Pulver'
define service{
    use                            default-service
    host_name                      dull
    service_description            Asterisk Peer Pulver
    check_command                  check_nrpe!asterisk_peer_pulver
    servicegroups                  ip_telephony
    contact_groups                 it-slav_sms,it-slav_mail,call_it-slav
    }

I also defined the other peers, i.e. digisip, 6002, 6003, 6004

The servicegroup is defined in servicegroups.cfg

# servicegroup 'ip_telephony'
define servicegroup{
    servicegroup_name              ip_telephony
    alias                          IP - Telephony
    }

This is the result, a screenshot from my op5 Monitor system:

I leave to the reader to define the standard checks for load, ping and total processes. If you use op5 Monitor or pnp4nagios the graphs are automatically created:

Links

 


12 Responses to “Asterisk monitoring with Nagios or op5 Monitor”

  1. BG Says:

    Hello.
    Which nagios version do you use?
    This skin looks great, is it in standard nagios package?

  2. peter Says:

    It is op5 Monitor, based on Nagios 3.x.
    op5 has changed the look and feel, what we are doing right now is developing a total new Gui and that will be GPL:ed.
    http://www.op5.org/community/projects/ninja

    If you want to try op5 Monitor, there is both a downloadable vmware image and a package for RHEL5 or CentOS5.
    https://shop.op5.com/free-trial-versions-c-13.html

    Regards
    Peter

  3. duncan Says:

    Did everything listed here including the visudo but i keep getting this error:

    NRPE: Unable to read output

    I am calling the check from a Nagios Monitoring server and i get that error, when i run the check on commandline on the monitored box, i get the output fine, any pointers, been scratching my head for a week now.

  4. Gas Says:

    I changed to “asterisk” the user and group used to start nrpe daemon in my init.d file.
    Now I am able to receive info from asterisk:
    ./check_nrpe -H 10.X.X.X -p 5666 -c check_asterisk_sip_peer -a OVH
    OK: OVH/003——– 91.121.129.17 N 5060 OK (21 ms)|time=21ms

    Reminder: to send argument to nrpe, you need to compile it with: ./configure –enable-command-args
    and enable i(n the nrpe.cfg file) this argument:
    dont_blame_nrpe=1

  5. sweasseenume Says:

    Any ideas on the newest Network Management system freeware?
    I searched the web and discovered the following:
    Kaseya.com
    Logmein.com

    They all look different… Does anyone has experience with any of them?
    In addition did anybody else try that software:
    N-able IT automation software ?

  6. peter Says:

    I’m totally amazed of all these different softwares that try to solve the same issues.
    Is there room for all of them on the market, my guess is NO!

    No I have not tried them.

  7. Pete Says:

    Hi Peter, great site! and great article.
    However I have a slight problem in understanding something, to which I am sure I’m not alone.

    Basically the above tasks are easy to implement, such as updating the commands.cfg (which on Debian BTW, is linux-commands.cfg), and linux-services.cfg.

    The problem I have is that this script is looking for the actual plugin and although I’ve downloaded it (Nagios check_sip plugin v1.2), I don’t know where to install it.
    I’ve tried putting it in /usr/src/nagios-plugins-1.4.13/plugins
    (this is because the plugin file (Nagios check_sip plugin v1.2) contains a simple script.

    Is it possible for you to add the steps to actually install the plugin please? or am I totally on the wrong track altogether?
    I’m sure that once that is shown, it would help many other people including me 😉

  8. peter Says:

    In op5 Monitor the macro $USER1$ is equal to /opt/plugins/ and we recommend our customers to put their homebrown or downloaded scripts into /opt/plugins/custom

    You can put it wherever you want just update the commands definition to suit your needs.

  9. Kimse Says:

    got the NRPE: Unable to read output error

    nagios ALL=(ALL) NOPASSWD: /usr/lib/nagios/plugins/sip_check_peers

    added that line to sudoers and got it working

  10. william Says:

    #HITS=`asterisk -r -x “sip show peers like phone*” | grep “$1.*OK (” | wc -l`
    HITS=`echo $LINE|wc -l`

    Well,asterisk -rx uses to much time,
    this can reduce half time ^_^

  11. peter Says:

    Yes, you are right!

Leave a Reply

You must be logged in to post a comment.





Book reviews
FreePBX 2.5
Powerful Telephony Solutions






Asterisk 1.6
Build a feature rich telephony system with Asterisk






Learning NAGIOS 3.0





Cacti 0.8 Network Monitoring,
Monitor your network with ease!