# findproc.pl searches for specified process on remote NT computers # and reports computers running the process (program). # Written 11-Jun-1998 by fhb. # Usage: findproc.pl procname computerfile # where procname is process name as reported by PULIST (reskit tool), # and computerfile contains list of remote computer names. # # Simple child of chkjobs.pl. # # blat must be installed in order for /notify to work. require "ctime.pl"; $bDebug = 0; MAIN: $sAppName = "FindProc"; $sDate = &ctime(time); $bNotify = 0; $sRptFile = "findproc-msg.rpt"; # set default for notify $sTempFile = "t.t"; $sTmp = ""; $sToday = substr($sDate,4); $nTotalFound = 0; $nTotalComputers = 0; $bUseRptFile = 0; $sVersion = "1.0"; print "$sAppName $sVersion "; unless ($#ARGV + 1) { die "Missing data file on command line.\n"; # somebody give me an arg! } $sTargProc = $ARGV[0]; $sTargProc =~ tr/A-Z/a-z/; # convert to lowercase $sComputerFile = $ARGV[1]; ParseCmdArgs(); # must FOLLOW any arg use! print "Searching computers for $sTargProc at $sToday..."; # Check each computer: open(FPCDATA,$sComputerFile) || die "Can't open file $sComputerFile: $!\n"; if ($bUseRptFile) { $sTmp = sprintf(">%s",$sRptFile); if ($bDebug) { print "sTmp = $sTmp\n"; } open(RPTFILE,$sTmp) || die "Can't open file $sRptFile: $!\n"; print RPTFILE "$sAppName $sVersion report generated at $sToday\n"; } while () { if (!/^\!/) { # skip comments chop $_; s/\s(.*)//; # remove whitespace s/^\\\\//; # remove leading '\\' $sComputer = $_; ChkForProc(); ++$nTotalComputers; print "."; } } close(FPCDATA); print "\n$sAppName: checked $nTotalComputers computers.\n"; if ($nTotalFound > 0) { $sSuffix = "s"; if ($nTotalFound == 1) { $sSuffix = ""; } $sTmp = "$nTotalFound computer$sSuffix running $sTargProc.\n"; print $sTmp; if ($bUseRptFile) { print RPTFILE $sTmp; close(RPTFILE); } if ($bNotify) { Notify(); } } else { print "$sTargProc not found.\n"; if ($bUseRptFile) { # nothing to report, so unlink $sRptFile; # delete empty rpt file } } ########### subroutines eh? ############ sub ChkForProc { # Capture output of PULIST command to list running processes in temp file: $sCmd = sprintf("pulist \\\\%s >%s",$sComputer,$sTempFile); if ($bDebug) { print "dbg> $sCmd\n"; } system($sCmd); $bFound = 0; # Check each line in file for match: open(FTEMP,$sTempFile) || print "Can't open file $sTempFile: $!\n"; while () { chop $_; $nLen = length($sTargProc); $sThisProc = substr($_,0,$nLen); $sThisProc =~ tr/A-Z/a-z/; # convert to lowercase if ($bDebug) { print "dbg> proc=$sThisProc,target=$sTargProc\n"; } if ($sTargProc eq $sThisProc) { $nTotalFound++; $bFound = 1; $sTmp = "$sTargProc running on $sComputer\n"; print $sTmp; if ($bUseRptFile) { $sTmp = "$sComputer\n"; print RPTFILE $sTmp; } last; } } close(FTEMP); if ($bDebug == 0) { unlink $sTempFile; } } sub Help { printf("%s version %s\n",$sAppName,$sVersion); printf("\nUsage: %s procname computerlist [options]\n",$sAppName); print "\nOptions: /output=filename <-- save output to a file\n"; print " /notify <-- email output \n"; } sub Notify { @Recipients = ('fran.jones','peter.key','jay.messier'); $sMailDomain = "ci.tacoma.wa.us"; print "Sending email...\n"; foreach $sName (@Recipients) { $sToList .= sprintf("%s\@%s,",$sName,$sMailDomain); } chop($sToList); # chop last comma from recipient list $sCmd = sprintf("blat %s -t %s -s \"Computers running %s\"",$sRptFile,$sToList,$sTargProc); if ($bDebug) { print "dbg: $sCmd\n"; } else { system($sCmd); } } sub ParseCmdArgs { while (@ARGV) { $_ = shift @ARGV; if ($bDebug) { print "dbg: parsing $_\n"; } if (/^[\/\-]/) { # is arg a switch? $arg = substr($_,1,3); # parse 1st 3 chars: # ternary operator doesn't work so don't use it! if ($arg eq "not") { $bNotify = 1; $bUseRptFile = 1; # need file to email to recipients } if (($arg eq "hel") || (substr($arg,0,1) eq "?")) { &Help(); exit; } if ($arg eq "out") { $i = index($_,"="); $i++; $sRptFile = sprintf("%s",substr($_,$i)); if ($sRptFile eq $sTempFile) { $sRptFile .= "2"; # prevent dup filename } $bUseRptFile = 1; } } } if ($bDebug) { print "dbg: bNotify = $bNotify\n"; print "dbg: sRptFile = $sRptFile\n"; print "dbg: bUseRptFile = $bUseRptFile\n"; } }