#!/usr/bin/perl
use Digest::CRC;
use IO::Socket::INET;
use strict;
#===主程序================================================
if ($ARGV[0] == "-q" && $ARGV[2] == "-H" && $ARGV[4] == "-P" && $ARGV[6] == "-w" && $ARGV[8] == "-c" && $ARGV[10] == "-C" && $ARGV[11] !="")
{
my $check = 1;
my $recall = $ARGV[1];
my $HOST = $ARGV[3];
my $PORT = $ARGV[5];
my $WARN = $ARGV[7];
my $CRIT = $ARGV[9];
my $COU = $ARGV[11];
my $query = &CRC ($recall);
#print "\$query = $query \n";
my @call = split(//,$recall);
my @dlen = (@call[8..11]);
my $lenth = join("",@dlen);
my $ll = hex($lenth)*2+5;
#print "\$ll = $ll \n";
my $respon = "";
while ($check == "1")
{
sleep 1;
$respon = &TEL($HOST,$PORT,$query,$ll);
#print "\$respon = $respon \n";
$check = &CRCCH($respon);
}
my $result = &CHAG($respon,$COU);
#=========数值判断==========================
if ($result < 1)
{
printf("UNKNOWN - Humidity is %.1f %rH !|ret=$result;$WARN;$CRIT\n",$result);
exit 3;
}
elsif ($result < $WARN)
{
printf("OK - Humidity is %.1f %rH .|hum=$result;$WARN;$CRIT\n",$result);
exit 0;
}elsif ($result < $CRIT)
{
printf("WARNING - Humidity is %.1f %rH .|hum=$result;$WARN;$CRIT\n",$result);
exit 1;
}elsif ($result < 100)
{
printf("CRITICAL - Humidity is %.1f %rH .|hum=$result;$WARN;$CRIT\n",$result);
exit 2;
}else
{
printf("UNKNOWN! - Humidity is %.1f %rH !|hum=$result;$WARN;$CRIT\n",$result);
exit 3;
}
}else{
&HELP;
}
exit;
#==子程序=====================================================
#==Telnet=取数据===============================================
sub TEL
{
my $host = $_[0];
my $port = $_[1];
my $response;
my @qu = $_[2];
my $qu = join("",@qu);
my $query = pack('H*',$qu);
my $tel = IO::Socket::INET->new( PeerAddr => $host,
PeerPort => $port,
Proto => "tcp",
Type => SOCK_STREAM);
my $xx = $tel->send($query);
sleep 1;
my $yy = $tel->recv($response,$_[3],0);
close($tel);
my $message = unpack('H*',$response);
return($message);
}
#==CRC16=校验=================================================
sub CRC
{
#my @in = @_;
my $input = join("",@_);
my $mes = pack('H*',$input);
my $jy = Digest::CRC->new(
width => 16,
init => 0xffff,
xorout => 0x0000,
poly => 0x8005,
refout => 1,
refin => 1,
cont => 1
);
$jy->add($mes);
my $c16 = $jy->hexdigest;
my @crc1 = split(//,$c16);
my$len = 4-scalar(@crc1);
my $i;
for ($i=1;$i<=$len;$i++)
{
@crc1 = ("0",@crc1);
}
my @crc = ($crc1[2], $crc1[3], $crc1[0], $crc1[1]);
my $crcstr = join("",@crc);
my $str = $input.$crcstr;
#print "sub CRC $str\n";
return($str);
}
#==CheckSum=返回值校验=====================================================
sub CRCCH
{
#my @checkstr = @_;
my $in = join("",@_);
my @checkstr = split(//,$in);
my $len = scalar(@checkstr);
my @chkstr = @checkstr[0..$len-5 ];
my $crcstr = join("",@chkstr);
my $crcout = CRC($crcstr);
#print "sbu CRCCH $crcout\n";
if ($in eq $crcout)
{return(0);}
else
{return(1);}
}
#=========HexToDec=十六进制转十进制=========================================
sub CHAG
{
my $r1 = join("",$_[0]);
my $c1 = $_[1],
my @r1 = split(//,$r1);
my @r2 = (@r1[6..9]);
my $r2 = join("",@r2);
my $r3 = hex($r2);
my $r4 = $r3 / $c1;
return($r4);
}
#=========HELP=========================================
sub HELP
{
print "\nUsage: check_modubs [-h] -q query -H host -P port -w warn -c crit -C count\n";
print "-h help\n";
print "-q query Query command such as:010400010001 Don't get the CRC.\n";
print "-H host Host is such as:10.0.0.1.\n";
print "-P port The port of Host such as:1001.\n";
print "-w Warning Value.\n";
print "-c Critical Valeu.\n";
print "-C count The count for the result caclu.\n";
print "If any problem, Please connact dongyanqiong\@cnpc.com.cn\n\n";
}
阅读(1649) | 评论(0) | 转发(0) |