[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3master/usr/local/sysmon/ -> disk-linux.pl (source)

   1  #!/usr/bin/perl
   2  # disk-linux.pl #####################################################
   3  #
   4  # MRTG Performance Enhancements v1.0.2
   5  #
   6  # This script grabs Linux Performance Monitoring (PM) data for MRTG 
   7  #    on the partition utilization of the system.
   8  # 
   9  # Statistics args accepted:
  10  # 1k-blocks, Used, Available, Use%
  11  #
  12  # Usage: disk-linux.pl host mount stat1 stat2 
  13  # Example: ./disk-linux.pl myhost / Used Available
  14  #
  15  # By:
  16  # Mark Miller Crave Technology markm@cravetechnology.com
  17  # Bill Lynch Crave Technology billl@cravetechnology.com
  18  #####################################################################
  19  
  20  #####  Subroutines  #####
  21  
  22  # Grab the PM data locally
  23  sub localh {
  24  $disk = `df -m`;
  25  $uptime = `uptime`;
  26  }
  27  
  28  # Grab the PM data remotely
  29  sub remoteh {
  30  $disk = `/usr/local/bin/ssh $host df -m`;
  31  $uptime = `/usr/local/bin/ssh $host uptime`;
  32  }
  33  
  34  sub Usage() {    #display correct usage info
  35    print "Usage: disk-linux.pl host mount stat1 stat2\n";
  36    print "      ex: ./disk-linux.pl myhost / Used Available\n";
  37  }
  38  
  39  sub Validate() {    #validate command line args
  40    if ((scalar(@ARGV) > 4) || ($ARGV[0] eq '')) {
  41       print "ERR: Must specify at least one stat to be monitored.\n";
  42       Usage;
  43       exit;
  44    }
  45  }
  46  
  47  sub parseuptime() {
  48  # This sub returns only the number of days of uptime
  49  #  A box with less than 1 day of uptime will show 0 days of uptime
  50  $uptime=~s/,//g;
  51  @utime = split /\s/, $uptime;
  52  for ($i = 1; $i < 8; $i++) {
  53      if ($utime[$i] eq "days" || $utime[$i] eq "day(s)" || $utime[$i] eq "day") {
  54          $upout = $utime[$i-1]." jour(s)";
  55      }
  56  }
  57  if ($upout eq "") { $upout = "0 jour(s)"; }
  58  }
  59  
  60  #####  Main Program Begins Here  ######
  61  Validate();
  62  
  63  # Get the hostnames
  64  $localhost = `/bin/hostname`;
  65  chop($localhost);
  66  $host = $ARGV[0];
  67  
  68  # Determine the short hostname
  69  @local = split(/\./,$localhost);
  70  $shorthost = $local[0];
  71  
  72  # assign command line args to variables
  73  $mount = $ARGV[1];
  74  $stat1 = $ARGV[2];
  75  $stat2 = $ARGV[3];
  76  
  77  # set vars ##########################################################
  78  $count = 0;
  79  $linenum = 0;
  80  
  81  # Determine if the host is local or remote
  82  if ($host eq $localhost) {
  83      localh();
  84  } elsif ($host eq $shorthost) {
  85      localh();
  86  } else {
  87      remoteh();
  88  }
  89  
  90  # parse df -k data
  91  @lines=split /^/m, $disk;
  92  foreach $line (@lines) {
  93    $count++;
  94    $line =~ /^\S+\s+\d+\s+\d+\s+\d+\s+\S+\s+(\S+)/;
  95    if ($1 eq $mount) {
  96      $linenum = $count-1;
  97    }
  98  }
  99  if ($linenum < 1) {
 100    print"ERR: Unknown mount point\n";
 101    Usage();
 102    exit;
 103  }
 104  $line = @lines[$linenum];
 105  $line =~ /^\S+\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/;
 106  $kbytes = $1;
 107  $used = $2;
 108  $avail = $3;
 109  $usepercent = $4;
 110  
 111  # output df -k data in MRTG format
 112  if ($stat1 eq "1k-blocks") { print "$kbytes\n"; }
 113  if ($stat1 eq "Used") { print "$used\n"; }
 114  if ($stat1 eq "Available") { print "$avail\n"; }
 115  if ($stat1 eq "Use%") { print "$usepercent\n"; }
 116  if ($stat2 eq "1k-blocks") { print "$kbytes\n"; }
 117  if ($stat2 eq "Used") { print "$used\n"; }
 118  if ($stat2 eq "Available") { print "$avail\n"; }
 119  if ($stat2 eq "Use%") { print "$usepercent\n"; }
 120  
 121  # Parse uptime data
 122  parseuptime();
 123  
 124  # Print uptime data in MRTG format
 125  print $upout."\n"; 


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1