[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3master/usr/local/sysmon/ -> mrtg-eth.pl (source)

   1  #! /usr/bin/perl -w
   2  # Modules
   3  use strict;
   4  use Getopt::Long;
   5  ###########################
   6  # mrtg-eth.pl             #
   7  my $version="1.5.5";      #
   8  # Mario Witte             #
   9  # mario.witte@chengfu.net #
  10  ###########################
  11  
  12  ################################################################################
  13  # Configuration                                                                #
  14  my $ssh="/usr/bin/ssh";                   # Path to ssh                        #
  15  my $ssh_opt="-o 'BatchMode yes' ";        # ssh-Options                        #
  16  $ssh_opt.="-o 'StrictHostKeyChecking no'";#                                    #
  17  my $devinfo="/proc/net/dev";              # Where to read device info from     #
  18  my $in_pos=0;                             # Position of bytes_in in $devinfo   #
  19  my $out_pos=8;                            # Position of bytes_out in $devinfo  #
  20  my $reverse=0;                            # reverse in/out bytes in output     #
  21  ################################################################################
  22  
  23  # Declare some variables
  24  my $help=0;
  25  my $helptext;
  26  my $device;
  27  my $remote_host;
  28  my $identity_file;
  29  my $remote_user;
  30  my $remote_port;
  31  my $ssh_protocol;
  32  my %devinfo;
  33  
  34  # Read Commandline parameters
  35  &GetOptions(    "device=s"    =>    \$device,
  36          "remotehost:s"    =>    \$remote_host,
  37          "identity:s"    =>    \$identity_file,
  38          "login:s"    =>    \$remote_user,
  39          "port:i"    =>    \$remote_port,
  40          "protocol:i"    =>    \$ssh_protocol,
  41          "pos_in:i"    =>    \$in_pos,
  42          "pos_out:i"    =>    \$out_pos,
  43          "t"    =>    \$reverse,
  44          "help"    =>    \$help);
  45  
  46  # If requested or no parameters given display help
  47  if (!$device) { $help=1; $helptext=""; }
  48  
  49  # Check if devicename is valid 
  50  if (($device) && ($device=~/^-/)) { 
  51      $help=1;
  52      $helptext.="'$device' doesn't look like a device name\n"; 
  53  } # end if $device
  54  
  55  # Check if remotehost is valid
  56  if (($remote_host) && ($remote_host=~/^-.{0,3}/)) {
  57      $help=1;
  58      $helptext.="'$remote_host' doesn't seem to be a hostname\n";
  59  } # end if $remote
  60  
  61  # Open help if requested/needed
  62  if ($help==1) {
  63      &help("$helptext");
  64      exit;
  65  }
  66  
  67  if( $ssh_protocol ) {
  68      if( $ssh_protocol == 1 or $ssh_protocol == 2 ) {
  69          $ssh_opt .= " -$ssh_protocol";
  70      }
  71  }
  72  
  73  if( $identity_file ) {
  74      $ssh_opt .= " -i $identity_file";
  75      $ENV{'SSH_AUTH_SOCK'} = '';
  76  }
  77  
  78  if( $remote_user ) {
  79      $ssh_opt .= " -l $remote_user";
  80  }
  81  
  82  if( $remote_port ) {
  83      $ssh_opt .= " -p $remote_port";
  84  }
  85  
  86  # Read statistics
  87  if ($remote_host) { # remote host given, connect via ssh
  88      my $ssh_cmd = "$ssh $ssh_opt $remote_host cat $devinfo";
  89      open (DEV, "$ssh_cmd|");
  90  } else { # read from localhost
  91      open (DEV, "< $devinfo");
  92  }
  93  
  94  map { @{$devinfo{$1}} = split /\s+/, $2 if( m/^\s*(.*):\s*(.*)$/); } <DEV>;
  95  close DEV;
  96  
  97  if (scalar keys %devinfo == 0) { &help("Could not read device info"); exit; }
  98  
  99  if( ! defined $devinfo{$device} ) { &help("device $device not found"); exit; }
 100  
 101  my $bytesin = $devinfo{$device}->[$in_pos];
 102  my $bytesout = $devinfo{$device}->[$out_pos];
 103  
 104  # Print Bytes per second to stdout
 105  if ($reverse == 0) { print $bytesin . "\n"; }
 106  print $bytesout . "\n";
 107  if ($reverse == 1) { print $bytesin . "\n"; }
 108  
 109  # Exit
 110  exit;
 111  
 112  
 113  # Subs
 114  sub help($) {
 115      if ($_[0]) { print "There were errors:\n $_[0]\n"; }
 116      print "mrtg-eth.pl version $version - mario.witte\@chengfu.net\n";
 117      print "\n";
 118      print "Usage: mrtg-eth.pl -d device [-r host [-l login] [-i identity] [--port port] [--protocol 1|2]] [--pos_in n] [--pos_out n] [-t] [-b]\n"; 
 119      print "\n";
 120      print "Options:\n";
 121      print "\t-d device   - Device to be monitored (e.g. eth0, ippp1)\n";
 122      print "\t-r host     - If set, will try to connect to remote\n";
 123      print "\t              host via ssh (SSH)\n";
 124      print "\t-l login    - user on remote host (SSH)\n";
 125      print "\t-i identity - use this private-key to connect to remote host (SSH)\n";
 126      print "\t--protocol  - use Protocol 1 or 2 to connect to remote host (SH)\n";
 127      print "\t--port p    - remote-sshd listens on port p (SSH)\n";
 128      print "\t\n";
 129      print "\t--pos_in n  - Position of bytes_in in $devinfo\n";
 130      print "\t--pos_out n - Position of bytes_out in $devinfo\n";
 131      print "\t\n";
 132      print "\t-t          - reverse in/out bytes in output\n";
 133      print "\n";
 134      print "Options marked with '(SSH)' are only useful when connecting\n";
 135      print "to a remote host using SSH\n";
 136      print "\n";
 137  } # end sub help


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