#!/usr/local/bin/perl
#
# Issues sms warning using smsclient. Once one sms warning is sent,
# no sms will be sent again until host/service is back up again.
# Written as warning plugin for Checkservice
# by Paul van Tilburg <paul@luon.net>
#
# Arguments are: -c confdir -h host -p port -s service -r result 

use CS::Functions;
use Getopt::Std;

getopts("h:p:s:c:r:");
my ($host, $ports, $service, $confdir) = ($opt_h, $opt_p, $opt_s, $opt_c);
my $res;   
my %config = ();
my $key;
my $value;

(! -d "$confdir") and
  die "$0: Can't find config dir ($confdir)\n";
                        
# Parsing global conffile
open (CONFIG, "$confdir/checkservice.conf") or                   
  die "$0: Can't open global configfile " 
      . "'$confdir/checkservice.conf'\n";

while (<CONFIG>) {
  chomp; s/#.*//;
  if (($key, $value) = /^\s*(\S+)\s*=\s*(\S.*)/) { $config{$key} = $value; }
}

# Parsing sms plugin's conffile
open (OCONFIG, "$confdir/plugins/sms.conf") or
  die "$0: Can't open plugins' configfile "
      . "'$confdir/plugins/sms.conf'\n";

while (<OCONFIG>) {
  chomp; s/#.*//;
  if (($key, $value) = /^\s*(\S+)\s*=\s*(\S.*)/) { $config{$key} = $value; }
}
	      
my $lockf = "$config{lockpath}/$host.$service";
my @wmethods = split(":", $config{wmethod});

if (! -r $lockf) {
  unless (defined $config{smscpath}) { $config{"smscpath"} = "/usr/bin"; }
  if ($service eq "-") {
    print "Running: $config{smscpath}/sms_client $config{smsserv}:"
		  . "$config{smsno} \"Host $host down\" > /dev/null 2>&1\n";
    $res = system("$config{smscpath}/sms_client $config{smsserv}:"
		  . "$config{smsno} \"Host $host down\" > /dev/null 2>&1");
  }
  else {
    print "Running: $config{smscpath}/sms_client $config{smsserv}:"
		  . "$config{smsno} \"Host $host: service $service down\">"
		  . " /dev/null 2>&1\n";
    $res = system("$config{smscpath}/sms_client $config{smsserv}:"
		  . "$config{smsno} \"Host $host: service $service down\">"
		  . " /dev/null 2>&1");
  }
  unless (absgrep("mail", @wmethods) || $res) {
    open LOCKF, ">$lockf" or
      die "$0: Could not create lockfile $lockf: $!\n";
    print LOCKF time, "\n";
  }
}



syntax highlighted by Code2HTML, v. 0.9.1