Nagios 3.x.x/Icinga 1.x.x Server Integration For SMS Alerts Via FoxBox Devices On Debian Squeeze

Want to support HowtoForge? Become a subscriber!
 
Submitted by luca177 (Contact Author) (Forums) on Wed, 2013-04-10 18:55. :: Monitoring

Nagios 3.x.x/Icinga 1.x.x Server Integration For SMS Alerts Via FoxBox Devices On Debian Squeeze

This tutorial explains how to integrate a standard Nagios (or Icinga) monitoring server with an SMS notification external device, the FoxBox (www.smsfoxbox.it).

 

1 Preliminary note

FoxBox, as stated by manufacturers, comes in two similar versions (G25 and LX800) differing on hardware specifics. In this guide we will refer to SMS Gateway LX800 (we bought this one because of its higher performance, and the CompactFlash storage), but I am confident that it could be extended to other versions without problems.

Our server runs the Nagios Core 3.5.0 (with 1.4.16 Nagios plugins) on a Debian Squeeze. Probably for different distros the paths will change, but the logic remains petty much the same.

 

2 Installation on the FoxBox side

The device is designed for these kind of communication tasks, so there is not much to do here except:

  • Provide a valid IP address, reachable from our monitoring server;
  • Insert a tested and full working SIM card, to send the SMS.

 

3 Installation on the Server side

First of all we need to place this script inside of the folder /usr/lib/nagios/plugins/:

