Guest User

rrdtools netstat script

a guest
Mar 20th, 2012
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.31 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. #
  4. # script for RRDtool to collect statistics abount active connections
  5. #
  6.  
  7. $step=60;
  8. $base="/Serv/rrd-scripts/sysinfo/db/netstat.rrd";
  9. $path=path_to_img
  10.  
  11. if ($ARGV[0] eq init){
  12.    
  13.     system("rrdtool create $base --step $step".
  14.     " DS:established:GAUGE:200:0:U".
  15.     " RRA:AVERAGE:0.5:1:1440".  # 1440 samples every minute - day
  16.     " RRA:AVERAGE:0.5:5:2016"); # 2016 samples every 5 minute - week
  17.    
  18.     exit;
  19. }
  20.  
  21. if ($ARGV[0] eq update){
  22.  
  23. # load avg
  24.     open LA,"netstat -tns |";
  25.    
  26.     $est=getestablished();
  27.     $n=time;
  28.     system("rrdtool update $base $n:$est");
  29.     #system("echo update $base $n:$est");
  30.    
  31.     exit;
  32. }
  33.  
  34. if ($ARGV[0] eq day){
  35.     system("rrdtool graph $path/day-established.gif -t \"Established Connections\" ".
  36.         "DEF:established=$base:established:AVERAGE ".
  37.         "AREA:established#ff0000:\"Established\" ".
  38.         "-h 200 ");    
  39.     exit;
  40. }
  41.  
  42. if($ARGV[0] eq remove){
  43.     unlink $base;
  44. }
  45.  
  46. if($ARGV[0] eq descr){
  47.     print("Number of ESTABLISHED Connections:$path/day-established.gif\n");
  48.  
  49. }
  50.  
  51.  
  52. sub getestablished()
  53. {
  54.     $est=0;
  55.     while( <LA>){
  56.    
  57.     #print $_;
  58.     $established='(\d*)\sconnections\sestablished';
  59.     if( $_ =~ $established ){
  60.         $est+=$1;
  61.         return $est;
  62.     }
  63.    
  64.     }
  65.     close LA;
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment