#!/usr/bin/perl # NSCA init script 1.0 # For use with nsca when not run through xinetd # Written 5/2/03 by Jason Lancaster use strict; # The following should be set to the process name nsca runs as for most systems # you should not need to change this. my $NSCA = "nsca"; # NSCA is built to run as a specific user, therefore we can assume it will only # run as the following user. my $NSCA_USER = "nagios"; # location to nsca.cfg my $NSCA_CFG = "/usr/local/nagios/etc/nsca.cfg"; # location to nsca binary my $NSCA_BIN = "/usr/local/nagios/bin/nsca"; # DO NOT change anything below this line. my $rc; my @pid; sub status() { my ($status, $d); open (CMD, "ps auxc |"); while () { # look for $NSCA_USER at beginning if (/^$NSCA_USER/) { # look for $NSCA process name at end if (/\s$NSCA$/) { if (/^*\s+(\d+)\s*/) { $pid[$d++] = "$1"; } } } } close (CMD); if ($d > 1) { if ($rc == 1) { print "NSCA already running!\n"; } elsif (($rc == 2) || ($rc == 3)) { &stop(); } elsif (($rc == 4) || ($rc == 5)) { printf("More than one process running!\nPID:%s\n", join("\nPID:", @pid)); } } elsif (not defined $pid[0]) { if ($rc == 1) { &start(); } elsif ($rc == 2) { print "NSCA already stopped\n"; } elsif ($rc == 3) { print "NSCA already stopped.\n"; &start(); } elsif ($rc == 4) { printf("NSCA process not found!\n"); } elsif ($rc == 5) { printf("Starting NSCA (previously stopped)...\n"); &start(); } } elsif (defined $pid[0]) { if ($rc == 1) { print "NSCA already running\n"; } elsif (($rc == 2) || ($rc == 3)) { &stop(); } elsif (($rc == 4) || ($rc == 5)) { printf("One process found.\nPID: $pid[0]\n"); } } print "\n"; exit 0; } sub start { qx{$NSCA_BIN -c $NSCA_CFG}; print("NSCA started\n"); qx{logger NSCA init script: started nsca}; &status($rc=4); exit 0; } sub stop { my ($process, $d); print("Stopping NSCA...\n"); foreach $process (@pid) { qx{kill -9 $process}; printf "Killed process %s\n", $process; undef @pid[$d++]; } if ($rc == 3) { &start(); } else { print "\n"; } qx{logger NSCA init script: stopped nsca}; exit 0; } sub process_args { # Process command line arguments my ($arg); foreach $arg (@ARGV) { if ($arg =~ /^start$/) { &status($rc=1); } if ($arg =~ /^stop$/) { &status($rc=2); } if ($arg =~ /^restart$/) { &status($rc=3); } if ($arg =~ /^status$/) { &status($rc=4); } if ($arg =~ /^check$/) { &status($rc=5); } else { printf STDERR "Please specify start|stop|restart|status|check\n\n"; } } } &process_args(); exit 1;