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 textCode: Select all
command[text]=/usr/local/nagios/libexec/./test.shCode: 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_OKIf 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.