#!/usr/bin/perl
Readonly
my
$memcached_conf
=> {
servers
=> [
'my.storage-server.example.com:11211'
],
namespace
=>
'yatg:'
,
};
my
$do_quit
=
sub
{
my
(
$msg
,
$status
) =
@_
;
$status
= 1
if
!
defined
$status
;
print
"$msg\n"
;
exit
$status
;
};
my
$combined_error
=
q{}
;
my
$combined_ok
=
q{}
;
my
$target_host
=
$ARGV
[0]
or
$do_quit
->(
'ERROR: No target host specified'
);
$target_host
=~ m/^
$RE
{net}{IPv4}$/
or
$do_quit
->(
'ERROR: Target host must be an IPv4 address'
);
my
$m
= Cache::Memcached->new(
$memcached_conf
)
or
$do_quit
->(
'ERROR: Failed to connect to memcached server'
);
my
@devices
=
eval
{ @{
$m
->get(
'yatg_devices'
) } }
or
$do_quit
->(
'ERROR: Failed to get list of devices from memcached'
);
((
scalar
grep
m/^
$target_host
$/,
@devices
) != 0)
or
$do_quit
->(
"ERROR: YATG did not poll $target_host ports"
);
my
@ports
=
eval
{ @{
$m
->get(
"ports_for:$target_host"
) } }
or
$do_quit
->(
"ERROR: Failed to get ports list for $target_host"
);
foreach
my
$port
(
@ports
) {
(
my
$O_key
=
join
':'
,
$target_host
,
'ifOperStatus'
,
$port
) =~ s/\s/_/g;
my
$ifOperStatus
=
eval
{
$m
->get(
$O_key
) }
or
$combined_error
.=
" ERR: Failed to get ifOperStatus for '$O_key';"
;
(
my
$A_key
=
join
':'
,
$target_host
,
'ifAlias'
,
$port
) =~ s/\s/_/g;
my
$ifAlias
=
eval
{
$m
->get(
$A_key
) } ||
''
;
if
(
$ifOperStatus
eq
'up'
) {
$combined_ok
.=
" i/f $port ($ifAlias) up;"
;
}
else
{
$combined_error
.=
" WARN: $port ($ifAlias) is $ifOperStatus;"
;
}
}
$do_quit
->(
"ERROR: $target_host$combined_error"
, 2 )
if
$combined_error
ne
''
;
$do_quit
->(
"OK: $target_host$combined_ok"
, 0 )
if
$combined_ok
ne
''
;
$do_quit
->(
'UNKNOWN: Please check the Nagios configuration'
, 1 );