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