Overriding monit alerts with mail-format on a per check basis

monit

I currently have two monit checks for each host I have running HTTPS:

check host www-example-https with address www.example.org
  if failed
    port 443
    protocol https
  then alert

check host www-example-certificate with address www.example.org
  every "25 10 * * *"
  if failed
    port 443
    protocol https
    and certificate valid > 30 days
  then alert

The first one warns me of a failure to connect at all, and runs every 2 minutes, whereas the second reminds me that a certificate has fewer that 30 days until expiry and only runs once a day.

At the moment both checks result in a similar alert message, which means that a certificate nearing expiry looks like a complete failure of HTTPS. I'd therefore like to override the default mail options on a per-alert basis.

I know I can do this with set mail-format if I want to change all alerts, but I can't figure out the syntax for a single alert if I'm using an if failed ... then alert block, and I can't find any examples of this specific use case in the manual.

Is it possible to override mail-format on a per-alert basis for the types of alerts I've defined above?

Best Answer

There is a sample in the documentation here https://mmonit.com/monit/documentation/#CONFIGURATION-EXAMPLES

From personnal test, you cannot avoid the "alert a@b.c" part, so to resume, to fit your needs, it leads to:

check host www-example-https with address www.example.org
  if failed
    port 443
    protocol https
  then alert
  alert me@example.com with mail-format {     # use local format
     subject: https is down on www.example.org
     message: https is down on www.example.org with port 443
  Yours sincerely,
  monit
  }


check host www-example-certificate with address www.example.org
  every "25 10 * * *"
  if failed
    port 443
    protocol https
    and certificate valid > 30 days
  then alert
  alert metwo@exmaple.com with mail-format  {     # use local format
     subject: https certificate expiration for www.example.org
     message: https is certificate is less than 30 days  on www.example.org with port 443
  $SERVICE $EVENT at $DATE
  Monit $ACTION $SERVICE at $DATE on $HOST: $DESCRIPTION.
  Yours Pal,
  MoMoMonit
  }

Also it seems that if host is "failed", the certificate check will not be performed.

Using a "set mail-format" anywhere, change it for ALL notifications

Related Topic