jablonka.czprosek.czf

hotsanic

Subversion Repositories:
[/] [branches/] [HotSaNIC-0.5.0-jablonecka/] [modules/] [system/] [platform/] [syssnmp.pm] - Blame information for rev 20

 

Line No. Rev Author Line
11simandlpackage syssnmp;
2 
3use lib "../../lib";
4use HotSaNICsnmp;
5 
6my ($host,$com);
7 
8sub version {
9 ($VERSION = '$Revision: 1.18 $') =~ s/.*(\d+\.\d+).*/$1/;
10 return "syssnmp.pm $VERSION";
11 }
12 
13sub do_snmp {
14 my ($arg,$vardir) = @_;
15 $arg =~ s/SNMP://;
16 ($host, $com) = split ":", $arg;
17 
18 # Now do the real work ...
19 do_cpu($vardir);
20 do_load();
21 do_proc();
22 do_mem();
23 do_users();
24}
25 
26# Gather data from SNMP agent
27 
28sub do_cpu {
29 my ($vardir)=@_;
30 my $oid = '.1.3.6.1.4.1.2021.11';
31 # enterprises.ucdavis.systemStats
32 
33 my $suf_user = '.50.0';
34 # .ssCpuRawUser.0
35 my $suf_nice = '.51.0';
36 # .ssCpuRawNice.0
37 my $suf_system = '.52.0';
38 # .ssCpuRawSystem.0
39 my $suf_idle = '.53.0';
40 # .ssCpuRawIdle.0
41 
42 undef my %cpu_new;
43 my $cpu_all;
44 my %cpu_old = ( user => 0, nice => 0, system => 0, idle => 0 );
45 my $filename = $vardir."/cpu_".$host.".dat";
46 
47 defined($res = HotSaNICsnmp::snmp_walk($host, $com, ($oid))) || return undef;
48 $cpu_new{'user'} = $$res{$oid.$suf_user} || 0;
49 $cpu_new{'nice'} = $$res{$oid.$suf_nice} || 0;
50 $cpu_new{'system'} = $$res{$oid.$suf_system} || 0;
51 $cpu_new{'idle'} = $$res{$oid.$suf_idle} || 0;
52 
53 if( -e $filename ) {
54 open(CPUFILE, "$filename");
55 my $line = do { local $/; <CPUFILE> };
56 ($user,$'nice,$system,$idle) = split " ", $line;
57 close CPUFILE;
58 
59 $cpu_old{'user'} = $user || 0;
60 $cpu_old{'nice'} = $nice || 0;
61 $cpu_old{'system'} = $system || 0;
62 $cpu_old{'idle'} = $idle || 0;
63 }
64 
65 $cpu_all =
66 ($cpu_new{'user'}+$cpu_new{'nice'}+$cpu_new{'system'}+$cpu_new{'idle'})
67 - ($cpu_old{'user'}+$cpu_old{'nice'}+$cpu_old{'system'}+$cpu_old{'idle'});
68 
69 open(CPUFILE, ">$filename");
70 print CPUFILE "$cpu_new{'user'} $cpu_new{'nice'} $cpu_new{'system'} $cpu_new{'idle'}";
71 close CPUFILE;
72 
73 if ( $cpu_all > 0 ) {
74 $cpu_new{'user'} = ($cpu_new{'user'}-$cpu_old{'user'})/$cpu_all;
75 $cpu_new{'nice'} = ($cpu_new{'nice'}-$cpu_old{'nice'})/$cpu_all;
76 $cpu_new{'system'} = ($cpu_new{'system'}-$cpu_old{'system'})/$cpu_all;
77 $cpu_new{'idle'} = ($cpu_new{'idle'}-$cpu_old{'idle'})/$cpu_all;
78 }
79 else {
80 $cpu_new{'user'} = "U";
81 $cpu_new{'nice'} = "U";
82 $cpu_new{'system'} = "U";
83 $cpu_new{'idle'} = "U";
84 }
85 
86 do_rrd_cpu(%cpu_new);
87 
88 #print "CPU: $cpu_new{'user'} $cpu_new{'nice'} $cpu_new{'system'} $cpu_new{'idle'}\n";
89 
90}
91 
92sub do_load {
93 my $oid = '.1.3.6.1.4.1.2021.10.1.3';
94 # enterprises.ucdavis.laTable.laEntry.laLoad
95 
96 my @load;
97 
98 defined(my $res = HotSaNICsnmp::snmp_walk($host, $com, ($oid))) || return undef;
99 foreach ( keys(%$res) ) {
100 push @load, $$res{$_} || 0;
101 }
102 
103 do_rrd_load(@load);
104 
105 #print "LOAD: $load[0] $load[1] $load[2]\n";
106}
107 
108sub do_proc {
109 my $oid = '.1.3.6.1.2.1.25.4.2.1.7';
110 # host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunStatus
111 
112 my %proc = ( running => 0,
113 runnable => 0,
114 notRunnable => 0,
115 invalid => 0
116 );
117 
118 defined(my $res = HotSaNICsnmp::snmp_walk($host, $com, ($oid))) || return undef;
119 foreach( keys(%$res) ) {
120 for ($$res{$_}) {
121 if (/(1|running)/) { $proc{'running'}++; }
122 elsif (/(2|runnable)/) { $proc{'runnable'}++; }
123 elsif (/(3|notRunnable)/) { $proc{'notRunnable'}++; }
124 elsif (/(4|invalid)/) { $proc{'invalid'}++; }
125 else { print "unknown state seen.\n"; }
126 }
127 }
128 
129 do_rrd_proc(%proc);
130 
131 #print "PROC: $proc{'running'} $proc{'runnable'} $proc{'notRunnable'} $proc{'invalid'}\n";
132}
133 
134sub do_mem {
135 my $oid = '.1.3.6.1.4.1.2021.4';
136 # enterprises.ucdavis.memory
137 
138 my $suf_total = '.5.0';
139 # .memTotalReal.0
140 my $suf_free = '.6.0';
141 # .memAvailReal.0
142 my $suf_buffer = '.14.0';
143 # .memBuffer.0
144 my $suf_cache = '.15.0';
145 # .memCached.0
146 
147 my $suf_swap_total = '.3.0';
148 # .memTotalSwap.0
149 my $suf_swap_free = '.4.0';
150 # .memAvailSwap.0
151 
152 my %mem;
153 
154 defined(my $res = HotSaNICsnmp::snmp_walk($host, $com, ($oid))) || return undef;
155 $mem{'totalReal'} = $$res{$oid.$suf_total} * 1024 || 0;
156 $mem{'availReal'} = $$res{$oid.$suf_free} * 1024 || 0;
157 $mem{'buffer'} = $$res{$oid.$suf_buffer} * 1024 || 0;
158 $mem{'cached'} = $$res{$oid.$suf_cache} * 1024 || 0;
159 $mem{'totalSwap'} = $$res{$oid.$suf_swap_total} * 1024 || 0;
160 $mem{'swapFree'} = $$res{$oid.$suf_swap_free} * 1024 || 0;
161 $mem{'swapUsed'} = $mem{'totalSwap'} - $mem{'swapFree'};
162 
163 $mem{'shared'} = $mem{'totalReal'}-$mem{'availReal'}-$mem{'buffer'}-$mem{'cached'};
164 
165 do_rrd_mem(%mem);
166 
167 #print "MEM: ";
168 #foreach ( keys(%mem) ) { print "$mem{$_} "; }
169 #print "\n";
170}
171 
172sub do_users {
173 my $oid = '.1.3.6.1.2.1.25.1.5.0';
174 # host.hrSystem.hrSystemNumUsers.0
175 
176 defined(my $res = HotSaNICsnmp::snmp_get($host, $com, ($oid))) || return undef;
177 
178 do_rrd_users($$res{$oid});
179 
180 #print "USERS: $res\n";
181}
182 
183# End gather functions
184#
185# Start rrd-functions
186 
187sub do_rrd_cpu() {
188 my %cpu_new = @_;
189 my $rrdname = "cpu_".$host;
190 
191 if ( ! -e "rrd/".$rrdname.".rrd" ) {
192 system("./makerrd", $rrdname);
193 }
194 
195 RRDs::update "rrd/".$rrdname.".rrd", time.":".$cpu_new{'user'}.":".$cpu_new{'nice'}.":".$cpu_new{'system'}.":".$cpu_new{'idle'};
196 if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update $rrdname.rrd: $ERROR\n"; }
197 
198 #print "Finished RRD update (CPU)\n";
199}
200 
201sub do_rrd_load() {
202 my @load = @_;
203 my $rrdname = "load_".$host;
204 
205 if ( ! -e "rrd/".$rrdname.".rrd" ) { system("./makerrd", $rrdname) }
206 RRDs::update "rrd/".$rrdname.".rrd", time.":".$load[2].":".$load[1].":".$load[0];
207 if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update $rrdname.rrd: $ERROR\n"; }
208 
209 #print "Finished RRD update (LOAD)\n";
210}
211 
212sub do_rrd_proc() {
213 my %proc = @_;
214 my $rrdname = "proc_".$host;
215 
216 if ( ! -e "rrd/".$rrdname.".rrd" ) { system("./makerrd",$rrdname) }
217 RRDs::update "rrd/".$rrdname.".rrd", time.":".$proc{'runnable'}.":".$proc{'running'}.":".$proc{'invalid'}.":".$proc{'notRunnable'}.":U";
218 if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update $rrdname.rrd: $ERROR\n"; }
219 
220 #print "Finished RRD update (PROC)\n";
221}
222 
223sub do_rrd_mem() {
224 my %mem = @_;
225 my $rrdname = "mem_".$host;
226 
227 if ( ! -e "rrd/".$rrdname.".rrd" ) { system("./makerrd",$rrdname) }
228 RRDs::update "rrd/".$rrdname.".rrd", time.":".$mem{'availReal'}.":".$mem{'shared'}.":".$mem{'buffer'}.":".$mem{'cached'}.":".$mem{'swapFree'}.":".$mem{'swapUsed'};
229 if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update $rrdname.rrd: $ERROR\n"; }
230 
231 #print "Finished RRD update (MEM)\n";
232}
233 
234sub do_rrd_users() {
235 my ($users) = @_;
236 my $rrdname = "users_".$host;
237 
238 if ( ! -e "rrd/".$rrdname.".rrd" ) { system("./makerrd",$rrdname) }
239 RRDs::update "rrd/".$rrdname.".rrd", time.":".$users.":U:U";
240 if ($ERROR = RRDs::error) { print time," ",$args{MODNAME},": unable to update $rrdname.rrd: $ERROR\n"; }
241 
242 #print "Finished RRD update (USERS)\n";
243}
244 
2451;

Powered by WebSVN 2.2.1