Host Downtime Duration Notification

How can I display the duration of an host’s downtime in the email notification subject line?

$HOSTDURATION$ isn’t working

My version is: Checkmk Community (formerly Raw) 2.5.0p5

1 Like

Following - same issue.

I guess it depends on what you want exactly. But $HOSTDURATION$ renders empty because it’s a macro that isn’t populated in the notification context. What could be closest to your preferred solution is a subject like “Check_MK: $HOSTNAME$ is $HOSTSTATE$ since $LASTHOSTSTATECHANGE_REL$”.

One important caveat is that $LASTHOSTSTATECHANGE_REL$ is simply the “time since the last state change,” which equals the current down‑duration only for problem notifications. On a recovery the state just changed to UP, so this value resets to ~0 and won’t tell you how long the host was down. There’s no context variable that hands you the total elapsed downtime as a ready string for the recovery case. But you could also use Downtime Start/End notifications, which then sends a notification when the downtime started & when it ended with the event date/time.

1 Like

Thank you for your reply.

I tried setting the $LASTHOSTSTATECHANGE_REL$ variable, but the result is always:

is UP from 0d 00:00:00 or

is DOWN from 0d 00:00:00

It is not being populated.

1 Like

Ok i see, since the notification is sent when the state changed (eg. from UP to DOWN) the $LASTHOSTSTATECHANGE_REL$ is 0 for the initial Down-notification. Could you maybe elaborate more, whats the reason you need the time in there, maybe that makes it easier to find a solution for it.

Hi Monika, It’s very useful for getting an immediate overview of the situation without having to open the console or check your email.

In Zabbix, I have a configuration where the email subject line is descriptive, like:

RESOLVED: HOST1 - Unavailable by ICMP ping - Resolved after 4m 0s
PROBLEM: HOST1 - Unavailable by ICMP ping - Severity: High

Hi @marste,

let me summarise where this stands, because the short answer is unfortunately “not with the built-in variables” — but there is a way to get what you want.

Why nothing you tried works

$HOSTDURATION$ is a Nagios variable that Checkmk never adopted. It is not part of the information Checkmk passes to a notification, and anything Checkmk does not
recognise is simply left out of the mail — which is why you saw no error, just nothing.

$LASTHOSTSTATECHANGE_REL$ does exist, but it means “how long ago did the state change that caused this mail”. A notification is always sent right after a state
change, so the answer is practically always zero. It only becomes interesting if you let Checkmk repeat notifications for an ongoing problem — the second and later
mails then show how long the problem has been running.

And that is also the core of the issue for your use case: when the host comes back up, the mail is about the change to UP. The moment the problem started is no
longer part of the information Checkmk sends along, so no variable can tell you “was down for 4 minutes”. @andreas-doehler came to the same conclusion in an older
thread
, and the wish has come up repeatedly over the years.

(One clarification on an earlier suggestion: the Downtime start/end notifications are about scheduled downtimes, i.e. planned maintenance — not about how long a
host was unreachable.)

How you can get it anyway

The duration can be calculated at the moment the mail is created: remember the time when the problem mail goes out, and subtract it when the recovery mail goes out.
That happens in a small extension of the e-mail notification, and you keep all your normal HTML mail settings in the GUI. Afterwards you can write something like

Checkmk: $HOSTNAME$ - $EVENT_TXT$ (resolved after $PROBLEMDURATION$)

in the subject field of your notification rule and get exactly the line you sketched.

And what we will do

I do think Checkmk should be able to do this out of the box: how long a problem lasted belongs in a recovery notification. I will bring this up internally as a
feature request and report back here.

Best regards,
Benni

1 Like