As you may know op5 monitor recommends a check interval of 5 minutes escalating to once time per minute if a check becomes critical.
Running a critical system it may be of interest to have the check intervals being even faster, the obvious case would be to decrease the check interval but in some cases that might not be sufficient. In these cases you might want to have your backend system send you alerts immediately.
NSCA is an addon available with op5 Monitor and Nagios allowing you to push passive checks from the backend system. Using this in conjunction with triggers gives you a pretty powerful instant notification system.
There’s multiple guides on how to configure NSCA but the essential portion is to modify nsca.cfg and identify the password and descryption_method used, these needs to be entered into send_nsca on the remote server to ensure that the packages are encrypted and authorized. Once finished you’re good to start the nsca service.
The program available below ensures that you can allow any software that usually writes to a file to forward it’s information via NSCA to Monitor. However there’s some preparation needed.
First of you need to create a pipe/FIFO which the log-messages are passed to, this is easily done using mknod.
mknod /var/log/pipe p
chmod 600 /var/log/pipe
Now point your program to log to /var/log/pipe and you’re good to get started.
#!/usr/bin/perl
#
# Copyright (c) op5 AB, Jonathan Petersson <jpetersson@op5.com>
# All Rights Reserved.
#
# This software has only been tested on Fedora 14, modifications
# may be needed for other distributions and operative-systems.
# send_nsca is required to run in the background to forward
# information to the monitor server.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# Further, this software is distributed without any warranty that it is
# free of the rightful claim of any third person regarding infringement
# or the like. Any license provided herein, whether implied or
# otherwise, applies only to this software file. Patent licenses, if
# any, provided herein do not apply to combinations of this program with
# other software, or any other product whatsoever.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc., 59 Temple Place – Suite 330, Boston MA 02111-1307, USA.
#
use warnings;
use Getopt::Long;
use POSIX qw(setsid);
my($host,$check,$r_host,$show_help,$pipe);
sub init {
Getopt::Long::GetOptions(‘host=s’ => \$host,
‘check|c=s’ => \$check,
‘remote|r=s’ => \$r_host,
‘pipe|p=s’ => \$pipe,
‘help|h’ => \$show_help,
);
if (!defined($host) || !defined($check) || !defined($r_host) || !defined($pipe)) {
$show_help = 1;
}
if ($show_help) {
print <<EOF
Syntax: $0 –host <host> –check <check>–remote <remotehost> –pipe <pipe> [options]
This utility is used to manage pipes to nsca
Options:
-h|–help : Show this
Flags:
-H|–host : Hostname (Of the server being monitored)
-c|–check : Name of the check
-r|–remote : Hostname or IP of the monitoring server
-p|–pipe : Pipe to be monitored
EOF
;
exit 1;
}
}
sub pipe_to_fork ($) {
my $parent = shift;
pipe my $child, $parent or die;
my $pid = fork();
die “fork() failed: $!” unless defined $pid;
if ($pid) {
close $child;
} else {
close $parent;
open(STDIN, “<&=” . fileno($child)) or die;
}
$pid;
}
init;
defined(my $pid = fork) or die “Can’t fork: $!”;
exit if $pid;
setsid or die “Can’t start a new session: $!”;
$SIG{INT} = \&terminate;
$SIG{HUP} = \&terminate;
$SIG{CHLD} = ‘IGNORE’;
sub terminate {
exit 0;
}
open(PIPE, “$pipe”);
while (1) {
while(my $line =
) {
if (pipe_to_fork(‘PT_TO_CHLD’)) {
print PT_TO_CHLD $line;
close PT_TO_CHLD;
} else {
while (my $line = ) {
chomp($line);
open(NSCA, “|send_nsca -H $r_host > /dev/null”);
printf NSCA “%s\t%s\t%s\t%s\n”,”$host”,”$check”,”2″,”$line”;
close NSCA;
exit 0;
}
}
}
}
close PIPE;
Now start the program and define what host and service_check it should update
log_to_nsca.pl –host webserver1 –check “Apache errors” –remote monitor –pipe /var/log/pipe
Replace the applicable hostnames and service_check name with your system parameters and you’ll have instant notification if a log appears.