2rs2ts
2rs2ts

Reputation: 11046

Monit check fails when I add test for status == 0

I am running M/Monit 5.14 on Debian Jessie. I have a check like this:

check program myscript with path "/etc/monit/scripts/test.sh"
  if status != 0 then exec "/etc/monit/scripts/record_failure.sh"
  if status == 0 then exec "/etc/monit/scripts/record_success.sh"

The idea is that these record scripts will send a record of the result of this test every time it runs. Sure, there are other ways to do this, but this is just a particular route I took in this case. I'm more concerned with the fact that adding the status == 0 line makes the monit check always fail, even though the last result in monit status is 0 and record_success.sh is being run.

There's nothing about this documented on the site. Is it failing the check because there is a matching condition, or did I mess up the syntax?

Upvotes: 2

Views: 1811

Answers (2)

totahuanocotl
totahuanocotl

Reputation: 56

Although this is an old post, I just came accross the same issue and it would have been helpful to have a solution.

According to the Serivce Testing General Syntax the right structure to check for testing a program should be

IF <test> THEN <action> [ELSE IF SUCCEEDED THEN <action>]

As you can see, the successful case (0 by convention) should be specified by the succeeded keyword.

In this case the following change should make it happy:

check program myscript with path "/etc/monit/scripts/test.sh"
    if status != 0 then exec "/etc/monit/scripts/record_failure.sh"
    else if succeeded then exec "/etc/monit/scripts/record_success.sh"

Upvotes: 4

Rabea
Rabea

Reputation: 2034

I made a small test on my side with the following script and your Monit configuration, returning the exit status depending on the last command, and tailing /var/log/tester.log

#!/bin/bash
echo "This is a test" >> /var/log/tester.log
exit

Failure:

#!/bin/bash
echo "failure in this case" >> /var/log/tester.log

Success:

#!/bin/bash
echo "success in this case" >> /var/log/tester.log

While switching from exit to exit 1 , the status was toggling with no issue. Monit version 5.6

  • I would try to debug the returned status from your script.
  • I would check if all those files are executable.

Upvotes: 0

Related Questions