Using Monit Environment Variables with exec

If you read the Monit documentation, it tells you exactly how to use Monit environment variables when using exec.

No environment variables are used by Monit. However, when Monit executes a start/stop/restart program or an exec action, it will set several environment variables which can be utilised by the executable to get information about the event, which triggered the action.

https://mmonit.com/monit/documentation/monit.html#ENVIRONMENT

I can be smart, but sometimes I can be daft. You don’t want to use the variables within your monit configuration, but instead, you want to use these variables in your exec script.

Here’s a great example of how to use $MONIT_EVENT. First set up a monit check

check system $HOST-steal
    if cpu (steal) > 0.1% for 1 cycles
        then exec "script.sh"
        AND repeat every 10 cycles

Now here’s script.sh which will use $MONIT_EVENT

#!/bin/bash
echo "Monit Event: $MONIT_EVENT" | mail -s "$MONIT_EVENT" [email protected]

I was in a rush and felt I had to post this to help others who might overlook this.