[Nagiosplug-devel] nagios plugin written in sh

Daniel Geske daniel.geske at yoc.de
Thu Jul 11 07:08:09 CEST 2002


> Carefully check your environment variables.  This is the problem
> I run into
> most often.  You may have things exported at the command line that the
> nagios daemon doesn't.
> --
> Tom Bertelson
I don't think I use any env variables.

> More info on this would be good.  Could you forward the script to the
> list?
>
> -Michael

Sure can.
Just have to add some explanations. I actually tried to make things simple
by writing about selecting a cell. It's not really like that.
I have scripts running on several servers in my location.
All scripts send some kind of "Hi, I'm alive" into my local database by
updating a row "last_updated" to now().
This shell script compares now() and last_updated and if this time is longer
than waring_period it sends WARNING, if longer than critical_period it sends
CRITICAL.
If it does not find the $script running on $hostname, both specified as
parameters of the script, it returns UNKNOWN.
If started by nagios, the script always returns UNKNOWN.
I did some checking for over 2 hours now and found that the mysql command
causes the problem.
Like I said, when I start the shell from a terminal (of course I did su to
nagios user first) it runs all right, but in the one nagios starts mysql
selects NULL and exits with 1. Maybe it doesn't get to do the SELECT and
that's why it is NULL.
But why would it work for me on the shell then? No ideas.
I'd be really happy if you guys have any and let me know. Thanks for taking
the time.

Here comes the shell script working on my SuSE Linux 7.3 box:

#!/bin/sh

# syntax: check_yocScripts <hostname> <script name>

# path to echo binary
e="/bin/echo -e";

#database where the scripts update their stati
database="yocISDN.yocScriptStati";

case $1 in
	--help|--hel|--he|--h|-help|-hel|-he|-h)
		$e "check_yocScripts plugin for Nagios 1.0b4";
		$e "created by:\tDaniel Geske, YOC AG.";
		$e "last modified:\t`/bin/date -r ./check_yocScripts.sh`\n";
		$e "usage:  check_yocScripts <hostname> <script>\n";
		$e "\t<hostname>\tsystem where <script> runs";
		$e "\t<script>\tname of the script to check\n";
		$e "\tA line with host=<hostname> and script=<script>\n\tmust exist in
database $database.\n";
		$e "Report bugs to <daniel.geske at gmx.net>.";
		exit $?;
esac


hostname=$1;
if [ -z $hostname ]
then
	hostname=hostname;
fi

script=$2;
if [ -z $script ]
then
	script=script;
fi

mysql="/usr/bin/mysql -N -e";

id=`$mysql "SELECT id FROM $database
	    WHERE host='$hostname'
	    AND script_name='$script'"`;

state=UNKNOWN;
rc=3;

if [ $id ]
then
	warning=`$mysql "SELECT id FROM $database
			WHERE id=$id
			AND ( ( unix_timestamp( now() )
			      - unix_timestamp( last_updated ) )
			     >= warn_period_sec)"`;

	if [ $warning ]
	then
		critical=`$mysql "SELECT id FROM $database
				WHERE id=$id
				AND ( ( unix_timestamp( now() )
				      - unix_timestamp( last_updated ) )
				     >= critical_period_sec)"`;
		if [ $critical ]
		then
			state=CRITICAL;
			rc=2;
		else
			state=WARNING;
			rc=1;
		fi
	else
		OK=`$mysql "SELECT id FROM $database
		    WHERE id=$id
		    AND ( ( unix_timestamp( now() )
		          - unix_timestamp( last_updated ) )
			  < warn_period_sec)"`;
		if [ $OK ]
		then
			state=OK;
			rc=0;
		fi
	fi
fi

$e "$script on $hostname (id=$id) is $state.";
exit $rc;





More information about the Devel mailing list