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”");

  }

How to install Open SSH on win32 (XP)

  1. download OpenSSH from http://sshwindows.sourceforge.net
  2. Unzip it, and run the resulting installer
  3. Install the program to the directory of your choice (I will use c:OpenSSH).  You will be best served by putting OpenSSH into a directory that does not contain any spaces.
  4. Take the defaults for the remaining setup screens
  5. After OpenSSH is installed, open a command prompt and cd to the OpenSSHBin directory
  6. Run mkgroup -l >> ..etcgroup
  7. Run  mkgroup -d >> ..etcgroup
  8. Run mkpasswd -l [-u ] >> ..etcpasswd
  9. Run mkpasswd -d [-u ] >> ..etcpasswd
  10. Run “net start opensshd“
  11. On a seperate system equipped with an SSH client (such as PuTTY), ensure you can connect to the new server using a username and password that is valid for the server
  12. Once you have verified the OpenSSH service is working correctly, go ahead and “net stop opensshd“ to stop it
  13. From the command window on the server, cdopensshetc
  14. Copy sshd_config to sshd_config.bak just in case something goes horribly wrong and you want to return to the default configuration and start over
  15. cd ..bin
  16. Run “ssh-keygen -t rsa“ (we will be generating an SSH2 RSA key, which is the currently recommended key)
  17. When asked for where we want the key files to be placed, DO NOT TAKE THE SUGGESTED LOCATION.  Instead enter “/etc/id_rsa“ (the use of “/“ is important here, you cannot use ““)
  18. Enter a passphrase (you can leave the passphrase blank, but a passphrase will prevent the key from being used if someone happens to acquire it)
  19. Enter the passphrase a second time
  20. Observe two files being generated, “id_rsa“ (your private key, which your client machines will need) and “id_rsa.pub“ (your public key, which stays on your server)
  21. Run “cd ..etc“
  22. Perform a “dir“ and ensure the two files are there.  If they aren’t go find them and put them there.
  23. Run “copy id_rsa.pub authorized_keys“  The goal is to make a copy of the public key file, named “authorized_keys“
  24. Grab the other file (id_rsa) and copy it by whatever means necessary (network, floppy, usb flash disk, pigeon, whatever it takes) to the machine you will be using as the client.  (This file is your private key, and should be treated as highly confidential.  If you used a well-thought-out passphrase when generating the key, then it is nigh unusable if someone comes across it.  Otherwise, if someone gets a hold of this file, they can IMPERSONATE YOU when connecting to your SSH server.  Pass phrase or not, this file should be treated as sensitive, and erased from whatever media you used to move it to your client.)
  25. Run “net start opensshd“ and check in the Services applet that is has started and is running.
  26. On the client, assuming you have SSH client tools installed, you can run “ssh -l “ from the directory where the id_rsa file was copied, and it should prompt you for the pass phrase, if you entered on during key generation.  Once the pass phrase is entered, it should connect to the SSH server and give you a command prompt.