alarm on existence of file in Monit
Asked Answered
I

4

6

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?

Internecine answered 12/4, 2013 at 3:23 Comment(0)
P
7

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
Perish answered 15/7, 2013 at 22:43 Comment(2)
looks like the snippet got copied and forgot to get changed. it should be alert at the end of the 2nd code snippet :) i can't change it since it's less than 6 chara.Certes
Should be fixed nowPerish
G
3

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

Guild answered 7/8, 2018 at 9:10 Comment(0)
G
2

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

Geraldina answered 12/5, 2014 at 15:34 Comment(0)
J
2

renab, your check should end with "then alert" not "then alarm" at least in my version (5.2.5).

testfile with path /path/to/file
  if not exist then exec "/bin/bash -c 'echo dne > /dev/null'" else if succeeded then alert
Juliannejuliano answered 1/6, 2014 at 16:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.