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