Nagios check script

From Notes

Jump to: navigation, search
#!/usr/bin/perl

#
# Author:  Joshua M. Miller
# Date:    04/19/2007
# Purpose: Nagios check for LDAPv3 servers.
#
# Inputs:  Must give ldap host as first argument.  No
#          other arguments are valid.
#
# Outputs: Returns 0 for OK, 2 for CRITICAL.
#

use strict ;
use Net::LDAP ;

unless ( $#ARGV eq 0 ) {
  printf "\n\tPlease pass the server hostname as the only argument\n\n" ;
  exit(1) ;
}

my $base_dn = "dc=example,dc=com" ;
my $FILTER = "cn=default" ;

my %service_status = (
                       "0" => "OK",
                       "1" => "WARNING",
                       "2" => "CRITICAL"
                     ) ;

my $exit = 0 ;

my $ldap = Net::LDAP -> new("$ARGV[0]") || ($exit = 2) ;
if ($exit) {
  printf "SERVICE STATUS: %s\n", $service_status{$exit} ;
  exit($exit) ;
}

my $mesg = $ldap->start_tls(
         verify => 'require',
         cafile => '/usr/share/ssl/certs/cacert.crt',
         sslversion => 'tlsv1',
         ciphers => 'HIGH:MEDIUM:+SSLv2:RSA'
       ) || ($exit = 2) ;
if ($exit) {
  printf "SERVICE STATUS: %s\n", $service_status{$exit} ;
  exit($exit) ;
}

$ldap -> bind() ;
$mesg = $ldap -> search(base => $base_dn, filter => $FILTER) ;
$mesg -> code() && die $mesg -> error ;
$ldap -> unbind() ;

printf "SERVICE STATUS: %s\n", $service_status{$exit} ;
exit($exit) ;
Personal tools