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.15 $') =~ 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 cpunic 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 | | | # read actual state |
56 | | | open(IN,"/proc/stat"); |
57 | | | while(<IN>) { |
58 | | | chomp; |
59 | | | if( index($_,"cpu") >=0 ) { |
60 | 26 | simandl | ($cpu, $cpu1, $cpu2, $cpu3, $cpu4, $cpu5, $cpu6, $cpu7, $cpu8)=split; |
61 | 1 | simandl | $cpu1{$cpu}=$cpu1; |
62 | | | $cpu2{$cpu}=$cpu2; |
63 | | | $cpu3{$cpu}=$cpu3; |
64 | | | $cpu4{$cpu}=$cpu4; |
65 | 26 | simandl | $cpu5{$cpu}=($cpu5+$cpu6+$cpu7+$cpu8); |
66 | 1 | simandl | $cpu1old{$cpu}=0; |
67 | | | $cpu2old{$cpu}=0; |
68 | | | $cpu3old{$cpu}=0; |
69 | | | $cpu4old{$cpu}=0; |
70 | | | $cpu5old{$cpu}=0; |
71 | | | if ($cpu eq "cpu1") { $multicpu=1; } |
72 | | | } |
73 | | | } |
74 | | | close(IN); |
75 | | | |
76 | | | # read last state and store actual state |
77 | | | # usr nice sys idle int |
78 | | | my $DATFILE=$args{VARDIR}."/cpu.dat"; |
79 | | | if ( -e $DATFILE ) { |
80 | | | open(CPU,$DATFILE); |
81 | | | while (<CPU>) { |
82 | | | $line=$_." 0 0 0 0 0 0"; |
83 | | | ($cpu, $cpu1old, $cpu2old, $cpu3old, $cpu4old, $cpu5old)=split / /,$line; |
84 | | | |
85 | | | if ($cpu !~ /cpu/) { |
86 | | | $cpu5old=$cpu4old; |
87 | | | $cpu4old=$cpu3old; |
88 | | | $cpu3old=$cpu2old; |
89 | | | $cpu2old=$cpu1old; |
90 | | | $cpu1old=$cpu; |
91 | | | $cpu="cpu"; |
92 | | | } |
93 | | | |
94 | | | $cpu1old{$cpu}=$cpu1old; |
95 | | | $cpu2old{$cpu}=$cpu2old; |
96 | | | $cpu3old{$cpu}=$cpu3old; |
97 | | | $cpu4old{$cpu}=$cpu4old; |
98 | | | $cpu5old{$cpu}=$cpu5old; |
99 | | | } |
100 | | | close(CPU); |
101 | | | } |
102 | | | |
103 | | | open(CPU,">".$DATFILE); |
104 | | | |
105 | | | foreach $cpu (keys %cpu1) { |
106 | | | print CPU $cpu." ".$cpu1{$cpu}." ".$cpu2{$cpu}." ".$cpu3{$cpu}." ".$cpu4{$cpu}." ".$cpu5{$cpu}."\n"; |
107 | | | |
108 | | | # calculate everything.... |
109 | | | # |
110 | | | |
111 | | | $all{$cpu}=($cpu1{$cpu}+$cpu2{$cpu}+$cpu3{$cpu}+$cpu4{$cpu}+$cpu5{$cpu})-($cpu1old{$cpu}+$cpu2old{$cpu}+$cpu3old{$cpu}+$cpu4old{$cpu}+$cpu5old{$cpu}); |
112 | | | |
113 | | | $cpuusr{$cpu}=($cpu1{$cpu}-$cpu1old{$cpu})/$all{$cpu}; |
114 | | | $cpunic{$cpu}=($cpu2{$cpu}-$cpu2old{$cpu})/$all{$cpu}; |
115 | | | $cpusys{$cpu}=($cpu3{$cpu}-$cpu3old{$cpu})/$all{$cpu}; |
116 | | | $cpuidl{$cpu}=($cpu4{$cpu}-$cpu4old{$cpu})/$all{$cpu}; |
117 | | | $cpuint{$cpu}=($cpu5{$cpu}-$cpu5old{$cpu})/$all{$cpu}; |
118 | | | |
119 | | | # store data |
120 | | | # |
121 | | | if ( ! -e "rrd/".$cpu.".rrd" ) { system("./makerrd",$cpu); } |
122 | | | RRDs::update "rrd/".$cpu.".rrd", time.":".$cpuusr{$cpu}.":".$cpunic{$cpu}.":".$cpusys{$cpu}.":".$cpuidl{$cpu}; |
123 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update cpu.rrd: $ERROR\n"; } |
124 | | | |
125 | | | # on single-cpu machines only the global database has to be updated. |
126 | | | # |
127 | | | last if ($multicpu==0); |
128 | | | |
129 | | | } |
130 | | | close(CPU); |
131 | | | } |
132 | | | |
133 | | | |
134 | | | |
135 | | | ##################################################################################### |
136 | | | # here we go with the load-average value ... |
137 | | | # |
138 | | | sub read_load { |
139 | | | my %args=@_; |
140 | | | open(IN,"/proc/loadavg"); ($load1,$load5,$load15)=split(/ /,<IN>); close(IN); |
141 | | | |
142 | | | if ( ! -e "rrd/load.rrd" ) { system("./makerrd","load") } |
143 | | | RRDs::update "rrd/load.rrd", time.":".$load15.":".$load5.":".$load1; |
144 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update load.rrd: $ERROR\n"; } |
145 | | | } |
146 | | | |
147 | | | |
148 | | | |
149 | | | ##################################################################################### |
150 | | | # now the processes. We have to check how many processes are sleeping, |
151 | | | # running, zombieng spwapped to disc or stopped. To do this we simply |
152 | | | # have to walk through the /proc tree, check all "stat" files of each |
153 | | | # process-id subdir and increment the according state-counter. |
154 | | | # |
155 | | | sub read_proc { |
156 | | | my %args=@_; |
157 | | | ($procslp,$procrun,$proczmb,$procstp,$procdsc)=(0,0,0,0,0); |
158 | | | opendir (DIR,"/proc");@dir=readdir (DIR);closedir(DIR); |
159 | | | foreach $entry (@dir) { |
160 | | | # check if entry is a numeric one |
161 | | | if (($entry =~ /^\d+$/) && ( -e "/proc/$entry/stat" )) { |
162 | | | open (STAT,"/proc/$entry/stat");$_=<STAT>;close(STAT); |
163 | | | if (index($_," S ") >=0 ) { $procslp++ } |
164 | | | elsif (index($_," D ") >=0 ) { $procdsc++ } |
165 | | | elsif (index($_," R ") >=0 ) { $procrun++ } |
166 | | | elsif (index($_," Z ") >=0 ) { $proczmb++ } |
167 | | | elsif (index($_," T ") >=0 ) { $procstp++ } |
168 | | | } |
169 | | | } |
170 | | | |
171 | | | if ( ! -e "rrd/proc.rrd" ) { system("./makerrd","proc") } |
172 | | | RRDs::update "rrd/proc.rrd", time.":".$procslp.":".$procrun.":".$proczmb.":".$procstp.":".$procdsc; |
173 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update proc.rrd: $ERROR\n"; } |
174 | | | } |
175 | | | |
176 | | | ##################################################################################### |
177 | | | # read users stats |
178 | | | # |
179 | | | sub read_users { |
180 | | | my %args=@_; |
181 | | | |
182 | | | |
183 | | | # build users-table first |
184 | | | # |
185 | | | my $command="/usr/bin/who"; |
186 | | | open FILE,"$command|" || print time," ",$MODNAME,": unable to run '$command': $!\n"; |
187 | | | my @users=(<FILE>); |
188 | | | close FILE; |
189 | | | |
190 | | | ($tty,$pty,$pts)=(0,0,0); |
191 | | | foreach (@users) { |
192 | | | # print $_; |
193 | | | if (index($_," tty") >=0 ) { $tty++; } |
194 | | | elsif (index($_," pty") >=0 ) { $pty++; } |
195 | | | elsif (index($_," pts") >=0 ) { $pts++; } |
196 | | | } |
197 | | | |
198 | | | if ( ! -e "rrd/users.rrd" ) { system("./makerrd","users") } |
199 | | | RRDs::update "rrd/users.rrd", time.":".$tty.":".$pty.":".$pts; |
200 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update users.rrd: $ERROR\n"; } |
201 | | | } |
202 | | | |
203 | | | #################################################################################### |
204 | | | # here we go with the memory and swapfile statistics... |
205 | | | # |
206 | | | sub read_mem { |
207 | | | |
208 | | | my %args=@_; |
209 | | | my $KERNEL=HotSaNICparser::identify_kernel(); |
210 | | | |
211 | | | if ( ! -e "rrd/mem.rrd" ) { system("./makerrd","mem") } |
212 | | | open(IN,"/proc/meminfo"); |
213 | | | while(<IN>) { |
214 | | | chomp; |
215 | | | if (index($_,"Mem:")>=0) { (undef,$memtot,undef,$memfre,$memshr,$membuf,$memcac)=split; } |
216 | | | elsif (index($_,"Swap:")>=0) { (undef,undef,$swpuse,$swpfre)=split; } |
217 | | | |
218 | | | if ($KERNEL>2.4) { |
219 | | | # This is kernel >= 2.5 code |
220 | | | if ($_ =~ /^MemTotal:/) { (undef,$memtot,undef)=split; $memtot*=1024; } |
221 | | | elsif ($_ =~ /^MemFree:/) { (undef,$memfre,undef)=split; $memfre*=1024; } |
222 | | | elsif ($_ =~ /^Buffers:/) { (undef,$membuf,undef)=split; $membuf*=1024; } |
223 | | | elsif ($_ =~ /^Cached:/) { (undef,$memcac,undef)=split; $memcac*=1024; } |
224 | | | elsif ($_ =~ /^SwapTotal:/) { (undef,$swptot,undef)=split; $swptot*=1024; } |
225 | | | elsif ($_ =~ /^SwapFree:/) { (undef,$swpfre,undef)=split; $swpfre*=1024; } |
226 | | | } |
227 | | | |
228 | | | } |
229 | | | close(IN); |
230 | | | |
231 | | | if ($KERNEL>2.4) { $swpuse=$swptot-$swpfre; } |
232 | | | $memshr=$memtot-$memfre-$membuf-$memcac; |
233 | | | |
234 | | | RRDs::update "rrd/mem.rrd", time.":".$memfre.":".$memshr.":".$membuf.":".$memcac.":".$swpfre.":".$swpuse; |
235 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update `mem.rrd': $ERROR\n"; } |
236 | | | } |
237 | | | |
238 | | | sub read_interrupts { |
239 | | | my %args=@_; |
240 | | | |
241 | | | # 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 |
242 | | | |
243 | | | my @stats; |
244 | | | open FILE,"/proc/stat"; |
245 | | | while (<FILE>) { (undef,@stats)=split if /intr/; } |
246 | | | close FILE; |
247 | | | |
248 | | | # need first 17 values from array |
249 | | | my $result=join(":",@stats[0..16]); |
250 | | | |
251 | | | if ( ! -e "rrd/irq.rrd" ) { system("./makerrd","irq") } |
252 | | | RRDs::update "rrd/irq.rrd",time.":".$result; |
253 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update `irq.rrd': $ERROR\n"; } |
254 | | | } |
255 | | | |
256 | | | |
257 | | | |
258 | | | ##################################################################################### |
259 | | | # here we go with the load-average value ... |
260 | | | # |
261 | | | sub read_uptime { |
262 | | | my %args=@_; |
263 | | | open(IN,"/proc/uptime"); ($up,$idle)=split(/ /,<IN>); close(IN); |
264 | | | $up/=86400; |
265 | | | $idle/=86400; |
266 | | | |
267 | | | if ( ! -e "rrd/uptime.rrd" ) { system("./makerrd","uptime") } |
268 | | | RRDs::update "rrd/uptime.rrd", time.":".$up.":".$idle; |
269 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update uptime.rrd: $ERROR\n"; } |
270 | | | } |
271 | | | |
272 | | | 1; |