"Fossies" - the Fresh Open Source Software Archive

Member "checksuite-3.3/checkload" (3 Jun 2010, 5050 Bytes) of package /linux/privat/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 # Load Monitor by Larry Long - larry@djslyde.com       #
    4 # [checkload] - 4/9 programs in checksuite v3.3        #
    5 #                              #
    6 # This greps the 5 minute load from /proc/loadavg      #
    7 # and if it goes over a certain threshold, it can log  #
    8 # and/or e-mail a notification.                        #
    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) -e (email) -p (pager)
   17 my %opt;getopts('hlot:e:p:', \%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,$proc,$pid,@note,$notify,$logdate,$logfile,$logsnip,$script,$subject,@pnote,$pmail,$psubject);
   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 = '7' 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/checkload";
   38 $logdate = `date '+%m/%d/%Y %H:%M:%S' `;
   39 $script = " - [checksuite] checkload\n";
   40 $logsnip = "----\n";
   41 $notify = 0;
   42 $subject = "[checksuite] advisory - load average is high on $host";
   43 $psubject = "[checksuite] checkload";
   44 chomp $host;chomp $logdate;chomp $subject;chomp $psubject;
   45 
   46 # Pull the 5 minute load
   47 push(@note, "\n");
   48 open(LOAD5, "/proc/loadavg") or die "Can't open /proc/loadavg!\n";
   49 while(<LOAD5>)
   50    {
   51    @load = split(/\s+/, $_);
   52    my $defload = 0;
   53    my $sload = $defload + $load[1];chomp $sload;
   54    if($sload ge $threshold)
   55       {
   56       push(@note, "Current 5 minute load average is above threshold ($threshold): $sload\n\n");
   57       push(@pnote, "Load average is $sload\n");
   58       push(@note, "Possible Contributions:\n");
   59 
   60 # Let's single out the processes that are contributing to the load
   61       open(PSLIST, "ps -elf --no-headers|");
   62       while(<PSLIST>)
   63          {
   64          chop;
   65          @ps_data = split(/\s+/, $_);
   66          if($ps_data[5] != 0)
   67             {
   68             $ps_data[15] = " " unless defined $ps_data[15];
   69             $ps_data[16] = " " unless defined $ps_data[16];
   70             $ps_data[17] = " " unless defined $ps_data[17];
   71             $ps_data[18] = " " unless defined $ps_data[18];
   72             $ps_data[19] = " " unless defined $ps_data[19];
   73             $ps_data[20] = " " unless defined $ps_data[20];
   74             $ps_data[21] = " " unless defined $ps_data[21];
   75             push(@note, "pid: $ps_data[9] |cpu time: $ps_data[5] |user: $ps_data[2] |command: $ps_data[14] $ps_data[15] $ps_data[16] $ps_data[17] $ps_data[18] $ps_data[19] $ps_data[20] $ps_data[21]\n");
   76             }
   77          }
   78       close(PSLIST);
   79       $notify++;
   80       }
   81    }
   82 close(LOAD5);
   83 
   84 # Define where the output goes
   85 if($notify > 0)
   86    {
   87    log_data() if exists $opt{l};
   88    email_data() if exists $opt{e};
   89    screen_data() if exists $opt{o};
   90    pager_data() if exists $opt{p};
   91    }
   92 
   93 # Subroutines
   94 sub usage_info
   95    {
   96    my $usage = "
   97 Usage: $0 [-h | -lo] [-t <threshold>] [-e <email>] [-p <email>]
   98 Options:
   99 -h              display this help
  100 -l              log the output to /var/log/checksuite.d/checkload
  101 -o              force output to screen
  102 -t              sets the threshold for notification
  103 -e              e-mail the output to a specified e-mail address
  104 -p              send shortened output to a pager or cell phone
  105 Where:
  106 <threshold>     threshold value - default is 7
  107 <email>         e-mail address of the recipient of the notification
  108         default is 'root'
  109 \n";
  110    die $usage;
  111    }
  112 
  113 sub log_data
  114    {
  115    open(LOG, ">>$logfile") or die "Can't open logfile!\n";
  116    print LOG $logdate,$script,@note,$logsnip;
  117    close(LOG);
  118    }
  119 
  120 sub screen_data
  121    {
  122    print STDERR @note;
  123    }
  124 
  125 sub email_data
  126    {
  127    my $smtp = Net::SMTP->new($host);
  128    if(! ref($smtp))
  129       {
  130       log_die("Cannot connect to SMTP\n");
  131       }
  132    $smtp->mail($email);
  133    $smtp->to($email);
  134    $smtp->data();
  135    $smtp->datasend("To: " . $email . "\n");
  136    $smtp->datasend("From: Checksuite Notification <root\@$host>\n");
  137    $smtp->datasend("Return-Path: " . $email. "\n");
  138    $smtp->datasend("Subject: " . $subject . "\n");
  139    $smtp->datasend("\n");
  140    $smtp->datasend(@note);
  141    $smtp->datasend();
  142    $smtp->quit();
  143    }
  144 
  145 sub pager_data
  146    {
  147    my $smtp = Net::SMTP->new($host);
  148    if(! ref($smtp))
  149       { 
  150       log_die("Cannot connect to SMTP\n");
  151       }
  152    $smtp->mail($pmail);
  153    $smtp->to($pmail);
  154    $smtp->data();
  155    $smtp->datasend("To: " . $pmail . "\n");
  156    $smtp->datasend("From: Checksuite Notification <root\@$host>\n");
  157    $smtp->datasend("Return-Path: " . $pmail. "\n");
  158    $smtp->datasend("Subject: " . $psubject . "\n");
  159    $smtp->datasend("\n");
  160    $smtp->datasend(@pnote);
  161    $smtp->datasend();
  162    $smtp->quit();
  163    }