"Fossies" - the Fresh Open Source Software Archive

Member "checksuite-3.3/checkzombie" (3 Jun 2010, 4219 Bytes) of package /linux/privat/old/checksuite-3.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/perl -w
    2 ########################################################
    3 # Z Process Monitor by Larry Long - larry@djslyde.com  #
    4 # [checkzombie] - 9/9 programs in checksuite v3.3      #
    5 #                                                      #
    6 # This checks for processes with the 'Z' state. If     #
    7 # found, it will send an alert and show which ones     #
    8 # are zombies.                                         #
    9 ########################################################
   10 use strict;
   11 use Getopt::Std;
   12 use Net::SMTP;
   13 
   14 $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin';
   15 
   16 # Options: -h (help) -l (log) -o (output to screen) -t (threshold) -e (email)
   17 my %opt;getopts('hlot:e:', \%opt);
   18 usage_info() unless defined @ARGV;
   19 usage_info() if exists $opt{h};
   20 
   21 # Localize variables throughout the rest of the program
   22 my($email,$host,$threshold,@ps_data,$plist,@load,@note,$notify,$logdate,$logfile,$logsnip,$script,$subject,@pnote,$pmail,$psubject,@proc,$proc,$state,$pid,$command);
   23 
   24 # Are we root?
   25 if($> != 0)
   26   {
   27   print STDERR "\n$0: This program HAS to be ran as root!!!\n\nPlease su to root
   28  or use 'sudo $0'!\n";
   29   exit 2;
   30   }
   31 
   32 # Define variables
   33 $threshold = $opt{t};$threshold = '1' unless defined $opt{t};
   34 $email = $opt{e};$email = 'root@localhost' unless defined $opt{e};
   35 $pmail = $opt{p};
   36 $host = `hostname`;
   37 $logfile = "/var/log/checksuite.d/checkzombie";
   38 $logdate = `date '+%m/%d/%Y %H:%M:%S' `;
   39 $script = " - [checksuite] checkzombie\n";
   40 $logsnip = "----\n";
   41 $notify = 0;
   42 $subject = "[checksuite] advisory - processes found with state Z on $host";
   43 $psubject = "[checksuite] checkzombie";
   44 chomp $host;chomp $logdate;chomp $subject;chomp $psubject;
   45 
   46 # Check the processes
   47 push(@note, "\n");
   48 open(PROC, "ps aux | awk '{ print \$8 \" \" \$2 \" \" \$11 }'| grep -v cron| grep -v grep| grep -v sh|");
   49 while(<PROC>)
   50    {
   51    @proc = split(/\s+/, $_);
   52    $state = $proc[0];
   53    $pid = $proc[1];
   54    $command = $proc[2];
   55    if($state eq "Z")
   56       {
   57       push(@note, "Zombie state process found: $pid - $command\n");
   58       $notify++;
   59       }
   60    }
   61 push(@note, "\nYou can force kill a process using 'kill -9' as root.\n\n");
   62 
   63 # Define where the output goes
   64 if($notify > 0)
   65    {
   66    log_data() if exists $opt{l};
   67    email_data() if exists $opt{e};
   68    screen_data() if exists $opt{o};
   69    pager_data() if exists $opt{p};
   70    }
   71 
   72 # Subroutines
   73 sub usage_info
   74    {
   75    my $usage = "
   76 Usage: $0 [-h | -lo] [-t <threshold>] [-e <email>] [-p <email>]
   77 Options:
   78 -h              display this help
   79 -l              log the output to /var/log/checksuite.d/checkzombie
   80 -o              force output to screen
   81 -t              sets the threshold for notification
   82 -e              e-mail the output to a specified e-mail address
   83 -p              send shortened output to a pager or cell phone
   84 Where:
   85 <threshold>     threshold value - default is 1 state Z process
   86 <email>         e-mail address of the recipient of the notification
   87                 default is 'root'
   88 \n";
   89    die $usage;
   90    }
   91 
   92 sub log_data
   93    {
   94    open(LOG, ">>$logfile") or die "Uh oh! Can't open logfile!\n";
   95    print LOG $logdate,$script,@note,$logsnip;
   96    close(LOG);
   97    }
   98 
   99 sub screen_data
  100    {
  101    print STDERR @note;
  102    }
  103 
  104 sub email_data
  105    {
  106    my $smtp = Net::SMTP->new($host);
  107    if(! ref($smtp))
  108       {
  109       log_die("Cannot connect to SMTP\n");
  110       }
  111    $smtp->mail($email);
  112    $smtp->to($email);
  113    $smtp->data();
  114    $smtp->datasend("To: " . $email . "\n");
  115    $smtp->datasend("From: Checksuite Notification <root\@$host>\n");
  116    $smtp->datasend("Return-Path: " . $email. "\n");
  117    $smtp->datasend("Subject: " . $subject . "\n");
  118    $smtp->datasend("\n");
  119    $smtp->datasend(@note);
  120    $smtp->datasend();
  121    $smtp->quit();
  122    }
  123 
  124 sub pager_data
  125    {
  126    my $smtp = Net::SMTP->new($host);
  127    if(! ref($smtp))
  128       {
  129       log_die("Cannot connect to SMTP\n");
  130       }
  131    $smtp->mail($pmail);
  132    $smtp->to($pmail);
  133    $smtp->data();
  134    $smtp->datasend("To: " . $pmail . "\n");
  135    $smtp->datasend("From: Checksuite Notification <root\@$host>\n");
  136    $smtp->datasend("Return-Path: " . $pmail. "\n");
  137    $smtp->datasend("Subject: " . $psubject . "\n");
  138    $smtp->datasend("\n");
  139    $smtp->datasend(@pnote);
  140    $smtp->datasend();
  141    $smtp->quit();
  142    }
  143