Perl - Snmp (win32 - Process stats module)
The below Perl script one can quickly modify to look at certain system processes via a windows schedule task. The below script incorporates SNMP traps so it will forward all related alerts to you enterprise management server. See below working example - here I’m checking is the MS SQL server agent (sqlagent.exe AND sqlmangr.exe) is up and running at a given interval via a windows schedule task that runs every 15 minutes.!!!
#!/usr/bin/perl
#
# PROGRAM: Process.pl
# DESCRIPTION: Checks win32 Process Info
# HISTORY
# Ver Date Author Description
# —- ———– ——- ————————————————–
# 1.0 11-Jan-2009 Faizel Isaacs Script will Monitor any
# Give Process on the
# Windows Machine
# And Forward SNMP
# Alerts to Support Staff
# If Process is no longer active
# On system!!!
# —- ———– ——- —————————————————
# Calling all required PERL Modules Needed !!!
use Win32::Process::Info;
use Sys::Hostname;
$host = hostname;
my $pi = Win32::Process::Info->new ();
#===========================================
#== Cleaning Up Previous Temp Files !!! ==#
system(”del F:\Perl\Initial.list”);
system(”del F:\Perl\Refine.list”);
system(”cd F:\Perl”);
#===========================================
# Kicking of the Process Stats !!!
foreach $proc ($pi->GetProcInfo ()) {
print “n”;
foreach (sort keys %$proc) {
open(OUTPUT, ‘>>f:PerlInitial.list’) ||die “can’t write to the file called script_output: $1″;
print OUTPUT “$_ => $proc->{$_}n”;
}
}
close(OUTPUT); # Closing my FileHandle
#==================================================
# Finish with My Process Stats Collection !!!
# Now we specify what we look for (Processes)
# And Filter to a output File I check Later
$pattern = “(sqlagent.exe|sqlmangr.exe|perl.exe|fazel.exe)”; #changes are done here on this line
#===================================================
system(”cd F:\Perl”);
open(INPUT, ‘F:PerlInitial.list’) || die “can’t open the file called Initial.list: $1″;
open(OUTPUT, ‘>>F:PerlRefine.list’) ||die “can’t write to the file called Refine.list: $1″;
while(<INPUT>) {
if ($_ =~(m/$pattern/g)) {
print OUTPUT “$_ n”;
}
}
close(INPUT); #Closing my FileHandle
close(OUTPUT); #Closing my FileHandle
#=============================================================
# Finish Capturing the list of Processes I’m interested IN
# Now we need to loop through them all to see what is running
# If a particular Process is not Found I will raise an Alert!!
#=============================================================
# Now I will go search through my
# Refine.list File for specific Prosses!!
#==============================================================
#================================
# Setting All Variables
# The Below Prc Variables
# I will Raise Alerts For
# If they not Active on the System
#================================
system(”cd F:\Perl”);
$Prc1 = “sqlagent.exe”;
$Prc2 = “sqlmangr.exe”;
$Prc3 = “FAIZEL”;
#=================================
# Will Call Windows Find Command
# For the Processes I’m after
# While searching through my
# Temp file f:PerlRefine.list
#=================================
$look1 = system(”find “$Prc1″ F:\Perl\Refine.list”);
$look2 = system(”find “$Prc2″ F:\Perl\Refine.list”);
$look3 = system(”find “$Prc3″ F:\Perl\Refine.list”);
print “This is my return code for $look1 n”;
#================================
# Now I’m Checking the System
# Return Code, if Code eq 256
# The Process is no longer
# Active on the system
#================================
if ($look1 eq 256) {
system(”trapgen -d SNMP-HOST -o 2.4.7.2.5.2.4286 -g 6 -s 9 -v 2.4.7.2.5.2.4286.4 STRING “SYSTEM1 Procs ApplicationIDRIVE $Prc1 is not running on $host”");
}
if ($look2 eq 256) {
system(”trapgen -d SNMP-HOST -o 2.4.7.2.5.2.4286 -g 6 -s 9 -v 2.4.7.2.5.2.4286.4 STRING “SYSTEM1 Procs ApplicationIDRIVE $Prc2 is not running on $host”");
}
if ($look3 eq 256) {
system(”trapgen -d SNMP-HOST -o 2.4.7.2.5.2.4286 -g 6 -s 9 -v 2.4.7.2.5.2.4286.4 STRING “SYSTEM1 Procs ApplicationIDRIVE $Prc3 is not running on $host”");
}

