nagios插件之监控路况RTTI返回值(模拟车机发起)

先把perl的帖上,节后用c再写一遍,替换该内容

#!/usr/bin/perl -w

require LWP::UserAgent;
use threads;
use threads::shared;
use warnings;
use strict;
use POSIX;
use Getopt::Long;
use Time::HiRes qw(time usleep);

my @cities;
my @lat_nl;	# north limit;
my @lat_sl;	# south limit;
my @long_el;	# east limit;
my @long_wl;	# west limit;

my $o_host="11x.x.x.x";
my $o_baseurl="/bmw/gateway/index.do";
my $o_port=80;

my $o_bbl = "boundingbox.csv";
my $o_output = "stress_test_output.csv";

my $o_tcars=5;
my $o_treqs=20;
my $o_interval=1.0;

my $o_timeout=15;
my $o_perf=undef;
my $o_warn=undef;
my $o_crit=undef;

my $magic=0.03089619;
my $magic_factor=1.5;

my $o_speed=600;
my $o_help="To_be_changed";

my $timediff=0;

my @time_table		:shared;
my @error_table		:shared;
my @length_table	:shared;
my @g_res_table		:shared;
#my @g_lat_table		:shared;
#my @g_lng_table		:shared;

sub exit_with_error{
	print $_[0];				# message
	if (defined($o_perf)){
		print "| 'timediff'=$timediff","s;$o_warn;$o_crit;0;30";
	}
	print "\n";
	exit($_[1]);
}



sub print_helpmessage()
{
	print "
Cennavi TPEG Loadtest tool.
Version: 0.3

Usage: ./cennavi_stresstest [-H <host>] [-p <port>] [-t <timeout>] [-b <baseurl>] [-a <initial latitude>] [-n <initial longitude>] [-T <total number of cars> -R <total number of requests>] [-i <interval between requests from a car>] [-h]

Options:
-H, --hostname
	Hostname or IP address of the target host
-p, --port
	TCP port of the target host
-b, --base_url
	Base url used to query the target host. Has to start with a \"\/\".
-f, --file
	The file that stores the coordination of the bounding boxes. Default to boundingbox.csv in the current folder.
-T, --total_cars
	Total number of cars to emulate
-R, --total_reqs
	Total number of requests to send. 
-I, --interval
	The interval between 2 requests for an individual car.
-o, --output
	The file where detailed information will be logged. Default to stress_test_output.csv in the current folder.
-h, --help
	Print this help message.

Notes:
- The coordination file has to be a well formatted csv file.
- If the total number of requests cannot be divided by total number of cars, you may get slightly in-accurate information. This will be fixed in the future.
- Requests generated from each individual car are sychronized. The next request will only be sent out after the previous one gets its response. If the previous request takes more than \"interval\" to finish, then the next request is sent out immediately. If the previous request takes less than \"interval\" to finish, then the script will hold the next request till the interval is reached.
- For this version, we can emulate 10^6 cars in parallel at most.
"
}

sub print_helpmessage_exit()
{
	print_helpmessage();
	exit(0);
}

sub rand_between
{
	return	int(rand($_[1] - $_[0])*0.2 + ($_[0]+$_[1])/2); 
}

sub min_max_sum {
	my(@number_array) = @{$_[0]};
	my $min = $number_array[0];
	my $max = $number_array[0];
	my $sum = 0;

	my $num=undef;
	foreach $num (@number_array) {
		if ($min > $num){
			$min = $num;}
		if ($max < $num){
			$max = $num;}
		$sum += $num;
	}
	return ($min, $max, $sum);
}


sub check_options {
	Getopt::Long::Configure("bundling");
	GetOptions(
	        'H:s'   => \$o_host,		'hostname:s'	=> \$o_host,
	        'p:i'   => \$o_port,   		'port:i'	=> \$o_port,
#		'l:s'	=> \$o_login,		'login:s'	=> \$o_login,
#		'x:s'	=> \$o_passwd,		'passwd:s'	=> \$o_passwd,
		'F:s'	=> \$o_bbl,		'file:s'	=> \$o_bbl,
		'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_warn,		'warn:i'	=> \$o_warn,
		'c:i'	=> \$o_crit,		'critical:i'	=> \$o_crit,
		'h:s'	=> \$o_help,		'help:s'	=> \$o_help
	);
	
}


