1 | 1 | simandl | package HotSaNICmod::OSdep; |
2 | | | |
3 | | | use RRDs; |
4 | | | |
5 | | | # return module version |
6 | | | # |
7 | | | sub version { |
8 | | | ($VERSION = '$Revision: 1.8 $') =~ 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 | | | $rawcpu=`sysctl kern.cp_time`; |
49 | | | chomp $rawcpu; # don't get why this is needed... |
50 | | | (undef, $cpu1, $cpu2, $cpu3, $cpu4, $cpu5)=split(/ /,$rawcpu ); |
51 | | | |
52 | | | $cpu1old=0; $cpu2old=0; $cpu3old=0; $cpu4old=0; $cpu5old=0; |
53 | | | |
54 | | | if ( -e "cpu.dat" ) { |
55 | | | open(CPU,"cpu.dat"); |
56 | | | while (<CPU>) { |
57 | | | ($cpu1old, $cpu2old, $cpu3old, $cpu4old, $cpu5old)=split / /; |
58 | | | } |
59 | | | close(CPU); |
60 | | | } |
61 | | | |
62 | | | open(CPU,">cpu.dat"); |
63 | | | print CPU $cpu1." ".$cpu2." ".$cpu3." ".$cpu4." ".$cpu5."\n"; |
64 | | | close(CPU); |
65 | | | |
66 | | | # deltas |
67 | | | $all=($cpu1+$cpu2+$cpu3+$cpu4+$cpu5)-($cpu1old+$cpu2old+$cpu3old+$cpu4old+$cpu5old); |
68 | | | $cpuusr=($cpu1-$cpu1old)/$all; |
69 | | | $cpunic=($cpu2-$cpu2old)/$all; |
70 | | | $cpusys=($cpu3-$cpu3old)/$all; |
71 | | | $cpuint=($cpu4-$cpu4old)/$all; |
72 | | | $cpuidl=($cpu5-$cpu5old)/$all; |
73 | | | |
74 | | | if ( ! -e "rrd/cpu.rrd" ) { system("./makerrd","cpu"); } |
75 | | | RRDs::update "rrd/cpu.rrd", time.":".$cpuusr.":".$cpunic.":".$cpusys.":".$cpuidl.":".$cpuint; |
76 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update cpu.rrd: $ERROR\n"; } |
77 | | | |
78 | | | } |
79 | | | |
80 | | | |
81 | | | |
82 | | | ##################################################################################### |
83 | | | # here we go with the load-average value ... |
84 | | | # |
85 | | | sub read_load { |
86 | | | my %args=@_; |
87 | | | (undef,undef,$load1,$load5,$load15,undef)=split(/ /,`/sbin/sysctl vm.loadavg`); |
88 | | | |
89 | | | if ( ! -e "rrd/load.rrd" ) { system("./makerrd","load") } |
90 | | | RRDs::update "rrd/load.rrd", time.":".$load15.":".$load5.":".$load1; |
91 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update load.rrd: $ERROR\n"; } |
92 | | | } |
93 | | | |
94 | | | |
95 | | | |
96 | | | ##################################################################################### |
97 | | | # now the processes. We have to check how many processes are sleeping, |
98 | | | # running, zombieng spwapped to disc or stopped. To do this we simply |
99 | | | # have to walk through the /proc tree, check all "stat" files of each |
100 | | | # process-id subdir and increment the according state-counter. |
101 | | | # |
102 | | | sub read_proc { |
103 | | | my %args=@_; |
104 | | | ($procslp,$procrun,$proczmb,$procstp,$procdsc)=(0,0,0,0,0); |
105 | | | |
106 | | | @states=`/bin/ps axostate`; |
107 | | | foreach (@states) { |
108 | | | $procslp++ if (/^[SI]/); #Sleeping (S=<20s, I=>20s) |
109 | | | $procdsc++ if (/^D/); #Disk wait |
110 | | | $procrun++ if (/^R/); #Running |
111 | | | $proczmb++ if (/^Z/); #Zombie |
112 | | | $procstp++ if (/^T/); #Stopped |
113 | | | } |
114 | | | |
115 | | | if ( ! -e "rrd/proc.rrd" ) { system("./makerrd","proc") } |
116 | | | RRDs::update "rrd/proc.rrd", time.":".$procslp.":".$procrun.":".$proczmb.":".$procstp.":".$procdsc; |
117 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update proc.rrd: $ERROR\n"; } |
118 | | | } |
119 | | | |
120 | | | |
121 | | | ##################################################################################### |
122 | | | # read users stats |
123 | | | # |
124 | | | sub read_users { |
125 | | | my %args=@_; |
126 | | | |
127 | | | # XXX im not shure if this will work. PR |
128 | | | @users=`who`; |
129 | | | ($tty,$pty,$pts)=(0,0,0); |
130 | | | foreach (@users) { |
131 | | | # print $_; |
132 | | | if (index($_," ttyv") >=0 ) { $tty++; } |
133 | | | elsif (index($_," ttyp") >=0 ) { $pty++; } |
134 | | | elsif (index($_," pts") >=0 ) { $pts++; } |
135 | | | } |
136 | | | |
137 | | | if ( ! -e "rrd/users.rrd" ) { system("./makerrd","users") } |
138 | | | RRDs::update "rrd/users.rrd", time.":".$tty.":".$pty.":".$pts; |
139 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update users.rrd: $ERROR\n"; } |
140 | | | } |
141 | | | |
142 | | | |
143 | | | #################################################################################### |
144 | | | # here we go with the memory and swap statistics... |
145 | | | # |
146 | | | sub read_mem { |
147 | | | my %args=@_; |
148 | | | |
149 | | | # FreeBSD sucks for this... |
150 | | | (undef, $pagesize) = split(/ /, `sysctl vm.stats.vm.v_page_size`); |
151 | | | (undef, $memtotl) = split(/ /, `sysctl vm.stats.vm.v_page_count`); $memtotl *= $pagesize; |
152 | | | (undef, $memfree) = split(/ /, `sysctl vm.stats.vm.v_free_count`); $memfree *= $pagesize; |
153 | | | (undef, $memwire) = split(/ /, `sysctl vm.stats.vm.v_wire_count`); $memwire *= $pagesize; |
154 | | | (undef, $memactv) = split(/ /, `sysctl vm.stats.vm.v_active_count`); $memactv *= $pagesize; |
155 | | | (undef, $meminac) = split(/ /, `sysctl vm.stats.vm.v_inactive_count`);$meminac *= $pagesize; |
156 | | | (undef, $memcach) = split(/ /, `sysctl vm.stats.vm.v_cache_count`); $memcach *= $pagesize; |
157 | | | |
158 | | | #get swap. |
159 | | | #may be dodgy on systems with multiple swap partitions. however the |
160 | | | #total should be printed on the last line, overwriting the vars |
161 | | | open(IN,"/usr/sbin/pstat -sk|"); |
162 | | | while(<IN>) { |
163 | | | (undef, undef, $swpuse, $swpfre) = split; |
164 | | | } |
165 | | | |
166 | | | $swpuse*=1024; |
167 | | | $swpfre*=1024; |
168 | | | |
169 | | | if ( ! -e "rrd/mem.rrd" ) { system("./makerrd","mem") } |
170 | | | RRDs::update "rrd/mem.rrd", time.":".$memfree.":".$memwire.":".$memactv.":".$meminac.":".$memcach.":".$swpfre.":".$swpuse; |
171 | | | if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": nable to update `mem.rrd': $ERROR\n"; } |
172 | | | } |
173 | | | |
174 | | | 1; |