bearrito
bearrito

Reputation: 2315

Monit IF/ELSE syntax

I attempting to monitor a service. My monit definition is below.

When I invoke monit -r I receive /etc/monit/conf.d/authentication.monit:10: syntax error 'else'

check host self with address myhost
start program = "/usr/bin/service start authentication"
stop program  = "/usr/bin/service stop  authentication"
if failed port 443 protocol https
  request /
  with timeout 5 seconds
  for 2 cycles
  then restart
if 1 restarts within 4 cycles then exec "/etc/monit/pagerduty-trigger authentication" else if passed for 2 cycles then exec "/etc/monit/pagerduty-resolve authentication"

All the documentation seems to indicate my syntax is correct.

I am attempting to follow the two docs

pagerduty

primary docs

Upvotes: 1

Views: 5536

Answers (2)

David Hall
David Hall

Reputation: 351

I'm not sure if you are still looking at this. I am also integrating Monit with Pagerduty. I have one simpler example which is working. What I note is the 'else' appears to only support "else if succeeded" . I think it is simply a long hand version of 'else' without to ability to add a more sophisticated expression that you are attempting.

Here is my example which triggers when service ( process in monit ) does not exist and resolves when it does.

check process tomcat8 with pidfile /var/run/tomcat8.pid
    if not exist
    then exec "/etc/monit/pagerduty-trigger tomcat8"
    else if succeeded then exec "/etc/monit/pagerduty-resolve tomcat8"

Upvotes: 1

TheCodeKiller
TheCodeKiller

Reputation: 1965

Syntax is :

IF test THEN action [ELSE IF SUCCEEDED THEN *action]

Also

The "if x restarts within y cycles then ..." statement doesn't support the "else" part: https://mmonit.com/monit/documentation/monit.html#SERVICE-RESTART-LIMIT

Else for restart is a bit non-sense as there is no contrario for restart

I'm trying to get your logic here for the if then else actions but don't understand the else part.

Here you want to "stop then "start" service "authentication when https://myhost:443/ is failing two times (called T0) Then on the next cycle, you want to run script /etc/monit/pagerduty-trigger authentication. (called T0 + 1 cycle) Here why within 4 cycles but not less like 2, bot ok.

I suppose that on the T0 + 1 + 2 cycles, if service a online again, you want to run "/etc/monit/pagerduty-resolve authentication"

One solution is to handle it at your failed test level with custom scripts

if failed port 443 protocol https request / with timeout 5 seconds for 2 cycles then exec "/var/lib/monit/scripts/notifyAndExecute.sh" else if succeeded then exec "/etc/monit/pagerduty-resolve authentication"

create file /var/lib/monit/scripts/notifyAndExecute.sh in charge of restarting and calling /etc/monit/pagerduty-trigger authentication

Upvotes: 2

Related Questions