jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [branches/] [HotSaNIC-0.5.0-pre6/] [modules/] [system/] [platform/] [linux.pm] - Blame information for rev 8

 

Line No. Rev Author Line
11simandlpackage HotSaNICmod::OSdep;
2 
3# return module version
4#
5sub version {
6 ($VERSION = '$Revision: 1.22 $') =~ s/.*(\d+\.\d+).*/$1/;
7 return "$^O.pm $VERSION";
8 }
9 
10# sample data
11#
12sub sample {
13 my %args=@_;
14 
15 @sections = split " ", ($args{SECTIONS});
16 foreach (@sections){
17 if ($_ eq "cpu") {read_cpu(%args)}
18 elsif ($_ eq "load") {read_load(%args)}
19 elsif ($_ eq "proc") {read_proc(%args)}
20 elsif ($_ eq "mem") {read_mem(%args)}
21 elsif ($_ eq "users") {read_users(%args)}
22 elsif ($_ eq "irq") {read_interrupts(%args)}
23 elsif ($_ eq "uptime") {read_uptime(%args)}
24 }
25 
26 }
27 
28# users.rrd -> tty pty pts
29# cpu.rrd -> cpuusr cpusys cpunic 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# build CPU statistics
36# in /proc/stat there is a CPU entry which provides us with the
37# number of ticks the CPU was in all states ( user nice system idle )
38# We simply have to gather this information and calculate the actual percentage :)
39# To do this we have to remember the last read values of course!
40#
41sub read_cpu {
42 my %args=@_;
43 my $multicpu=0;
44 
45 # read actual state
46 open(IN,"/proc/stat");
47 while(<IN>) {
48 chomp;
49 if( index($_,"cpu") >=0 ) {
50 ($cpu, $cpu1, $cpu2, $cpu3, $cpu4)=split;
51 $cpu1{$cpu}=$cpu1;
52 $cpu2{$cpu}=$cpu2;
53 $cpu3{$cpu}=$cpu3;
54 $cpu4{$cpu}=$cpu4;
55 $cpu5{$cpu}=0;
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 close(IN);
65 
66 # read last state and store actual state
67 # usr nice sys idle int
68 my $DATFILE=$args{VARDIR}."/cpu.dat";
69 if ( -e $DATFILE ) {
70 open(CPU,$DATFILE);
71 while (<CPU>) {
72 $line=$_." 0 0 0 0 0 0";
73 ($cpu, $cpu1old, $cpu2old, $cpu3old, $cpu4old, $cpu5old)=split / /,$line;
74 
75 if ($cpu !~ /cpu/) {
76 $cpu5old=$cpu4old;
77 $cpu4old=$cpu3old;
78 $cpu3old=$cpu2old;
79 $cpu2old=$cpu1old;
80 $cpu1old=$cpu;
81 $cpu="cpu";
82 }
83 
84 $cpu1old{$cpu}=$cpu1old;
85 $cpu2old{$cpu}=$cpu2old;
86 $cpu3old{$cpu}=$cpu3old;
87 $cpu4old{$cpu}=$cpu4old;
88 $cpu5old{$cpu}=$cpu5old;
89 }
90 close(CPU);
91 }
92 
93 open(CPU,">".$DATFILE);
94 
95 foreach $cpu (sort keys %cpu1) {
96 print CPU $cpu." ".$cpu1{$cpu}." ".$cpu2{$cpu}." ".$cpu3{$cpu}." ".$cpu4{$cpu}." ".$cpu5{$cpu}."\n";
97 
98# calculate everything....
99#
100 
101 $all{$cpu}=($cpu1{$cpu}+$cpu2{$cpu}+$cpu3{$cpu}+$cpu4{$cpu}+$cpu5{$cpu})-($cpu1old{$cpu}+$cpu2old{$cpu}+$cpu3old{$cpu}+$cpu4old{$cpu}+$cpu5old{$cpu});
102 
103 $cpuusr{$cpu}=($cpu1{$cpu}-$cpu1old{$cpu})/$all{$cpu};
104 $cpunic{$cpu}=($cpu2{$cpu}-$cpu2old{$cpu})/$all{$cpu};
105 $cpusys{$cpu}=($cpu3{$cpu}-$cpu3old{$cpu})/$all{$cpu};
106 $cpuidl{$cpu}=($cpu4{$cpu}-$cpu4old{$cpu})/$all{$cpu};
107 $cpuint{$cpu}=($cpu5{$cpu}-$cpu5old{$cpu})/$all{$cpu};
108 
109 HotSaNICmod::do_rrd($cpu,"U",time,$cpuusr{$cpu},$cpunic{$cpu},$cpusys{$cpu},$cpuidl{$cpu});
110 
111# on single-cpu machines only the global database has to be updated.
112#
113 last if ($multicpu==0);
114 
115 }
116 close(CPU);
117 }
118 
119 
120 
121#####################################################################################
122# here we go with the load-average value ...
123#
124sub read_load {
125 my %args=@_;
126 open(IN,"/proc/loadavg"); ($load1,$load5,$load15)=split(/ /,<IN>); close(IN);
127 HotSaNICmod::do_rrd("load","U",time,$load15,$load5,$load1);
128 }
129 
130 
131 
132#####################################################################################
133# now the processes. We have to check how many processes are sleeping,
134# running, zombieng spwapped to disc or stopped. To do this we simply
135# have to walk through the /proc tree, check all "stat" files of each
136# process-id subdir and increment the according state-counter.
137#
138sub read_proc {
139 my %args=@_;
140 ($procslp,$procrun,$proczmb,$procstp,$procdsc)=(0,0,0,0,0);
141 opendir (DIR,"/proc");@dir=readdir (DIR);closedir(DIR);
142 foreach $entry (@dir) {
143 # check if entry is a numeric one
144 if (($entry =~ /^\d+$/) && ( -e "/proc/$entry/stat" )) {
145 if (open (STAT,"/proc/$entry/stat")) {
146 $_=<STAT>;
147 close(STAT);
148 if (index($_," S ") >=0 ) { $procslp++ }
149 elsif (index($_," D ") >=0 ) { $procdsc++ }
150 elsif (index($_," R ") >=0 ) { $procrun++ }
151 elsif (index($_," Z ") >=0 ) { $proczmb++ }
152 elsif (index($_," T ") >=0 ) { $procstp++ }
153 }
154 }
155 }
156 HotSaNICmod::do_rrd("proc","U",time,$procslp,$procrun,$proczmb,$procstp,$procdsc);
157 }
158 
159#####################################################################################
160# read users stats
161#
162sub read_users {
163 my %args=@_;
164 
165 
166 # build users-table first
167 #
168 my $command="/usr/bin/who";
169 open FILE,"$command|" || HotSaNIClog::error("unable to run '$command': $!");
170 my @users=(<FILE>);
171 close FILE;
172 
173 ($tty,$pty,$pts)=(0,0,0);
174 foreach (@users) {
175 if (index($_," tty") >=0 ) { $tty++; }
176 elsif (index($_," pty") >=0 ) { $pty++; }
177 elsif (index($_," pts") >=0 ) { $pts++; }
178 }
179 HotSaNICmod::do_rrd("users","U",time,$tty,$pty,$pts);
180 }
181 
182####################################################################################
183# here we go with the memory and swapfile statistics...
184#
185sub read_mem {
186 
187 my %args=@_;
188 my $KERNEL=HotSaNICparser::identify_kernel();
189 
190 open(IN,"/proc/meminfo");
191 while(<IN>) {
192 chomp;
193 if (index($_,"Mem:")>=0) { (undef,$memtot,undef,$memfre,$memshr,$membuf,$memcac)=split; }
194 elsif (index($_,"Swap:")>=0) { (undef,undef,$swpuse,$swpfre)=split; }
195 
196 if ($KERNEL>2.4) {
197 # This is kernel >= 2.5 code
198 if ($_ =~ /^MemTotal:/) { (undef,$memtot,undef)=split; $memtot*=1024; }
199 elsif ($_ =~ /^MemFree:/) { (undef,$memfre,undef)=split; $memfre*=1024; }
200 elsif ($_ =~ /^Buffers:/) { (undef,$membuf,undef)=split; $membuf*=1024; }
201 elsif ($_ =~ /^Cached:/) { (undef,$memcac,undef)=split; $memcac*=1024; }
202 elsif ($_ =~ /^SwapTotal:/) { (undef,$swptot,undef)=split; $swptot*=1024; }
203 elsif ($_ =~ /^SwapFree:/) { (undef,$swpfre,undef)=split; $swpfre*=1024; }
204 }
205 
206 }
207 close(IN);
208 
209 if ($KERNEL>2.4) { $swpuse=$swptot-$swpfre; }
210 $memshr=$memtot-$memfre-$membuf-$memcac;
211 
212 HotSaNICmod::do_rrd("mem","U",time,$memfre,$memshr,$membuf,$memcac,$swpfre,$swpuse);
213 }
214 
215sub read_interrupts {
216 my %args=@_;
217 
218 # 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
219 
220 my @stats;
221 open FILE,"/proc/stat";
222 while (<FILE>) { (undef,@stats)=split if /intr/; }
223 close FILE;
224 
225 # need first 17 values from array
226 HotSaNICmod::do_rrd("irq","U",time,@stats[0..16]);
227 }
228 
229 
230 
231#####################################################################################
232# here we go with the load-average value ...
233#
234sub read_uptime {
235 my %args=@_;
236 open(IN,"/proc/uptime"); ($up,$idle)=split(/ /,<IN>); close(IN);
237 $up/=86400;
238 $idle/=86400;
239 
240 HotSaNICmod::do_rrd("uptime","U",time,$up,$idle);
241 }
242 
2431;

Powered by WebSVN 2.2.1