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
?
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
since monit 5.21.0, alterting on existence is directly supported:
check file testfile with path /path/to/file
if exist then alert
see in changelog https://mmonit.com/monit/changes/#5.21.0
Please check with :
check program not_exist_file_root_test with path "/bin/ls /root/test"
if status = 0 then alert
or
check program not_exist_file_root_test with path /bin/sh -c "test -f /root/test"
if status = 0 then alert
My 2 cents
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With