Page 1 of 1

Logs within a custom script

Posted: Wed Oct 21, 2015 3:54 am
by Ticcio
Hey!

I'm extremely new with Nagios and Bash scripting, but I've managed to install it with various NRPE Clients on some Virtual Machines.
I created some custom script to do different checks and they work fine, but the problem is when I try to log them.
Public Keys are set, /etc/sudoers/ is fine, others script on NRPE ( without the log feature ) are working fine and all needed permissions are given.

command ( test ) on NagiosQL:

Code: Select all

$USER1$/check_nrpe -H $HOSTADDRESS$ -c text
nrpe.cfg interested part:

Code: Select all

command[text]=/usr/local/nagios/libexec/./test.sh

Code: Select all

#!/bin/bash

PROGNAME=`basename "$0"`
PROGPATH=`echo "$0" | /bin/sed -e 's,[\\/][^\\/][^\\/]*$,,'`
REVISION=`echo '$Revision: v0.3 $' | sed -e 's/[^0-9.]//g'`

. $PROGPATH/GlobalNagios.sh

#Logs by redirected streams
#set -x
! [ "$UniqueCallID" ] && UniqueCallID="$(uuidgen -t 2>>${ErrorLogFile:-/dev/null} | sed -e "s/\-//g" 2>>${ErrorLogFile:-/dev/null})" && ! [ "$UniqueCallID" ] && UniqueCallID="$(/bin/date +%s%N 2>>${ErrorLogFile:-/dev/null})"

	
LOG_FILE=Logs.out
LOG_FILE_ERR=Logs_error_${UniqueCallID}.out		
		
trap ' echo "$(date) - ERROR - `cat "${LOG_FILE_ERR}"`"; rm -f ${LOG_FILE_ERR:-NO_LOG}' ERR

trap ' rm -f ${LOG_FILE_ERR} ' EXIT

exec 3>&1 2>>${LOG_FILE_ERR} 1>>${LOG_FILE}

EchoLogger(){
	echo "$(date) - INFO - $*"
}

EchoLoggerConsole(){
	echo "$*" 1>&3 
}

EchoLoggerConsoleFile(){
	EchoLogger "$*"
	EchoLoggerConsole "$*"
}

print_usage() {
	echo "Usage: $PROGNAME [-h help] [-H hostname] [-w warning] [-c critical]
        -H  hostname
        -w  (optional) warning threshold
        -c  (optional) critical threshold
        -h --help help

Usage: $PROGNAME --help
Usage: $PROGNAME --version
"
}
#Defaults:
REMOTEHOST=""
WARNING=80
CRITICAL=90
Logger="EchoLogger"
Logger_Console="EchoLoggerConsole"
Logger_Both="EchoLoggerConsoleFile"
# $Logger "File only"
# $Logger_Console	"Console only"
# $Logger_Both "Both File and Console"
while test -n "$1"; do
	case "$1" in
		-H) REMOTEHOST=$2; shift ;;
		-w) WARNING=$2; shift ;;
		-c) CRITICAL=$2; shift ;;
		--help) print_usage; exit $STATE_OK ;;
		-h) print_usage; exit $STATE_OK ;;
		--version) 
			echo $PROGNAME $REVISION; exit $STATE_OK ;;
		*) 
			echo "Unknown Argument: $1"
					print_usage; exit $STATE_UNKNOWN ;;
	esac
	shift
done 
$Logger -e "\n $PROGNAME - START LOGS  $REMOTEHOST\n" >>Logs.out
#User check:
$Logger [[ "`id -u 2>>/dev/null`" != "0" ]] && SUDO="sudo "
$Logger USER=`id -u -n`
if	[[ -z $REMOTEHOST ]]; then
	#I'm calculating how many resources my brain is using, sir.
	$Logger CPULOAD=`${SUDO}free -m | awk 'NR==2{printf "%u\n",$3*100/$2 }'`
	CPULOAD=`${SUDO}free -m | awk 'NR==2{printf "%u\n",$3*100/$2 }'`
