Tengo que controlar el estado de los discos de unos servidores Fujitsu Primergy y lo puedo hacer con SNMP (con snmpwalk). He hecho un programilla que en sí funciona pero me falta la guinda
Aquí está el código:
Using perl Syntax Highlighting
#!/usr/bin/perl
#===============================================================================
#
# FILE: check-fs-array.pl
#
# USAGE: ./check-fs-array.pl
#
# DESCRIPTION: check disk array controller health through SNMP
#
# OPTIONS: ---
# REQUIREMENTS: snmputils (snmpwalk, in particular)
# BUGS: probably a lot <img src="http://perlenespanol.com/foro/images/smilies/icon_smile.gif" alt=":)" title="Smile" />
# NOTES: ---
# AUTHOR: José Asenjo (JA)
# COMPANY:
# VERSION: 1.0
# CREATED: 04/20/2009 02:45:14 PM
# REVISION: ---
#===============================================================================
use strict;
use warnings;
use Getopt::Long;
# some variables
my $host;
Getopt::Long::Configure("bundling");
GetOptions(
'H|host=s' => \$host,
'u|?|usage' => sub { usage(); },
'h|help' => sub { help(); },
) or help();
# oid is here nummeric form of
# .iso.org.dod.internet.private.enterprises.sni.sniProductMibs.fscRAIDMIB.svrObjects.svrStatus.
my $oid_srvStatus = ".1.3.6.1.4.1.231.2.49.1.3";
my $number_of_lines = 0;
my $ok_result = 0 ;
my $failure_result = 0 ;
if ( !defined $host ) {
die "I need a hostname. See $0 --help for details\n";
}
check_srvStatus();
sub check_srvStatus {
open (PIPE, "snmpwalk $host -c public -v 1 -m FSC-RAID-MIB $oid_srvStatus |")
or die "cannot snmpwalk to $host: $!\n";
while ( my $result = (<PIPE>) ) {
# count number of lines in result of query
$number_of_lines++ ;
chomp $result ;
if ( $result =~ m/.*INTEGER: ok.*$/ ) {
$ok_result++;
}
if ( $result =~ m/.*INTEGER: .*failure.*$/ ) {
$failure_result++ ;
}
}
if ( $failure_result > 0 ) {
print "WARNING: there is a problem with the logical or the physical
volume of the disk array. Check https://$host:3263 for details\n";
exit 1;
}
if ($number_of_lines == $ok_result ) {
print "OK: All logical and physical devices in array are working fine\n";
exit 0;
}
#my ( $par1 ) = @_;
#return ;
} # ---------- end of subroutine check_srvStatus ----------
sub usage {
print "Usage: $0 --host [enter]\n";
print "See $0 --help for details\n";
return;
} # ---------- end of subroutine usage ----------
sub help {
usage();
my $heredoc = <<EOF;
-H, --host
-u, --usage
-h, --help
EOF
print $heredoc;
exit 0;
} # ---------- end of subroutine help ----------
#===============================================================================
#
# FILE: check-fs-array.pl
#
# USAGE: ./check-fs-array.pl
#
# DESCRIPTION: check disk array controller health through SNMP
#
# OPTIONS: ---
# REQUIREMENTS: snmputils (snmpwalk, in particular)
# BUGS: probably a lot <img src="http://perlenespanol.com/foro/images/smilies/icon_smile.gif" alt=":)" title="Smile" />
# NOTES: ---
# AUTHOR: José Asenjo (JA)
# COMPANY:
# VERSION: 1.0
# CREATED: 04/20/2009 02:45:14 PM
# REVISION: ---
#===============================================================================
use strict;
use warnings;
use Getopt::Long;
# some variables
my $host;
Getopt::Long::Configure("bundling");
GetOptions(
'H|host=s' => \$host,
'u|?|usage' => sub { usage(); },
'h|help' => sub { help(); },
) or help();
# oid is here nummeric form of
# .iso.org.dod.internet.private.enterprises.sni.sniProductMibs.fscRAIDMIB.svrObjects.svrStatus.
my $oid_srvStatus = ".1.3.6.1.4.1.231.2.49.1.3";
my $number_of_lines = 0;
my $ok_result = 0 ;
my $failure_result = 0 ;
if ( !defined $host ) {
die "I need a hostname. See $0 --help for details\n";
}
check_srvStatus();
sub check_srvStatus {
open (PIPE, "snmpwalk $host -c public -v 1 -m FSC-RAID-MIB $oid_srvStatus |")
or die "cannot snmpwalk to $host: $!\n";
while ( my $result = (<PIPE>) ) {
# count number of lines in result of query
$number_of_lines++ ;
chomp $result ;
if ( $result =~ m/.*INTEGER: ok.*$/ ) {
$ok_result++;
}
if ( $result =~ m/.*INTEGER: .*failure.*$/ ) {
$failure_result++ ;
}
}
if ( $failure_result > 0 ) {
print "WARNING: there is a problem with the logical or the physical
volume of the disk array. Check https://$host:3263 for details\n";
exit 1;
}
if ($number_of_lines == $ok_result ) {
print "OK: All logical and physical devices in array are working fine\n";
exit 0;
}
#my ( $par1 ) = @_;
#return ;
} # ---------- end of subroutine check_srvStatus ----------
sub usage {
print "Usage: $0 --host [enter]\n";
print "See $0 --help for details\n";
return;
} # ---------- end of subroutine usage ----------
sub help {
usage();
my $heredoc = <<EOF;
-H, --host
-u, --usage
-h, --help
EOF
print $heredoc;
exit 0;
} # ---------- end of subroutine help ----------
Coloreado en 0.005 segundos, usando GeSHi 1.0.8.4
Si ejecuto el programa con una máquina existente y accesible funciona bien:
Using bash Syntax Highlighting
$ ./check-fs-array -H izvm05
OK: All logical and physical devices in array are working fine
OK: All logical and physical devices in array are working fine
Coloreado en 0.003 segundos, usando GeSHi 1.0.8.4
Pero si la ejecuto para un servidor que no existe o no está disponible también me da resultado Ok aunque sea erróneo:
Using bash Syntax Highlighting
./check-fs-array -H izvm06
getaddrinfo: izvm06 Name or service not known
snmpwalk: Unknown host (izvm06)
OK: All logical and physical devices in array are working fine
getaddrinfo: izvm06 Name or service not known
snmpwalk: Unknown host (izvm06)
OK: All logical and physical devices in array are working fine
Coloreado en 0.002 segundos, usando GeSHi 1.0.8.4
El error está claro, no puede resolver el nombre de la máquina, snmpwalk no encuentra la máquina por consiguiente, pero sin embargo me devuelve el resultado equivocado OK.
¿Cómo puedo terminar el script si la llamada a snmpwalk no termina en éxito? Es el problema de mezclar Perl con comandos de la shell.