1 | 1 | simandl | package HotSaNICmod::OSdep; |
2 | | | |
3 | | | use RRDs; |
4 | | | |
5 | | | # return module version |
6 | | | # |
7 | | | sub version { |
8 | | | ($VERSION = '$Revision: 1.4 $') =~ s/.*(\d+\.\d+).*/$1/; |
9 | | | return "$^O.pm $VERSION"; |
10 | | | } |
11 | | | |
12 | | | # sample data |
13 | | | # |
14 | | | sub sample { |
15 | | | my %args=@_; |
16 | | | |
17 | | | @sections = split " ", ($args{SECTIONS}); |
18 | | | foreach (@sections){ |
19 | | | if ($_ eq "cpu") {read_cpu(%args)} |
20 | | | if ($_ eq "load") {read_load(%args)} |
21 | | | if ($_ eq "proc") {read_proc(%args)} |
22 | | | if ($_ eq "mem") {read_mem(%args)} |
23 | | | if ($_ eq "users") {read_users(%args)} |
24 | | | } |
25 | | | } |
26 | | | |
27 | | | # users.rrd -> tty pty pts |
28 | | | # cpu.rrd -> cpuusr cpusys cpunic cpuidl |
29 | | | # load.rrd -> load15 load5 load1 |
30 | | | # proc.rrd -> procslp procrun proczmb procstp |
31 | | | # mem.rrd (Linux) -> memfre memshr membuf memcac swpfre swpuse |
32 | | | |
33 | | | ##################################################################################### |
34 | | | # build CPU statistics |
35 | | | # in /proc/stat there is a CPU entry which provides us with the |
36 | | | # number of ticks the CPU was in all states ( user nice system idle ) |
37 | | | # We simply have to gather this information and calculate the actual percentage :) |
38 | | | # To do this we have to remember the last read values of course! |
39 | | | # |
40 | | | sub read_cpu { |
41 | | | my %args=@_; |
42 | | | my $multicpu=0; |
43 | | | |
44 | | | # read actual state |
45 | | | |
46 | | | # assumed order is: user nice sys intr idle |
47 | | | $cpuraw = `sysctl kern.cp_time`; |
48 | | | chomp $cpuraw; |
49 | | | ($cpu, $cpu1, $cpu2, $cpu3, $cpu4, $cpu5)=split / /, $cpuraw; |
50 | | | if ($cpu eq "kern.cp_time:") { $cpu = "cpu"; } |
51 | | | $cpu1{$cpu}=$cpu1; |
52 | | | $cpu2{$cpu}=$cpu2; |
53 | | | $cpu3{$cpu}=$cpu3; |
54 | | | $cpu4{$cpu}=$cpu4; |
55 | | | $cpu5{$cpu}=$cpu5; |
56 | | | $cpu1old{$cpu}=0; |
57 | | | $cpu2old{$cpu}=0; |
58 | | | $cpu3old{$cpu}=0; |
59 | | | $cpu4old{$cpu}=0; |
60 | | | $cpu5old{$cpu}=0; |
61 | | | if ($cpu eq "cpu1") { $multicpu=1; } |
62 | | | } |
63 | | | |
64 | | | # read last state and store actual state |
65 | | | # usr nice sys idle int |
66 | | | if ( -e "cpu.dat" ) { |
67 | | | open(CPU,"cpu.dat"); |
68 | | | while (<CPU>) { |
69 | | | $line=$_." 0 0 0 0 0 0"; |
70 | | | ($cpu, $cpu1old, $cpu2old, $cpu3old, $cpu4old, $cpu5old)=split / /,$line; |
71 | | | |
72 | | | if ($cpu !~ /cpu/) { |
73 | | | $cpu5old=$cpu4old; |
74 | | | $cpu4old=$cpu3old; |
75 | | | $cpu3old=$cpu2old; |
76 | | | $cpu2old=$cpu1old; |
77 | | | $cpu1old=$cpu; |
78 | | | $cpu="cpu"; |
79 | | | } |
80 | | | |
81 | | | $cpu1old{$cpu}=$cpu1old; |
82 | | | $cpu2old{$cpu}=$cpu2old; |
83 | | | $cpu3old{$cpu}=$cpu3old; |
84 | | | $cpu4old{$cpu}=$cpu4old; |
85 | | | $cpu5old{$cpu}=$cpu5old; |
86 | | | } |
87 | | | close(CPU); |
88 | | | } |
89 | | | |
90 | | | open(CPU,">cpu.dat"); |
91 | | | |
92 | | | foreach $cpu (keys %cpu1) { |
93 | | | print CPU $cpu." ".$cpu1{$cpu}." ".$cpu2{$cpu}." ".$cpu3{$cpu}." ".$cpu4{$cpu}." ".$cpu5{$cpu}."\n"; |
94 | | | |
95 | | | # calculate everything.... |
96 | | | # |
97 | | | |
98 | | | $all{$cpu}=($cpu1{$cpu}+$cpu2{$cpu}+$cpu3{$cpu}+$cpu4{$cpu}+$cpu5{$cpu})-($cpu1old{$cpu}+$cpu2old{$cpu}+$cpu3old{$cpu}+$cpu4old{$cpu}+$cpu5old{$cpu}); |
99 | | | |
100 | | | $cpuusr{$cpu}=($cpu1{$cpu}-$cpu1old{$cpu})/$all{$cpu}; |
101 | | | $cpunic{$cpu}=($cpu2{$cpu}-$cpu2old{$cpu})/$all{$cpu}; |
102 | | | $cpusys{$cpu}=($cpu3{$cpu}-$cpu3old{$cpu})/$all{$cpu}; |
103 | | | $cpuidl{$cpu}=($cpu4{$cpu}-$cpu4old{$cpu})/$all{$cpu}; |
104 | | | $cpuint{$cpu}=($cpu5{$cpu}-$cpu5old{$cpu})/$all{$cpu}; |
105 | | | |
106 | | | # store data |
107 | | | # |
108 | | | if ( ! -e "rrd/".$cpu.".rrd" ) { system("./makerrd",$cpu); } |
109 | | | RRDs::update "rrd/".$cpu.".rrd", time.":".$cpuusr{$cpu}.":".$cpunic{$cpu}.":".$cpusys{$cpu}.":".$cpuidl{$cpu}.":".$cpuint{$cpu}; |
110 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update cpu.rrd: $ERROR\n"; } |
111 | | | |
112 | | | # on single-cpu machines only the global database has to be updated. |
113 | | | # |
114 | | | last if ($multicpu==0); |
115 | | | |
116 | | | } |
117 | | | close(CPU); |
118 | | | } |
119 | | | |
120 | | | |
121 | | | |
122 | | | ##################################################################################### |
123 | | | # here we go with the load-average value ... |
124 | | | # |
125 | | | sub read_load { |
126 | | | my %args=@_; |
127 | | | (undef,undef,$load1,$load5,$load15)=split(/ /,`/usr/sbin/sysctl vm.loadavg`); |
128 | | | |
129 | | | if ( ! -e "rrd/load.rrd" ) { system("./makerrd","load") } |
130 | | | RRDs::update "rrd/load.rrd", time.":".$load15.":".$load5.":".$load1; |
131 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update load.rrd: $ERROR\n"; } |
132 | | | } |
133 | | | |
134 | | | |
135 | | | |
136 | | | ##################################################################################### |
137 | | | # now the processes. We have to check how many processes are sleeping, |
138 | | | # running, zombieng spwapped to disc or stopped. To do this we simply |
139 | | | # have to walk through the /proc tree, check all "stat" files of each |
140 | | | # process-id subdir and increment the according state-counter. |
141 | | | # |
142 | | | sub read_proc { |
143 | | | my %args=@_; |
144 | | | $procslp=$procrun=$proczmb=$procstp=$procdsc=0; |
145 | | | @states=`/bin/ps axostate`; |
146 | | | foreach (@states) { |
147 | | | $procslp++ if (/^S/); |
148 | | | $procdsc++ if (/^D/); |
149 | | | $procrun++ if (/^R/); |
150 | | | $proczmb++ if (/^Z/); |
151 | | | $procstp++ if (/^T/); |
152 | | | } |
153 | | | |
154 | | | if ( ! -e "rrd/proc.rrd" ) { system("./makerrd","proc") } |
155 | | | RRDs::update "rrd/proc.rrd", time.":".$procslp.":".$procrun.":".$proczmb.":".$procstp.":".$procdsc; |
156 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update proc.rrd: $ERROR\n"; } |
157 | | | } |
158 | | | |
159 | | | ##################################################################################### |
160 | | | # read users stats |
161 | | | # |
162 | | | sub read_users { |
163 | | | my %args=@_; |
164 | | | |
165 | | | @users=`who`; |
166 | | | ($tty,$pty,$pts)=(0,0,0); |
167 | | | foreach (@users) { |
168 | | | # print $_; |
169 | | | if (index($_," tty") >=0 ) { $tty++; } |
170 | | | elsif (index($_," pty") >=0 ) { $pty++; } |
171 | | | elsif (index($_," pts") >=0 ) { $pts++; } |
172 | | | } |
173 | | | |
174 | | | if ( ! -e "rrd/users.rrd" ) { system("./makerrd","users") } |
175 | | | RRDs::update "rrd/users.rrd", time.":".$tty.":".$pty.":".$pts; |
176 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update users.rrd: $ERROR\n"; } |
177 | | | } |
178 | | | |
179 | | | #################################################################################### |
180 | | | # here we go with the memory and swapfile statistics... |
181 | | | # |
182 | | | sub read_mem { |
183 | | | my %args=@_; |
184 | | | |
185 | | | # if ( ! -e "rrd/mem.rrd" ) { system("./makerrd","mem") } |
186 | | | # RRDs::update "rrd/mem.rrd", time.":".$memfree.":".$memwire.":".$memactv.":".$meminac.":".$memcach.":".$swpfre.":".$swpuse; |
187 | | | # if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update `mem.rrd': $ERROR\n"; } |
188 | | | } |
189 | | | |
190 | | | 1; |