#!/usr/bin/perl # # SYNTAX: # send_nsca_cached [cache_time] # # DESCRIPTION: # Used to pass passive results. Caches results and submits at 5 second # intervals by default. The cache time can be specified on # command line - 0 to send immediately # # Requires Nagios 2.0+ # # Warning: this script needs to be invoked for a send_nsca to occur, so # if you only have 1 service on a slave that is run every minute, the # minimum time between sends is 1 minute, regardless of the cache_time setting. # So you should only use on a busy slave. # # Warning 2: Do not use a cache time that is too large. Even a cache time of # 1 second will help performance dramatically on a busy slave. # # AUTHORS: # Copyright (C) 2006 Altinity Limited # # This file is part of Opsview # # Opsview is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # Opsview is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Opsview; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA # use strict; my $cache_time = shift @ARGV; $cache_time = 5 unless defined $cache_time; if ($cache_time == 0) { open SEND_NSCA, "| /usr/local/nagios/bin/send_nsca -H localhost -c /usr/local/nagios/etc/send_nsca.cfg"; print SEND_NSCA &output; close SEND_NSCA; exit; } my $cache_file = "/usr/local/nagios/var/send_nsca.cache"; my $now = time; my $last_updated; if (-e $cache_file) { open CACHE, "+<", $cache_file; $last_updated = ; #print "Last updated: ", scalar localtime $last_updated, $/; } else { open CACHE, "+>", $cache_file; print CACHE $now, $/; $last_updated = time; #print "New cache",$/; } if ($now - $last_updated < $cache_time) { seek CACHE, 0, 2; # Goto end print CACHE &output; } else { open SEND_NSCA, "| /usr/local/nagios/bin/send_nsca -H localhost -c /usr/local/nagios/etc/send_nsca.cfg"; print SEND_NSCA , &output; close SEND_NSCA; #print "Will send:", $/; #print ; #close CACHE; #print "Plus this one:", &output; # Reset time open CACHE, ">", $cache_file; print CACHE time, $/; # Update send_nsca status my $status_file = "/usr/local/nagios/var/ocsp.status"; open STATUS, ">", $status_file; if ($? == 0) { print STATUS "0"; } else { print STATUS "2"; } close STATUS; } close CACHE; exit; sub output { "$ENV{NAGIOS_HOSTNAME}\t$ENV{NAGIOS_SERVICEDESC}\t$ENV{NAGIOS_SERVICESTATEID}\t$ENV{NAGIOS_SERVICEOUTPUT}|$ENV{NAGIOS_SERVICEPERFDATA}\n"; }