分类: 网络与安全
2017-03-19 11:13:22
# /usr/local/nagios/libexec/check_snmp_process -H 127.0.0.1 -C public -s 'svchost.exe'
Status is OK - svchost.exe(9 process) is working ...
# /usr/local/nagios/libexec/check_snmp_process -H 127.0.0.1 -C public -l
1.3.6.1.2.1.25.4.2.1.2.1 = OCTET STRING: System Idle Process
1.3.6.1.2.1.25.4.2.1.2.4 = OCTET STRING: System
1.3.6.1.2.1.25.4.2.1.2.216 = OCTET STRING: smss.exe
1.3.6.1.2.1.25.4.2.1.2.248 = OCTET STRING: svchost.exe
1.3.6.1.2.1.25.4.2.1.2.300 = OCTET STRING: csrss.exe
1.3.6.1.2.1.25.4.2.1.2.352 = OCTET STRING: csrss.exe
1.3.6.1.2.1.25.4.2.1.2.360 = OCTET STRING: wininit.exe
1.3.6.1.2.1.25.4.2.1.2.384 = OCTET STRING: winlogon.exe
1.3.6.1.2.1.25.4.2.1.2.448 = OCTET STRING: services.exe
1.3.6.1.2.1.25.4.2.1.2.456 = OCTET STRING: lsass.exe
1.3.6.1.2.1.25.4.2.1.2.464 = OCTET STRING: lsm.exe
1.3.6.1.2.1.25.4.2.1.2.552 = OCTET STRING: dwm.exe
1.3.6.1.2.1.25.4.2.1.2.608 = OCTET STRING: svchost.exe
1.3.6.1.2.1.25.4.2.1.2.688 = OCTET STRING: svchost.exe
1.3.6.1.2.1.25.4.2.1.2.768 = OCTET STRING: LogonUI.exe
1.3.6.1.2.1.25.4.2.1.2.780 = OCTET STRING: svchost.exe
# /usr/local/nagios/libexec/check_snmp_process -h
check_snmp_process v0.1
Usage: check_snmp_process -H
Options: -H Hostname or IP address
-p snmp port(default is tcp161)
-v snmp Version(default is v2c)
-C Community (default is public)
-l List snmp value
-s Match process string
-h or -v Help
define command{
command_name check_snmp_process
command_line $USER1$/check_snmp_process -H $HOSTADDRESS$ -C $ARG1$ -s $ARG2$
}
define service{
use local-service
host_name MT
service_description httpd
check_command check_snmp_process!127.0.0.1!'svchost.exe'
}
脚本详细代码
-------------------------------------------------------------------------------------------
#!/usr/bin/perl
# Version : 0.1
# Date : 2017-02-28
# Author : Darry Guo
# Help :
# Licence : GPL -
use strict;
use Net::SNMP qw{ :asn1 :snmp :translate };
use Getopt::Std;
my $script = GetScriptName($0);
my $script_version = "0.1";
my $ipaddress = "127.0.0.1";
my $port = 161;
my $version = "snmpv2c";
my $community = "public";
my $vMAX = 100;
my $timeout = 2;
my $status = 0;
my $returnstring = "";
my $OidBase = '1.3.6.1.2.1.25.4.2.1.2';
my $string = "test";
my $list_yes = 0;
my @arry_value = ();
if (@ARGV < 1) {
print "Too few arguments\n";
usage();
}
my %opt=();
getopts("hvlH:C:s:",\%opt);
if($opt{h} || $opt{v})
{
usage();
exit(0);
}
if($opt{l})
{
$list_yes = 1;
}
main();
####################################################################
# sub program #
####################################################################
sub main()
{
#Create the SNMP session
$ipaddress = $opt{H} if defined $opt{H} ;
$community = $opt{C} if defined $opt{C} ;
$string = $opt{s} if defined $opt{s} ;
@arry_value = GetOIDbyString($ipaddress, $community, $port, $version, $timeout, $OidBase, $string, $list_yes);
#if not list mode,then it's plug output
if ($arry_value[0] ne 'LIST_OID' )
{
my $len = @arry_value ;
if ($len == 0 )
{
#print "Not find : $string . \n";
$status = 2;
$returnstring = "Not find : $string process.";
}
else
{
#if ($len == 1 )
#{
#printf("OID : %s, value : %s, index: %i \n",$arry_value[0]->[0],$arry_value[0]->[1],$arry_value[0]->[2]);
#$Storage_ID = $arry_value[0]->[2];
#}
#else
#{
#foreach (@arry_value)
#{
# printf("OID : %s, value : %s, index: %i \n",$_->[0],$_->[1],$_->[2]);
#}
#print "Soryy,string($string} is not the only one, total $len records.\n";
#$status = 3;
#$returnstring = "Soryy,string($string} is not the only one, total $len records.";
#}
$status = 0;
$returnstring = "$string($len process) is working ...";
}
}
#if not list mode,then it's plug output
if ($arry_value[0] ne 'LIST_OID' )
{
if ($returnstring eq ""){
$status = 3;
}
if ($status == 0){
print "Status is OK - $returnstring\n";
}
elsif ($status == 1){
print "Status is a WARNING level - $returnstring\n";
}
elsif ($status == 2){
print "Status is CRITICAL - $returnstring\n";
}
else{
print "Status is UNKNOWN - $returnstring\n";
}
exit $status;
}
}
sub usage {
print << "USAGE";
--------------------------------------------------------------------
$script v$script_version
Usage: $script -H
Options: -H Hostname or IP address
-p snmp port(default is tcp161)
-v snmp Version(default is v2c)
-C Community (default is public)
-l List snmp value
-s Match process string
-h or -v Help
--------------------------------------------------------------------
Copyright 2017 Limited
This program is free software; you can redistribute it or modify
it under the terms of the GNU General Public License
--------------------------------------------------------------------
USAGE
exit 1;
}
sub GetScriptName
{
my @str = @_;
my $len = rindex($str[0],"/");
return substr($str[0],$len + 1);
}
sub GetOIDbyString
{
#USAGE
# GetOIDbyString($ipaddress, $community, $port, $version, $timeout, $OidBase, $string, $list_yes)
#
#Return value
#Format 1:
# @arry =(
# [oid_1,value1,index1],
# [oid_2,value2,index2],
# ...,
# [oid_N,valueN,indexN]
# )
# return @arry
#example
# #./get_oid_by_string_new_v2 -H 10.1.8.251 -C aspireeip -o 1.3.6.1.2.1.25.4.2.1.2 -s 'svchost.exe'
# OID : 1.3.6.1.2.1.25.4.2.1.2.248, value : svchost.exe, index: 248
# OID : 1.3.6.1.2.1.25.4.2.1.2.608, value : svchost.exe, index: 608
# OID : 1.3.6.1.2.1.25.4.2.1.2.688, value : svchost.exe, index: 688
# #./get_oid_by_string_new_v2 -H 10.1.1.254 -C aspiretest -o 1.3.6.1.2.1.2.2.1.2 -s 'FastEthernet1/0/48'
# OID : 1.3.6.1.2.1.2.2.1.2.10048, value : FastEthernet1/0/48, index: 10048
# #./get_oid_by_string_new_v2 -H 10.1.8.251 -C aspireeip -o 1.3.6.1.2.1.25.4.2.1.2 -l
# 1.3.6.1.2.1.25.4.2.1.2.1 = OCTET STRING: System Idle Process
# 1.3.6.1.2.1.25.4.2.1.2.4 = OCTET STRING: System
# 1.3.6.1.2.1.25.4.2.1.2.216 = OCTET STRING: smss.exe
# 1.3.6.1.2.1.25.4.2.1.2.248 = OCTET STRING: svchost.exe
# 1.3.6.1.2.1.25.4.2.1.2.300 = OCTET STRING: csrss.exe
# 1.3.6.1.2.1.25.4.2.1.2.352 = OCTET STRING: csrss.exe
#
#Format 2:
# return 'LIST_OID'
#
my $ipaddress = $_[0];
my $community = $_[1];
my $port = $_[2];
my $version = $_[3];
my $timeout = $_[4];
my $OidBase = $_[5];
my $string = $_[6];
my $list_yes = $_[7];
my @return_arry = ();
my @arry_id = ();
my ($session, $error) = Net::SNMP->session(
-community => $community,
-hostname => $ipaddress,
-port => $port,
-version => $version,
-timeout => $timeout
);
my $result = $session->get_table(-baseoid =>$OidBase);
$session->snmp_dispatcher();
for ($session->var_bind_names())
{
#my $type = snmp_type_ntop($session->var_bind_types()->{$_});
#my $value = $session->var_bind_list()->{$_};
#my $return = "$_,$type,$value \n";
#print $return;
#printf("%s = %s: %s\n",$_,snmp_type_ntop($session->var_bind_types()->{$_}),$session->var_bind_list()->{$_}) if ($opt{l});
if ($list_yes)
{ #list all OID value
printf("%s = %s: %s\n",$_,snmp_type_ntop($session->var_bind_types()->{$_}),$session->var_bind_list()->{$_});
}
else
{
if ( $string eq $session->var_bind_list()->{$_} )
{
#printf("%s = %s: %s ok \n",$_,snmp_type_ntop($session->var_bind_types()->{$_}),$session->var_bind_list()->{$_});
= ($_,$session->var_bind_list()->{$_});
#$length = length($_);
#$ID_in_oid = substr($_,$length-1,1);
@arry_id = split(/\./,$_);
push (@return_arry,[$_,$session->var_bind_list()->{$_},$arry_id[-1]]);
}
}
}
#Close the session
$session->close();
#return
if ($list_yes == 0)
{
return @return_arry;
}
else
{
return "LIST_OID";
}
}