1 | 1 | simandl | package HotSaNICmod::OSdep; |
2 | | | |
3 | | | use RRDs; |
4 | | | |
5 | | | # return module version |
6 | | | # |
7 | | | sub version { |
8 | | | ($VERSION = '$Revision: 1.2 $') =~ 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 | | | |
28 | | | # users.rrd -> tty pty pts |
29 | | | # cpu.rrd -> cpuusr cpunic cpusys cpuint 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 | | | ##################################################################################### |
36 | | | # build CPU statistics |
37 | | | # |
38 | | | # We get info from the kern.cp_time sysctl. works on 4.6-4.7-STABLE and |
39 | | | # should work at least on single-cpu 5.0 boxes too. Values for SMP systems |
40 | | | # are cumulative until I (or anyone else) can get per-cpu info out of |
41 | | | # the kernel. Gkrellm can't even do that!!! :( |
42 | | | # |
43 | | | # sysctl order is: user nice sys intr idle |
44 | | | |
45 | | | sub read_cpu { |
46 | | | my %args=@_; |
47 | | | |
48 | | | @top = `top -l2`; |
49 | | | |
50 | | | while(@top){ |
51 | | | if(/CPU Usage:\s+(\d+\.\d+)\% user,\s+(\d+\.\d+)\% sys,\s+(\d+\.\d+)\%/){ |
52 | | | $cpuusr = $1; |
53 | | | $cpusys = $2; |
54 | | | $cpuidl = $3; |
55 | | | last; |
56 | | | } |
57 | | | } |
58 | | | |
59 | | | if ( ! -e "rrd/cpu.rrd" ) { system("./makerrd",$cpu); } |
60 | | | RRDs::update "rrd/cpu.rrd", time.":".$cpuusr.":".$cpusys.":".$cpuidl; |
61 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update cpu.rrd: $ERROR\n"; } |
62 | | | |
63 | | | } |
64 | | | |
65 | | | |
66 | | | |
67 | | | ##################################################################################### |
68 | | | # here we go with the load-average value ... |
69 | | | # |
70 | | | sub read_load { |
71 | | | my %args=@_; |
72 | | | (undef,$load1,$load5,$load15)=split(/ /,`/sbin/sysctl vm.loadavg`); |
73 | | | |
74 | | | if ( ! -e "rrd/load.rrd" ) { system("./makerrd","load") } |
75 | | | RRDs::update "rrd/load.rrd", time.":".$load15.":".$load5.":".$load1; |
76 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update load.rrd: $ERROR\n"; } |
77 | | | } |
78 | | | |
79 | | | |
80 | | | |
81 | | | ##################################################################################### |
82 | | | # now the processes. We have to check how many processes are sleeping, |
83 | | | # running, zombieng spwapped to disc or stopped. To do this we simply |
84 | | | # have to walk through the /proc tree, check all "stat" files of each |
85 | | | # process-id subdir and increment the according state-counter. |
86 | | | # |
87 | | | sub read_proc { |
88 | | | my %args=@_; |
89 | | | ($procslp,$procrun,$proczmb,$procstp,$procdsc)=(0,0,0,0,0); |
90 | | | |
91 | | | @states=`/bin/ps axostate`; |
92 | | | foreach (@states) { |
93 | | | $procslp++ if (/^[SI]/); #Sleeping (S=<20s, I=>20s) |
94 | | | $procdsc++ if (/^D/); #Disk wait |
95 | | | $procrun++ if (/^R/); #Running |
96 | | | $proczmb++ if (/^Z/); #Zombie |
97 | | | $procstp++ if (/^T/); #Stopped |
98 | | | } |
99 | | | |
100 | | | if ( ! -e "rrd/proc.rrd" ) { system("./makerrd","proc") } |
101 | | | RRDs::update "rrd/proc.rrd", time.":".$procslp.":".$procrun.":".$proczmb.":".$procstp.":".$procdsc; |
102 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update proc.rrd: $ERROR\n"; } |
103 | | | } |
104 | | | |
105 | | | |
106 | | | ##################################################################################### |
107 | | | # read users stats |
108 | | | # |
109 | | | sub read_users { |
110 | | | my %args=@_; |
111 | | | |
112 | | | # XXX im not shure if this will work. PR |
113 | | | @users=`who`; |
114 | | | ($tty,$pty,$pts)=(0,0,0); |
115 | | | foreach (@users) { |
116 | | | # print $_; |
117 | | | if (index($_," ttyv") >=0 ) { $tty++; } |
118 | | | elsif (index($_," ttyp") >=0 ) { $pty++; } |
119 | | | elsif (index($_," pts") >=0 ) { $pts++; } |
120 | | | } |
121 | | | |
122 | | | if ( ! -e "rrd/users.rrd" ) { system("./makerrd","users") } |
123 | | | RRDs::update "rrd/users.rrd", time.":".$tty.":".$pty.":".$pts; |
124 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update users.rrd: $ERROR\n"; } |
125 | | | } |
126 | | | |
127 | | | |
128 | | | #################################################################################### |
129 | | | # here we go with the memory and swap statistics... |
130 | | | # |
131 | | | sub read_mem { |
132 | | | my %args=@_; |
133 | | | |
134 | | | @top = `top -l2`; |
135 | | | |
136 | | | #PhysMem: 97.4M wired, 144M active, 567M inactive, 809M used, 727M free |
137 | | | #VM: 3.27G + 3.62M 14904(14904) pageins, 44(44) pageouts |
138 | | | |
139 | | | while(@top){ |
140 | | | if(/^PhysMem:/){ |
141 | | | s/^[0-9\.\sKMG]//g; |
142 | | | split; |
143 | | | foreach(@_){ |
144 | | | #convert SI to integer bytes |
145 | | | if(/\dG/){ s/G//; $_ *= (1024**3) } |
146 | | | if(/\dM/){ s/M//; $_ *= (1024**2) } |
147 | | | if(/\dK/){ s/K//; $_ *= 1024 } |
148 | | | $_ = int |
149 | | | } |
150 | | | (undef, $memwire, $memactv, $meminac, $memused, $memfree) = @_; |
151 | | | last; |
152 | | | } |
153 | | | } |
154 | | | |
155 | | | |
156 | | | #get swap. |
157 | | | # to be done! |
158 | | | open(IN,"/usr/sbin/pstat -sk|"); |
159 | | | while(<IN>) { |
160 | | | (undef, undef, $swpuse, $swpfre) = split; |
161 | | | } |
162 | | | |
163 | | | $swpuse*=1024; |
164 | | | $swpfre*=1024; |
165 | | | |
166 | | | if ( ! -e "rrd/mem.rrd" ) { system("./makerrd","mem") } |
167 | | | RRDs::update "rrd/mem.rrd", time.":".$memfree.":".$memwire.":".$memactv.":".$meminac.":".$swpfre.":".$swpuse; |
168 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update `mem.rrd': $ERROR\n"; } |
169 | | | } |
170 | | | |
171 | | | 1; |