"Fossies" - the Fresh Open Source Software Archive

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