27 |
use Net::SNMP; |
use Net::SNMP; |
28 |
use Getopt::Long; |
use Getopt::Long; |
29 |
use Nagios::Plugin; |
use Nagios::Plugin; |
30 |
|
use Locale::gettext; |
31 |
|
use POSIX qw(setlocale); |
32 |
|
|
33 |
|
my $DEBUG = 0; |
34 |
my ( $vsnmp, $timeout ) = ( 1, 2 ); |
my ( $vsnmp, $timeout ) = ( 1, 2 ); |
35 |
my $base_oid = '.1.3.6.1.2.1.25.3.8.1'; |
my $base_oid = '.1.3.6.1.2.1.25.3.8.1'; |
36 |
my $mesg_reponse = 'Invalid status'; |
my $mesg_reponse = 'Invalid status'; |
38 |
my $liste_fs_ro; |
my $liste_fs_ro; |
39 |
|
|
40 |
my $np = Nagios::Plugin->new( |
my $np = Nagios::Plugin->new( |
41 |
version => $version, |
version => $version, |
42 |
blurb => 'Plugin de test d\'acces en lecture-ecriture des filesystems', |
blurb => 'Plugin de test d\'acces en lecture-ecriture des filesystems', |
43 |
usage => "Usage: %s -H|--host <host> [-p|--port=<portSNMP>] [-c|--communaute=<communaute_snmp>]", |
usage => "Usage: %s -H|--host <host> [-p|--port=<portSNMP>] [-c|--communaute=<communaute_snmp>]", |
44 |
timeout => 10 |
timeout => 10 |
45 |
); |
); |
46 |
|
|
47 |
$np->add_arg ( |
$np->add_arg ( |
48 |
spec => 'host|H=s', |
spec => 'host|H=s', |
49 |
help => 'name of the host to test', |
help => 'Name of the host to test', |
50 |
default => 'localhost', |
required => 1, |
51 |
); |
); |
52 |
|
|
53 |
$np->add_arg ( |
$np->add_arg ( |
54 |
spec => 'port|p=i', |
spec => 'port|p=i', |
55 |
help => 'SNMP port', |
help => 'SNMP port', |
56 |
default => 161, |
default => 161, |
57 |
); |
); |
58 |
|
|
59 |
$np->add_arg ( |
$np->add_arg ( |
60 |
spec => 'communaute|c=s', |
spec => 'community|c=s', |
61 |
help => 'SNMP community', |
help => 'SNMP community', |
62 |
default => 'public', |
default => 'public', |
63 |
|
); |
64 |
|
|
65 |
|
$np->add_arg ( |
66 |
|
spec => 'debug|d', |
67 |
|
help => _gt('Debug level'), |
68 |
); |
); |
69 |
|
|
70 |
$np->getopts; |
$np->getopts; |
71 |
my $host = $np->opts->get('host'); |
my $host = $np->opts->get('host'); |
72 |
my $port = $np->opts->get('port'); |
my $port = $np->opts->get('port'); |
73 |
my $comm = $np->opts->get('communaute'); |
my $comm = $np->opts->get('community'); |
74 |
|
$DEBUG = $np->opts->get('debug'); |
75 |
|
|
76 |
my ( $session, $error ) = Net::SNMP->session( |
my ( $session, $error ) = Net::SNMP->session( |
77 |
-hostname => $host, |
-hostname => $host, |
78 |
-community => $comm, |
-community => $comm, |
79 |
-timeout => $timeout, |
-timeout => $timeout, |
80 |
-version => $vsnmp, |
-version => $vsnmp, |
81 |
-port => $port, |
-port => $port, |
82 |
); |
); |
83 |
|
|
84 |
if ( !defined $session ) { |
if ( !defined $session ) { |
85 |
$np->nagios_exit( UNKNOWN, "ERROR: %s." ); |
$np->nagios_exit( UNKNOWN, "ERROR: %s." ); |
86 |
} |
} |
87 |
|
|
88 |
my $oid = $base_oid . '.1'; |
my $oid = $base_oid . '.1'; |
89 |
my $reponse = $session->get_table( $oid ); |
my $reponse = $session->get_table( $oid ); |
90 |
if ( !defined $reponse ) { |
if ( !defined $reponse ) { |
91 |
$np->nagios_exit( UNKNOWN, "Unable to get SNMP values" ); |
$np->nagios_exit( UNKNOWN, "Unable to get SNMP values" ); |
92 |
} |
} |
93 |
my @tab_ix = values( %$reponse ); |
my @tab_ix = values( %$reponse ); |
94 |
my $status = 0; |
my $status = OK; |
95 |
$mesg_reponse = 'All filesystems RW'; |
my $count = 0; |
96 |
map { |
map { |
97 |
my $indice = $_; |
my $indice = $_; |
98 |
my $oid = $base_oid . '.2.' . $indice; |
my $oid = $base_oid . '.2.' . $indice; |
99 |
my $rep = $session->get_next_request( $oid ); |
my $rep = $session->get_next_request( $oid ); |
100 |
my @tab_res = values( %$rep ); |
my @tab_res = values( %$rep ); |
101 |
my $nom_fs = $tab_res[ 0 ]; |
my $nom_fs = $tab_res[ 0 ]; |
102 |
$oid = $base_oid . '.5.' . $indice; |
$oid = $base_oid . '.5.' . $indice; |
103 |
$rep = $session->get_next_request( $oid ); |
$rep = $session->get_next_request( $oid ); |
104 |
@tab_res = values( %$rep ); |
@tab_res = values( %$rep ); |
105 |
my $st_fs = $tab_res[ 0 ]; |
my $st_fs = $tab_res[ 0 ]; |
106 |
if ( ( $st_fs == 2 ) && ( $nom_fs !~ /cdrom/ ) ) { |
&logD("$indice: $nom_fs => $st_fs"); |
107 |
$liste_fs_ro .= $nom_fs . ' '; |
if ( ( $st_fs == 2 ) && ( $nom_fs !~ /cdrom/ ) ) { |
108 |
if ( ( $nom_fs eq '/' ) || ( $nom_fs eq '/var' ) ) { |
$liste_fs_ro .= $nom_fs . ' '; |
109 |
$status = 2; |
if ( ( $nom_fs eq '/' ) || ( $nom_fs eq '/var' ) ) { |
110 |
} else { |
$status = CRITICAL; |
111 |
$status = 1 unless ( $status eq 2 ); |
} else { |
112 |
} |
$status = WARNING unless ( $status eq CRITICAL ); |
113 |
} |
} |
114 |
|
} |
115 |
|
$count++; |
116 |
} @tab_ix; |
} @tab_ix; |
117 |
$session->close(); |
$session->close(); |
118 |
$mesg_reponse = "Filesystem(s) $liste_fs_ro: readonly" if $status; |
|
119 |
|
if ($status) { |
120 |
|
$mesg_reponse = sprintf("Filesystem(s) %s: readonly" , $liste_fs_ro) |
121 |
|
} else { |
122 |
|
$mesg_reponse = sprintf("All filesystems (%d) RW", $count); |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
$np->nagios_exit( $status, $mesg_reponse ); |
$np->nagios_exit( $status, $mesg_reponse ); |
127 |
|
|
128 |
|
|
129 |
|
sub logD { |
130 |
|
print STDERR 'DEBUG: '.$_[0]."\n" if ($DEBUG); |
131 |
|
} |
132 |
|
sub logW { |
133 |
|
print STDERR 'WARNING: '.$_[0]."\n" if ($DEBUG); |
134 |
|
} |
135 |
|
# Gettext wrapper |
136 |
|
sub _gt { |
137 |
|
return gettext($_[0]); |
138 |
|
} |