Check script for remaining credit on the Meteorsis SMS Service

check_sms_credit.pl
#!/usr/bin/perl -w

# script to check remaining credits for Meteors SMS Service
# last update: 2011-03-28

# load required modules
use strict;
use POSIX;
use Getopt::Long;
use LWP::UserAgent;

# declare config file customizable variables
our ( $gw_userid, $gw_passwd );

# declare variables
my (
        $s, $warn, $crit, $credits,
        $url, $ua, $response,
        $help, $verbose
);

# read in config file
do "/etc/smsd.conf" || die "Error reading configuration file!\n";

# define subs
sub syntax {
        $s = shift or $s = 'Unknown';
        print STDERR "Error: $s\n\n" unless ($help);
        print STDERR <<EOT;
Credits check for Meteors SMS Service
(C) 2007-2011 Michael Fung  http://www.3open.org/. All rights reserved.
Licensed under the GPL.

Syntax: check_sms_credit.pl -w <warn credits> -c <crit credits> [-h]
EOT

        exit(0) if $help;
        exit(3);
}


Getopt::Long::Configure('bundling');
GetOptions(
        "w=i" => \$warn,
        "c=i" => \$crit,
        "h" => \$help, "help" => \$help,
        "v" => \$verbose, "verbose" => \$verbose
) || syntax("Invalid Options.");

# syntax checks
syntax if ($help);

syntax("Invalid options")
        unless ($gw_userid && $gw_passwd && defined($warn) && defined($crit));


$url = "https://www.meteorsis.com/f_getusercredit.aspx?username=$gw_userid&password=$gw_passwd";
$ua = LWP::UserAgent->new;
$ua->max_size(1024);
$ua->max_redirect(0);

$response = $ua->get($url);
if ($response->is_success) {
        if ($response->content =~ /CREDIT:(\d+)$/) {
                $credits = $1;
                if ($credits < $crit || $credits == 0) {
                        print "CRITICAL: " . $response->content . "\n";
                        exit 2;
                } elsif ($credits < $warn) {
                        print "WARNING: " . $response->content . "\n";
                        exit 1;
                } else {
                        print "OK: " . $response->content . "\n";
                        exit 0;
                }
        } else {
                print "UNKNOWN: Gateway server returned error: ". $response->content . "\n";

                exit 3;
        }

} else {
        print "UNKNOWN: Gateway server request failed: " . $response->status_line . "\n";
        exit 3;
}

Alternate version

This version reads the username and password from the command line.

check_sms_credit.pl
#!/usr/bin/perl -w

# script to check remaining credits for Meteors SMS Service
# last update: 2011-03-28

# load required modules
use strict;
use POSIX;
use Getopt::Long;
use LWP::UserAgent;

# declare variables
my (
        $s, $gw_userid, $gw_passwd, $warn, $crit, $credits,
        $url, $ua, $response,
        $help, $verbose
);

# define subs
sub syntax {
        $s = shift or $s = 'Unknown';
        print STDERR "Error: $s\n\n" unless ($help);
        print STDERR <<EOT;
Credits check for Meteors SMS Service
(C) 2007-2011 Michael Fung  http://www.3open.org/. All rights reserved.
Licensed under the GPL.

Syntax: check_sms_credit.pl -u <userid> -p <password> -w <warn credits> -c <crit credits> [-h]
EOT

        exit(0) if $help;
        exit(3);
}


Getopt::Long::Configure('bundling');
GetOptions(
        "u=s" => \$gw_userid,
        "p=s" => \$gw_passwd,
        "w=i" => \$warn,
        "c=i" => \$crit,
        "h" => \$help, "help" => \$help,
        "v" => \$verbose, "verbose" => \$verbose
) || syntax("Invalid Options.");

# syntax checks
syntax if ($help);

syntax("Invalid options")
        unless ($gw_userid && $gw_passwd && defined($warn) && defined($crit));



$url = "https://www.meteorsis.com/f_getusercredit.aspx?username=$gw_userid&password=$gw_passwd";
$ua = LWP::UserAgent->new;
$ua->max_size(1024);
$ua->max_redirect(0);

$response = $ua->get($url);
if ($response->is_success) {
        if ($response->content =~ /CREDIT:(\d+)$/) {
                $credits = $1;
                if ($credits < $crit || $credits == 0) {
                        print "CRITICAL: " . $response->content . "\n";
                        exit 2;
                } elsif ($credits < $warn) {
                        print "WARNING: " . $response->content . "\n";
                        exit 1;
                } else {
                        print "OK: " . $response->content . "\n";
                        exit 0;
                }
        } else {
                print "UNKNOWN: Gateway server returned error: ". $response->content . "\n";

                exit 3;
        }

} else {
        print "UNKNOWN: Gateway server request failed: " . $response->status_line . "\n";
        exit 3;
}
nagios/check_sms_credit.pl.txt · Last modified: 2011-03-28 15:54 by admin
Back to top
GNU Free Documentation License 1.3
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0