1 | 1 | simandl | package HotSaNICmod::OSdep; |
2 | | | |
3 | | | use RRDs; |
4 | | | |
5 | | | eval { require "platform/syssnmp.pm"; }; |
6 | | | $NO_SNMP=1 if($@); |
7 | | | |
8 | | | # return module version |
9 | | | # |
10 | | | sub version { |
11 | | | ($VERSION = '$Revision: 1.10 $') =~ s/.*(\d+\.\d+).*/$1/; |
12 | | | return "$^O.pm $VERSION"; |
13 | | | } |
14 | | | |
15 | | | # sample data |
16 | | | # |
17 | | | sub sample { |
18 | | | my %args=@_; |
19 | | | if(!defined($NO_SNMP)) { |
20 | | | foreach ( keys(%args) ) { |
21 | | | syssnmp::do_snmp($args{$_},$args{VARDIR}) if(/HOST/); |
22 | | | } |
23 | | | } |
24 | | | |
25 | | | @sections = split " ", ($args{SECTIONS}); |
26 | | | foreach (@sections){ |
27 | | | if ($_ eq "cpu") {read_cpu(%args)} |
28 | | | elsif ($_ eq "load") {read_load(%args)} |
29 | | | elsif ($_ eq "proc") {read_proc(%args)} |
30 | | | elsif ($_ eq "mem") {read_mem(%args)} |
31 | | | elsif ($_ eq "users") {read_users(%args)} |
32 | | | # elsif ($_ eq "int") {read_interrupts(%args)} |
33 | | | # elsif ($_ eq "uptime") {read_uptime(%args)} |
34 | | | } |
35 | | | |
36 | | | } |
37 | | | |
38 | | | # users.rrd -> tty pty pts |
39 | | | # cpu.rrd -> cpuusr cpusys cpuwait cpuidl |
40 | | | # load.rrd -> load15 load5 load1 |
41 | | | # proc.rrd -> procslp procrun proczmb procstp |
42 | | | # mem.rrd (Linux) -> memfre memshr membuf memcac swpfre swpuse |
43 | | | |
44 | | | ##################################################################################### |
45 | | | # build CPU statistics |
46 | | | # in /proc/stat there is a CPU entry which provides us with the |
47 | | | # number of ticks the CPU was in all states ( user nice system idle ) |
48 | | | # We simply have to gather this information and calculate the actual percentage :) |
49 | | | # To do this we have to remember the last read values of course! |
50 | | | # |
51 | | | sub read_cpu { |
52 | | | my %args=@_; |
53 | | | my $multicpu=0; |
54 | | | |
55 | | | # vmstat -s |
56 | | | # |
57 | | | # 23506934 user cpu |
58 | | | # 2794729 system cpu |
59 | | | # 44912336 idle cpu |
60 | | | # 449606 wait cpu |
61 | | | |
62 | | | # read actual state |
63 | | | @lines=`kstat cpu_stat`; |
64 | | | |
65 | | | # module: cpu_stat instance: 0 |
66 | | | # name: cpu_stat0 class: misc |
67 | | | # idle 23071073 |
68 | | | # kernel 1159786 |
69 | | | # user 11466265 |
70 | | | # wait 200397 |
71 | | | # |
72 | | | |
73 | | | $cpu1{cpu}=0; |
74 | | | $cpu2{cpu}=0; |
75 | | | $cpu3{cpu}=0; |
76 | | | $cpu4{cpu}=0; |
77 | | | $cpu5{cpu}=0; |
78 | | | |
79 | | | foreach (@lines) { |
80 | | | ($name,$value)=split; |
81 | | | if (defined $name) { |
82 | | | if (!defined $value) { $value=0; } |
83 | | | if ( $name eq "name:") { |
84 | | | ($cpu=$value) =~ s/_stat//; |
85 | | | $cpu1{$cpu}=0; |
86 | | | $cpu2{$cpu}=0; |
87 | | | $cpu3{$cpu}=0; |
88 | | | $cpu4{$cpu}=0; |
89 | | | $cpu5{$cpu}=0; |
90 | | | $cpu1old{$cpu}=0; |
91 | | | $cpu2old{$cpu}=0; |
92 | | | $cpu3old{$cpu}=0; |
93 | | | $cpu4old{$cpu}=0; |
94 | | | $cpu5old{$cpu}=0; |
95 | | | if ($cpu eq "cpu1") {$multicpu=1; } |
96 | | | } |
97 | | | if ($name eq "user") { $cpu1{$cpu}=$value; $cpu1{cpu}+=$value; } |
98 | | | if ($name eq "kernel") { $cpu2{$cpu}=$value; $cpu2{cpu}+=$value; } |
99 | | | if ($name eq "wait") { $cpu3{$cpu}=$value; $cpu3{cpu}+=$value; } |
100 | | | if ($name eq "idle") { $cpu4{$cpu}=$value; $cpu4{cpu}+=$value; } |
101 | | | } |
102 | | | } |
103 | | | |
104 | | | # read last state and store actual state |
105 | | | # usr nice sys idle int |
106 | | | my $DATFILE=$args{VARDIR}."/cpu.dat"; |
107 | | | if ( -e $DATFILE ) { |
108 | | | open(CPU,$DATFILE); |
109 | | | while (<CPU>) { |
110 | | | $line=$_." 0 0 0 0 0 0"; |
111 | | | ($cpu, $cpu1old, $cpu2old, $cpu3old, $cpu4old, $cpu5old)=split / /,$line; |
112 | | | |
113 | | | if ($cpu !~ /cpu/) { |
114 | | | $cpu5old=$cpu4old; |
115 | | | $cpu4old=$cpu3old; |
116 | | | $cpu3old=$cpu2old; |
117 | | | $cpu2old=$cpu1old; |
118 | | | $cpu1old=$cpu; |
119 | | | $cpu="cpu"; |
120 | | | } |
121 | | | |
122 | | | $cpu1old{$cpu}=$cpu1old; |
123 | | | $cpu2old{$cpu}=$cpu2old; |
124 | | | $cpu3old{$cpu}=$cpu3old; |
125 | | | $cpu4old{$cpu}=$cpu4old; |
126 | | | $cpu5old{$cpu}=$cpu5old; |
127 | | | } |
128 | | | close(CPU); |
129 | | | } |
130 | | | |
131 | | | open(CPU,">".$DATFILE); |
132 | | | |
133 | | | foreach $cpu (keys %cpu1) { |
134 | | | print CPU $cpu." ".$cpu1{$cpu}." ".$cpu2{$cpu}." ".$cpu3{$cpu}." ".$cpu4{$cpu}." ".$cpu5{$cpu}."\n"; |
135 | | | |
136 | | | # calculate everything.... |
137 | | | # |
138 | | | |
139 | | | $all{$cpu}=($cpu1{$cpu}+$cpu2{$cpu}+$cpu3{$cpu}+$cpu4{$cpu}+$cpu5{$cpu})-($cpu1old{$cpu}+$cpu2old{$cpu}+$cpu3old{$cpu}+$cpu4old{$cpu}+$cpu5old{$cpu}); |
140 | | | |
141 | | | $cpuusr{$cpu}=($cpu1{$cpu}-$cpu1old{$cpu})/$all{$cpu}; |
142 | | | $cpunic{$cpu}=($cpu2{$cpu}-$cpu2old{$cpu})/$all{$cpu}; |
143 | | | $cpusys{$cpu}=($cpu3{$cpu}-$cpu3old{$cpu})/$all{$cpu}; |
144 | | | $cpuidl{$cpu}=($cpu4{$cpu}-$cpu4old{$cpu})/$all{$cpu}; |
145 | | | $cpuint{$cpu}=($cpu5{$cpu}-$cpu5old{$cpu})/$all{$cpu}; |
146 | | | |
147 | | | # store data |
148 | | | # |
149 | | | if ( ! -e "rrd/".$cpu.".rrd" ) { system("./makerrd",$cpu); } |
150 | | | RRDs::update "rrd/".$cpu.".rrd", time.":".$cpuusr{$cpu}.":".$cpunic{$cpu}.":".$cpusys{$cpu}.":".$cpuidl{$cpu}; |
151 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update cpu.rrd: $ERROR\n"; } |
152 | | | |
153 | | | # on single-cpu machines only the global database has to be updated. |
154 | | | # |
155 | | | last if ($multicpu==0); |
156 | | | |
157 | | | } |
158 | | | close(CPU); |
159 | | | } |
160 | | | |
161 | | | |
162 | | | |
163 | | | ##################################################################################### |
164 | | | # here we go with the load-average value ... |
165 | | | # |
166 | | | sub read_load { |
167 | | | my %args=@_; |
168 | | | ($_)=`uptime`; |
169 | | | @values=split; |
170 | | | $load15=pop @values; |
171 | | | $load5=pop @values; |
172 | | | $load1=pop @values; |
173 | | | |
174 | | | if ( ! -e "rrd/load.rrd" ) { system("./makerrd","load") } |
175 | | | RRDs::update "rrd/load.rrd", time.":".$load15.":".$load5.":".$load1; |
176 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update load.rrd: $ERROR\n"; } |
177 | | | } |
178 | | | |
179 | | | |
180 | | | |
181 | | | ##################################################################################### |
182 | | | # now the processes. We have to check how many processes are sleeping, |
183 | | | # running, zombieng spwapped to disc or stopped. To do this we simply |
184 | | | # have to walk through the /proc tree, check all "stat" files of each |
185 | | | # process-id subdir and increment the according state-counter. |
186 | | | # |
187 | | | sub read_proc { |
188 | | | my %args=@_; |
189 | | | ($procslp,$procrun,$proczmb,$procstp,$procque)=(0,0,0,0,0); |
190 | | | |
191 | | | open FILE,"ps -aef -o s|"; |
192 | | | while (<FILE>) { |
193 | | | if (index($_,"S") >=0 ) { $procslp++ } |
194 | | | elsif (index($_,"R") >=0 ) { $procque++ } |
195 | | | elsif (index($_,"O") >=0 ) { $procrun++ } |
196 | | | elsif (index($_,"Z") >=0 ) { $proczmb++ } |
197 | | | elsif (index($_,"T") >=0 ) { $procstp++ } |
198 | | | } |
199 | | | close FILE; |
200 | | | |
201 | | | if ( ! -e "rrd/proc.rrd" ) { system("./makerrd","proc") } |
202 | | | RRDs::update "rrd/proc.rrd", time.":".$procslp.":".$procrun.":".$proczmb.":".$procstp.":".$procque; |
203 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update proc.rrd: $ERROR\n"; } |
204 | | | } |
205 | | | |
206 | | | ##################################################################################### |
207 | | | # read users stats |
208 | | | # |
209 | | | sub read_users { |
210 | | | my %args=@_; |
211 | | | @users=`who`; |
212 | | | ($tty,$pty,$pts)=(0,0,0); |
213 | | | foreach (@users) { |
214 | | | if (index($_," tty") >=0 ) { $tty++; } |
215 | | | elsif (index($_," pty") >=0 ) { $pty++; } |
216 | | | elsif (index($_," pts") >=0 ) { $pts++; } |
217 | | | } |
218 | | | |
219 | | | if ( ! -e "rrd/users.rrd" ) { system("./makerrd","users") } |
220 | | | RRDs::update "rrd/users.rrd", time.":".$tty.":".$pty.":".$pts; |
221 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update users.rrd: $ERROR\n"; } |
222 | | | } |
223 | | | |
224 | | | #################################################################################### |
225 | | | # here we go with the memory and swapfile statistics... |
226 | | | # |
227 | | | sub read_mem { |
228 | | | my %args=@_; |
229 | | | if ( ! -e "rrd/mem.rrd" ) { system("./makerrd","mem") } |
230 | | | |
231 | | | # read swap state |
232 | | | ($_)=`swap -s`; |
233 | | | @stats=split; |
234 | | | ($swpuse=$stats[8]) =~ s/k//; |
235 | | | ($swpfre=$stats[10]) =~ s/k//; |
236 | | | $swpuse*=1024; |
237 | | | $swpfre*=1024; |
238 | | | |
239 | | | # read memory state |
240 | | | $memshr=0; |
241 | | | $memtot=0; |
242 | | | $memfre=0; |
243 | | | $membuf=0; |
244 | | | $memcac=0; |
245 | | | |
246 | | | ($line)=grep /^Memory:/, `top -b`; |
247 | | | |
248 | | | # Memory: 4096M real, 154M free, 339M swap in use, 20G swap free |
249 | | | |
250 | | | @fields=split / /,$line; |
251 | | | ($memtot=$fields[1]) =~ s/M//g; |
252 | | | ($memfre=$fields[3]) =~ s/M//g; |
253 | | | |
254 | | | $memtot*=1048576; |
255 | | | $memfre*=1048576; |
256 | | | |
257 | | | $memshr=$memtot-$memfre; |
258 | | | |
259 | | | RRDs::update "rrd/mem.rrd", time.":".$memfre.":".$memshr.":".$membuf.":".$memcac.":".$swpfre.":".$swpuse; |
260 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update `mem.rrd': $ERROR\n"; } |
261 | | | } |
262 | | | |
263 | | | sub read_interrupts { |
264 | | | my %args=@_; |
265 | | | |
266 | | | # intr 905974335 490612570 3015 0 0 0 3 3 3 2 71611549 0 145961329 197785844 0 0 17 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
267 | | | |
268 | | | my @stats; |
269 | | | open FILE,"/proc/stat"; |
270 | | | while (<FILE>) { (undef,@stats)=split if /intr/; } |
271 | | | close FILE; |
272 | | | |
273 | | | # need first 17 values from array |
274 | | | my $result=join(":",@stats[0..16]); |
275 | | | |
276 | | | if ( ! -e "rrd/irq.rrd" ) { system("./makerrd","irq") } |
277 | | | RRDs::update "rrd/irq.rrd",time.":".$result; |
278 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update `irq.rrd': $ERROR\n"; } |
279 | | | } |
280 | | | |
281 | | | |
282 | | | |
283 | | | ##################################################################################### |
284 | | | # here we go with the load-average value ... |
285 | | | # |
286 | | | sub read_uptime { |
287 | | | my %args=@_; |
288 | | | open(IN,"/proc/uptime"); ($up,$idle)=split(/ /,<IN>); close(IN); |
289 | | | $up/=86400; |
290 | | | $idle/=86400; |
291 | | | |
292 | | | if ( ! -e "rrd/uptime.rrd" ) { system("./makerrd","uptime") } |
293 | | | RRDs::update "rrd/uptime.rrd", time.":".$up.":".$idle; |
294 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update uptime.rrd: $ERROR\n"; } |
295 | | | } |
296 | | | |
297 | | | 1; |