Archive

Posts Tagged ‘SMS’

Various Nagios plugins

October 15th, 2009 No comments

I’ve now written several Nagios plugins and submitted them all to MonitoringExchange.

Here’s a quick summary:

  • check_temper for monitoring the temperature with a TEMPer USB thermometer
  • check_kernel for checking that the currently running kernel on an RPM-based system is the most recent installed kernel (not necessarily the latest available kernel in the repository)
  • check_aql_balance for monitoring the number of SMS text message credits on your AQL account[1]
  • check_k8temp for monitoring the temperature of an AMD K8 (e.g. Athlon or Sempron) CPU

[1] See my blog post if you are interested in setting up SMS alerts with Nagios

Monitoring AQL SMS credit with Nagios

August 12th, 2009 No comments

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 balance\n";
    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 balance\n";
    exit $ERRORS{'UNKNOWN'};
} elsif ($content =~ m/AUTHERROR/i) {
    print "UNKNOWN, Unable to retrieve account balance\n";
    exit $ERRORS{'UNKNOWN'};
}

$content =~ s/[^0-9]//gi;
if ($content >=0) {
    if ($content < $criticalval) {
        # critical
        print "CRITICAL, Balance $content credits\n";
        exit $ERRORS{'CRITICAL'};
    } elsif ($content < $warningval) {
        # warning
        print "WARNING, Balance $content credits\n";
        exit $ERRORS{'WARNING'};
    } else {
        # ok
        print "OK, Balance $content credits\n";
        exit $ERRORS{'OK'};
    }
} else {
    # invalid number
    print "UNKNOWN ,Unable to retrieve account balance\n";
    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!

Categories: Guides, Linux, Nagios Tags: , ,

SMS alerts with Nagios

August 11th, 2009 7 comments

In a previous post I mentioned how easy it is to increase functionality in Nagios.

Today I was asked to set up SMS alerts in Nagios, as well as the existing email alerts. I am by no means the first person to write about this, but this post is intended to be a start-to-finish guide, covering all aspects.

Choosing a provider

The first step is choosing a provider which has a decent API for sending SMS messages. I chose AQL, as I have used them in the past. They allow you to send SMS messages via a web interface, HTTP GET, HTTP POST, or email.

In that way, perhaps the easiest way to get SMS alerts is to get Nagios to email its alerts to the AQL SMS gateway. But I wanted to do it “properly”.

So I signed up for an account with AQL and bought a small number of SMS credits for the account. It’s also possible to have a contract for heavy usage, but I can always upgrade to that if I need to.

Defining a new method of alerting in Nagios

In the file /usr/local/nagios/etc/objects/commands.cfg there is a section where notification commands are defined. I added a couple more definitions for SMS alerts for hosts and services. My SMS script would have a calling syntax like /path/to/script phone_number message.

So I added a couple of definitions like this, obviously using a real mobile phone number:

define command{
command_name alert-service-by-sms
command_line /usr/local/nagios/libexec/alert-by-sms 01234567890 "Nagios Service $NOTIFICATIONTYPE$ Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$"
}

define command{
command_name alert-host-by-sms
command_line /usr/local/nagios/libexec/alert-by-sms 01234567890 "Nagios Host $NOTIFICATIONTYPE$ Alert: $HOSTALIAS$ is $HOSTSTATE$"
}

The script

Now all remains is to write the script that will do the legwork. If you decide to go with AQL as your provider, you need to install their Perl module from CPAN. Use a command like this:

cpan SMS::AQL

And then make a Perl script like this. You can save it anywhere you like; I chose to put it with the other Nagios executables in /usr/local/nagios/libexec just to keep it with the rest. Adjust the username and password to match your AQL credentials, and set the sender parameter to be either a UK mobile number (so the recipient can reply to the message) or simply a text string which appears as a name to the recipient, and does not allow them to reply.

#!/usr/bin/perl -wT

use strict;
use SMS::AQL;

my $to = $ARGV[0];
my $msg = $ARGV[1];
$to =~ s/[^0-9]//gi;

my $sms = new SMS::AQL({
username => 'fred',
password => 'bloggs',
options => {
sender => 'Nagios',
},
});

my ($ok, $error) = $sms->send_sms($to, $msg);
if (!$ok) {
print "Failed to send the message, error: $error\n";
} else {
print "Success!\n";
}

It is, of course, wise to test that your script works by calling it from the command line. Once you’re happy it works, it’s time to tell Nagios to start sending alerts.

Enabling SMS alerts

This time, we need to edit /usr/local/nagios/etc/objects/contacts.cfg. Modify your contact entry to add the lines in bold. This assumes you have only one user with a mobile phone – remember their mobile number is hard-coded into the command definition!

If you have more than one user and you set alert-service-by-sms or alert-host-by-sms for both, you will get two text messages sent to the same phone number for each Nagios alert. As I only have one user it’s not a problem for me, but in the future I will probably post a more elegant solution where each user can get an individual text message.

define contact{
contact_name                    jonathan
use                             generic-contact
alias                           Jonathan
email                           alerts@example.com
service_notification_commands   alert-service-by-sms
host_notification_commands      alert-host-by-sms

}

And that should be everything! Now you have to test it, either by breaking a host or service, or setting up a bogus one that will definitely fail.

Categories: Guides, Linux, Nagios Tags: , ,