#!/usr/bin/perl
use LWP::UserAgent;
use Getopt::Long;
use strict;
use warnings;
use constant OK => 0;
use constant WARNING => 1;
use constant CRITICAL => 2;
use constant UNKNOWN => 3;
use constant SEND_PAGE => "/source/send_sms.php";
my($host);
my($username);
my($password);
my($number);
my($message);
sub usage() {
    print("Usage: send_sms -h|--host <host> -u|--user <username> --pw|--pass
<password> -n|--number
<phone_number> -m|--message <message>\n\n");
    print("<host> - IP address or hostname of the SMS FoxBox\n");
    print("<username> - name of the SMS FoxBox administrator\n");
    print("
<password> - password of the SMS FoxBox administrator\n");
    print("
<phone_number> - phone number where SMS will be sent\n");
    print("<message> - message to be sent\n");
}
sub send_sms {
    my($host, $user, $pass, $phonenum, $text) = @_;
    my($ua);
    my($html_page);
    my($response);
    my($status_code);
    $ua = LWP::UserAgent-&gt;new;
    $ua-&gt;timeout(10);
    $response = $ua-&gt;post("http://$host" . SEND_PAGE,
                [
                    "username" =&gt; $user,
                    "pwd" =&gt; $pass,
                    "from" =&gt; 'Nagios',
                    "nphone" =&gt; $phonenum,
                    "testo" =&gt; $text,
                    "nc" =&gt; "http://$host" . SEND_PAGE
                ]);
    if(!$response-&gt;is_success) {
        print("ERROR: " . $response-&gt;status_line . "\n");
        $status_code = UNKNOWN;
    }
    $html_page =  $response-&gt;content;
    if($html_page =~ /p class="(\w+)"/g) {
        if($1 eq "confneg") {
            print("ERROR: Unable to send SMS\n");
            $status_code = UNKNOWN;
        }
        else {
            $status_code = OK;
        }
    }
    else {
        print("ERROR: Unknown page output\n");
        $status_code = UNKNOWN;
    }
    return $status_code;
}
undef $host;
undef $username;
undef $password;
undef $number;
undef $message;
GetOptions(    'host|H=s'    =&gt; \$host,
        'user|u=s'     =&gt; \$username,
        'pass|pw=s'     =&gt; \$password,
        'number|n=s'     =&gt; \$number,
        'message|m=s'     =&gt; \$message);
if(!defined $host || !defined $username || !defined $password || !defined $number || !defined $message) {
    usage();
    exit(UNKNOWN);
}

$message =~ s/\\n/\n/g;
my($ret_status);
$ret_status = send_sms($host, $username, $password, $number, $message);
exit($ret_status);

It is also important to set properly the permissions on this file, to allow the Nagios user executing it.

Now we should add the new notification commands, working on the SMS channel instead of the classic email one. To do so, we have to add these lines to the file /etc/nagios3/commands.cfg:

# 'notify-host-by-foxbox' command definition
define command{
        command_name    notify-host-by-foxbox
        command_line    /usr/lib/nagios/plugins/sendSMS.sh -h "127.0.0.1" -u "nagiosadmin" -pw "nagios" -n "$CONTACTPAGER$" -m "Host Alert: $HOSTNAME$ \nHost State: $HOSTSTATE$ \nDate/Time: $LONGDATETIME$"
        }
# 'notify-service-by-foxbox' command definition
define command{
        command_name    notify-service-by-foxbox
        command_line    /usr/lib/nagios/plugins/sendSMS.sh -h "127.0.0.1" -u "nagiosadmin" -pw "nagios" -n "$CONTACTPAGER$" -m "Service Alert: $HOSTALIAS$/$SERVICEDESC$ \nService State: $SERVICESTATE$ \nDate/Time: $LONGDATETIME$"
        }

As you can see, we need a new information for the contacts: the phone number. Thus, we have to define it as "pager" in the file /etc/nagios3/conf.d/contacts_nagios2.cfg.

Moreover, we set up the service/host notification command. By default these are using the email channel, while we want to alert with the new notification commands, so we have to edit the parameters "service_notification_commands" and "host_notification_commands" too:

define contact{
        contact_name                    test-contact
        use                             generic-contact
        alias                           tester
        email                           yourname@domain
        host_notification_commands      notify-host-by-foxbox
        service_notification_commands   notify-service-by-foxbox
        pager                           12453683421
        }

Obviously, once finished the configurations we have to restart the Nagios service in order to see their effects.

To check that everything is ok, maybe you could launch the pre-flight check with

nagios3 -v /etc/nagios3/nagios.cfg

As I see, this architecture has also been implemented on a pair of FoxBox versions, providing an all-in-one notification solution (EasyG2 G25 and Monitoring LX800).

(Reference: www.smsfoxbox.it)


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by shofer (registered user) on Fri, 2013-04-12 15:26.
Alternative and cheaper way, is to use gnokii and old Nokia phone.
Nagios can execute any php script over network, that's meant Nagios can add data (like alert info) to mysql database, next step i to read that data by script and send info by sms.
Submitted by Jeff (not registered) on Fri, 2013-04-26 11:05.
These configurations are hectic and sometimes they dont work. The best is to try Nagios SMS alert. It sends out SMS text message to you whenever you get email from nagios. Its fast and works in more than 200 counties. Try it.
Submitted by luca177 (registered user) on Tue, 2013-04-16 15:44.

Hello,
you are right that this solution is cheaper, but as a company we cannot install in our production environment a system based on an old Nokia phone (or something similar), mainly for reliability reasons.
Moreover, FoxBox is much more powerful and flexible of a common phone (being a full Debian device), giving us the possibility of executing additional processing, thus in our case our solution is more efficient than yours.
Anyway, thanks for the hint ;)

Submitted by shofer (registered user) on Tue, 2013-04-23 15:26.

Hi

I wish to have  solution like FoxBox, but all is always about tha money. In small company with small IT budget, are always promoted cheap or free solutions. Typical phone with gnooki can receiving and sending sms, rest is on Linux and scripts side (your skills and ideas) , so I bet that these solutions are quite similar in work ;)
There is difference ofcourse, FoxBox is more professional than typical phone and some custom scripts ;)

 

Submitted by luca177 (registered user) on Tue, 2013-04-30 15:39.
Hello,

I don't want to seem a lazy one, but in my (small) company we decided to buy an out-of-the-box solution because it's not just a matter of your skills and ideas, but also your time.

I mean, developing from scratch a stable system similar to FoxBox would surely take some week full-time, for an expert employee... And this is a cost too! :D