nagios插件之监控casa业务

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 "\n";
	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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值