1 |
dpavlin |
1.1 |
#!/usr/bin/perl -w |
2 |
|
|
|
3 |
|
|
# This program is free software; you can redistribute it and/or modify |
4 |
|
|
# it under the terms of the GNU General Public License as published by |
5 |
|
|
# the Free Software Foundation; either version 2 of the License, or |
6 |
|
|
# (at your option) any later version. |
7 |
|
|
# |
8 |
|
|
# This program is distributed in the hope that it will be useful, |
9 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 |
|
|
# GNU General Public License for more details. |
12 |
|
|
# |
13 |
|
|
# You should have received a copy of the GNU General Public License |
14 |
|
|
# along with this program; if not, write to the Free Software |
15 |
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 |
|
|
# |
17 |
|
|
|
18 |
|
|
use POSIX; |
19 |
|
|
use RRDs; |
20 |
|
|
use strict; |
21 |
|
|
|
22 |
|
|
# |
23 |
|
|
# Basic setup |
24 |
|
|
# |
25 |
|
|
|
26 |
|
|
my $iface = 'wlan0'; # wireless interface |
27 |
|
|
my $step = 10; # sec |
28 |
|
|
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 |
30 |
|
|
my $update_graph_period = 10; # How often will be graph updated (1..1000) |
31 |
|
|
|
32 |
|
|
# |
33 |
|
|
# Setup files locations |
34 |
|
|
# |
35 |
|
|
|
36 |
|
|
my $wifi_proc_file='/proc/net/wireless'; |
37 |
|
|
my $dev_proc_file='/proc/net/dev'; |
38 |
|
|
my $rrd='./istatd.rrd'; |
39 |
|
|
my $signal_image='signal.png'; |
40 |
|
|
my $transfer_image='transfer.png'; |
41 |
|
|
my $pid_file='./istatd.pid'; |
42 |
|
|
|
43 |
|
|
# |
44 |
|
|
# End setup |
45 |
|
|
# |
46 |
|
|
############################################################ |
47 |
|
|
|
48 |
|
|
my $start=time; |
49 |
|
|
my $syncrorun; |
50 |
|
|
my $switcher=$ARGV[0]; |
51 |
|
|
if (!($switcher)) { $switcher='undef'; } |
52 |
|
|
|
53 |
|
|
if ($switcher eq 'start') { |
54 |
|
|
&start(); |
55 |
|
|
} |
56 |
|
|
elsif ($switcher eq 'stop') { |
57 |
|
|
&stop(); |
58 |
|
|
} |
59 |
|
|
elsif ($switcher eq 'graph') { |
60 |
|
|
&update_graph(); |
61 |
|
|
} |
62 |
|
|
elsif ($switcher eq 'install') { |
63 |
|
|
&install(); |
64 |
|
|
} |
65 |
|
|
else { |
66 |
|
|
print qq(Usage: ./istatd.pl option |
67 |
|
|
option: start - Starting daemon |
68 |
|
|
stop - Stopping daemon |
69 |
|
|
graph - Get the latest graph |
70 |
|
|
install - Init database |
71 |
|
|
); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
############################################################### |
75 |
|
|
sub install { |
76 |
|
|
my $sec_in_the_day=86400; |
77 |
|
|
my $sec_in_the_week=$sec_in_the_day*7; |
78 |
|
|
my $sec_in_the_month=$sec_in_the_day*31; |
79 |
|
|
my $minvalue=0; |
80 |
|
|
my $maxvalue=255; |
81 |
|
|
my $xfiles_factor=0.5; |
82 |
|
|
my $max_interval=$step/$xfiles_factor; |
83 |
|
|
my $number_steps_day=floor($sec_in_the_day/$step); |
84 |
|
|
my $number_steps_week=floor($sec_in_the_week/($step*$next1_factor)); |
85 |
|
|
my $number_steps_month=floor($sec_in_the_month/($step*$next2_factor)); |
86 |
|
|
RRDs::create ($rrd, "--start",$start-1, "--step",$step, |
87 |
|
|
"DS:link:GAUGE:$max_interval:$minvalue:$maxvalue", |
88 |
|
|
"DS:level:GAUGE:$max_interval:$minvalue:$maxvalue", |
89 |
|
|
"DS:noise:GAUGE:$max_interval:$minvalue:$maxvalue", |
90 |
|
|
"DS:in:COUNTER:$max_interval:0:U", |
91 |
|
|
"DS:out:COUNTER:$max_interval:0:U", |
92 |
|
|
"RRA:AVERAGE:$xfiles_factor:1:$number_steps_day", |
93 |
|
|
"RRA:AVERAGE:$xfiles_factor:$next1_factor:$number_steps_week", |
94 |
|
|
"RRA:AVERAGE:$xfiles_factor:$next2_factor:$number_steps_month"); |
95 |
|
|
my $ERROR = RRDs::error; |
96 |
|
|
die "$0: unable to create `$rrd': $ERROR\n" if $ERROR; |
97 |
|
|
print "Done.\n"; |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
############################################################### |
101 |
|
|
sub start { |
102 |
|
|
my $pid=fork; |
103 |
|
|
exit if $pid; |
104 |
|
|
die "Could't fork: $!" unless defined($pid); |
105 |
|
|
POSIX::setsid() or die "Can't start a new session: $!"; |
106 |
|
|
$SIG{INT}=$SIG{TERM}=$SIG{HUP}=\&syncro_handler; |
107 |
|
|
$syncrorun=1; |
108 |
|
|
my $pid_id=$$; |
109 |
|
|
open(FILE, ">>$pid_file") || die "Cant write to $pid_file file: $!"; |
110 |
|
|
print FILE $pid_id; |
111 |
|
|
close(FILE); |
112 |
|
|
my $counter=0; |
113 |
|
|
while ($syncrorun) { |
114 |
|
|
$counter++; |
115 |
|
|
if (!( -e $pid_file)) { $syncrorun=0; } |
116 |
|
|
&get_data(); |
117 |
|
|
if ($counter == $update_graph_period) { |
118 |
|
|
&update_graph(); |
119 |
|
|
$counter=0; |
120 |
|
|
} |
121 |
|
|
sleep($step); |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
############################################################## |
126 |
|
|
sub stop { |
127 |
|
|
my $pid=`cat $pid_file`; |
128 |
|
|
`kill $pid`; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
############################################################## |
132 |
|
|
sub update_graph { |
133 |
|
|
RRDs::graph "$signal_image", |
134 |
|
|
"--title", " $iface signal statistics", |
135 |
|
|
"--start", "now-12h", |
136 |
|
|
"--end", "now", |
137 |
|
|
"--lower-limit=0", |
138 |
|
|
"--interlace", |
139 |
|
|
"--imgformat","PNG", |
140 |
|
|
"--width=450", |
141 |
|
|
"DEF:link=$rrd:link:AVERAGE", |
142 |
|
|
# "DEF:level=$rrd:level:AVERAGE", |
143 |
|
|
# "DEF:noise=$rrd:noise:AVERAGE", |
144 |
|
|
"AREA:link#00b6e4:link", |
145 |
|
|
# "LINE1:level#0022e9:level", |
146 |
|
|
# "LINE1:noise#cc0000:noise" |
147 |
|
|
; |
148 |
|
|
print "ERROR ".RRDs::error."\n" if (RRDs::error); |
149 |
|
|
|
150 |
|
|
RRDs::graph "$transfer_image", |
151 |
|
|
"--title", " $iface transfer statistics", |
152 |
|
|
"--start", "now-12h", |
153 |
|
|
"--end", "now", |
154 |
|
|
"--lower-limit=0", |
155 |
|
|
"--interlace", |
156 |
|
|
"--imgformat","PNG", |
157 |
|
|
"--width=450", |
158 |
|
|
"DEF:in=$rrd:in:AVERAGE", |
159 |
|
|
"DEF:out=$rrd:out:AVERAGE", |
160 |
|
|
"AREA:in#00b6e4:in", |
161 |
|
|
"LINE1:out#0022e9:out", |
162 |
|
|
; |
163 |
|
|
print "ERROR ".RRDs::error."\n" if (RRDs::error); |
164 |
|
|
} |
165 |
|
|
|
166 |
|
|
############################################################### |
167 |
|
|
sub get_data { |
168 |
|
|
my ($link,$level,$noise,$in,$out) = ("U","U","U","U","U"); |
169 |
|
|
|
170 |
|
|
open(FILE, "$wifi_proc_file") || die "Cant open $wifi_proc_file file: $!"; |
171 |
|
|
while (my $line=<FILE>) { |
172 |
|
|
($link,$level,$noise) = ($1,$2,$3) if ($line=~/\s*?$iface\:\s+?\d+?\s+?(\d+)\.*?\d*?\s+?(\d+)\.*?\d*?\s+?(\d+)\.*?\d*?.*/); |
173 |
|
|
} |
174 |
|
|
close(FILE); |
175 |
|
|
|
176 |
|
|
open(FILE, "$dev_proc_file") || die "Cant open $dev_proc_file file: $!"; |
177 |
|
|
while (my $line=<FILE>) { |
178 |
|
|
if ($line=~/\s*?$iface\:\s*?(\d+.*)$/) { |
179 |
|
|
my @v = split(/\s+/,$1,16); |
180 |
|
|
($in,$out) = ($v[0],$v[8]); |
181 |
|
|
} |
182 |
|
|
} |
183 |
|
|
close(FILE); |
184 |
|
|
RRDs::update ("$rrd", "--template", "link:level:noise:in:out", "N:$link:$level:$noise:$in:$out"); |
185 |
|
|
print "ERROR ".RRDs::error."\n" if (RRDs::error); |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
############################################################### |
189 |
|
|
sub syncro_handler { |
190 |
|
|
unlink($pid_file) || die "Cant unlink $pid_file file: $!"; |
191 |
|
|
$syncrorun=0; |
192 |
|
|
} |