"Fossies" - the Fresh Open Source Software Archive

Member "checksuite-3.3/checkinodes" (3 Jun 2010, 4217 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 # Inode Monitor by Larry Long - larry@djslyde.com      #
    4 # [checkinodes] - 3/9 programs in checksuite v3.3      #
    5 #                              #
    6 # This greps from the 'df' command for the inode       #
    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: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,$util,@disk_fields,@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 = '85' 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/checkinodes";
   38 $logdate = `date '+%m/%d/%Y %H:%M:%S' `;
   39 $script = " - [checksuite] checkinodes\n";
   40 $logsnip = "----\n";
   41 $notify = 0;
   42 $subject = "[checksuite] advisory - inode utilization is high on $host";
   43 $psubject = "[checksuite] checkinodes";
   44 chomp $host;chomp $logdate;chomp $subject;chomp $psubject;
   45 
   46 # Pull the inode utilization
   47 open(DISK, "df -iP -x none -x tmpfs -x iso9660|grep dev|");
   48 while(<DISK>)
   49    {
   50    @disk_fields = split(/\s+/, $_);
   51    $util = $disk_fields[4];
   52    $util =~ s/\%//g;
   53    if($util >= $threshold)
   54       {
   55       push(@note, "Current inode utilization for $disk_fields[5] is equal to or above $threshold: $disk_fields[4]\n\n");
   56       push(@pnote, "$disk_fields[5] is at $disk_fields[4]\n");
   57       $notify++;
   58       }
   59    }
   60 close(DISK);
   61 
   62 # Define where the output goes
   63 if($notify > 0)
   64    {
   65    $total = `df -iT -x none -x tmpfs -x iso9660`;
   66    push(@note, "Total Inode Utilization:\n$total\n");
   67    log_data() if exists $opt{l};
   68    email_data() if exists $opt{e};
   69    screen_data() if exists $opt{o};
   70    pager_data() if exists $opt{p};
   71    }
   72 
   73 # Subroutines
   74 sub usage_info
   75    {
   76    my $usage = "
   77 Usage: $0 [-h | -lo] [-t <threshold>] [-e <email>] [-p <email>]
   78 Options:
   79 -h              display this help
   80 -l              log the output to /var/log/checksuite.d/checkinodes
   81 -o              force output to screen
   82 -t              sets the threshold for notification
   83 -e              e-mail the output to a specified e-mail address
   84 -p              send shortened output to a pager or cell phone
   85 Where:
   86 <threshold>     threshold value - default is 85 percent
   87 <email>         e-mail address of the recipient of the notification
   88         default is 'root'
   89 \n";
   90    die $usage;
   91    }
   92 
   93 sub log_data
   94    {
   95    open(LOG, ">>$logfile") or die "Can't open logfile!\n";
   96    print LOG $logdate,$script,@note,$logsnip;
   97    close(LOG);
   98    }
   99 
  100 sub screen_data
  101    {
  102    print STDERR @note;
  103    }
  104 
  105 sub email_data
  106    {
  107    my $smtp = Net::SMTP->new($host);
  108    if(! ref($smtp))
  109       {
  110       log_die("Cannot connect to SMTP\n");
  111       }
  112    $smtp->mail($email);
  113    $smtp->to($email);
  114    $smtp->data();
  115    $smtp->datasend("To: " . $email . "\n");
  116    $smtp->datasend("From: Checksuite Notification <root\@$host>\n");
  117    $smtp->datasend("Return-Path: " . $email. "\n");
  118    $smtp->datasend("Subject: " . $subject . "\n");
  119    $smtp->datasend("\n");
  120    $smtp->datasend(@note);
  121    $smtp->datasend();
  122    $smtp->quit();
  123    }
  124 
  125 sub pager_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($pmail);
  133    $smtp->to($pmail);
  134    $smtp->data();
  135    $smtp->datasend("To: " . $pmail . "\n");
  136    $smtp->datasend("From: Checksuite Notification <root\@$host>\n");
  137    $smtp->datasend("Return-Path: " . $pmail. "\n");
  138    $smtp->datasend("Subject: " . $psubject . "\n");
  139    $smtp->datasend("\n");
  140    $smtp->datasend(@pnote);
  141    $smtp->datasend();
  142    $smtp->quit();
  143    }