1

I have the following plugin whose status is OK on core.

#!/usr/local/bin/bash

if [ "$1" = "-w" ] && [ "$2" -lt "101" ] && [ "$3" = "-c" ] && [ "$4" -lt "101" ] ; then
  warn=$2
  crit=$4

  AVAILMEMPERC=$(free -m | grep mem_avail | awk '{print $7}'| tr -d %])

  if [ ${AVAILMEMPERC} -gt $warn ] && [ ${AVAILMEMPERC} -gt $crit ];then
    echo "OK - Available Memory = $AVAILMEMPERC% | Available memory=$AVAILMEMPERC%;$warn;$crit;0;100"
    exit 0
  elif [ ${AVAILMEMPERC} -lt $warn ] && [ ${AVAILMEMPERC} -gt $crit ]; then
    echo "WARNING - Available Memory = $AVAILMEMPERC% | Available memory=$AVAILMEMPERC%;$warn;$crit;0;100"
    exit 1
  else
    echo "CRITICAL - Available Memory = $AVAILMEMPERC% | Available memory=$AVAILMEMPERC%;$warn;$crit;0;100"
    exit 2
  fi
else
  echo "$0 - Nagios Plugin for checking the available memory in a Linux system"
  echo ""
  echo "Usage:    $0 -w <warnlevel> -c <critlevel>"
  echo "  = warnlevel and critlevel is warning and critical value for alerts."
  echo ""
  echo "EXAMPLE:  $0 -w 10 -c 5 "
  echo "  = This will send warning alert when available memory is less than 10%, and send critical when it is less than 5%"
  echo ""
  exit 3
fi

When I run it locally on the remote machine, it runs fine. I get the right output. But on the web GUI, I see that Nagios cannot extract the variable AVAILMEMPERC value

For example, if I simplify the plugin to below

#!/usr/local/bin/bash

warn=$2
crit=$4

AVAIL_MEM_PERCENTAGE="$(free -m)"

echo "OK - ${AVAIL_MEM_PERCENTAGE}"

The only output I see on GUI is

OK -

When I run it on command line, I do get the entire free -m output

Tried the following and it doesn't write anything. I gave 777 permissions to /tmp and the files.

free -m > /tmp/check_avail_memory.out

Seems like a permissions issue? It runs on Nagios Core though. If I replace free with top nagios is able to write to the file.

I have downloaded free from here http://people.freebsd.org/~rse/dist/freebsd-memory. As I said it runs fine on the remote machine. I have made sure the paths are correct on FreeBSD and it is executable.

Couldn't find any relevant logs to this except for the plugin output.

pdns
  • 265
  • 1
  • 3
  • 11
  • can you try using tee or echo command to store output of $(free -m) I am not exactly sure about the syntax but i assume the above command is executing in separate sub shell but its output is getting lost. – Amit Mahajan Oct 26 '17 at 01:36
  • @amitmah nope, I still don't get output on gui. it does run locally again. – pdns Oct 26 '17 at 01:49
  • what if you try AVAIL_MEM_PERCENTAGE=cat -u >$(free -m) – Amit Mahajan Oct 26 '17 at 01:53
  • I am not sure if the syntax you specified is correct. It doesn't run. – pdns Oct 26 '17 at 02:01
  • @pdns ok let's start with this. Change your line `AVAILMEMPERC=$(free -m | grep mem_avail | awk '{print $7}'| tr -d %])` to `availmemperc=$(free -m | awk '/mem_avail:/{print $7+0}')` – Valentin Bajrami Oct 26 '17 at 09:49
  • @val0x00ff I have tried `free -m > /tmp/check_avail_memory.out` after giving 0777 access to /tmp and the file. And I don't see anything in the file. So the `free -m` itself is not running I guess. – pdns Oct 26 '17 at 11:38
  • @pdns what does `command -v free` tell you? Also does it do anything if you use the absolute path to the `free` command e.g `/usr/bin/free -m >/tmp/results.log` – Valentin Bajrami Oct 26 '17 at 12:03
  • 1
    The absolute path worked. Thanks a lot! What is the issue with not specifying absolute path? – pdns Oct 26 '17 at 12:21
  • @pdns you can export the PATH in your script using `export PATH="$PATH"` – Valentin Bajrami Oct 26 '17 at 20:26

0 Answers0