#!/usr/bin/perl # notify_by_smsaql Copyright (C) 2011 Mohan Cheema # # Checks a file's size and modification time to make sure it's not empty # and that it's sufficiently recent. # # # 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 2 # 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. # # you should have received a copy of the GNU General Public License # along with this program (or with Nagios); if not, write to the # Free Software Foundation, Inc., 59 Temple Place - Suite 330, # Boston, MA 02111-1307, USA # ============================= SETUP NOTES ==================================== # # Copy this file to your Nagios plugin folder # On a Centos install this is /usr/lib/nagios/plugins or /usr/lib64/nagios/plugins # other distributions may vary. # # NAGIOS SETUP # # 1. Create the SMS notification commands. (Commonly found in commands.cfg) # Don't forget to add your username and password. # # define command{ # command_name notify-service-by-sms # command_line $USER1$/notify_by_smsaql -u USERNAME -p PASSWORD -t $CONTACTPAGER$ -f Nagios -m "Service: $SERVICEDESC$\\nHost: $HOSTNAME$\\nAddress: $HOSTADDRESS$\\nState: $SERVICESTATE$\\nInfo: $SERVICEOUTPUT$\\nDate: $LONGDATETIME$" # } # # define command{ # command_name notify-host-by-sms # command_line $USER1$/notify_sms.pl -u USERNAME -p PASSWORD -t $CONTACTPAGER$ -f Nagios -m "Host $HOSTNAME$ is $HOSTSTATE$\\nInfo: $HOSTOUTPUT$\\nTime: $LONGDATETIME$" # } # # 2. In your nagios contacts (Commonly found on contacts.cfg) add # the SMS notification commands: # # service_notification_commands notify-service-by-sms # host_notification_commands notify-host-by-sms # # 3. Add a pager number to your contacts # # pager 447700900000 # use strict; use Getopt::Long; use LWP; use URI::Escape; my $version='1.0'; my $verbose=undef; my $username=undef; my $password=undef; my $SMSpath="/sms/postmsg.php"; my $message=undef; my $from='Nagios'; my $to=undef; sub print_version { print "$0: version $version\n"; exit(1); }; sub verb { my $t=shift; print "VERBOSE: ",$t,"\n" if defined($verbose) ; } sub print_usage { print "Usage: $0 [-v] -u -p -t [-f ] -m \n"; } sub help { print "\nNotify by SMS Plugin ", $version, "\n"; print " AQL Ltd - http://www.aql.co.uk/\n\n"; print_usage(); print < \$verbose, 'verbose' => \$verbose, 'V' => \&print_version, 'version' => \&print_version, 'h' => \&help, 'help' => \&help, 'u=s' => \$username, 'username=s' => \$username, 'p=s' => \$password, 'password=s' => \$password, 't=s' => \$to, 'to=s' => \$to, 'f=s' => \$from, 'from=s' => \$from, 'm=s' => \$message, 'message=s' => \$message ); if (!defined($username)) { print "ERROR: No username defined!\n"; print_usage(); exit(1); } if (!defined($password)) { print "ERROR: No password defined!\n"; print_usage(); exit(1); } if (!defined($to)) { print "ERROR: No to defined!\n"; print_usage(); exit(1); } if (!defined($message)) { print "ERROR: No message defined!\n"; print_usage(); exit(1); } verb "username = $username"; verb "password = $password"; verb "to_num = $to"; verb "from = $from"; verb "message = $message"; } sub SendSMS { my $username = shift; my $password = shift; my $to = shift; my $from = shift; my $message = shift; # URL Encode parameters before making the HTTP POST $username=$username; $password=$password; $to=$to; $from=$from; $message=uri_escape($message); my $result; my $server = 'http://gw.aql.com/'.$SMSpath; my $post = 'username='.$username; $post.='&password='.$password; $post.='&to_num='.$to; $post.='&orig='.$from; $post.='&message='.$message; verb("Post Data: ".$post); my $ua=LWP::UserAgent->new(); $ua->timeout(30); $ua->agent('Nagios-AQL-SMS-Plugin/'.$version); my $req=HTTP::Request->new('POST',$server); $req->content_type('application/x-www-form-urlencoded'); $req->content($post); my $res = $ua->request($req); verb("POST Status: ".$res->status_line); verb("POST Response: ".$res->content); if($res->is_success) { if($res->content=~/AQSMS-AUTHERROR/i) { print $res->content.' There was an authentication error'; $result = 1; } elsif($res->content=~/AQSMS-NOMSG/i) { print $res->content.' There was no message or mobile number'; $result = 1; } elsif($res->content=~/AQSMS-NOCREDIT/i) { print $res->content.' Your account has no credit'; $result = 1; } elsif($res->content=~/AQSMS-INVALID_DESTINATION/i) { print $res->content.' The mobile number is not valid'; $result = 1; } else { $result = 0; } } else { $result = 1; print $res->status_line; } return $result; } check_options(); my $send_result = SendSMS($username, $password, $to, $from, $message); exit($send_result);