"Fossies" - the Fresh Open Source Software Archive

Member "checksuite-3.3/checkdisk" (3 Jun 2010, 5046 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 # Disk Monitor by Larry Long - larry@djslyde.com       #
    4 # [checkdisk] - 1/9 programs in checksuite v3.3        #
    5 #                              #
    6 # This greps from the 'df' command for the disk        #
    7 # utilization and if it goes over a certain threshold, #
    8 # it 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) -t (threshold) -e (email) -p (pager)
   17 my %opt;getopts('hlot:x: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,$util,@disk_fields,@note,$notify,$logdate,$logfile,$logsnip,@df,$du,$script,$total,$subject,@pnote,$pmail,$psubject,$exclude);
   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 $host = `hostname`;
   34 $threshold = $opt{t};$threshold = '85' unless defined $opt{t};
   35 $email = $opt{e};$email = 'root@localhost' unless defined $opt{e};
   36 $pmail = $opt{p};
   37 $exclude = $opt{x};$exclude =~ s/\///;
   38 $logfile = "/var/log/checksuite.d/checkdisk";
   39 $logdate = `date '+%m/%d/%Y %H:%M:%S' `;
   40 $script = " - [checksuite] checkdisk\n";
   41 $logsnip = "----\n";
   42 $notify = 0;
   43 $subject = "[checksuite] advisory - disk utilization is high on $host";
   44 $psubject = "[checksuite] checkdisk";
   45 chomp $host;chomp $logdate;chomp $subject;chomp $psubject;chomp $exclude;
   46 
   47 # Pull the disk utilization
   48 push(@note, "\n");
   49 open(DISK, "df -hP -x none -x tmpfs -x $exclude -x iso9660|grep dev|");
   50 while(<DISK>)
   51    {
   52    @disk_fields = split(/\s+/, $_);
   53    $util = $disk_fields[4];
   54    $util =~ s/\%//g;
   55    if($util >= $threshold)
   56       {
   57       push(@note, "Current disk utilization for $disk_fields[5] is equal to or above $threshold: $disk_fields[4]\n\n");
   58       push(@pnote, "$disk_fields[5] is at $disk_fields[4]\n");
   59       if(($disk_fields[5] eq "/") && (exists $opt{x}))
   60          {
   61          $du = `cd / && ls -I *proc* -I $exclude /|xargs du -hs --exclude *$exclude*`;
   62          push(@note, "Disk Usage for $disk_fields[5] by Content:\n$du\n");
   63          $notify++;
   64          }
   65       elsif($disk_fields[5] eq "/")
   66          {
   67          $du = `cd / && ls -I *proc* /|xargs du -hs`;
   68          push(@note, "Disk Usage for $disk_fields[5] by Content:\n$du\n");
   69          $notify++;
   70          }
   71       else
   72          {
   73          $du = `du -hs $disk_fields[5]/*`;
   74          push(@note, "Disk Usage for $disk_fields[5] by Content:\n$du\n");
   75          $notify++;
   76          }
   77       }
   78    }
   79 close(DISK);
   80 
   81 # Define where the output goes
   82 if($notify > 0)
   83    {
   84    $total = `df -hP -x none -x tmpfs -x iso9660`;
   85    push(@note, "Disk Usage Summary:\n$total");
   86    log_data() if exists $opt{l};
   87    email_data() if exists $opt{e};
   88    screen_data() if exists $opt{o};
   89    pager_data() if exists $opt{p};
   90    }
   91 
   92 # Subroutines
   93 sub usage_info
   94    {
   95    my $usage = "
   96 Usage: $0 [-h | -lo] [-x <directory>] [-t <threshold>] [-e <email>] [-p <email>]
   97 Options:
   98 -h              display this help
   99 -l              log the output to /var/log/checksuite.d/checkdisk
  100 -o              force output to screen
  101 -x      exclude a specific directory of the root directory
  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 <directory> directory to exclude from the root directory
  107 <threshold>     threshold value - default is 85 percent
  108 <email>         e-mail address of the recipient of the notification
  109         default is 'root'
  110 \n";
  111    die $usage;
  112    }
  113 
  114 sub log_data
  115    {
  116    open(LOG, ">>$logfile") or die "Uh oh! Can't open logfile!\n";
  117    print LOG $logdate,$script,@note,$logsnip;
  118    close(LOG);
  119    }
  120 
  121 sub screen_data
  122    {
  123    print STDERR @note;
  124    }
  125 
  126 sub email_data
  127    {
  128    my $smtp = Net::SMTP->new($host);
  129    if(! ref($smtp))
  130       {
  131       log_die("Cannot connect to SMTP\n");
  132       }
  133    $smtp->mail($email);
  134    $smtp->to($email);
  135    $smtp->data();
  136    $smtp->datasend("To: " . $email . "\n");
  137    $smtp->datasend("From: Checksuite Notification <root\@$host>\n");
  138    $smtp->datasend("Return-Path: " . $email. "\n");
  139    $smtp->datasend("Subject: " . $subject . "\n");
  140    $smtp->datasend("\n");
  141    $smtp->datasend(@note);
  142    $smtp->datasend();
  143    $smtp->quit();
  144    }
  145 
  146 sub pager_data
  147    {
  148    my $smtp = Net::SMTP->new($host);
  149    if(! ref($smtp))
  150       {
  151       log_die("Cannot connect to SMTP\n");
  152       }
  153    $smtp->mail($pmail);
  154    $smtp->to($pmail);
  155    $smtp->data();
  156    $smtp->datasend("To: " . $pmail . "\n");
  157    $smtp->datasend("From: Checksuite Notification <root\@$host>\n");
  158    $smtp->datasend("Return-Path: " . $pmail. "\n");
  159    $smtp->datasend("Subject: " . $psubject . "\n");
  160    $smtp->datasend("\n");
  161    $smtp->datasend(@pnote);
  162    $smtp->datasend();
  163    $smtp->quit();
  164    }