#!/usr/bin/perl # check_aqlbalance 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 check_aqlbalance notification commands. (Commonly found in commands.cfg) # # define command{ # command_name check_aqlbalance # command_line $USER1$/check_aqlbalance -w $ARG1$ -c $ARG2$ -u $ARG3$ -p $ARG4$ # } # # 2. In your service config (Commonly found on linux.cfg may vary in your case) add # the service check commands: # #define service{ # use generic-service # host_name localhost # service_description AQL Credit Balance # check_command check_aqlbalance!10!5!username!password # } use strict; use Getopt::Long; use LWP; use URI::Escape; use vars qw($PROGNAME); use lib "/usr/local/nagios/libexec" ; use utils qw (%ERRORS &print_revision &support); my $verbose=undef; my $SMSpath="/sms/postmsg.php"; sub print_help (); sub print_usage (); my ($crit, $warn, $opt_h, $opt_V, $username, $password, $verbose); my ($result, $respo, $balance, $send_result, $message); $PROGNAME="check_aqlbalance"; $warn = 10; $crit = 5; Getopt::Long::Configure('bundling'); GetOptions( "v" => \$verbose, "verbose" => \$verbose, "V" => \$opt_V, "version" => \$opt_V, "h" => \$opt_h, "help" => \$opt_h, "w=f" => \$warn, "warning-credit=f" => \$warn, "c=f" => \$crit, "critical-credit=f" => \$crit, "u=s" => \$username, "username=s" => \$username, "p=s" => \$password, "password=s" => \$password ); if ($opt_V) { print_revision($PROGNAME, '1.0'); exit $ERRORS{'OK'}; } if ($opt_h) { print_help(); exit $ERRORS{'OK'}; } if (!defined($username)) { print_usage(); exit $ERRORS{'UNKNOWN'}; } if (!defined($password)) { print_usage(); exit $ERRORS{'UNKNOWN'}; } sub verb { my $t=shift; print "VERBOSE: ",$t,"\n" if defined($verbose); } $result = 'OK'; sub CheckBalance { verb "Username: $username"; verb "Password: $password"; my $server = 'http://gw.aql.com/'.$SMSpath; my $post = 'username='.$username; $post.='&password='.$password; $post.='&cmd=credit'; $respo; $balance; verb("Post Data: ".$post); my $ua=LWP::UserAgent->new(); $ua->timeout(30); $ua->agent('Nagios-AQL-SMS-Plugin/1.0'); 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) { $result = 'CRITICAL'; } else { ($respo,$balance) = split(/=/,$res->content); } } else { $result = 'CRTITICAL'; } return $balance; } $send_result = CheckBalance($username, $password); if($warn and $send_result < $warn) { $result = 'WARNING'; } elsif($crit and $send_result < $crit) { $result = 'CRITICAL'; } print "Aql Credit Balance $result: Available credit $send_result"; exit $ERRORS{$result}; sub print_usage () { print "Usage:\n"; print " $PROGNAME [-v] [-w ] [-c ] -u -p \n"; print " $PROGNAME [-h | --help]\n"; print " $PROGNAME [-V | --version]\n"; } sub print_help () { print_revision($PROGNAME, '1.0'); print "Copyright (c) 2011 Mohan Cheema\n\n"; print_usage(); print "\n"; support(); }