/[perl]/istatd.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /istatd.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by dpavlin, Sun May 11 12:43:02 2003 UTC revision 1.2 by dpavlin, Tue Jun 24 21:18:21 2003 UTC
# Line 23  use strict; Line 23  use strict;
23  # Basic setup  # Basic setup
24  #  #
25    
26  my $iface               = 'wlan0';   # wireless interface  my @ifaces      = qw(wlan0 eth0 eth1);   # wireless and other interface
27  my $step                = 10;       # sec  my $step        = 10;   # sec
28  my $next1_factor        = 60;       # $step * $next1_factor = 10*60  = 600 sec  = 10 min  my $next1_factor = 60;  # $step * $next1_factor = 10*60  = 600 sec  = 10 min
29  my $next2_factor        = 360;      # $step * $next2_factor = 10*360 = 3600 sec = 1 hour  my $next2_factor = 360; # $step * $next2_factor = 10*360 = 3600 sec = 1 hour
30  my $update_graph_period = 10;       # How often will be graph updated (1..1000)  my $update_graph_period = 10;   # How often will be graph updated (1..1000)
31    
32  #  #
33  # Setup files locations  # Setup files locations
# Line 35  my $update_graph_period = 10;       # Ho Line 35  my $update_graph_period = 10;       # Ho
35    
36  my $wifi_proc_file='/proc/net/wireless';  my $wifi_proc_file='/proc/net/wireless';
37  my $dev_proc_file='/proc/net/dev';  my $dev_proc_file='/proc/net/dev';
38  my $rrd='./istatd.rrd';  my ($pid_file,$base_dir);
39  my $signal_image='signal.png';  if ($0 =~ m#^(.*?)/(.+)\.pl$#i) {
40  my $transfer_image='transfer.png';          $base_dir = $1;
41  my $pid_file='./istatd.pid';          $pid_file = "$base_dir/$2.pid";
42    } else {
43            die "can't decode base dir and script name from '$0'";
44    }
45    
46  #  #
47  # End setup  # End setup
# Line 51  my $switcher=$ARGV[0]; Line 54  my $switcher=$ARGV[0];
54  if (!($switcher)) { $switcher='undef'; }  if (!($switcher)) { $switcher='undef'; }
55    
56  if ($switcher eq 'start') {  if ($switcher eq 'start') {
57      &start();          &start();
58  }  } elsif ($switcher eq 'stop') {
59  elsif ($switcher eq 'stop') {          &stop();
60      &stop();  } elsif ($switcher eq 'graph') {
61  }          foreach (@ifaces) {
62  elsif ($switcher eq 'graph') {                  &update_graph($_);
63      &update_graph();          }
64  }  } elsif ($switcher eq 'install') {
65  elsif ($switcher eq 'install') {          foreach (@ifaces) {
66      &install();                  &install($_);
67  }          }
68  else {  } else {
69      print qq(Usage: ./istatd.pl option          print qq(Usage: ./istatd.pl option
70      option: start   - Starting daemon          option: start   - Starting daemon
71              stop    - Stopping daemon                  stop    - Stopping daemon
72              graph   - Get the latest graph                  graph   - Get the latest graph
73              install - Init database                  install - Init database
74      );          );
75  }  }
76    
77  ###############################################################  ###############################################################
78  sub install {  sub install {
79      my $sec_in_the_day=86400;          my $iface = shift || die "need interface name!";
80      my $sec_in_the_week=$sec_in_the_day*7;          my $rrd = "$base_dir/$iface.rrd";
81      my $sec_in_the_month=$sec_in_the_day*31;          my $sec_in_the_day=86400;
82      my $minvalue=0;          my $sec_in_the_week=$sec_in_the_day*7;
83      my $maxvalue=255;          my $sec_in_the_month=$sec_in_the_day*31;
84      my $xfiles_factor=0.5;          my $minvalue=0;
85      my $max_interval=$step/$xfiles_factor;          my $maxvalue=255;
86      my $number_steps_day=floor($sec_in_the_day/$step);          my $xfiles_factor=0.5;
87      my $number_steps_week=floor($sec_in_the_week/($step*$next1_factor));          my $max_interval=$step/$xfiles_factor;
88      my $number_steps_month=floor($sec_in_the_month/($step*$next2_factor));          my $number_steps_day=floor($sec_in_the_day/$step);
89      RRDs::create ($rrd, "--start",$start-1, "--step",$step,          my $number_steps_week=floor($sec_in_the_week/($step*$next1_factor));
90            "DS:link:GAUGE:$max_interval:$minvalue:$maxvalue",          my $number_steps_month=floor($sec_in_the_month/($step*$next2_factor));
91            "DS:level:GAUGE:$max_interval:$minvalue:$maxvalue",          RRDs::create ($rrd, "--start",$start-1, "--step",$step,
92            "DS:noise:GAUGE:$max_interval:$minvalue:$maxvalue",                  "DS:link:GAUGE:$max_interval:$minvalue:$maxvalue",
93            "DS:in:COUNTER:$max_interval:0:U",                  "DS:level:GAUGE:$max_interval:$minvalue:$maxvalue",
94            "DS:out:COUNTER:$max_interval:0:U",                  "DS:noise:GAUGE:$max_interval:$minvalue:$maxvalue",
95            "RRA:AVERAGE:$xfiles_factor:1:$number_steps_day",                  "DS:in:COUNTER:$max_interval:0:U",
96            "RRA:AVERAGE:$xfiles_factor:$next1_factor:$number_steps_week",                  "DS:out:COUNTER:$max_interval:0:U",
97            "RRA:AVERAGE:$xfiles_factor:$next2_factor:$number_steps_month");                  "RRA:AVERAGE:$xfiles_factor:1:$number_steps_day",
98      my $ERROR = RRDs::error;                  "RRA:AVERAGE:$xfiles_factor:$next1_factor:$number_steps_week",
99      die "$0: unable to create `$rrd': $ERROR\n" if $ERROR;                  "RRA:AVERAGE:$xfiles_factor:$next2_factor:$number_steps_month");
100      print "Done.\n";          my $ERROR = RRDs::error;
101            die "$0: unable to create `$rrd': $ERROR\n" if $ERROR;
102            print "$iface done.\n";
103  }  }
104    
105  ###############################################################  ###############################################################
106  sub start {  sub start {
107      my $pid=fork;          my $pid=fork;
108      exit if $pid;          exit if $pid;
109      die "Could't fork: $!" unless defined($pid);          die "Could't fork: $!" unless defined($pid);
110      POSIX::setsid() or die "Can't start a new session: $!";          POSIX::setsid() or die "Can't start a new session: $!";
111      $SIG{INT}=$SIG{TERM}=$SIG{HUP}=\&syncro_handler;          $SIG{INT}=$SIG{TERM}=$SIG{HUP}=\&syncro_handler;
112      $syncrorun=1;          $syncrorun=1;
113      my $pid_id=$$;          my $pid_id=$$;
114      open(FILE, ">>$pid_file") || die "Cant write to $pid_file file: $!";          open(FILE, ">>$pid_file") || die "Cant write to $pid_file file: $!";
115      print FILE $pid_id;          print FILE $pid_id;
116      close(FILE);          close(FILE);
117      my $counter=0;          my $counter=0;
118      while ($syncrorun) {          while ($syncrorun) {
119          $counter++;                  $counter++;
120          if (!( -e $pid_file)) { $syncrorun=0; }                  if (!( -e $pid_file)) { $syncrorun=0; }
121          &get_data();                  foreach (@ifaces) {
122          if ($counter == $update_graph_period) {                          &get_data($_);
123              &update_graph();                  }
124              $counter=0;                  if ($counter == $update_graph_period) {
125          }                          foreach (@ifaces) {
126          sleep($step);                                  &update_graph($_);
127      }                          }
128                            $counter=0;
129                    }
130                    sleep($step);
131            }
132  }  }
133    
134  ##############################################################  ##############################################################
135  sub stop {  sub stop {
136      my $pid=`cat $pid_file`;          my $pid=`cat $pid_file`;
137      `kill $pid`;          `kill $pid`;
138  }  }
139    
140  ##############################################################  ##############################################################
141  sub update_graph {  sub update_graph {
142      RRDs::graph "$signal_image",          my $iface = shift || die "need interface name!";
143        "--title", " $iface signal statistics",          my $rrd = "$base_dir/$iface.rrd";
144        "--start", "now-12h",  
145        "--end", "now",          RRDs::graph "$iface-signal.png",
146        "--lower-limit=0",                  "--title", " $iface signal statistics",
147        "--interlace",                  "--start", "now-12h",
148        "--imgformat","PNG",                  "--end", "now",
149        "--width=450",                  "--lower-limit=0",
150        "DEF:link=$rrd:link:AVERAGE",                  "--interlace",
151  #      "DEF:level=$rrd:level:AVERAGE",                  "--imgformat","PNG",
152  #      "DEF:noise=$rrd:noise:AVERAGE",                  "--width=450",
153        "AREA:link#00b6e4:link",                  "DEF:link=$rrd:link:AVERAGE",
154  #      "LINE1:level#0022e9:level",  #               "DEF:level=$rrd:level:AVERAGE",
155  #      "LINE1:noise#cc0000:noise"  #               "DEF:noise=$rrd:noise:AVERAGE",
156      ;                  "AREA:link#00b6e4:link",
157      print "ERROR ".RRDs::error."\n" if (RRDs::error);  #               "LINE1:level#0022e9:level",
158    #               "LINE1:noise#cc0000:noise"
159      RRDs::graph "$transfer_image",          ;
160        "--title", " $iface transfer statistics",          print "ERROR ".RRDs::error."\n" if (RRDs::error);
161        "--start", "now-12h",  
162        "--end", "now",          RRDs::graph "$iface-transfer.png",
163        "--lower-limit=0",                  "--title", " $iface transfer statistics",
164        "--interlace",                  "--start", "now-12h",
165        "--imgformat","PNG",                  "--end", "now",
166        "--width=450",                  "--lower-limit=0",
167        "DEF:in=$rrd:in:AVERAGE",                  "--interlace",
168        "DEF:out=$rrd:out:AVERAGE",                  "--imgformat","PNG",
169        "AREA:in#00b6e4:in",                  "--width=450",
170        "LINE1:out#0022e9:out",                  "DEF:in=$rrd:in:AVERAGE",
171      ;                  "DEF:out=$rrd:out:AVERAGE",
172      print "ERROR ".RRDs::error."\n" if (RRDs::error);                  "AREA:in#00b6e4:in",
173                    "LINE1:out#0022e9:out",
174            ;
175            print "ERROR ".RRDs::error."\n" if (RRDs::error);
176  }  }
177    
178  ###############################################################  ###############################################################
179  sub get_data {  sub get_data {
180            my $iface = shift || die "need interface name!";
181            my $rrd = "$base_dir/$iface.rrd";
182    
183          my ($link,$level,$noise,$in,$out) = ("U","U","U","U","U");          my ($link,$level,$noise,$in,$out) = ("U","U","U","U","U");
184    
185          open(FILE, "$wifi_proc_file") || die "Cant open $wifi_proc_file file: $!";          open(FILE, "$wifi_proc_file") || die "Cant open $wifi_proc_file file: $!";

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.26