#!/bin/bash
#
# nagiosstatus.sh - simple CGI-BIN script checking the Nagios process status
#
# Version 1.0, latest version, documentation and bugtracker available at:
#               https://gitlab.lindenaar.net/scripts/nagios-plugins
#
# Copyright (c) 2015 Frederik Lindenaar
#
# This script is free software: you can redistribute and/or modify it under the
# terms of version 3 of the GNU General Public License as published by the Free
# Software Foundation, or (at your option) any later version of the license.
#
# This script 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
# this program.  If not, visit <http://www.gnu.org/licenses/> to download it.

# Default settings for Debian Linux, adjust for your system if necessary
PROGRAM=nagios3
PIDFILE=/var/run/nagios3/nagios3.pid
STATFILE=/var/cache/nagios3/status.dat
MAXAGE="5 minutes"

# First output the HTTP header
DATE=`date -R`
cat << EOH
Status: 200 OK
Content-Type: text/plain
Date: $DATE
Expires: $DATE
Last-Modified: $DATE
Cache-Control: no-cache
Connection: close

EOH

# Check whether the process is running
if [ -f "$PIDFILE" -a "$PROGRAM" = `ps --no-header -o comm -p \`cat $PIDFILE\`` ]; then
	# It's running, check whether the Statfile exists and is recent
	if [ -n "$STATFILE" -a -n "$MAXAGE" -a -f "$STATFILE" -a \
	     `stat -c %Y $STATFILE` -lt `date +%s -d "$MAXAGE ago"` ]; then
		echo STALLED
	else
    		echo OK
	fi
else
    echo STOPPED
fi