PERL code snippets


Linux / Unix& PERL code snippets20 Feb 2008 06:23 am

Another simple script I've written and use often (actually runs via cron every 5 minutes) to verify the contents of a particular page to determine of it has changed for any reason like a webserver cracked/hacked or if the dynamic content is not what is expected.

In this case it is specifically monitoring a page from a PeopleSoft application that I do not entirely trust, if the content doesn't match my checksum that generally means the Tuxedo application server is hosed and needs to be restarted.

Easily adapted to fit your particular needs, is use it on all of my public websites that are on shared hosting accounts etc...

Comments for bigger sections are thrown in to give you an idea of what to tweak for your needs, remember to update $file2 with a new copy of the markup if you modify the landing page you are monitoring!

CODE:
  1. #!/pshome/usr/local/bin/perl
  2.  
  3. # This just checks what the current working directory is,
  4. # if dev or test then set the debug flags to on...etc.
  5. unshift(@INC, "/pshome/psmgr/bin");
  6. unshift(@INC, "/pshome/psmgr/bin.test") if ("/pshome/psmgr/bin.test" eq "$ENV{'PWD'}");
  7. unshift(@INC, "/pshome/psmgr/stat/bin") if ("/pshome/psmgr/stat/bin" eq "$ENV{'PWD'}");
  8. require sr;
  9.  
  10. $debug = 1;
  11. $debug = 1 if ("/home/bin.test" eq "$ENV{'PWD'}");
  12. $debug = 1 if ("/home/stat/bin" eq "$ENV{'PWD'}");
  13. $execute = 1;
  14.  
  15. $file1 = "/home/files/sitename.tmp";
  16. $file2 = "/home/files/sitename_known_good.txt";
  17. $url = "https://yoursite.com/psc/hrmsext/EMPLOYEE/HRMS/c/ROLE_APPLICANT.ER_APPLICANT_HOME.GBL?&";
  18. $url = "https://yoursite.com/psc/test/EMPLOYEE/HRMS/c/ROLE_APPLICANT.ER_APPLICANT_HOME.GBL" if ("/pshome/psmgr/bin.test" eq "$ENV{'PWD'}");
  19. $url = "https://yourseite.com/psc/test/EMPLOYEE/HRMS/c/ROLE_APPLICANT.ER_APPLICANT_HOME.GBL" if ("/pshome/psmgr/stat/bin" eq "$ENV{'PWD'}");
  20. $restarting = "/pshome/psmgr/files/careers.restarting";
  21. $restartingold = "/pshome/psmgr/files/careers.restarting.old";
  22. $check = "0";
  23.  
  24. # check to see if the restart is already in progress so we don't interrupt it
  25. # likely a cleaner way to do this, but it works for now!
  26. if (-e $restarting) { # check if restart file exists
  27.   print "Restart file exists, servers may be restarting, check again in 5 mins\n";
  28.   print "Renaming file and Exiting\n";
  29.   $cmd = "mv $restarting $restartingold";
  30.   print "$cmd\n" if ($debug);
  31.   system ($cmd) if ($execute);
  32.   exit;
  33. } #end if restart file exists
  34. if (-e $restartingold) { # check if restarting.old file still exists
  35.   print "Restarting file existed for at least 5 mins, servers should be back up, rechecking";
  36.   print "Remove restart file\n";
  37.   $check = "1";
  38.   $cmd = "rm $restartingold";
  39.   print "$cmd\n" if ($debug);
  40.   system ($cmd) if ($execute);
  41. } #end if restarting.1 file exists
  42. $cmd = "wget -b --no-check-certificate --output-document=$file1 $url";
  43. print "$cmd\n" if ($debug);
  44. system ($cmd) if ($execute);
  45. $cmd = "sleep 15";
  46. print "$cmd - sleeping 15 secs to allow complete xfer of file\n" if ($debug);
  47. system ($cmd) if ($execute);
  48. $diff1=`cksum $file1`;
  49. $diff2=`cksum $file2`;
  50. $diff1value = substr($diff1, 0, 9);
  51. $diff2value = substr($diff2, 0, 9);
  52. print "diff1value = $diff1value\n" if ($debug);
  53. print "diff2value = $diff2value\n" if ($debug);
  54. if ($diff1value != $diff2value) {
  55.   &notifydown;
  56.   &repair;
  57.   &cleanup;
  58.   exit;
  59.   } else {
  60.   print "Files match, this site is up!\n";
  61.   &cleanup;
  62.   print "check = $check\n" if ($debug);
  63.   if ($check == "1") { #check if recovering from restart
  64.     &notifyup;
  65.   } # end check if recovered from restart 
  66.   exit;
  67. }
  68. sub notifydown
  69. {
  70. print "This site appears to be down, sending page and email\n" if ($debug);
  71. &sr::send_email($debug,"Site Down!","Site is down, restarting app servers now!","David Cochran");
  72. &sr::send_page($debug,"Careers site is down, restarting app servers now!","David Cochran");
  73. } # end sub notifydown
  74. sub repair
  75. { # restart HRMSEXT App servers
  76. $cmd = "mv $file1 $restarting";
  77. print "$cmd\n" if ($debug);
  78. system ($cmd) if ($execute);
  79. $cmd = "/commands to restart the application server go here";
  80. print "$cmd\n" if ($debug);
  81. system ($cmd) if ($execute);
  82. } #end sub repair
  83. sub cleanup
  84. { # cleanup temp files
  85. $cmd = "rm -f wget-log*";
  86. print "$cmd\n" if ($debug);
  87. system ($cmd) if ($execute);
  88. $cmd = "rm -f $file1";
  89. print "$cmd\n" if ($debug);
  90. system ($cmd) if ($execute);
  91. } #end sub cleanup
  92. sub notifyup
  93. {
  94. print "site is back up, sending page and email.\n" if ($debug);
  95. &sr::send_email($debug,"site is OK!","Careers site is OK.","David Cochran");
  96. &sr::send_page($debug,"Careers site is back up","David Cochran");
  97. } # end sub notifyup

PERL code snippets07 Feb 2008 11:39 am
MySQL Examples& PERL code snippets15 Jun 2006 12:21 pm
PERL code snippets& Webmaster / SEO26 Apr 2006 11:02 am
PERL code snippets24 Apr 2006 04:09 pm

Next Page »


Web Hosting - Wordpress Themes - Credit Consolidation - Reno Nevada Real Estate - Credit Consolidation
X10 Home Security|Dakar's Photos
Listed on BlogShares