#!/usr/bin/perl -w ############################## check_wrapper ################################################################################ # # Name : check_wrapper # Version : 1.0 # Date : Jan 31, 2012 # Author : Anton Hofland # Company : 2024Sight (www.2024sight.com) # Licence : GPL - http://www.fsf.org/licenses/gpl.txt # # This plugin was developed to act like a wrapper around any other Nagios plugin. The objective of the wrapper is to create # a capability to test the Nagios system in line and without having to make any changes to the system. The wrapper will # check whether a plugin is in test mode. If it is, then it returns the pre-defined error code. The wrapper also decrements # the test counter. Should the counter reach 0, the wrapper will reset the test mode. The next time the plugin is invoked it # will execute normally. If the plugin is not in test mode, then wrapper will exec the plugin. The wrapper script assumes # it is installed in the same location as the other plugins. It also assumes it will be invoked using an absolute path. The # wrapper uses its own invokation path to create the path to the nagios plugin. # # Seperately, the wrapper can also syslog the invocation of a plugin. This will contnue until it is explicitely cancelled. # ############################################################################################################################# use strict; use File::Basename; use Sys::Syslog qw( :DEFAULT setlogsock); my $Version = '1.0'; my $Command = $0; my $Path = dirname( $Command ); my $Configuration_File = "/var/lib/nagios/logs/check_wrapper"; my $O_Help = undef; my $O_Version = undef; my $O_Configure = undef; my $O_Count = undef; my $O_Error = undef; my $O_Reset = undef; my $O_Log = undef; my $O_NoLog = undef; my $O_Plugin = undef; my %Configuration; my %Errors = ( 'OK' => 0, 'WARNING' => 1, 'CRITICAL' => 2, 'UNKNOWN' => 3, 'DEPENDENT' => 4 ); ############################################################################################################################# sub print_version { print "check_wrapper version : $Version\n"; } ############################################################################################################################# sub print_usage { print "Usage: check_wrapper --help\n"; print " check_wrapper --version\n"; print " check_wrapper [--file config file] --configure \n"; print " check_wrapper [--file config file] --reset |all\n"; print " check_wrapper [--file config file] --log \n"; print " check_wrapper [--file config file] --nolog |all\n"; print " check_wrapper [--file config file] [nagios plugin options]\n"; } ############################################################################################################################# sub print_help { my $Key = undef; print "\n"; print "Nagios Plugin Wrapper to test the Nagios system by simulating a return code and by syslogging the invokation\n"; print "\n"; print_usage(); print "\n"; print " --help Print help message\n"; print " --version Print version\n"; print " --configure Configure the to return the the next times\n"; print " --reset Remove the configuration of the for the or all plugins\n"; print " --log Enables logging to syslog of every invocation of the and arguments\n"; print " --nolog Disables logging to syslog of or all plugins\n"; print " --file Specify the path to the check_wrapper configuration file\n"; print "\n"; print " Error Codes: "; foreach $Key ( keys %Errors ) { print "$Key "; } print "\n\n"; print " The should be greater than zero.\n\n"; print " If no check_wrapper options are specified, the wrapper checks whether the plugin is\n"; print " in test mode. If it is then the wrapper will return the specifed return code. If it\n"; print " not then the wrapper will execute the plugin with the remaining arguments. By\n"; print " inserting a call to check_wrapper in the nagios configuration or nrpe.cfg nagios can\n"; print " be instrumented with this test capability\n\n"; print " The wrapper also accepts the old style command line options\n"; print "\n\n"; print "This module maintains a configuration file that has to be writable by the nagios system.\n"; print "The full path is: $Configuration_File\n"; print "\n\n"; print "The Nagios check_wrapper plugin has been developed by 2024Sight (www.2024sight.com)\n"; print "\n"; } ############################################################################################################################# sub check_options { if (( defined( $ARGV[ 0 ] )) && ( $ARGV[ 0 ] =~ /^(--help|-h)$/ )) { $O_Help = 1; shift @ARGV; } elsif (( defined( $ARGV[ 0 ] )) && ( $ARGV[ 0 ] =~ /^(--version|-v)$/ )) { $O_Version = 1; shift @ARGV; if ( defined( $ARGV[ 0 ] )) { $O_Help = 1; } } elsif (( defined( $ARGV[ 0 ] )) && ( $ARGV[ 0 ] =~ /^(--file|-f)$/ )) { shift @ARGV; if ( defined( $ARGV[ 0 ] )) { $Configuration_File = $ARGV[ 0 ]; shift @ARGV; } else { $O_Help = 1; } } if (( defined( $ARGV[ 0 ] )) && ( $ARGV[ 0 ] =~ /^(--configure|-c)$/ )) { $O_Configure = 1; shift @ARGV; if ( defined( $ARGV[ 0 ] )) { my $Pattern = undef; my $Key = undef; $O_Plugin = $ARGV[ 0 ]; shift @ARGV; foreach $Key ( keys %Errors ) { if ( defined( $Pattern )) { $Pattern = "$Pattern". "|" . $Key; } else { $Pattern = $Key; } } if (( defined( $ARGV[ 0 ] )) && ( $ARGV[ 0 ] =~ /^($Pattern)$/i )) { $O_Error = $ARGV[ 0]; $O_Error =~ tr/a-z/A-Z/; shift @ARGV; if (( defined( $ARGV[ 0 ] )) && ( $ARGV[ 0 ] =~ /^\d+$/ )) { $O_Count = $ARGV[ 0 ]; shift @ARGV; if (( $O_Count <= 0 ) || ( defined( $ARGV[ 0 ] ))) { $O_Help = 1; } } else { $O_Help = 1; } } else { $O_Help = 1; } } else { $O_Help = 1; } } elsif (( defined( $ARGV[ 0 ] )) && ( $ARGV[ 0 ] =~ /^(--reset|-r)$/ )) { $O_Reset = 1; shift @ARGV; if ( defined( $ARGV[ 0 ] )) { $O_Plugin = $ARGV[ 0 ]; shift @ARGV; if ( defined( $ARGV[ 0 ] )) { $O_Help = 1; } } else { $O_Help = 1; } } elsif (( defined( $ARGV[ 0 ] )) && ( $ARGV[ 0 ] =~ /^(--log|-l)$/ )) { $O_Log = 1; shift @ARGV; if ( defined( $ARGV[ 0 ] )) { $O_Plugin = $ARGV[ 0 ]; shift @ARGV; if ( defined( $ARGV[ 0 ] )) { $O_Help = 1; } } else { $O_Help = 1; } } elsif (( defined( $ARGV[ 0 ] )) && ( $ARGV[ 0 ] =~ /^(--nolog|-n)$/ )) { $O_NoLog = 1; shift @ARGV; if ( defined( $ARGV[ 0 ] )) { $O_Plugin = $ARGV[ 0 ]; shift @ARGV; if ( defined( $ARGV[ 0 ] )) { $O_Help = 1; } } else { $O_Help = 1; } } elsif (( defined( $ARGV[ 0 ] )) && ( ! ( $ARGV[ 0 ] =~ /^-+/ ))) { $O_Plugin = $ARGV[ 0 ]; shift @ARGV; } else { $O_Help = 1; } } ############################################################################################################################# sub load_configuration { my $Plug_In = undef; my $Log = undef; my $Error = undef; my $Count = undef; my $Line = undef; if ( open( CONFIG, "$Configuration_File" )) { while ( $Line = ) { chomp( $Line ); if ( ! ( $Line =~ /^#/ )) { ( $Plug_In, $Log, $Error, $Count ) = split( /;/, $Line ); $Configuration{ $Plug_In } = $Log . ";" . $Error . ";" . $Count; } } close( CONFIG ); } } ############################################################################################################################# sub save_configuration { my $Plug_In = undef; my $Log = undef; my $Error = undef; my $Count = undef; if ( open( CONFIG, ">$Configuration_File" )) { print CONFIG "# check_wrapper configuration file\n"; foreach $Plug_In ( keys %Configuration ) { ( $Log, $Error, $Count ) = split( /;/, $Configuration{ $Plug_In } ); if (( $Count > 0 ) || ( $Log =~ /^log$/ )) { print CONFIG "$Plug_In;$Configuration{ $Plug_In }\n"; } } close( CONFIG ); } else { print "check_wrapper: could not open configuration file $Configuration_File\n"; exit $Errors{ "UNKNOWN" }; } } ################################### MAIN #################################################################################### # # The main program starts here. The first thing is to read the options. # ############################################################################################################################# check_options(); if ( defined( $O_Version )) { print_version(); exit $Errors{ "UNKNOWN" }; } elsif ( defined( $O_Help )) { print_help(); exit $Errors{ "UNKNOWN" }; } load_configuration(); if ( defined( $O_Configure )) { my $Log = "nolog"; my $Error = undef; my $Count = undef; if ( defined( $Configuration{ $O_Plugin } )) { ( $Log, $Error, $Count ) = split( /;/, $Configuration{ $O_Plugin } ); } $Configuration{ $O_Plugin } = $Log . ";" . $O_Error . ";" . $O_Count; save_configuration(); exit $Errors{ "UNKNOWN" }; } elsif ( defined( $O_Reset )) { my $Log = "nolog"; my $Error = undef; my $Count = undef; if ( $O_Plugin =~ /^all$/i ) { my $Key = undef; foreach $Key ( keys %Configuration ) { ( $Log, $Error, $Count ) = split( /;/, $Configuration{ $O_Plugin } ); $Configuration{ $Key } = $Log . ";OK;0"; } } else { if ( defined( $Configuration{ $O_Plugin } )) { ( $Log, $Error, $Count ) = split( /;/, $Configuration{ $O_Plugin } ); } $Configuration{ $O_Plugin } = $Log . ";OK;0"; } save_configuration(); exit $Errors{ "UNKNOWN" }; } elsif ( defined( $O_Log )) { my $Log = undef; my $Error = "OK"; my $Count = 0; if ( defined( $Configuration{ $O_Plugin } )) { ( $Log, $Error, $Count ) = split( /;/, $Configuration{ $O_Plugin } ); } $Configuration{ $O_Plugin } = "log;" . $Error . ";" . $Count; save_configuration(); exit $Errors{ "UNKNOWN" }; } elsif ( defined( $O_NoLog )) { my $Log = "undef"; my $Error = "OK"; my $Count = 0; if ( $O_Plugin =~ /^all$/i ) { my $Key = undef; foreach $Key ( keys %Configuration ) { ( $Log, $Error, $Count ) = split( /;/, $Configuration{ $Key } ); $Configuration{ $Key } = "nolog;" . $Error . ";" . $Count; } } else { if ( defined( $Configuration{ $O_Plugin } )) { ( $Log, $Error, $Count ) = split( /;/, $Configuration{ $O_Plugin } ); } $Configuration{ $O_Plugin } = "nolog;" . $Error . ";" . $Count; } save_configuration(); exit $Errors{ "UNKNOWN" }; } if ( defined( $Configuration{ $O_Plugin } )) { my $Log = undef; my $Count = undef; my $Error = undef; ( $Log, $Error, $Count ) = split( /;/, $Configuration{ $O_Plugin } ); if ( $Log =~ /^log$/ ) { my $user = $ENV{'USER'}; my $Argument = undef; my $Arguments = undef; foreach $Argument( @ARGV ) { if ( defined( $Arguments )) { $Arguments = $Arguments . " " . $Argument; } else { $Arguments = $Argument; } } if ( ! defined( $Arguments )) { $Arguments = "No arguments provided"; } setlogsock( 'unix' ); openlog( $O_Plugin,'','user'); syslog( 'debug', "$Arguments" ); closelog; } if ( $Count > 0 ) { $Count = $Count - 1; $Configuration{ $O_Plugin } = $Log . ";" . $Error . ";" . $Count; save_configuration(); print "$O_Plugin: check_wrapper simulated return code $Error ($Count)\n"; exit $Errors{ $Error }; } else { save_configuration(); } } $Command = $Path . '/' . $O_Plugin; if ( -x $Command ) { exec $Command, @ARGV; } print "check_wrapper: could not execute $Command\n"; exit $Errors{ "CRITICAL" }; #############################################################################################################################