Linux – Monit: how do you stop monit from running the exec statement every time the test fails

linuxmonit

How do you stop monit from running the exec statement every time the test fails? The statement in my monitrc is:

check filesystem tmpfs with path /var                                           
    if space > 90% then exec "/usr/bin/logger -p daemon.crit 'MAJOR: space test'"

This seems weird because someone else asked a question in which he was doing an alert and it had the behaviour I wanted. I'm ready to start choke slamming linux boxes.

Edit: here is the opposite case Repeat monit alerts

Is it because he is using alert not exec?

Best Answer

I had to deal with a similar issue a few times ago.

The fact is that monit is not able to do this, as far as i know.

With monit you can deal with X times and/or Y cycles directives, but more or less quickly the exec action will be triggered more than once, depending on time you spend to fix the issue.

So, finally, i've decided to write my own check script to handle all the logic, based on flags.

I'm going to share this with you, then you take or not, it's up to you.


First : Write the script to monitor FS usage, let's say /root/check_fsspace.sh :

#!/bin/sh

myFS=/var
myTreshold=90
flagFile=/tmp/flag

spaceused=$(df -h | grep "$myFS" | tr -s " " | cut -d" " -f5 | cut -d"%" -f1)

if [ $spaceused -gt $myTreshold ]; then
  if [ ! -f $flagFile ]; then
     touch $flagFile
     exit 1
  else
     exit 0
  fi
fi

if [ $spaceused -le $myTreshold ]; then
   rm -f $flagFile
   exit 0
fi

Here i assume you can understand the script. If not, tell me, i will explain it.

Second : Setup your monit service definition :

check program check_fs with path "/root/check_fsspace.sh"
  if status != 0 then exec "/usr/bin/logger -p daemon.crit 'MAJOR: space test'"
Related Topic