else
	$Logger CPULOAD=`ssh $USER@$REMOTEHOST 2>>/dev/null "${SUDO}free -m" | awk 'NR==2{printf "%u\n",$3*100/$2}'`
	CPULOAD=`ssh $USER@$REMOTEHOST 2>>/dev/null "${SUDO}free -m" | awk 'NR==2{printf "%u\n",$3*100/$2}' `
	
fi
awdawfwa #Test, successfully logged while running the script without NRPE agent
exec 1>&3 
if [[ "$CPULOAD" -lt "$WARNING" ]]; then
	$Logger_Console -e "CPU Used: $CPULOAD%\t\t\t[ OK ]"
	exit $STATE_OK
elif [[ "$CPULOAD" -lt "$CRITICAL" ]];	then
	$Logger_Console -e "CPU Used: $CPULOAD%\t\t\t[ WARNING ]"
	exit $STATE_WARNING
elif [[ "$CPULOAD" -ge "$CRITICAL" ]]; then
	$Logger_Console -e "CPU Used: $CPULOAD%\t\t\t[ CRITICAL ]"
	exit $STATE_CRITICAL
fi
exit $STATE_OK
Whenever I run this script locally ( or with an hostaddress as -H option ) it works fine, it creates a file and it logs everything I specified.
If I try to run it on Nagios with the NRPE Client it will just throw on the output every single line without creating any file:

Status Information: Wed Oct 21 10:44:27 CEST 2015 - ERROR -
Wed Oct 21 10:44:27 CEST 2015 - ERROR -
Wed Oct 21 10:44:27 CEST 2015 - INFO - [[ 1000 != 0 ]]
Wed Oct 21 10:44:27 CEST 2015 - INFO - USER=nagios
Wed Oct 21 10:44:27 CEST 2015 - INFO - CPULOAD=18
Wed Oct 21 10:44:27 CEST 2015 - ERROR -
Wed Oct 21 10:44:27 CEST 2015 - ERROR -
CPU Used: 18% [ OK ] ( This is the only information I need to write under the Status Information column. )

May someone help me? Is it a script error, or something like Nagios messing up with stream redirection?
I'm really sorry if it's an OT or the wrong place to post this kind of problem.
Apologize eventually for grammar errors, it is not my main language.

Thanks in advice, Ticcio.

Re: Logs within a custom script

Posted: Wed Oct 21, 2015 3:15 pm
by hsmith
What user are you running the script as on the remote system?

Re: Logs within a custom script

Posted: Thu Oct 22, 2015 3:12 am
by Ticcio
I think that was kinda a stupid error

Code: Select all

LOG_FILE=$PROGPATH/nagioslogs.out
LOG_FILE_ERR=$PROGPATH/nagioslogs_error_${UniqueCallID}.out
I forgot to add the path before the logs files, so it ended up he could not write.
The exit status for the files' redirect was 1 indeed.

Other than that, I'm running with a nagios user which has Permission problems when he needs to write on logs files

Isn't this correct?

Code: Select all

[[ -a "${LOG_FILE_ERR}" ]] || install -m 666 /dev/null ${LOG_FILE_ERR} 
I execute this with a trap whenever an ERR signal is detected but still he can't write on them

Code: Select all

./check_disk.sh: line 21: /dev/stdout: Permission denied
install: cannot create regular file ‘./nagioslogs_error_.out’: Permission denied
cat: ./nagioslogs_error_.out: No such file or directory
22 October 2015 - 12:09:54 - ERROR -
./check_disk.sh: line 21: /dev/stderr: Permission denied
install: cannot create regular file ‘./nagioslogs_error_.out’: Permission denied
cat: ./nagioslogs_error_.out: No such file or directory
Thank you for the help, the big problem is solved thou. c:

Re: Logs within a custom script

Posted: Thu Oct 22, 2015 5:00 pm
by tmcdonald
I think at this point you would get better results from a forum dedicated to bash scripting. We can help with Nagios-specific things, but this looks like it's a pretty general Bash/Linux issue.

Re: Logs within a custom script

Posted: Fri Oct 23, 2015 2:15 am
by Ticcio
Yes, you are right. Thank you very much, you can close this up c:

Re: Logs within a custom script

Posted: Fri Oct 23, 2015 9:12 am
by rkennedy
i will now close this thread. If you end up needing help with the Nagios portion, feel free to open another thread.