Debian Codegraph @

check_hpasm/src/plugins-scripts/HP/Server.pm

raw
1 package HP::Server;
2
3 use strict;
4 use constant { OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => 3 };
5
6 sub new {
7 my $class = shift;
8 my %params = @_;
9 my $self = {
10 runtime => $params{runtime},
11 productname => 'unknown',
12 };
13 bless $self, $class;
14 if (! ($self->{runtime}->{plugin}->opts->hostname ||
15 $self->{runtime}->{plugin}->opts->snmpwalk)) {
16 bless $self, 'HP::Proliant::CLI';
17 $self->{method} = 'cli';
18 } else {
19 $self->check_snmp_and_model();
20 if ($self->{runtime}->{options}->{servertype}) {
21 $self->{productname} = 'ProLiant' if
22 $self->{runtime}->{options}->{servertype} eq 'proliant';
23 $self->{productname} = 'BladeSystem' if
24 $self->{runtime}->{options}->{servertype} eq 'bladesystem';
25 $self->{productname} = 'Storage' if
26 $self->{runtime}->{options}->{servertype} eq 'storage';
27 }
28 if (! $self->{runtime}->{plugin}->check_messages()) {
29 if ($self->{productname} =~ /ProLiant/) {
30 bless $self, 'HP::Proliant::SNMP';
31 $self->trace(3, 'using HP::Proliant::SNMP');
32 } elsif ($self->{productname} =~ /^DL\d+\s*G\d+/) {
33 bless $self, 'HP::Proliant::SNMP';
34 $self->trace(3, 'using HP::Proliant::SNMP');
35 } elsif ($self->{productname} =~ /OpenView .* appliance/) {
36 bless $self, 'HP::Proliant::SNMP';
37 $self->trace(3, 'using HP::Proliant::SNMP');
38 } elsif ($self->{productname} =~ /Synergy.*Gen/i) {
39 bless $self, 'HP::Proliant::SNMP';
40 $self->trace(3, 'using HP::Proliant::SNMP');
41 } elsif ($self->{productname} =~ /Superdome.*Enclosure/i) {
42 bless $self, 'HP::BladeSystem';
43 $self->trace(3, 'using HP::BladeSystem');
44 } elsif ($self->{productname} =~ /BladeSystem/) {
45 bless $self, 'HP::BladeSystem';
46 $self->trace(3, 'using HP::BladeSystem');
47 } elsif ($self->{productname} =~ /PROLIANT 4LEE/) {
48 bless $self, 'HP::Storage';
49 $self->trace(3, 'using HP::Storage');
50 } elsif ($self->{productname} =~ /X\d+[\s\w]* Network Storage/) {
51 # HP X1600 Network Storage System
52 # HP X1600 G2 Network Storage System
53 bless $self, 'HP::Proliant::SNMP';
54 $self->trace(3, 'using HP::Proliant::SNMP');
55 } elsif ($self->{productname} =~ /StorageWorks/i) {
56 bless $self, 'HP::StorageWorks';
57 $self->trace(3, 'using HP::StorageWorks');
58 } elsif ($self->{productname} =~ /MSA/i) {
59 bless $self, 'HP::StorageWorks';
60 $self->trace(3, 'using HP::StorageWorks');
61 } elsif ($self->{productname} =~ /StoreEasy/i) {
62 bless $self, 'HP::Proliant::SNMP';
63 $self->trace(3, 'using HP::Proliant::SNMP');
64 } elsif ($self->{productname} =~ /Storage/) { # fake
65 bless $self, 'HP::Storage';
66 $self->trace(3, 'using HP::Storage');
67 } else {
68 $self->add_message(CRITICAL,
69 sprintf('unknown device%s', $self->{productname} eq 'unknown' ?
70 '' : '('.$self->{productname}.')'));
71 }
72 $self->{method} = 'snmp';
73 }
74 }
75 if ($self->{runtime}->{options}->{blacklist} &&
76 -f $self->{runtime}->{options}->{blacklist}) {
77 $self->{runtime}->{options}->{blacklist} = do {
78 local (@ARGV, $/) = $self->{runtime}->{options}->{blacklist}; <> };
79 }
80 return $self;
81 }
82
83 sub check_snmp_and_model {
84 # uptime pruefen
85 # dann whoami
86 my $self = shift;
87 if ($self->{runtime}->{plugin}->opts->snmpwalk) {
88 my $response = {};
89 if (! -f $self->{runtime}->{plugin}->opts->snmpwalk) {
90 $self->{runtime}->{plugin}->add_message(CRITICAL,
91 sprintf 'file %s not found',
92 $self->{runtime}->{plugin}->opts->snmpwalk);
93 } elsif (-x $self->{runtime}->{plugin}->opts->snmpwalk) {
94 my $cmd = sprintf "%s -On -v%s -c%s %s:%s 1.3.6.1.4.1.232 2>&1",
95 $self->{runtime}->{plugin}->opts->snmpwalk,
96 $self->{runtime}->{plugin}->opts->protocol,
97 $self->{runtime}->{plugin}->opts->community,
98 $self->{runtime}->{plugin}->opts->domain,
99 $self->{runtime}->{plugin}->opts->hostname;
100 open(WALK, "$cmd |");
101 while (<WALK>) {
102 if (/^.*?\.(232\.[\d\.]+) = .*?: (\-*\d+)/) {
103 $response->{'1.3.6.1.4.1.'.$1} = $2;
104 } elsif (/^.*?\.(232\.[\d\.]+) = .*?: "(.*?)"/) {
105 $response->{'1.3.6.1.4.1.'.$1} = $2;
106 $response->{'1.3.6.1.4.1.'.$1} =~ s/\s+$//;
107 }
108 }
109 close WALK;
110 } else {
111 open(MESS, $self->{runtime}->{plugin}->opts->snmpwalk);
112 my $in_string = 0;
113 my $in_hex_string = 0;
114 my $hex_oid = 0;
115 while(<MESS>) {
116 chomp;
117 if ($in_hex_string && /^(([0-9a-fA-F]{2})( [0-9a-fA-F]{2})*)\s*$/) {
118 $response->{$hex_oid} .= " ".$1;
119 } elsif ($in_string) {
120 if (/(.*)"$/) {
121 $response->{$hex_oid} .= $1;
122 $in_string = 0;
123 } else {
124 $response->{$hex_oid} .= $_;
125 }
126 } elsif (/^.*?\.(232\.[\d\.]+) = .*?: (\-*\d+)\s*$/) {
127 # SNMPv2-SMI::enterprises.232.6.2.6.7.1.3.1.4 = INTEGER: 6
128 $response->{'1.3.6.1.4.1.'.$1} = $2;
129 $in_hex_string = 0;
130 } elsif (/^.*?\.(232\.[\d\.]+) = .*?: "(.*?)"/) {
131 $response->{'1.3.6.1.4.1.'.$1} = $2;
132 $response->{'1.3.6.1.4.1.'.$1} =~ s/\s+$//;
133 $in_hex_string = 0;
134 } elsif (/^.*?\.(232\.[\d\.]+) = (\-*\d+)/) {
135 $response->{'1.3.6.1.4.1.'.$1} = $2;
136 $in_hex_string = 0;
137 } elsif (/^.*?\.(232\.[\d\.]+) = "(.*?)"/) {
138 $response->{'1.3.6.1.4.1.'.$1} = $2;
139 $response->{'1.3.6.1.4.1.'.$1} =~ s/\s+$//;
140 $in_hex_string = 0;
141 } elsif (/^.*?\.(232\.[\d\.]+) = STRING: "(.*?[^"])$/) {
142 $response->{'1.3.6.1.4.1.'.$1} = $2;
143 $response->{'1.3.6.1.4.1.'.$1} =~ s/\s+$//;
144 $in_string = 0;
145 $in_hex_string = 0;
146 } elsif (/^.*?\.(232\.[\d\.]+) = Hex-STRING: (.*)/) {
147 $response->{'1.3.6.1.4.1.'.$1} = $2;
148 $in_hex_string = 1;
149 $hex_oid = '1.3.6.1.4.1.'.$1;
150 } elsif (/^.*?\.(232\.[\d\.]+) =[ ]{1,2}Hex: (.*)/) {
151 $response->{'1.3.6.1.4.1.'.$1} = $2;
152 $in_hex_string = 1;
153 $hex_oid = '1.3.6.1.4.1.'.$1;
154 }
155 }
156 close MESS;
157 }
158 map { $response->{$_} =~ s/^\s+//; $response->{$_} =~ s/\s+$//; }
159 keys %$response;
160 $self->{rawdata} = $response;
161 $self->whoami();
162 } else {
163 if (eval "require Net::SNMP") {
164 my %params = ();
165 my $net_snmp_version = Net::SNMP->VERSION(); # 5.002000 or 6.000000
166 $params{'-translate'} = [
167 -timeticks => 0x0
168 ];
169 $params{'-hostname'} = $self->{runtime}->{plugin}->opts->hostname;
170 $params{'-domain'} = $self->{runtime}->{plugin}->opts->domain;
171 $params{'-version'} = $self->{runtime}->{plugin}->opts->protocol;
172 if ($self->{runtime}->{plugin}->opts->port) {
173 $params{'-port'} = $self->{runtime}->{plugin}->opts->port;
174 }
175 if ($self->{runtime}->{plugin}->opts->protocol eq '3') {
176 $params{'-username'} = $self->{runtime}->{plugin}->opts->username;
177 if ($self->{runtime}->{plugin}->opts->authpassword) {
178 $params{'-authpassword'} = $self->decode_password($self->{runtime}->{plugin}->opts->authpassword);
179 }
180 if ($self->{runtime}->{plugin}->opts->authprotocol) {
181 $params{'-authprotocol'} = $self->{runtime}->{plugin}->opts->authprotocol;
182 }
183 if ($self->{runtime}->{plugin}->opts->privpassword) {
184 $params{'-privpassword'} = $self->decode_password($self->{runtime}->{plugin}->opts->privpassword);
185 }
186 if ($self->{runtime}->{plugin}->opts->privprotocol) {
187 $params{'-privprotocol'} = $self->{runtime}->{plugin}->opts->privprotocol;
188 }
189 } elsif ($self->decode_password($self->{runtime}->{plugin}->opts->community) =~ /^snmpv3(.)(.+)/) {
190 my $separator = $1;
191 my ($authprotocol, $authpassword, $privprotocol, $privpassword,
192 $username, $contextengineid, $contextname) = split(/$separator/, $2);
193 $params{'-version'} = 3;
194 $params{'-username'} = $self->decode_password($username) if $username;
195 $params{'-authprotocol'} = $self->decode_password($authprotocol) if $authprotocol;
196 $params{'-authpassword'} = $self->decode_password($authpassword) if $authpassword;
197 $params{'-privprotocol'} = $self->decode_password($privprotocol) if $privprotocol;
198 $params{'-privpassword'} = $self->decode_password($privpassword) if $privpassword;
199 } else {
200 $params{'-community'} = $self->decode_password($self->{runtime}->{plugin}->opts->community);
201 }
202 $self->{runtime}->{snmpparams} = \%params;
203 my ($session, $error) = Net::SNMP->session(%params);
204 $self->{session} = $session;
205 if (! defined $session) {
206 $self->add_message(CRITICAL, 'cannot create session object (maybe wrong hostname)');
207 $self->trace(1, Data::Dumper::Dumper(\%params));
208 } else {
209 my $sysUpTime = '1.3.6.1.2.1.1.3.0';
210 my $result = $session->get_request(
211 -varbindlist => [$sysUpTime]
212 );
213 if (!defined($result)) {
214 $self->add_message(CRITICAL,
215 'could not contact snmp agent');
216 $session->close;
217 } else {
218 $self->trace(3, 'snmp agent answered');
219 $self->whoami();
220 }
221 }
222 } else {
223 $self->add_message(CRITICAL,
224 'could not find Net::SNMP module');
225 }
226 }
227 }
228
229 sub whoami {
230 my $self = shift;
231 my $productname = undef;
232 if ($self->{runtime}->{plugin}->opts->snmpwalk) {
233 my $cpqSiProductName = '1.3.6.1.4.1.232.2.2.4.2.0';
234 my $cpqSsMibRevMajor = '1.3.6.1.4.1.232.8.1.1.0';
235 my $cpqSsBackplaneModel = '1.3.6.1.4.1.232.8.2.2.6.1.9'.'.1.1';
236 my $cpqHoMibStatusArray = '1.3.6.1.4.1.232.11.2.10.1.0';
237 if ($productname = $self->{rawdata}->{$cpqSiProductName}) {
238 if (! $productname) {
239 $self->{productname} = 'ProLiant';
240 } else {
241 $self->{productname} = $self->{rawdata}->{$cpqSiProductName};
242 }
243 } elsif (exists $self->{rawdata}->{$cpqSsBackplaneModel}) {
244 $self->{productname} = $self->{rawdata}->{$cpqSsBackplaneModel};
245 } elsif (exists $self->{rawdata}->{$cpqSsMibRevMajor}) {
246 # at least there is a CPQSTSYS-MIB
247 $self->{productname} = 'Storage'
248 } else {
249 $self->add_message(CRITICAL,
250 'snmpwalk returns no product name (cpqsinfo-mib)');
251 }
252 } else {
253 my $cpqSiProductName = '1.3.6.1.4.1.232.2.2.4.2.0';
254 my $cpqSsMibRevMajor = '1.3.6.1.4.1.232.8.1.1.0';
255 my $cpqSsBackplaneModel = '1.3.6.1.4.1.232.8.2.2.6.1.9'.'.1.1';
256 my $cpqHoMibStatusArray = '1.3.6.1.4.1.232.11.2.10.1.0';
257 my $dummy = '1.3.6.1.2.1.1.5.0';
258 if ($productname = $self->valid_response($cpqSiProductName)) {
259 if ($productname =~ /HP P\d+ .*SAS/ && $self->valid_response($cpqHoMibStatusArray)) {
260 $self->{productname} = 'StorageWorks'
261 } elsif ($productname eq '') {
262 $self->{productname} = 'ProLiant';
263 } else {
264 $self->{productname} = $productname;
265 }
266 } elsif ($productname = $self->valid_response($cpqSsBackplaneModel)) {
267 $self->{productname} = $productname;
268 } elsif ($self->valid_response($cpqSsMibRevMajor)) {
269 # at least there is a CPQSTSYS-MIB
270 $self->{productname} = 'Storage'
271 } elsif ($self->valid_response($cpqHoMibStatusArray)) {
272 $self->{productname} = 'StorageWorks'
273 } else {
274 $self->add_message(CRITICAL,
275 'snmpwalk returns no product name (cpqsinfo-mib)');
276 $self->{session}->close;
277 }
278 $self->trace(3, 'whoami: '.$self->{productname});
279 }
280 }
281
282 sub valid_response {
283 my $self = shift;
284 my $oid = shift;
285 my $result = $self->{session}->get_request(
286 -varbindlist => [$oid]
287 );
288 if (!defined($result) ||
289 ! defined $result->{$oid} ||
290 $result->{$oid} eq 'noSuchInstance' ||
291 $result->{$oid} eq 'noSuchObject' ||
292 $result->{$oid} eq 'endOfMibView') {
293 return undef;
294 } else {
295 return $result->{$oid};
296 }
297 }
298
299 sub trace {
300 my $self = shift;
301 my $level = shift;
302 my $message = shift;
303 if ($self->{runtime}->{options}->{verbose} >= $level) {
304 printf "%s\n", $message;
305 }
306 }
307
308 sub blacklist {
309 my $self = shift;
310 my $type = shift;
311 my $name = shift;
312 $self->{blacklisted} = $self->is_blacklisted($type, $name);
313 }
314
315 sub add_blacklist {
316 my $self = shift;
317 my $list = shift;
318 $self->{runtime}->{options}->{blacklist} = join('/',
319 (split('/', $self->{runtime}->{options}->{blacklist}), $list));
320 }
321
322 sub is_blacklisted {
323 my $self = shift;
324 my $type = shift;
325 my $name = shift;
326 my $blacklisted = 0;
327 # $name =~ s/\:/-/g;
328 foreach my $bl_items (split(/\//, $self->{runtime}->{options}->{blacklist})) {
329 if ($bl_items =~ /^(\w+):([\:\w\-,]+)$/) {
330 my $bl_type = $1;
331 my $bl_names = $2;
332 foreach my $bl_name (split(/,/, $bl_names)) {
333 if ($bl_type eq $type && $bl_name eq $name) {
334 $blacklisted = 1;
335 }
336 }
337 } elsif ($bl_items =~ /^(\w+)$/) {
338 my $bl_type = $1;
339 if ($bl_type eq $type) {
340 $blacklisted = 1;
341 }
342 }
343 }
344 return $blacklisted;
345 }
346
347 sub add_message {
348 my $self = shift;
349 my $level = shift;
350 my $message = shift;
351 $self->{runtime}->{plugin}->add_message($level, $message)
352 unless $self->{blacklisted};
353 if (exists $self->{failed}) {
354 if ($level == UNKNOWN && $self->{failed} == OK) {
355 $self->{failed} = $level;
356 } elsif ($level > $self->{failed}) {
357 $self->{failed} = $level;
358 }
359 }
360 }
361
362 sub remove_message {
363 my $self = shift;
364 my $level = shift;
365 my $message = shift;
366 $self->{runtime}->{plugin}->remove_message($level) ;
367 }
368
369 sub has_failed {
370 my $self = shift;
371 return $self->{failed};
372 }
373
374 sub add_info {
375 my $self = shift;
376 my $info = shift;
377 $info = $self->{blacklisted} ? $info.' (blacklisted)' : $info;
378 $self->{info} = $info;
379 if (! exists $self->{runtime}->{plugin}->{info}) {
380 $self->{runtime}->{plugin}->{info} = [];
381 }
382 push(@{$self->{runtime}->{plugin}->{info}}, $info);
383 }
384
385 sub annotate_info {
386 my $self = shift;
387 my $annotation = shift;
388 my $lastinfo = pop(@{$self->{runtime}->{plugin}->{info}});
389 $lastinfo .= sprintf ' (%s)', $annotation;
390 push(@{$self->{runtime}->{plugin}->{info}}, $lastinfo);
391 }
392
393 sub add_extendedinfo {
394 my $self = shift;
395 my $info = shift;
396 $self->{extendedinfo} = $info;
397 return if ! $self->{runtime}->{options}->{extendedinfo};
398 if (! exists $self->{runtime}->{plugin}->{extendedinfo}) {
399 $self->{runtime}->{plugin}->{extendedinfo} = [];
400 }
401 push(@{$self->{runtime}->{plugin}->{extendedinfo}}, $info);
402 }
403
404 sub get_extendedinfo {
405 my $self = shift;
406 if (! exists $self->{runtime}->{plugin}->{extendedinfo}) {
407 $self->{runtime}->{plugin}->{extendedinfo} = [];
408 }
409 return join(' ', @{$self->{runtime}->{plugin}->{extendedinfo}});
410 }
411
412 sub add_summary {
413 my $self = shift;
414 my $summary = shift;
415 if (! exists $self->{runtime}->{plugin}->{summary}) {
416 $self->{runtime}->{plugin}->{summary} = [];
417 }
418 push(@{$self->{runtime}->{plugin}->{summary}}, $summary);
419 }
420
421 sub get_summary {
422 my $self = shift;
423 if (! exists $self->{runtime}->{plugin}->{summary}) {
424 $self->{runtime}->{plugin}->{summary} = [];
425 }
426 return join(', ', @{$self->{runtime}->{plugin}->{summary}});
427 }
428
429 sub dumper {
430 my $self = shift;
431 my $object = shift;
432 my $run = $object->{runtime};
433 delete $object->{runtime};
434 printf STDERR "%s\n", Data::Dumper::Dumper($object);
435 $object->{runtime} = $run;
436 }
437
438 sub decode_password {
439 my $self = shift;
440 my $password = shift;
441 if ($password && $password =~ /^rfc3986:\/\/(.*)/) {
442 $password = $1;
443 $password =~ s/\%([A-Fa-f0-9]{2})/pack('C', hex($1))/seg;
444 }
445 return $password;
446 }
447