Wednesday, 27 April 2011

Getting SNMP info by phone.

Hi,

I have a small Asterisk VOIP PBX running. It's the PBX in a Flash version from the Nerd Vittles site and I have it running off a USB key plugged into a Acer Revo small PC.

One of the useful things about having your own PBX is that you can play about with things like IVR's (Interactive Voice Response), those annoying voice menu things you get when you ring up the gas board with a billing query. On Asterisk IVR's can also be very useful for technical doing stuff like running Linux command line utilities and getting the output read back to you with a text to speech voice. I have mine reading back my current ADSL connection speed, as my mine gets gradually slower after a few days.

Here's how to do it:

If you have a Centos/Redhat based Asterisk server you need to install SNMP utils.

$> yum install net-snmp-utils

Install any dependencies too.

This will get you the snmpget command line tool.

To get the connection speed out of my router, I would need to issue the command:

snmpget -v 1 -c public 192.168.0.1 .1.3.6.1.2.1.2.2.1.5.4

The long number at the end is the location (MIB) of the connection speed in my router (which is 192.168.0.1). You can change this last bit to get any other SNMP info you might be interested in.

This command will give the output:

IF-MIB::ifSpeed.4 = Gauge32: 5970000

My connection speed being the last number. (Yes, it's slow I know)

To get this in a usable format I stick it in a shell script called "get-net-speed", located in /usr/local/bin:

#!/bin/sh

cmd="snmpget -v 1 -c public 192.168.0.1 .1.3.6.1.2.1.2.2.1.5.4"

speed=`$cmd | cut -f 4 -d :`
echo $speed

(The SNMPGET line is all on one line, the formatting makes it go funny)
Save, exit and make the script executable (chmod 0755 get-net-speed).

This will, when run just give me the speed number.

To get it into asterisk, we need to put it into a phpagi script.

phpagi is a php scripting language for asterisk, it allows you to do more complex programming based things than the built in asterisk system will allow. This includes using SQL databases and complex conditional routines based on external events. Here we are just going to do a simple command line execution:

In the directory: /var/lib/asterisk/agi-bin

We need to put in the following script called getnetspeed.php:

#!/usr/bin/php -q
<?
set_time_limit(30);

require('/var/lib/asterisk/agi-bin/phpagi.php');
error_reporting(E_ALL);
$agi = new AGI();
$agi->answer();
$data = shell_exec('/usr/local/bin/get-net-speed');
$agi->exec("Flite","\"The current internet connection speed is: $data\"");
$agi->hangup();
?>


This uses the phpagi class which simplifies phpagi communication.

Flite is the text to speech engine included in PBX in a Flash and does a pretty good job of speaking what you give it.

The last bit is to reference the php script in asterisk.

cd to the asterisk config directory:

$> cd /etc/asterisk

and edit the extensions_custom.conf file, this is the custom extensions file, it isn't wiped by the FreePBX config when that is edited.

$> joe extensions_custom.conf

and locate the area just below the "[from-internal-custom]" block:

find a gap and put in:

exten => 6566,1,Answer
exten => 6566,2,AGI(getnetspeed.php)
exten => 6566,3,Hangup

6566 is the phone extension you dial to get the info.

Finally go into your asterisk console and reload the config:

$> asterisk -rvvv

and type "reload":

buzby*CLI> reload

If your config reloaded OK, you can now test it by dialling 6566 on a phone.

This is quite a powerful thing, as it means that almost any command line tool can be attached to a phone extension or IVR menu. If you want to monitor drive temperatures, you can trim the output of a "smartctl" command, if you want to know how much disk space is free, "du". You can even use it to reboot servers (I'd stick a pin number in for that though". If you have remote dial in to your PBX, then you can do all this remotely.













No comments:

Post a Comment