# Duration in recovery notifications

**URL:** <https://community.icinga.com/t/duration-in-recovery-notifications/646>\
**Category:** Icinga 2\
**Created:** [March 19, 2019, 5:51pm UTC](https://community.icinga.com/t/duration-in-recovery-notifications/646 "2019-03-19T17:51:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![MDKamijo](https://community.icinga.com/user_avatar/community.icinga.com/mdkamijo/32/1404_2.png) [@MDKamijo](https://community.icinga.com/u/MDKamijo)\
**Post date:** [March 19, 2019, 5:51pm UTC](https://community.icinga.com/t/duration-in-recovery-notifications/646/1 "2019-03-19T17:51:13Z")

</div>

I want to have the duration time from non-ok state to ok state in my notification emails. I’ve tried a bunch of “last\_state…” variables but none of them are pretty use full. Do any of you guys out there have any ideas of how to solve that?

What I want to do is to have a row in the emails saying:  
Problem duration: 1h 2m 32s

…or something like that. I thought I could use service.duration\_sec but that doesn’t work since that will be “duration” since last state change and that state changes is from non-ok to ok when sending out a recovery notification.

---

<div class="post-metadata">

**Author:** ![aflatto](https://community.icinga.com/user_avatar/community.icinga.com/aflatto/32/160_2.png) [@aflatto](https://community.icinga.com/u/aflatto)\
**Post date:** [March 20, 2019, 7:19am UTC](https://community.icinga.com/t/duration-in-recovery-notifications/646/2 "2019-03-20T07:19:22Z")

</div>

Hello and welcome.

AFAIK there is no direct way to do this, but maybe you can get that data by querying the database for the time of the issues first notification and then do a calculation of the duration to the recovery state change?  
you will have to change the notification scripts and it will slow them down ( not to mention add load on the DB) , but this is one solution I can think of that will give you what you want.

---

<div class="post-metadata">

**Author:** ![dnsmichi](https://community.icinga.com/user_avatar/community.icinga.com/dnsmichi/32/7721_2.png) [@dnsmichi](https://community.icinga.com/u/dnsmichi)\
**Post date:** [March 20, 2019, 7:27am UTC](https://community.icinga.com/t/duration-in-recovery-notifications/646/3 "2019-03-20T07:27:10Z")

</div>

Hi,

should it be the first not-ok timestamp for SOFT or HARD states - the latter is when the first problem notification is sent. That’s not implemented in the core, but I’d like to figure out whether this qualifies for a feature request (cc @bsheqa).

Cheers,  
Michael

---

<div class="post-metadata">

**Author:** ![MDKamijo](https://community.icinga.com/user_avatar/community.icinga.com/mdkamijo/32/1404_2.png) [@MDKamijo](https://community.icinga.com/u/MDKamijo)\
**Post date:** [March 20, 2019, 7:36am UTC](https://community.icinga.com/t/duration-in-recovery-notifications/646/4 "2019-03-20T07:36:13Z")

</div>

Thanks for the answer! I have also thought about that. But for now I think I will handle this in a slightly different way. When the first notification is fired off for a service the notification script will save the start time to a table in the db. Then when the recovery for the same service shows up the start time will be fetched and and the duration will be calculated. That’s the short version 🙂 Might not be the best of solutions but a pretty easy one at least.

---

<div class="post-metadata">

**Author:** ![MDKamijo](https://community.icinga.com/user_avatar/community.icinga.com/mdkamijo/32/1404_2.png) [@MDKamijo](https://community.icinga.com/u/MDKamijo)\
**Post date:** [March 20, 2019, 7:38am UTC](https://community.icinga.com/t/duration-in-recovery-notifications/646/5 "2019-03-20T07:38:20Z")

</div>

Sounds like something that could be really good. If we could have that kind of timestamp things would be really nice and easy to handle.

---

<div class="post-metadata">

**Author:** ![dnsmichi](https://community.icinga.com/user_avatar/community.icinga.com/dnsmichi/32/7721_2.png) [@dnsmichi](https://community.icinga.com/u/dnsmichi)\
**Post date:** [March 20, 2019, 7:46am UTC](https://community.icinga.com/t/duration-in-recovery-notifications/646/6 "2019-03-20T07:46:52Z")

</div>

Please clarify first, which scenario exactly is needed, e.g. with a timeline picture.

---

<div class="post-metadata">

**Author:** ![MDKamijo](https://community.icinga.com/user_avatar/community.icinga.com/mdkamijo/32/1404_2.png) [@MDKamijo](https://community.icinga.com/u/MDKamijo)\
**Post date:** [April 3, 2019, 11:06am UTC](https://community.icinga.com/t/duration-in-recovery-notifications/646/7 "2019-04-03T11:06:52Z")

</div>

Sorry for the late reply…  
I’m not really sure what you mean with “timeline picutre” but I’ll try to explain here.

1. Let’s say we have a bunch of services for which we are sending notifications to a timeseries database like InfluxDB. On every notification we are sending measurement points to InfluxDB with a few tags like ‘problem\_type’, ‘owner’, ‘environment’ etc.

2. A service becomes critical and we send something like the following to InfluxDB: `notifications,service=the_check,owner=myteam,zone=eu,site=london,host=the-host,notification_type=PROBLEM,state=CRITICAL duration=0 1554288881`

3. A while later someone is acknowledging the problem. Then we send a new measurement point to InfluxDB almost same as in 2. but this time we add duration since the last state change. So the line might look like this:  
`notifications,service=the_check,owner=myteam,zone=eu,site=london,host=the-host,notification_type=ACKNOWLEDGEMENT,state=CRITICAL duration=637 1554289548`

4. Later on when the problem have recovered and a recovery notification is sent out we send the total duration:  
`notifications,service=the_check,owner=myteam,zone=eu,site=london,host=the-host,notification_type=ACKNOWLEDGEMENT,state=CRITICAL duration=2943 1554292491`

This is not only for this kind of notifications could be really great to include in the notification emails, slack messages or what ever you are using. Why not in the GUI as well so when you check the service / host details you could see how long the service / host have been in the current state.

The issue now is that we need to calculate the “duration” on our own now. For the moment we are using a database table with every notification in. So when we send a notification we add that notification with the receiver and start time to a line. Then when that problem the notification belonged to we use the start time saved in the db table and calculate the duration. That’s the short and lazy description of how we do it.

If there was a variable containing the duration of the last problem it would make life a lot easier.
