WMI template + proc

Subhendu Ghosh sghosh at sghosh.org
Wed Mar 24 00:55:05 CET 2004


FYI:
Attached is the WMI plugin template and an initial stab at the process 
list.

Need to regex filters for proc name and and posible w/c thresholds for 
VirtualSize

-- 
-sg


-------------- next part --------------
#!/usr/bin/perl

# Copyright 2004 Nagios Plugin Development Team
#
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#    Author: Subhendu Ghosh
# 
#  $Id: $

use Win32;
use Win32::OLE qw(in);
use Getopt::Long;
use lib ".";
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use constant wbemFlagForwardOnly       => 0x20;
use constant wbemFlagReturnImmediately => 0x10;
use constant PerfFlag => wbemFlagForwardOnly | wbemFlagReturnImmediately ;

my ($PROGNAME,$opt_V,$opt_h,$opt_t,$verbose,$hostname,$auth,$user,$pass,
	$warn,$crit,$regex,$flag,$wmi,$whd);

$PROGNAME = "check_wmi_template";
$flag = PerfFlag;

process_arguments();

$status = 'OK';

wmi_connect($hostname,$namespace,$user,$pass);

# Main code goes here - use $whd->InstancesOf(classes)





exit $ERRORS{$status};


###################################################################
#Subs

sub wmi_connect {
	my $host = shift ;	# hostname
	my $ns   = shift ;	# namespace
	my $us   = shift ;	# username
	my $pw   = shift ;	# password
	
	my $err;
	
	$wmi = Win32::OLE->new('WbemScripting.SWbemLocator');
	$whd = $wmi->ConnectServer($host,$ns,$us,$pw);
	$err = $Win32::OLE->LastError();
	if ($err) {
		print "Connect error: $err";
		exit $ERRORS{'UNKNOWN'};
	}
		
	
	
}

sub process_arguments{
	Getopt::Long::Configure('bundling');
	Getopt::Long::Configure("prefix_pattern=(-|\/)" );  # windows style options
	my $status = GetOptions(
		"V" => \$opt_V,		"version"	=> \$opt_V,
		"h" => \$opt_h,		"help"		=> \$opt_h,
		"v" => \$verbose,	"verbose"	=> \$verbose,
		"t=i" => \$opt_t,	"timeout=i"	=> \$opt_t,
		"H=s" => \$hostname,"hostname=s"=> \$hostname,
		"n=s" => \$namespace, "namespace=s"	=> \$namespace,
		"a=s" => \$auth,	"auth=s"	=> \$auth,
		"w=i" => \$warn,	"warn=i"	=> \$warn,
		"c=i" => \$crit,	"critical=i"	=> \$crit,
		"r=s" => \$regex,	"regex=s"		=> \$regex,
		
		
		);
		
		if (defined $opt_V) {
			print_revision( $PROGNAME, '$Revision: $');
			exit $ERRORS{'OK'};
		}
		if (defined $opt_h) {
			print_help();
			exit $ERRORS{'OK'};
		}
		unless (defined $hostname){
			syntax("No hostname specified!");
		}
		if (defined $auth){
			($user,$pass) = split(/:/,$auth);
		}
		
		
		unless ( defined $namespace){
			$namespace = 'root\cimv2';
		}
				
		if (defined $warn || defined $crit) {
			unless( $crit> $warn) {
				syntax("Critical must be greater than warning!") 
			}
		}
			
		
	
	
}

sub print_help {
	print "$PROGNAME -H <hostname> \n";
	print " -H (--hostname)   Hostname to connect to.\n";
	print " -a (--auth)       \"username:password\" credentials \n";
	print "                   You can specify username as domain\username as well\n";
	print " -n (--namespace)  WMI Namespace to connect to (default:root\cimv2)\n";
	print " -w (--warning)    Warning threshold\n";
	print " -c (--critical)   Critical threshold\n";
	
	print " -v (--verbose)    Testing mode with debugging info.\n";
	print " -V (--version)    print version info\n";
	print " -t (--timeout)    Timeout \n";
	print " -h (--help)       This help screen\n";
	
}

sub syntax {
	my $str = shift;
	print "Syntax error: $str\n";
	exit $ERRORS{'UNKNOWN'};
}

-------------- next part --------------
#!/usr/bin/perl

# Copyright 2004 Nagios Plugin Development Team
#
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#    Author: Subhendu Ghosh
# 
#  $Id: $

