#!/usr/bin/perl -w # nagios: +epn #-------------------------------------------------------------------------------- # Nom : check_smb_share.pl $VERSION = '0.22'; # Date : 2012/14/13 # Langage : Perl # Descrip : Verifie que l'acces en ecriture a un partage smb est possible # Auteur : F. BRACHOT / PTSR/Ingenierie/Infra #-------------------------------------------------------------------------------- #use strict; use warnings; use Nagios::Plugin; use Filesys::SmbClient; use vars qw/ $VERSION /; my $LICENCE = "Written by Fred BRACHOT on 14 septembre 2012"; my $log_level = 0; # nagios_exit( $code, $message ) sub nagios_exit { my ($code, $message, $arg) = @_; # Handle named parameters if (defined $code && ($code eq 'return_code' || $code eq 'message')) { # Remove last argument if odd no and last is ref if (int(@_ / 2) != @_ / 2 && ref $_[$#_]) { $arg = pop @_; } else { undef $arg; } my %arg = @_; $code = $arg{return_code}; $message = $arg{message}; } $arg ||= {}; # Handle string codes $code = $ERRORS{$code} if defined $code && exists $ERRORS{$code}; # Set defaults $code = UNKNOWN unless defined $code && exists $STATUS_TEXT{$code}; $message = '' unless defined $message; if (ref $message && ref $message eq 'ARRAY') { $message = join(' ', map { chomp; $_ } @$message); } else { chomp $message; } # Setup output my $output = "TOTO $STATUS_TEXT{$code}"; $output .= " - $message" if defined $message && $message ne ''; my $shortname = ($arg->{plugin} ? $arg->{plugin}->shortname : undef); $shortname ||= get_shortname(); # Should happen only if funnctions are called directly $output = "$shortname $output" if $shortname; if ($arg->{plugin}) { my $plugin = $arg->{plugin}; $output .= " | ". $plugin->all_perfoutput if $plugin->perfdata && $plugin->all_perfoutput; } $output .= "\n"; # Don't actually exit if _fake_exit set if ($_fake_exit) { require Nagios::Plugin::ExitResult; return Nagios::Plugin::ExitResult->new($code, $output); } _nagios_exit($code, $output); } my $plugin_nagios = Nagios::Plugin->new( version => $VERSION, blurb => 'Nagios plugin to check write access to a smb share', usage => 'Usage : %s -H -s -f [-u ] [-p ] [-v]', license => $LICENCE ); # Adresse du serveur $plugin_nagios->add_arg( spec => 'hostname|H=s', help => 'Host name or IP address', required => 1, label => 'ADDRESS' ); # Nom du partage SMB $plugin_nagios->add_arg( spec => 'share|s=s', help => 'Name of SMB share', required => 1 ); # Fichier dans lequel il faut ecrire $plugin_nagios->add_arg( spec => 'file|f=s', help => 'Write into file', required => 1 ); # Compte a utiliser pour se connecter $plugin_nagios->add_arg( spec => 'username|u=s', help => 'Username to log in to server (default: guest)', default => 'guest', required => 0 ); # Mot de passe du compte utilise pour la connexion $plugin_nagios->add_arg( spec => 'password|p=s', help => 'Password to log in to server (default: empty password)', default => '', required => 0 ); # Activer le parsing des options de ligne de commande $plugin_nagios->getopts; if ( $plugin_nagios->opts->verbose ) { print "Connexion a \\\\$plugin_nagios->{opts}->{hostname}\\$plugin_nagios->{opts}->{share} avec le compte $plugin_nagios->{opts}->{username}\n"; $log_level = 4; } my $smb = new Filesys::SmbClient(username => "$plugin_nagios->{opts}->{username}", password => "$plugin_nagios->{opts}->{password}", debug => $log_level); my $start_check = time; my $fd = $smb->open(">smb://$plugin_nagios->{opts}->{hostname}/$plugin_nagios->{opts}->{share}/$plugin_nagios->{opts}->{file}", '0666'); unless ( defined $fd ) { print "[CRITICAL] Can't create or open file: ", $!, "\n"; exit 2; } my $moment = localtime(); unless ( $smb->write($fd, "$moment") or print $!,"\n" ) { print "[CRITICAL] Can't write into file: ", $!, "\n"; exit 2; } $smb->close($fd); my $delta = time - $start_check; $plugin_nagios->add_perfdata( label => "time", value => sprintf("%.4f", $delta), uom => "s", critical => $plugin_nagios->{opts}->{timeout} ); printf ("[OK] write into file in %.3f seconds\n", $delta); #exit 0; $plugin_nagios->nagios_exit( OK, "not exists" );