Monitoring AQL SMS credit with Nagios

Further to yesterday’s post about setting up SMS alerts from Nagios, I decided I wanted to monitor how many SMS credits I have left in my account.

AQL provide a way of finding out via an HTTP request, so I set about writing a perl module to check and return the result to Nagios.

N.B. I’ve now published this module on Monitoring Exchange. Please download the plugin from there, as I will keep that copy up to date if there are changes in the future (and the copy on this page is likely to go out of date).

check_aql_balance

#! /usr/bin/perl -w
# Usage: check_aql_balance [username] [password] [warning] [critical]
# Example: check_raid fred bloggs 100 50
#         WARNING Balance 23 credits

use strict;
use LWP::Simple;
use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS);

my $username = $ARGV[0];
my $password = $ARGV[1];
my $warningval;
my $criticalval;
$warningval = $ARGV[2] or $warningval = 20;
$criticalval = $ARGV[3] or $criticalval = 10;
$warningval =~ s/[^0-9]//gi;
$criticalval =~ s/[^0-9]//gi;

if (!defined $username || !defined $password) {
    print "UNKNOWN, Unable to retrieve account balancen";
    exit $ERRORS{'UNKNOWN'};
}

my $url = "http://gw1.aql.com/sms/postmsg.php?username=$username&password=$password&cmd=credit";
my $content = get $url;

if (!defined $content) {
    print "UNKNOWN, Unable to retrieve account balancen";
    exit $ERRORS{'UNKNOWN'};
} elsif ($content =~ m/AUTHERROR/i) {
    print "UNKNOWN, Unable to retrieve account balancen";
    exit $ERRORS{'UNKNOWN'};
}

$content =~ s/[^0-9]//gi;
if ($content >=0) {
    if ($content < $criticalval) {
        # critical
        print "CRITICAL, Balance $content creditsn";
        exit $ERRORS{'CRITICAL'};
    } elsif ($content < $warningval) {
        # warning
        print "WARNING, Balance $content creditsn";
        exit $ERRORS{'WARNING'};
    } else {
        # ok
        print "OK, Balance $content creditsn";
        exit $ERRORS{'OK'};
    }
} else {
    # invalid number
    print "UNKNOWN ,Unable to retrieve account balancen";
    exit $ERRORS{'UNKNOWN'};
}

The only required arguments are the AQL username and password, but you can optionally specify the limits that trigger Warning or Critical status. If you omit these, the script defaults to values of 20 and 10.

In your commands.cfg, add a block like this to define the command. Again, you can omit the last 2 parameters if you are happy with the defaults..

define command{
    command_name    check_aql_balance
    command_line    $USER1$/check_aql_balance $ARG1$ $ARG2$ $ARG3$ $ARG4$
}

And finally, in the localhost.cfg (or any other config file for hosts/services) you can add the service like this.

define service{
    use                             local-service
    host_name                       localhost
    service_description             AQL account balance
    check_command                   check_aql_balance!fred!bloggs!20!10
    notifications_enabled           1
}

Simples!

One Comment

Leave a Reply