nagiosプラグインの監視casa業務

4343 ワード

vi check_casa.pl
#! /usr/bin/perl -w
use strict;
use Getopt::Long;
use Time::HiRes qw(time);

my $o_host;
my $o_port=80;
my $o_baseurl='/bmwwebservice/services/ChinaUnicomSIMCardServices';
my $o_soapaction='"getSIMCard"';
my $o_iccid="8986011162310xxxxxx";
#my $o_iccid;

my $o_warning;
my $o_critical;
my $o_timeout;
my $ts;
my $o_perf;
my $o_help;

my $regex1="\<?xml";
my $regex2="\<status\>([^<>]+)\<\/status\>";

use LWP::UserAgent;
use HTTP::Request::Common;

sub exit_with_error{
	print $_[0];				# message
	if (defined($o_perf)){
		print "| 'timediff'=$ts","s;$o_warning;$o_critical;0;30";
	}
	print "
"; exit($_[1]); } sub print_helpmessage_exit() { print_helpmessage(); exit(0); } sub check_options { Getopt::Long::Configure("bundling"); GetOptions( 'H:s' => \$o_host, 'hostname:s' => \$o_host, 'p:i' => \$o_port, 'port:i' => \$o_port, 'i:s' => \$o_iccid, 'iccid:s' => \$o_iccid, 'f' => \$o_perf, 'perf' => \$o_perf, 't:i' => \$o_timeout, 'timeout:i' => \$o_timeout, 'b:s' => \$o_baseurl, 'base_url:s' => \$o_baseurl, 'w:i' => \$o_warning, 'warn:i' => \$o_warning, 'c:i' => \$o_critical, 'critical:i' => \$o_critical, 'h:s' => \$o_help, 'help:s' => \$o_help ); } check_options(); my $userAgent = LWP::UserAgent->new(agent => 'perl post'); #$userAgent->timeout($o_timeout); my $message = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:sim=\"http://www.bmw.de/casa/SIMCardServicesTypes\"> <soapenv:Header/> <soapenv:Body> <sim:getSIMCardRequest> <requestor>BMWCasa</requestor> <iccId>$o_iccid</iccId> </sim:getSIMCardRequest> </soapenv:Body> </soapenv:Envelope>"; my $request = HTTP::Request->new(POST => "http://$o_host:$o_port$o_baseurl"); #$request->header(SOAPAction => '"http://114.66.80.165:17008/bmwwebservice/services/getSIMCard"'); $request->header(SOAPAction => $o_soapaction); $request->content($message); $request->content_type("text/xml; charset=utf-8"); my $start_time=time(); my $response = $userAgent->request($request); my $end_time=time(); #print $response->error_as_HTML unless $response->is_success; $ts=$end_time - $start_time; my $content = $response->decoded_content(); if (!$response->is_success) { exit_with_error("Server response error: ".$response->status_line.$content,2); #critical } #print $content; if (($ts>$o_warning) && ($ts<$o_critical)) { exit_with_error("timediff: $ts sec: WARNING",1); #warning; } if ($ts>$o_critical) { exit_with_error("timediff: $ts sec: CRITICAL",2); #critical } if ($content =~ $regex1) { #format ok if ($content =~ /$regex2/) { #extract the status value if ($1 ne 'InUse' and $1 ne 'Active') { exit_with_error("Unexpected SIM status: $1",2); # wrong status, critical } else { exit_with_error("Status: $1, timediff: $ts sec: OK",0); # Everything's fine. OK. } } else { exit_with_error("Cannot find SIM status! Critical",2); #Cannot find "in use", cirtical } } else { exit_with_error("Malformed response content. Critical",2); # content malformed. Critical }