[EMAIL PROTECTED] ha scritto:

>> Is your amavisd-new is up and running, 
 > how to restart it?

via console:

/usr/local/bin/restartsmtpscan.py

I use a small script (found in this Mail
List and a little modified by me) to check some daemons.
You can add some other daemon, if you need...


-> in crontab:
*/2 * * * *     /usr/sbin/check_daemons.sh >> /var/log/check_daemons.log



-> # cat /usr/sbin/check_daemons.sh
#! /bin/sh
perl /usr/sbin/check_daemons.pl >> /var/log/check_daemons.log
echo "--------------------- done -------------------" >> 
/var/log/check_daemons.log
tail -n 500 /var/log/check_daemons.log > /var/log/check_daemons.log.tmp
rm /var/log/check_daemons.log
mv /var/log/check_daemons.log.tmp /var/log/check_daemons.log
exit 0



-> # cat /usr/sbin/check_daemons.pl
#!/usr/bin/perl

# ========================================================================
# File    : check_daemons.pl
# Purpose : Runs from cron.
# Author: Tim Baker & Mike Tremaine mgt-@@-stellarcore.net
# ========================================================================
# This is cron'd to run once every  15mins (0,15,30,45)

use strict;

&init_global();

#Check Daemons everytime we run
foreach my $daemon( keys %{ $global::daemons } ) {
   &check_daemon( $daemon );
   }

exit;


# ------------------------------------------------------------------------
# init_global() - setup the Lumberjack stream
#
# in    : none
# out   : none
# glob  : Lots
# err   : none
# notes : none
# ------------------------------------------------------------------------

sub init_global {

#Logs
$global::log_file               = "check_daemon.log";
$global::log_dir                = "/var/log/check_daemon.log";
$global::run_dir                = "/var/run";

#Commands
$global::p_rm                   = "/bin/rm";
$global::p_xargs                = "/bin/xargs";
$global::p_find                 = "/bin/find";
$global::ps_cmd         = '/bin/ps';
$global::ps_flags       = '-f -p';
$global::grep_cmd       = '/bin/grep';
$global::grep_flags     = '';

#Daemon Config
# Info about the daemons to watch
$global::daemons = {
         spamd  => {
                 greptest => 'spamd',
                 pidfile  => "$global::run_dir/spamd/spamd.pid",
                 command  => "rm /var/run/spamd/spamd.pid && 
/usr/local/bin/restartspamassassin.py"
                            },
         sshd  => {
                 greptest => 'sshd',
                 pidfile  => "$global::run_dir/sshd.pid",
                 command  => "/usr/local/bin/restartssh"
                            },
         amavisd  => {
                 greptest => 'amavisd',
                 pidfile  => "$global::run_dir/amavisd/amavisd.pid",
                 command  => "/etc/init.d/amavisd restart"
                         },
         pyzord => {
                 greptest => 'pyzord',
                 pidfile => "$global::run_dir/pyzord.pid",
                 command => "/usr/local/bin/restartpyzord.py"
                         },
         clamd => {
                 greptest => 'clamd',
                 pidfile => "$global::run_dir/clamav/clamd.pid",
                 command => "/usr/local/bin/restartclamav.py"
                         },
         p3scan => {
                 greptest => 'p3scan',
                 pidfile => "$global::run_dir/p3scan/p3scan.pid",
                 command => "rm /var/run/p3scan/p3scan.pid && 
/usr/local/bin/restartpopscan.py"
                         }
         };

#Time values for functions
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year = $year + 1900;
$mon = $mon + 1;

if ($min < 10) { $min = "0" . $min; };
if ($mon < 10) { $mon = "0" . $mon; };
if ($mday < 10) { $mday = "0" . $mday; };
if ($hour < 10) { $hour = "0" . $hour; };

$global::timestamp = "[" . $mon . "/" . $mday . "/" . $year . "  " . 
$hour . ":"
  . $min . " ]";

} # sub init_global

#######################################
#Check Daemons

sub check_daemon {

   my $daemon    = shift;

   my $grep_test = $global::daemons->{$daemon}->{greptest};
   my $pid_file  = $global::daemons->{$daemon}->{pidfile};

   # if there's a PID file , see if that process is running
   if( open( PID, "< $pid_file" ) ) {
     chomp( my $pid = <PID> );
     close PID;

     my $call = "$global::ps_cmd $global::ps_flags $pid | " .
                "$global::grep_cmd '$grep_test' | $global::grep_cmd -v 
'tail -f '
";

     my @daemons = grep { index( $_, 'grep' ) == -1 } `$call`;

     # if it is running
     if( (scalar @daemons) ) {
           print "$global::timestamp daemon $daemon is running\n";
       return;
     }
     else {
     #make sure the daemon is not running under another PID
         my $call = "$global::ps_cmd -ef | $global::grep_cmd 
'$grep_test' | " .
         "$global::grep_cmd -v grep | $global::grep_cmd -v 'tail -f '";

          my @daemons = `$call`;

          if( (scalar @daemons) ) {
           print "$global::timestamp daemon $daemon is running but PID 
file is wr
ong\n";
           my $pid_restore = "$global::ps_cmd -ef | $global::grep_cmd 
'$grep_test
' ".
           "| $global::grep_cmd -v grep | $global::grep_cmd -v 'tail -f 
' | awk '
{ print \$2 }'";
           `$pid_restore > $pid_file`;
           print "$global::timestamp daemon '$daemon' $pid_file file 
created\n";
           return;
          } else {
           print "$global::timestamp daemon '$daemon' is not running," .
           "but it left a PID file behind\n";

           # ...and restart the daemon
           start_daemon( $daemon );
          }

     }
   }
   else {
     # is there a daemon running without a pid file?
     my $call = "$global::ps_cmd -ef | $global::grep_cmd '$grep_test' | " .
       "$global::grep_cmd -v grep | $global::grep_cmd -v 'tail -f '";

     my @daemons = `$call`;

     # daemon is already running
     if( (scalar @daemons) ) {
       print "$global::timestamp daemon '$daemon' is running with no PID 
file\n";
           #print @daemons;
           #Be nice to make one -mgt
           my $pid_restore = "$global::ps_cmd -ef | $global::grep_cmd 
'$grep_test
'" .
           "| $global::grep_cmd -v grep | $global::grep_cmd -v 'tail -f 
' | awk '
{ print \$2 }'";
           `$pid_restore > $pid_file`;
       return;
     }
     else {
       print "$global::timestamp daemon '$daemon' is not running\n";
       start_daemon( $daemon );
     }
   }

} # sub check_daemon

# ------------------------------------------------------------------------
# start_daemon() - start up a daemon
#
# in    : the key of the daemon in the global daemons hash
# out   : none
# mod   : none
# glob  : $global::daemons
# err   : none
# notes : none
# ------------------------------------------------------------------------

sub start_daemon {

   my $daemon = shift;

   my $cmd    = $global::daemons->{$daemon}->{command};

   print "$global::timestamp restarting daemon '$daemon'\n";
   system ( $cmd ) == 0 || do {
     print "$global::timestamp Unable to spawn $cmd: $!\n";
     return;
   };
   print "$global::timestamp daemon '$daemon' restarted\n";

} # sub start_daemon
# -- END SCRIPT --

Ciao,
Manuel


-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Efw-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/efw-user

Reply via email to