################################################################################
#################################### Main ######################################
################################################################################


check_options();
if ($o_help ne "To_be_changed"){

	print_helpmessage_exit();

}

if($o_tcars > $o_treqs) {
	print "You ought to have more requests than cars\n";
	exit(0);
}

if($o_tcars >= 1000000 || $o_tcars<=0) {
	print "Sorry, that exceeds my capacity.\n";
	exit(127);
}

if ($o_baseurl =~ m/^\//){
}
else{
	$o_baseurl="/".$o_baseurl;
}


############################################################################
# Read the location of the bounding boxes from the configuration file.
############################################################################


my $suc = open BOXFILE, "<", $o_bbl;
if ($suc){
	while(<BOXFILE>) {
		#chomp;
		#push @cities, $_;
		my $line = $_;
		#if ($line =~ //){					# file content validation, will be done in the future.
		my @rec=split /,/, $line;
		chomp @rec;
		push (@cities, $rec[0]);
		push (@lat_nl, floor($rec[1] * (2**32) /360));
		push (@long_wl, ceil($rec[2] * (2**32) /360));
		push (@lat_sl, ceil($rec[3] * (2**32) /360));
		push (@long_el, floor($rec[4]* (2**32)/360));
	}	
}else {
	die "Cannot open config file!\n";
}
close BOXFILE;


my $drive_id=sprintf('%06s',int(rand()*1000));
$drive_id ="0000000000H".$drive_id;
my $city_id=int(rand($#cities));

my $cur_lat = rand_between($lat_sl[$city_id],$lat_nl[$city_id]);
my $cur_lat = rand_between($lat_sl[$city_id],$lat_nl[$city_id]);
my $des_lat = rand_between($lat_sl[$city_id],$lat_nl[$city_id]);
my $cur_lng = rand_between($long_wl[$city_id], $long_el[$city_id]);
my $des_lng = rand_between($long_wl[$city_id], $long_el[$city_id]);

my $target_url="http://$o_host$o_baseurl?DriveID=$drive_id&Decoding_Feat=&Clat=$cur_lat&Dlat=$des_lat&TP_SID=&OP=gtm&Vers=1112.05.07.00&Guidance=KD&Velocity=0&TReq=&Clon=$cur_lng&Dlon=$des_lng&Bearing=&TP_Apps=TEC-TFP";

my $regex="\<TransportFrame .+\\>\\S*\<\/TransportFrame\>";

my $ua = LWP::UserAgent->new;
$ua->timeout($o_timeout);
$ua->env_proxy;



my $content=undef;
my $start_time=time();
my $response = $ua->get($target_url);
my $end_time=time();
$timediff=$end_time - $start_time;
$content=$response->decoded_content();
my $l_len = length($content);

#print $target_url;
#print "\n";
#print $content;
#print "\n";
#


if ($timediff<$o_timeout){

if ($response->is_success) {
	if($content =~ m/$regex/){
		if ($timediff<=$o_warn) {
			exit_with_error("timediff: $timediff sec: OK",0);
		}else{
			if ($timediff <= $o_crit) {
				exit_with_error("timediff: $timediff sec: WARNING",1);
			}
		}
		#OK;
	} else {
		exit_with_error("Unknown Content: $content : CRITICAL"."\nDriveID: $drive_id \nCityID: $city_id\n$cur_lat\n$cur_lng\n$des_lat\n$des_lng\n", 2);
	}

}else {
	exit_with_error("Server error: $response->status_line : CRITICAL",2);
}
}
else{
	exit_with_error("timediff: $timediff sec: CRITICAL",2);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值