#!/usr/bin/perl -w
################################################
#
# SMS Dispatcher Client for use with Nagios
#
# Written by: Michael Fung http://www.3open.org
# Last Updated: 2011-01-18
#
################################################
# Usage Example:
# ssms.pl -t PROBLEM -a SVC -n 85291234567 -g mygroup -H myhost -s HTTP -d 2 -o "Socket Timeout" -e 7200
# ssms.pl -t RECOVERY -a HOST -n 85291234567 -g mygroup -H myhost -d 0 -e 7200
# load modules
use strict;
use Getopt::Long;
use DBI;
use POSIX qw(strftime);
# customisable settings
our ( $dbhost, $dbname, $dbuser, $dbpw, $gw_userid, $gw_passwd, $gw_sendername );
do "/etc/smsd.conf" || die "Error reading configuration!\n";
my $logfile = "/tmp/ssms.log";
# declare variables
my ( $s, $notify_type, $alert_type, $hostgroup, $hostname, $phone_no, $service, $state_id, $service_output,
$expire, $not_before, $not_after, $sms_text, $done,
$dsn, $dbh, $sql, $now_text,
$help
);
my %svc_state = ("0" => "OK",
"1" => "WARNING",
"2" => "CRITICAL",
"3" => "UNKNOWN"
);
my %host_state = ("0" => "UP",
"1" => "DOWN",
"2" => "UNREACHABLE"
);
my $now = time();
# logging
sub printlog {
my $msg = shift;
my $date_text = strftime "%Y-%m-%d %H:%M:%S", localtime;
my $logtext = "[" . $date_text . "] $msg\n";
open(LOGFP, ">> $logfile");
print LOGFP "$logtext";
close(LOGFP);
}
sub syntax {
$s = shift or $s = 'Unknown';
printf STDERR ("Error: $s\n") unless ($help);
printf STDERR ("Syntax: %s -t <PROBLEM|RECOVERY> -a <SVC|HOST> -n <phone no.> -g <hostgroup> -H <hostname> -s <service> -d <state id> -o <status text> -e <expire seconds>\n", $0);
exit(0) if $help;
exit(3);
}
# debug use:
printlog("$0 @ARGV");
Getopt::Long::Configure('bundling');
GetOptions(
"t=s" => \$notify_type,
"a=s" => \$alert_type,
"H=s" => \$hostname,
"g=s" => \$hostgroup,
"n=s" => \$phone_no,
"s=s" => \$service,
"d=i" => \$state_id,
"o=s" => \$service_output,
"e=i" => \$expire,
"h" => \$help, "help" => \$help
) || syntax("Invalid option(s)");
# syntax checking
syntax if ($help);
syntax("Invalid options")
unless ($notify_type && $alert_type && ($alert_type eq 'SVC' || $alert_type eq 'HOST') && $hostgroup && $hostname && $phone_no && defined($state_id));
if ($alert_type eq 'SVC') {
syntax("<Status Text> required.") unless ($service && $service_output);
}
# database connection
$dsn = "DBI:mysql:database=$dbname;host=$dbhost;mysql_socket=/var/lib/mysql/mysql.sock";
$dbh = DBI->connect($dsn, $dbuser, $dbpw, { RaiseError => 1, AutoCommit => 1 }) or die $DBI::errstr;
# disable sms for all notification types other than PROBLEM and RECOVERY
exit 0 unless ($notify_type eq 'PROBLEM' || $notify_type eq 'RECOVERY');
# query assembly
if (defined($expire)) {
$not_after = $now + $expire;
} else {
$not_after = 'NULL';
}
$now_text = strftime "%m-%d %H:%M", localtime;
if ($alert_type eq 'HOST') {
$alert_type = 'H';
$sms_text = "$hostname is " . $host_state{"$state_id"} . ". $now_text";
} else {
$alert_type = 'S';
# status text is not very useful, so disabled:
#$sms_text = "Nagios Alert: $hostname/$service is " . $svc_state{"$state_id"} . ".($service_output) $now_text";
$sms_text = "$hostname - $service is " . $svc_state{"$state_id"} . ". $now_text";
}
$sql = <<SQL;
insert into sms_queue set
queue_date=$now,
phone_no='$phone_no',
not_after=$not_after,
sms_text='$sms_text'
SQL
# update it
$dbh->do($sql);
# clean up
exit;