use Win32;
use Win32::OLE qw(in);
use Getopt::Long;
use lib ".";
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use constant wbemFlagForwardOnly       => 0x20;
use constant wbemFlagReturnImmediately => 0x10;
use constant PerfFlag => wbemFlagForwardOnly | wbemFlagReturnImmediately ;

my ($PROGNAME,$opt_V,$opt_h,$opt_t,$verbose,$hostname,$auth,$user,$pass,
	$warn,$crit,$regex,$nregex,$flag,$wmi,$whd);

$PROGNAME = "check_win32_wmi_process";
$flag = PerfFlag;

process_arguments();
$status = 'OK';
wmi_connect($hostname,$namespace,$user,$pass);

$coll = $whd->InstancesOf("Win32_Process", $flag);

if (!$@ && defined $coll){
	

	foreach $PInst ( in $coll) {
		
		if ($verbose) {
			print "$PInst->{'Name'}\t$PInst->{'ProcessId'}\t$PInst->{'ThreadCount'}\t$PInst->{'VirtualSize'}\n";
			
		}
		
	}
	
}else{
	$status = 'CRITICAL';
	print "WMI_Proc $status:", Win32::OLE->LastError(), "\n";
	
}

exit $ERRORS{$status};

###################################################################
#Subs

sub wmi_connect {
	my $host = shift;					# hostname
	my $ns   = shift || 'root\cimv2' ;	# namespace
	my $us   = shift || '' ;			# username
	my $pw   = shift || '' ;			# password
	
	my $err;
	print "$host,$ns,$us,$pw" if $verbose;
	$wmi = Win32::OLE->new('WbemScripting.SWbemLocator');
	$whd = $wmi->ConnectServer($host,$ns,$us,$pw);
	$err = Win32::OLE->LastError();
	if ($err) {
		print "Connect error: $err";
		exit $ERRORS{'UNKNOWN'};
	}
		
	
	
}

sub process_arguments{
	Getopt::Long::Configure('bundling');
	Getopt::Long::Configure("prefix_pattern=(-|\/)" );  # windows style options
	my $status = GetOptions(
		"V" => \$opt_V,		"version"	=> \$opt_V,
		"h" => \$opt_h,		"help"		=> \$opt_h,
		"v" => \$verbose,	"verbose"	=> \$verbose,
		"t=i" => \$opt_t,	"timeout=i"	=> \$opt_t,
		"H=s" => \$hostname,"hostname=s"=> \$hostname,
		"a=s" => \$auth,	"auth=s"	=> \$auth,
		"n=s" => \$namespace, "namespace=s"	=> \$namespace,
		"w=i" => \$warn,	"warn=i"	=> \$warn,
		"c=i" => \$crit,	"critical=i"	=> \$crit,
		"r=s" => \$regex,	"regex=s"		=> \$regex,
		"n=s" => \$nregex,  "nregex=s"		=> \$nregex,
		
		
		);
		
		if (defined $opt_V) {
			print_revision( $PROGNAME, '$Revision: $');
			exit $ERRORS{'OK'};
		}
		
		if (defined $opt_h) {
			print_help();
			exit $ERRORS{'OK'};
		}
		
		unless (defined $hostname){
			syntax("No hostname specified!");
		}
		
		if (defined $auth){
			($user,$pass) = split(/:/,$auth);
			print "user = $user\npass = $pass\n" if $verbose;
		}else{
			$user = '';
			$pass = '';
		}
		
		unless ( defined $namespace){
			$namespace = 'root\cimv2';
		}
		
		if (defined $warn || defined $crit) {
			unless( $crit> $warn) {
				syntax("Critical must be greater than warning!") 
			}
		}
		
		
		
	
	
}

sub print_help {
	print "$PROGNAME -H <hostname> \n";
	
	print " -H (--hostname)   Hostname to connect to.\n";
	print " -a (--auth)       \"username:password\" credentials \n";
	print "                   You can specify username as domain\username as well\n";
	print " -n (--namespace)  WMI Namespace to connect to (default:root\\cimv2)\n";
	print " -w (--warning)    Warning threshold\n";
	print " -c (--critical)   Critical threshold\n";
	print " -v (--verbose)    Testing mode with debugging info.\n";
	print " -V (--version)    print version info\n";
	print " -t (--timeout)    Timeout \n";
	print " -h (--help)       This help screen\n";
	
}

sub syntax {
	my $str = shift;
	print "Syntax error: $str\n";
	exit $ERRORS{'UNKNOWN'};
}



More information about the Users mailing list