jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [branches/] [HotSaNIC-0.5.0-jablonecka/] [modules/] [system/] [platform/] [netbsd.pm] - Blame information for rev 3

 

Line No. Rev Author Line
11simandlpackage HotSaNICmod::OSdep;
2 
3use RRDs;
4use Data::Dumper;
5 
6# return module version
7#
8sub version {
9 ($VERSION = '$Revision: 1.10 $') =~ s/.*(\d+\.\d+).*/$1/;
10 return "$^O.pm $VERSION";
11 }
12 
13# sample data
14#
15sub sample {
16 my %args=@_;
17 
18 @sections = split " ", ($args{SECTIONS});
19 foreach (@sections){
20 if ($_ eq "cpu") {read_cpu(%args)}
21 elsif ($_ eq "load") {read_load(%args)}
22 elsif ($_ eq "proc") {read_proc(%args)}
23 elsif ($_ eq "mem") {read_mem(%args)}
24 elsif ($_ eq "users") {read_users(%args)}
25 elsif ($_ eq "int") {read_interrupts(%args)}
26 elsif ($_ eq "uptime") {read_uptime(%args)}
27 }
28 
29 }
30 
31# users.rrd -> tty pty pts
32# cpu.rrd -> cpuusr cpusys cpunic cpuidl
33# load.rrd -> load15 load5 load1
34# proc.rrd -> procslp procrun proczmb procstp
35# mem.rrd (Linux) -> memfre memshr membuf memcac swpfre swpuse
36 
37#####################################################################################
38# build CPU statistics
39# in /proc/stat there is a CPU entry which provides us with the
40# number of ticks the CPU was in all states ( user nice system idle )
41# We simply have to gather this information and calculate the actual percentage :)
42# To do this we have to remember the last read values of course!
43#
44sub read_cpu {
45 my %args=@_;
46 my $multicpu=0;
47 
48 # read actual state
49 
50 # assumed order is: user nice sys intr idle
51 $cpuraw = `/sbin/sysctl kern.cp_time`;
52 $cpuraw =~ /^.* user = (\d+), nice = (\d+), sys = (\d+), intr = (\d+), idle = (\d+)$/;
53 $cpu="cpu";
54 $cpu1{$cpu}=$1;
55 $cpu2{$cpu}=$2;
56 $cpu3{$cpu}=$3;
57 $cpu4{$cpu}=$5;
58 $cpu5{$cpu}=$4;
59 $cpu1old{$cpu}=0;
60 $cpu2old{$cpu}=0;
61 $cpu3old{$cpu}=0;
62 $cpu4old{$cpu}=0;
63 $cpu5old{$cpu}=0;
64 
65 # read last state and store actual state
66 # usr nice sys idle int
67 if ( -e "cpu.dat" ) {
68 open(CPU,"cpu.dat");
69 while (<CPU>) {
70 $line=$_." 0 0 0 0 0 0";
71 ($cpu, $cpu1old, $cpu2old, $cpu3old, $cpu4old, $cpu5old)=split / /,$line;
72 
73 if ($cpu !~ /cpu/) {
74 $cpu5old=$cpu4old;
75 $cpu4old=$cpu3old;
76 $cpu3old=$cpu2old;
77 $cpu2old=$cpu1old;
78 $cpu1old=$cpu;
79 $cpu="cpu";
80 }
81 
82 $cpu1old{$cpu}=$cpu1old;
83 $cpu2old{$cpu}=$cpu2old;
84 $cpu3old{$cpu}=$cpu3old;
85 $cpu4old{$cpu}=$cpu4old;
86 $cpu5old{$cpu}=$cpu5old;
87 }
88 close(CPU);
89 }
90 
91 open(CPU,">cpu.dat");
92 
93 foreach $cpu (keys %cpu1) {
94 print CPU $cpu." ".$cpu1{$cpu}." ".$cpu2{$cpu}." ".$cpu3{$cpu}." ".$cpu4{$cpu}." ".$cpu5{$cpu}."\n";
95 
96# calculate everything....
97#
98 
99 $all{$cpu}=($cpu1{$cpu}+$cpu2{$cpu}+$cpu3{$cpu}+$cpu4{$cpu}+$cpu5{$cpu})-($cpu1old{$cpu}+$cpu2old{$cpu}+$cpu3old{$cpu}+$cpu4old{$cpu}+$cpu5old{$cpu});
100 
101 $cpuusr{$cpu}=($cpu1{$cpu}-$cpu1old{$cpu})/$all{$cpu};
102 $cpunic{$cpu}=($cpu2{$cpu}-$cpu2old{$cpu})/$all{$cpu};
103 $cpusys{$cpu}=($cpu3{$cpu}-$cpu3old{$cpu})/$all{$cpu};
104 $cpuidl{$cpu}=($cpu4{$cpu}-$cpu4old{$cpu})/$all{$cpu};
105 $cpuint{$cpu}=($cpu5{$cpu}-$cpu5old{$cpu})/$all{$cpu};
106 
107# store data
108#
109 if ( ! -e "rrd/".$cpu.".rrd" ) { system("./makerrd",$cpu); }
110 RRDs::update "rrd/".$cpu.".rrd", time.":".$cpuusr{$cpu}.":".$cpunic{$cpu}.":".$cpusys{$cpu}.":".$cpuidl{$cpu}.":".$cpuint{$cpu};
111 if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update cpu.rrd: $ERROR\n"; }
112 
113# on single-cpu machines only the global database has to be updated.
114#
115 last if ($multicpu==0);
116 
117 }
118 close(CPU);
119 }
120 
121 
122 
123#####################################################################################
124# here we go with the load-average value ...
125#
126sub read_load {
127 my %args=@_;
128 (undef,$load1,$load5,$load15)=split(/ /,`/sbin/sysctl vm.loadavg`);
129 
130 if ( ! -e "rrd/load.rrd" ) { system("./makerrd","load") }
131 RRDs::update "rrd/load.rrd", time.":".$load15.":".$load5.":".$load1;
132 if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update load.rrd: $ERROR\n"; }
133 }
134 
135 
136 
137#####################################################################################
138# now the processes. We have to check how many processes are sleeping,
139# running, zombieng spwapped to disc or stopped. To do this we simply
140# have to walk through the /proc tree, check all "stat" files of each
141# process-id subdir and increment the according state-counter.
142#
143sub read_proc {
144 my %args=@_;
145 ($procslp,$procrun,$proczmb,$procstp,$procdsc)=(0,0,0,0,0);
146 
147 # ok, this needs work doing to it. Unfortunately I'm not quite
148 # familiar enough with FreeBSD to do this properly - any takers? - MJB
149 
150 $procslp=$procrun=$proczmb=$procstp=$procdsc=0;
151 open FILE,"/bin/ps axostate|";
152 while (<FILE>) {
153 $procslp++ if (/^S/);
154 $procdsc++ if (/^D/);
155 $procrun++ if (/^R/);
156 $proczmb++ if (/^Z/);
157 $procstp++ if (/^T/);
158 }
159 close FILE;
160 
161 if ( ! -e "rrd/proc.rrd" ) { system("./makerrd","proc") }
162 RRDs::update "rrd/proc.rrd", time.":".$procslp.":".$procrun.":".$proczmb.":".$procstp.":".$procdsc;
163 if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update proc.rrd: $ERROR\n"; }
164 }
165 
166#####################################################################################
167# read users stats
168#
169sub read_users {
170 my %args=@_;
171 
172 @users=`who`;
173 ($tty,$pty,$pts)=(0,0,0);
174 foreach (@users) {
175 # print $_;
176 if (index($_," ttyE") >=0 ) { $tty++; }
177 elsif (index($_," ttyp") >=0 ) { $pty++; }
178 elsif (index($_," pts") >=0 ) { $pts++; }
179 }
180 
181 if ( ! -e "rrd/users.rrd" ) { system("./makerrd","users") }
182 RRDs::update "rrd/users.rrd", time.":".$tty.":".$pty.":".$pts;
183 if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update users.rrd: $ERROR\n"; }
184 }
185 
186####################################################################################
187# here we go with the memory and swapfile statistics...
188#
189sub read_mem {
190 my %args=@_;
191 
192 my ($line, $bpp, $totalmem, $free, $active, $inactive, $fcache, $ecache, $totalswap, $swapused);
193 my @vmstat = `/usr/bin/vmstat -s`;
194 foreach $line ( @vmstat ) {
195 chomp $line;
196 ($bpp = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / bytes per page$/);
197 ($totalmem = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / pages managed$/);
198 ($free = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / pages free$/);
199 ($active = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / pages active$/);
200 ($inactive = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / pages inactive$/);
201 ($wired = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / pages wired$/);
202 ($fcache = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / cached file pages$/);
203 ($ecache = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / cached executable pages$/);
204 ($totalswap = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / swap pages$/);
205 ($swapused = $line) =~ s/^.*?(\d+) .*$/$1/ if ($line =~ / swap pages in use$/);
206 }
207 my $memfree = $free * $bpp ;
208 my $meminac = $inactive * $bpp ;
209 my $memacti = $active * $bpp ;
210 my $memwire = $wired * $bpp ;
211 my $memcach = ($fcache + $ecache) * $bpp ;
212 my $swpfree = ($totalswap - $swapused) *$bpp ;
213 my $swpused = $swapused * $bpp ;
214 #print STDERR "$memactv,$memfree,$swpused,$swpfree\n";
215 if ( ! -e "rrd/mem.rrd" ) { system("./makerrd","mem") }
216 RRDs::update "rrd/mem.rrd", time.":".$memfree.":".$meminac.":".$memacti.":".$memwire.":".$memcach.":".$swpfree.":".$swpused;
217 if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update `mem.rrd': $ERROR\n"; }
218}
219 
220####################################################################################
221# here we go with the interrupts...
222#
223sub read_interrupts {
224 my %args=@_;
225 
226 my @stats=(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
227 my $i;
228 open FILE,"/usr/bin/vmstat -i |";
229 while (<FILE>) {
230 next if (/interrupts/);
231 chomp;
232 ($irq,$total,) = split;
233 ($i = $irq ) =~ s/irq(\d+)/$1/;
234 $i =~ s/Total/-1/;
235 $stats[$i+1] = $total ;
236 }
237 close FILE;
238 
239 # need first 17 values from array
240 my $result=join(":",@stats[0..16]);
241 
242 if ( ! -e "rrd/irq.rrd" ) { system("./makerrd","irq") }
243 RRDs::update "rrd/irq.rrd",time.":".$result;
244 if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update `irq.rrd': $ERROR\n"; }
245}
246 
247#####################################################################################
248# here we go with the load-average value ...
249#
250sub read_uptime {
251 my %args=@_;
252# open(IN,"/usr/bin/uptime |"); ($up,$idle)=split(/ /,<IN>); close(IN);
253# $up/=86400;
254# $idle/=86400;
255#
256# if ( ! -e "rrd/uptime.rrd" ) { system("./makerrd","uptime") }
257# RRDs::update "rrd/uptime.rrd", time.":".$up.":".$idle;
258# if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update uptime.rrd: $ERROR\n"; }
259}
260 
261 
2621;

Powered by WebSVN 2.2.1