Linux / Unix


Linux / Unix06 May 2008 03:49 pm

Simple one line command for listing all installed Perl modules

CODE:
  1. perl -MExtUtils::Installed -e'my $inst = ExtUtils::Installed->new(); print $_, $/ for $inst->modules'

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

Linux / Unix07 Feb 2008 10:50 am

Depending on how you build your filesystem, locating large files such as logs or other output from scripts, programs, or daemons can be frustrating at best. Since I'm not a GUI kind of guy this little snippet for the shell can save tons of time. (tested with KSH and Bash shells, likely should work with most if not all)

CODE:
  1. find / -type f -size +20000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

This will search beginning at the root / directory for all files over 20M. To adapt it for your use simply change the / to represent any beginning path you wish and the 20000k to the minimum filesize you would like to find.

Hard to get any easier than this. A detailed description of the all powerful awk can be found by reading the associated man pages should you be so inclined.

Happy hacking!

Linux / Unix01 Feb 2008 11:22 am

Occasionally recurring jobs will get hung, especially with jobs fired off from cron that are scheduled to run every minute. In my case there is a job that runs every minute to pick up status files from numerous remote servers, the script gets the files, parses them, and then plugs them into a MySQL database. That data is later parsed on demand by a php page to display different status information about the remote servers. All in all it works quite well, until something gets hung, especially if the NFS mount for the database has a hiccup, once the first database connect hangs it tends to hang the subsequent jobs as well... quickly filling up the available memory and of course crashes or hangs any other web pages trying to access the database.

A quick and dirty way to right things without typing or copy/pasting a bazillion pids to a kill -9

CODE:
  1. for pid in ` ps -ef |grep process_name | awk '{print $2}'`
  2. do 
  3. kill -9 $pid
  4. done

Nothing magical about it, the for loop scans the output from the ps command for the process_name, awk parses out the pid #, the loop performs a kill -9 for each pid returned.
Like I mentioned, nothing magical about it, but a quick efficient method of killing what can be hundreds of processes.

Happy hacking!

Linux / Unix17 Jan 2008 11:36 am

Another surprise development by Sun, paying out $1 billion to aquire MySQL, that's reported to be $800K in cash and $200K in options. Rumor had it that Oracle has been trying to aquire MySQL a number of times in the past however they would not sell out to the database holding giant. Likely fearing that it would be squashed or strangled into non-existance or having licensing fees placed on the millions of open source community users.

Sun, having no other real database offering stands to be in a great position in the Open Source community. History shows Sun is willing to stand up for open source projects, MySQL developers should be allowed to continue development to improve the already stable database platform with new funding.

MySQL has a very proven track record with LAMP, which stands for Linux, Apache, MySQL, and PHP as the leading webserver platform used all over the world. No other OS platform can match the performance and stability of LAMP proven time and again in native Linux enviroments. It has forever changed the landscape of the internet as a whole, for large web hosting companies and the general developer.

As reported by ComputerWorld and more details can be found here.

Next Page »


Mortgage - Remortgaging - Internet Advertising - Mortgage - Loans
X10 Home Security|Dakar's Photos
Listed on BlogShares