1 | 1 | simandl | package HotSaNICmod::syssnmp; |
2 | | | |
3 | | | use lib "../../lib"; |
4 | | | use HotSaNICsnmp; |
5 | | | |
6 | | | sub version { |
7 | | | ($VERSION = '$Revision: 1.29 $') =~ s/.*(\d+\.\d+).*/$1/; |
8 | | | return "syssnmp.pm $VERSION"; |
9 | | | } |
10 | | | |
11 | | | sub sample { |
12 | | | %args=@_; |
13 | | | |
14 | | | foreach $entry (@{$args{HOST}}) { |
15 | | | my ($host,$community,$item,$dbname,$name,$file,$description,$targets)=HotSaNICmod::common::get_names($entry); |
16 | | | if ($host ne "") { |
17 | | | # Now do the real work ... |
18 | | | if (index($targets,"cpu" )>=0) { do_cpu($host,$community,$args{VARDIR}); } |
19 | | | if (index($targets,"load" )>=0) { do_load($host,$community); } |
20 | | | if (index($targets,"proc" )>=0) { do_proc($host,$community); } |
21 | | | if ( (index($targets,"mem" )>=0) or (index($targets,"swap") >=0) ) { do_mem($host,$community); } |
22 | | | if (index($targets,"users")>=0) { do_users($host,$community); } |
23 | | | } |
24 | | | } |
25 | | | } |
26 | | | |
27 | | | # Gather data from SNMP agent |
28 | | | |
29 | | | # uptime-oid: .1.3.6.1.2.1.25.1.1.0 |
30 | | | |
31 | | | sub do_cpu { |
32 | | | my ($host,$com,$vardir)=@_; |
33 | | | my $oid = '.1.3.6.1.4.1.2021.11'; # enterprises.ucdavis.systemStats |
34 | | | my $suf_user = '.50.0'; # .ssCpuRawUser.0 |
35 | | | my $suf_nice = '.51.0'; # .ssCpuRawNice.0 |
36 | | | my $suf_system = '.52.0'; # .ssCpuRawSystem.0 |
37 | | | my $suf_idle = '.53.0'; # .ssCpuRawIdle.0 |
38 | | | my $suf_intr = '.56.0'; # .ssCpuRawIdle.0 |
39 | | | |
40 | | | undef my %cpu_new; |
41 | | | my $cpu_all; |
42 | | | my %cpu_old = ( user => 0, nice => 0, system => 0, idle => 0 ); |
43 | | | my $filename = $vardir."/cpu_".$host.".dat"; |
44 | | | |
45 | | | defined($res = HotSaNICsnmp::snmp_walk($host, $com, ($oid))) || return undef; |
46 | | | $cpu_new{'user'} = $$res{$oid.$suf_user} || 0; |
47 | | | $cpu_new{'nice'} = $$res{$oid.$suf_nice} || 0; |
48 | | | $cpu_new{'system'} = $$res{$oid.$suf_system} || 0; |
49 | | | $cpu_new{'idle'} = $$res{$oid.$suf_idle} || 0; |
50 | | | $cpu_new{'intr'} = $$res{$oid.$suf_intr} || 0; |
51 | | | |
52 | | | if( -e $filename ) { |
53 | | | open(CPUFILE, "$filename"); |
54 | | | my $line = do { local $/; <CPUFILE> }; |
55 | | | ($user,$nice,$system,$idle) = split " ", $line; |
56 | | | close CPUFILE; |
57 | | | |
58 | | | $cpu_old{'user'} = $user || 0; |
59 | | | $cpu_old{'nice'} = $nice || 0; |
60 | | | $cpu_old{'system'} = $system || 0; |
61 | | | $cpu_old{'idle'} = $idle || 0; |
62 | | | } |
63 | | | |
64 | | | $cpu_all = |
65 | | | ($cpu_new{'user'}+$cpu_new{'nice'}+$cpu_new{'system'}+$cpu_new{'idle'}) |
66 | | | - ($cpu_old{'user'}+$cpu_old{'nice'}+$cpu_old{'system'}+$cpu_old{'idle'}); |
67 | | | |
68 | | | open(CPUFILE, ">$filename"); |
69 | | | print CPUFILE "$cpu_new{'user'} $cpu_new{'nice'} $cpu_new{'system'} $cpu_new{'idle'}"; |
70 | | | close CPUFILE; |
71 | | | |
72 | | | if ( $cpu_all > 0 ) { |
73 | | | $cpu_new{'user'} = ($cpu_new{'user'}-$cpu_old{'user'})/$cpu_all; |
74 | | | $cpu_new{'nice'} = ($cpu_new{'nice'}-$cpu_old{'nice'})/$cpu_all; |
75 | | | $cpu_new{'system'} = ($cpu_new{'system'}-$cpu_old{'system'})/$cpu_all; |
76 | | | $cpu_new{'idle'} = ($cpu_new{'idle'}-$cpu_old{'idle'})/$cpu_all; |
77 | | | } |
78 | | | else { |
79 | | | $cpu_new{'user'} = "U"; |
80 | | | $cpu_new{'nice'} = "U"; |
81 | | | $cpu_new{'system'} = "U"; |
82 | | | $cpu_new{'idle'} = "U"; |
83 | | | } |
84 | | | |
85 | | | my $rrdname = "cpu_".$host; |
86 | | | |
87 | | | if ( $^O =~ /bsd/i ) { |
88 | | | HotSaNICmod::do_rrd($rrdname,"U",time,$cpu_new{'user'},$cpu_new{'nice'},$cpu_new{'system'},$cpu_new{'idle'},$cpu_new{'intr'}); |
89 | | | } else { |
90 | | | HotSaNICmod::do_rrd($rrdname,"U",time,$cpu_new{'user'},$cpu_new{'nice'},$cpu_new{'system'},$cpu_new{'idle'}); |
91 | | | } |
92 | | | |
93 | | | } |
94 | | | |
95 | | | sub do_load { |
96 | | | my ($host,$com)=@_; |
97 | | | my $oid = '.1.3.6.1.4.1.2021.10.1.3'; # enterprises.ucdavis.laTable.laEntry.laLoad |
98 | | | |
99 | | | my @load; |
100 | | | |
101 | | | defined(my $res = HotSaNICsnmp::snmp_walk($host, $com, ($oid))) || return undef; |
102 | | | foreach ( sort keys(%$res) ) { |
103 | | | push @load, $$res{$_} || 0; |
104 | | | } |
105 | | | |
106 | | | my $rrdname = "load_".$host; |
107 | | | |
108 | | | HotSaNICmod::do_rrd($rrdname,"U",time,$load[2],$load[1],$load[0]); |
109 | | | } |
110 | | | |
111 | | | sub do_proc { |
112 | | | my ($host,$com)=@_; |
113 | | | my $oid = '.1.3.6.1.2.1.25.4.2.1.7'; # host.hrSWRun.hrSWRunTable.hrSWRunEntry.hrSWRunStatus |
114 | | | |
115 | | | my %proc = ( running => 0, |
116 | | | runnable => 0, |
117 | | | notRunnable => 0, |
118 | | | invalid => 0 |
119 | | | ); |
120 | | | |
121 | | | defined(my $res = HotSaNICsnmp::snmp_walk($host, $com, ($oid))) || return undef; |
122 | | | foreach( keys(%$res) ) { |
123 | | | for ($$res{$_}) { |
124 | | | if (/(1|running)/) { $proc{'running'}++; } |
125 | | | elsif (/(2|runnable)/) { $proc{'runnable'}++; } |
126 | | | elsif (/(3|notRunnable)/) { $proc{'notRunnable'}++; } |
127 | | | elsif (/(4|invalid)/) { $proc{'invalid'}++; } |
128 | | | else { HotSaNIClog::warn("unknown state seen."); } |
129 | | | } |
130 | | | } |
131 | | | |
132 | | | my $rrdname = "proc_".$host; |
133 | | | |
134 | | | HotSaNICmod::do_rrd($rrdname,"U",time,$proc{'runnable'},$proc{'running'},$proc{'invalid'},$proc{'notRunnable'},"U"); |
135 | | | } |
136 | | | |
137 | | | sub do_mem { |
138 | | | my ($host,$com)=@_; |
139 | | | my $oid = '.1.3.6.1.4.1.2021.4'; # enterprises.ucdavis.memory |
140 | | | my $suf_total = '.5.0'; # .memTotalReal.0 |
141 | | | my $suf_free = '.6.0'; # .memAvailReal.0 |
142 | | | my $suf_buffer = '.14.0'; # .memBuffer.0 |
143 | | | my $suf_cache = '.15.0'; # .memCached.0 |
144 | | | my $suf_inactive = '0'; # inactive not readable via snmp |
145 | | | |
146 | | | my $suf_swap_total = '.3.0'; # .memTotalSwap.0 |
147 | | | my $suf_swap_free = '.4.0'; # .memAvailSwap.0 |
148 | | | |
149 | | | my %mem; |
150 | | | |
151 | | | defined(my $res = HotSaNICsnmp::snmp_walk($host, $com, ($oid))) || return undef; |
152 | | | $mem{'totalReal'} = $$res{$oid.$suf_total} * 1024 || 0; |
153 | | | $mem{'availReal'} = $$res{$oid.$suf_free} * 1024 || 0; |
154 | | | $mem{'buffer'} = $$res{$oid.$suf_buffer} * 1024 || 0; |
155 | | | $mem{'cached'} = $$res{$oid.$suf_cache} * 1024 || 0; |
156 | | | $mem{'totalSwap'} = $$res{$oid.$suf_swap_total} * 1024 || 0; |
157 | | | $mem{'swapFree'} = $$res{$oid.$suf_swap_free} * 1024 || 0; |
158 | | | $mem{'swapUsed'} = $mem{'totalSwap'} - $mem{'swapFree'}; |
159 | | | |
160 | | | $mem{'shared'} = $mem{'totalReal'}-$mem{'availReal'}-$mem{'buffer'}-$mem{'cached'}; |
161 | | | |
162 | | | my $rrdname = "mem_".$host; |
163 | | | |
164 | | | if ( $^O =~ /bsd/ ) { |
165 | | | HotSaNICmod::do_rrd($rrdname,"U",time,$mem{'availReal'},$mem{'inactive'},$mem{'shared'},$mem{'buffer'},$mem{'cached'},$mem{'swapFree'},$mem{'swapUsed'}); |
166 | | | # HotSaNICmod::do_rrd("mem","U",time,$memfree,$meminac,$memactv,$memwire,$memcach,$swpfree,$swpused); |
167 | | | } else { |
168 | | | HotSaNICmod::do_rrd($rrdname,"U",time,$mem{'availReal'},$mem{'shared'},$mem{'buffer'},$mem{'cached'},$mem{'swapFree'},$mem{'swapUsed'}); |
169 | | | } |
170 | | | } |
171 | | | |
172 | | | sub do_users { |
173 | | | my ($host,$com)=@_; |
174 | | | my $oid = '.1.3.6.1.2.1.25.1.5.0'; # host.hrSystem.hrSystemNumUsers.0 |
175 | | | |
176 | | | defined(my $res = HotSaNICsnmp::snmp_get($host, $com, ($oid))) || return undef; |
177 | | | |
178 | | | my $users=$$res{$oid}; |
179 | | | my $rrdname = "users_".$host; |
180 | | | |
181 | | | HotSaNICmod::do_rrd($rrdname,"U",time,$users,"U","U"); |
182 | | | } |
183 | | | |
184 | | | 1; |