[Nagiosplug-devel] Pb with NET::Telnet help ! ...
Gilles THOMAS
gthomas at telemarket.fr
Mon Jul 29 06:20:01 CEST 2002
hi all,
First excuse me for my english I'm a newbee in english and in Perl language...
I run Nagios V. 1.0b3 on a linux box (RedHat 7.3), and I compile it with no embedded perl. Nagios run under nagios user, and ...
I make a perl script for openning a telnet session on a Cisco routeur and retrive the cpu usage, here the script :
#!/usr/bin/perl
# écrit par Moi !
use strict;
use Net::Telnet;
use Getopt::Long;
&Getopt::Long::config('auto_abbrev');
#---------------------------------------
my %ERRORS = ('UNKNOWN' , '-1',
'OK' , '0',
'WARNING', '1',
'CRITICAL', '2');
my $state = "UNKNOWN";
my ($host, $user, $pass, $warning, $critical);
my $telnettimeout =60;
my $ltime = localtime();
my $code = -1;
# Evaluation des paramètres passés à la commande
my $status = GetOptions(
"host=s",\$host,
"user=s",\$user,
"pass=s",\$pass,
"warning=s",\$warning,
"critical=s",\$critical,
);
usage() if ($status == 0 || ! ($host && $pass ));
# -h means host name
$host = shift unless ($host);
unless ($host) { usage(); exit (-1); }
if ($host &&
$host =~ m/^([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+|[a-zA-Z][-a-zA-Z0-9]*(\.[a-zA-Z][-a-zA-Z0-9]*)*)$/)
{
$host = $1;
} else {
print "$host is not a valid host name \n";
exit (-1);
}
# -u means user name not needed
# -p means password
$pass = shift unless ($pass);
unless ($pass) { usage(); exit (-1); }
# -w means warning
$warning = shift unless ($warning);
$warning = 60 unless ($warning);
# -c means critical
$critical = shift unless ($critical);
$critical = 90 unless ($critical);
# Si tous les paramètres sont bons, on recupère les info en telnet...
my $line;
my @lines;
my $cpuval;
my $t;
$t = new Net::Telnet (Timeout => $telnettimeout);
$t->open($host);
if ($user){
$t->prompt("/:/");
@lines = $t->cmd($user);
}
$t->prompt("/>/");
@lines = $t->cmd($pass);
@lines = $t->cmd("term len 0");
@lines = $t->cmd("show processes cpu");
$line = $lines[0];
if ($line=~/one minute\s*:\s*(\d+)\%/){
$cpuval = $1;
}
eval{
$t->cmd("exit");
};
my $detail;
my $report;
if ($cpuval!~/^\d+$/){
my $report = "$ltime, $detail";
$state = "UNKNOWN";
nsexit($report,'UNKNOWN');
}
elsif ($cpuval < $warning){
$report = "$ltime, OK - Cisco CPU on $host is at $cpuval %, $line";
$state = "OK";
nsexit($report,'OK');
}
elsif ($cpuval < $critical){
my $report = "$ltime, WARNING - Cisco CPU on $host is at $cpuval %, $line";
$state = "WARNING";
nsexit($report,'WARNING');
}
else{
#Par default CRITICAL :
my $report = "$ltime, CRITICAL - Cisco CPU on $host is at $cpuval %, $line";
$state = "CRITICAL";
nsexit($report,'CRITICAL');
}
# ----------------------------------------------------------------------
sub nsexit {
my ($report,$code) = @_;
$code=$state if (!defined $code);
print "$code : $report\n" if (defined $report);
exit $ERRORS{$code};
}
# ----------------------------------------------------------------------
sub usage {
print "Moniteur charge CPU Cisco\n";
print "Ce plugin questionne un routeur cisco et recupère la moyenne en % de la charge CPU depuis une minute.\n";
print "Usage : \n";
print " Les options suivantes doivent être fournis : \n";
print " -host=texte Host du serveur FTP \n";
print " -user=texte Login pour la connection, pas obligatoire \n";
print " -pass=texte Mot de passe pour la session Telnet \n";
print " -warning=pourcentage Le niveau pour le Warning pas obligatoire (par defaut : 60)\n";
print " -critical=pourcentage Le niveau pour le Critical pas obligatoire (par defaut : 90)\n";
print " Les options peuvent être abrégées \n";
print " Example: \n";
print " -h=host -pa=pw -us=user -wa=warning -cr=critical\n" ;
print " Copyleft Moi \n";
print "This script comes with ABSOLUTELY NO WARRANTY \n";
print "This programm is licensed under the terms of the ";
print "GNU General Public License \n \n";
exit $ERRORS{"UNKNOWN"};
}
# ---------------------------------------------------------------------
My problem is, in the nagios cgi the "Current Status" is always CRITICAL and "Status Information" is (no output!) ...
I don't understand because when I run the plugin in command line like 'root' or 'nagios' user, the plugin return me something like :
OK: Mon Jul 29 14:19:47 2002 OK - Cisco CPU on xxx.xxx.xxx.xxx is at 0 %
And with a strace "the command line" I see the plugin exit with the good exit code... like 0, 1, 2 or -1
So I think my script work fine but not with nagios ????
Anyone could help me ? please !
I plan to make other script for CISCO with telnet or snmp session (when this one run correctly, off course !), so If anyone is interrest with other plugin for Cisco tell me, or if anyone could give plugin for Cisco, I was very happy...
Best regards,
Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://nagios-plugins.org/archive/devel/attachments/20020729/652eb01c/attachment.html>
More information about the Devel
mailing list