# Copyright (c) 2004 Shirshanka Das 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# Shirshanka Das 
# Computer Science Department
# University of California, Los Angeles
# 3803A Boelter Hall
# Los Angeles, CA 90095-1596
# U.S.A.
# mailto:shanky@cs.ucla.edu
# http://www.cs.ucla.edu/~shanky

#!/usr/bin/perl
use strict;

#This script can be modified to monitor a set of processes that you may be running ..
#It checks for whether the processes are still running goes to sleep for 5 minutes, comes back up 
#Each of the processes can be associated with a number which specifies the number of processes 
#each of which will have the same name e.g. when the process forks a number of other processes. 
#In this example simpleserver forks two processes and therefore appears thrice in the process list
#Thus the number associated with simpleserver is 3. 
#With the restart option the script with shut down the process and then restart them
#Useful if you're running a set of servers and modify their code and want to restart them
#Usage : server_checker.pl [restart]
#


my %processes = ("torrent_server",1,
	      "tracker",1,
	      "simpleserver",3,
	      "queerservernotcp",2,
	      "weirdserver",3,
	      "almostgoodserver",3);


my $restart = undef;
$restart = shift;
my $hostname = `hostname`;
chomp($hostname);
while (1) { 
check_for_entity_restart_if_needed("torrent_server");
check_for_entity_restart_if_needed("tracker");
check_for_entity_restart_if_needed("simpleserver");
check_for_entity_restart_if_needed("queerservernotcp");
check_for_entity_restart_if_needed("weirdserver");
check_for_entity_restart_if_needed("almostgoodserver");
# unset the restart value
$restart  = undef;  
  sleep 300;
} 
        

sub check_for_entity_restart_if_needed {
chomp(my $username = `whoami`);
my $pattern = shift;
my @buffer = `ps -ef | grep $username`;
my $line = undef;
 
my $found  = 0;
my @pids = ();

foreach $line(@buffer) {
  if ($line =~ /$username\s+(\d+)\s+(.*)$pattern/) {
       # server still running 
       # sleep 5;
       $found = $found +1;
       @pids = ($1,@pids);
        } 
  } 
 if ($found > 0) { 
   if (defined $restart) { 
   foreach my $pid (@pids) {
    print "Shutting down $pattern: kill $pid \n";
    system("kill -9 $pid");
    }
    sleep 2;   
    
   
         system ("nohup perl $pattern.pl > $pattern\_$hostname &");
         print "started up $pattern\n";
         sleep 2;
  } elsif ($found < $processes{$pattern}) {
         # need to shutdown old processes and restart them 
         system ("perl generic-stop.pl $pattern");
         system ("nohup perl $pattern.pl > $pattern\_$hostname &");
         print "started up $pattern\n";
         sleep 2;
       } 
  

 }
else {
         #start up pattern 
         system ("nohup perl $pattern.pl > $pattern\_$hostname &");
         print "started up $pattern\n";
         sleep 2;
        }
}
   
