alarm on existence of file in Monit

tedder42 picture tedder42 · Apr 12, 2013 · Viewed 8k times · Source

I've been using monit for a little while, but I want to alarm if a file exists. This is the opposite use case from the main documentation.

Here's the doc says:

IF [DOES] NOT EXIST [[<X>] <Y> CYCLES] THEN action [ELSE IF SUCCEEDED [[<X>] <Y> CYCLES] THEN action]
action is a choice of "ALERT", "RESTART", "START", "STOP", "EXEC" or "UNMONITOR".

This gives me the recipe for "freak out if file is missing". But I want to "freak out if the file's there". And the choice of actions implies there's no "do nothing" action. I could shell out to a no-op, but that's really silly for the standard case of "do nothing".

I guessed some basic cases:

IF EXISTS THEN alarm
IF EXIST THEN ALARM

So, is there a standard way to do IF IT DOES EXIST?

Answer

renab picture renab · Jul 16, 2013

I recently was looking for the same solution as you and unfortunately, I was unable to discover a way of doing this in monit.

My situation differs slightly from yours so I ended up alarming if the file did not exist, and executed a shell script if it did. Like you, I did not want to spawn a shell just because the file did not exist, and having "file does not exist" show up in /var/log/messages isn't a big deal for me.

I know you said that you could shell out to a no-op so you probably don't need the following but I am adding it for those who might have the same issue and not know how to do it.

check file testfile with path /path/to/file
    if not exist then exec "/bin/bash -c 'echo dne > /dev/null'" else if succeeded then alarm

Note that you must exec /bin/bash to write the output of echo to /dev/null or monit will literally echo "dne > /dev/null"

Edit: As it was brought to my attention by disasteraverted, newer versions of Monit use alert rather than alarm, so the check would look like this:

check file testfile with path /path/to/file
    if not exist then exec "/bin/bash -c 'echo dne > /dev/null'" else if succeeded then alert