#! /bin/sh# # chkconfig: 345 99 01# description: Nagios network monitor## File : nagios## Author : Jorge Sanchez Aymar (jsanchez@lanchile.cl)# # Changelog :## 1999-07-09 Karl DeBisschop <kdebisschop@infoplease.com># - setup for autoconf# - add reload function# 1999-08-06 Ethan Galstad <egalstad@nagios.org># - Added configuration info for use with RedHat's chkconfig tool# per Fran Boon's suggestion# 1999-08-13 Jim Popovitch <jimpop@rocketship.com># - added variable for nagios/var directory# - cd into nagios/var directory before creating tmp files on startup# 1999-08-16 Ethan Galstad <egalstad@nagios.org># - Added test for rc.d directory as suggested by Karl DeBisschop# 2000-07-23 Karl DeBisschop <kdebisschop@users.sourceforge.net># - Clean out redhat macros and other dependencies# 2003-01-11 Ethan Galstad <egalstad@nagios.org># - Updated su syntax (Gary Miller)# 2009-09-24 Alexander Skwar <alexander@skwar.name># - Load Solaris SMF file, if present# - Use SMF exitcodes, if SMF file was loaded# - use printf instead of "echo -n"## Description: Starts and stops the Nagios monitor# used to provide network services status.## Load Solaris SMF Variablestest -r /lib/svc/share/smf_include.sh && . /lib/svc/share/smf_include.shstatus_nagios (){ if test -x $NagiosCGI/daemonchk.cgi; then if $NagiosCGI/daemonchk.cgi -l $NagiosRunFile; then return 0 else return 1 fi else if ps -p $NagiosPID > /dev/null 2>&1; then return 0 else return 1 fi fi return 1}printstatus_nagios(){ if status_nagios $1 $2; then echo "nagios (pid $NagiosPID) is running..." else echo "nagios is not running" fi}killproc_nagios (){ kill $2 $NagiosPID}pid_nagios (){ if test ! -f $NagiosRunFile; then echo "No lock file found in $NagiosRunFile" test -z "$SMF_EXIT_ERR_FATAL" && exit 1 test -n "$SMF_EXIT_ERR_FATAL" && exit $SMF_EXIT_ERR_FATAL fi NagiosPID=`head -n 1 $NagiosRunFile`}# Source function library# Solaris doesn't have an rc.d directory, so do a test firstif [ -f /etc/rc.d/init.d/functions ]; then . /etc/rc.d/init.d/functionselif [ -f /etc/init.d/functions ]; then . /etc/init.d/functionsfiprefix=/opt/local/nagios-3.2.0exec_prefix=${prefix}NagiosBin=${exec_prefix}/bin/nagiosNagiosCfgFile=${prefix}/etc/nagios.cfgNagiosStatusFile=${prefix}/var/status.datNagiosRetentionFile=${prefix}/var/retention.datNagiosCommandFile=${prefix}/var/rw/nagios.cmdNagiosVarDir=${prefix}/varNagiosRunFile=${prefix}/var/nagios.lockNagiosLockDir=/var/lock/subsysNagiosLockFile=nagiosNagiosCGIDir=${exec_prefix}/sbinNagiosUser=webservdNagiosGroup=webservd# Check that nagios exists.if [ ! -f $NagiosBin ]; then echo "Executable file $NagiosBin not found. Exiting." test -z "$SMF_EXIT_ERR_FATAL" && exit 1 test -n "$SMF_EXIT_ERR_FATAL" && exit $SMF_EXIT_ERR_FATALfi# Check that nagios.cfg exists.if [ ! -f $NagiosCfgFile ]; then echo "Configuration file $NagiosCfgFile not found. Exiting." test -z "$SMF_EXIT_ERR_CONFIG" && exit 1 test -n "$SMF_EXIT_ERR_CONFIG" && exit $SMF_EXIT_ERR_CONFIGfi# See how we were called.case "$1" in start) printf "Starting nagios:" $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1; if [ $? -eq 0 ]; then su - $NagiosUser -c "touch $NagiosVarDir/nagios.log $NagiosRetentionFile" rm -f $NagiosCommandFile touch $NagiosRunFile chown $NagiosUser:$NagiosGroup $NagiosRunFile #/usr/bin/truss -adefo /tmp/nagios.truss $NagiosBin -d $NagiosCfgFile #/usr/bin/truss -afo /tmp/nagios.truss.$$ $NagiosBin -d $NagiosCfgFile env > /tmp/nagios.env $NagiosBin -d $NagiosCfgFile if [ -d $NagiosLockDir ]; then touch $NagiosLockDir/$NagiosLockFile; fi echo " done." test -z "$SMF_EXIT_OK" && exit 0 test -n "$SMF_EXIT_OK" && exit $SMF_EXIT_OK else echo "CONFIG ERROR! Start aborted. Check your Nagios configuration." test -z "$SMF_EXIT_ERR_CONFIG" && exit 1 test -n "$SMF_EXIT_ERR_CONFIG" && exit $SMF_EXIT_ERR_CONFIG fi ;; stop) printf "Stopping nagios: " pid_nagios killproc_nagios nagios # now we have to wait for nagios to exit and remove its # own NagiosRunFile, otherwise a following "start" could # happen, and then the exiting nagios will remove the # new NagiosRunFile, allowing multiple nagios daemons # to (sooner or later) run - John Sellens #printf 'Waiting for nagios to exit .' for i in 1 2 3 4 5 6 7 8 9 10 ; do if status_nagios > /dev/null; then printf '.' sleep 1 else break fi done if status_nagios > /dev/null; then echo '' echo 'Warning - nagios did not exit in a timely manner' else echo 'done.' fi rm -f $NagiosStatusFile $NagiosRunFile $NagiosLockDir/$NagiosLockFile $NagiosCommandFile ;; status) pid_nagios printstatus_nagios nagios ;; checkconfig) printf "Running configuration check..." $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1; if [ $? -eq 0 ]; then echo " OK." else echo " CONFIG ERROR! Check your Nagios configuration." test -z "$SMF_EXIT_ERR_CONFIG" && exit 1 test -n "$SMF_EXIT_ERR_CONFIG" && exit $SMF_EXIT_ERR_CONFIG fi ;; restart) printf "Running configuration check..." $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1; if [ $? -eq 0 ]; then echo "done." "$0" stop "$0" start else echo " CONFIG ERROR! Restart aborted. Check your Nagios configuration." test -z "$SMF_EXIT_ERR_CONFIG" && exit 1 test -n "$SMF_EXIT_ERR_CONFIG" && exit $SMF_EXIT_ERR_CONFIG fi ;; reload|force-reload) printf "Running configuration check..." $NagiosBin -v $NagiosCfgFile > /dev/null 2>&1; if [ $? -eq 0 ]; then echo "done." if test ! -f $NagiosRunFile; then "$0" start else pid_nagios if status_nagios > /dev/null; then printf "Reloading nagios configuration..." killproc_nagios nagios -HUP echo "done" else "$0" stop "$0" start fi fi else echo " CONFIG ERROR! Reload aborted. Check your Nagios configuration." test -z "$SMF_EXIT_ERR_CONFIG" && exit 1 test -n "$SMF_EXIT_ERR_CONFIG" && exit $SMF_EXIT_ERR_CONFIG fi ;; *) echo "Usage: nagios {start|stop|restart|reload|force-reload|status|checkconfig}" test -z "$SMF_EXIT_ERR_FATAL" && exit 1 test -n "$SMF_EXIT_ERR_FATAL" && exit $SMF_EXIT_ERR_FATAL ;;esac# End of this script