1 diff --git a/common/downtime.c b/common/downtime.c
2 --- a/common/downtime.c
3 +++ b/common/downtime.c
4 @@ -360,6 +360,14 @@ int register_downtime(int type, unsigned long downtime_id) {
8 + /* If the downtime is triggered and was in effect, mark it as not in
9 + effect so it gets scheduled correctly */
10 + if((temp_downtime->triggered_by != 0) &&
11 + (TRUE == temp_downtime->is_in_effect)) {
12 + was_in_effect = temp_downtime->is_in_effect;
13 + temp_downtime->is_in_effect = FALSE;
16 if((FALSE == temp_downtime->fixed) && (FALSE == was_in_effect)) {
17 /* increment pending flex downtime counter */
18 if(temp_downtime->type == HOST_DOWNTIME)
19 @@ -1111,6 +1119,39 @@ int add_downtime(int downtime_type, char *host_name, char *svc_description, time
20 static int downtime_compar(const void *p1, const void *p2) {
21 scheduled_downtime *d1 = *(scheduled_downtime **)p1;
22 scheduled_downtime *d2 = *(scheduled_downtime **)p2;
25 + If the start times of two downtimes are equal and one is triggered but
26 + but the other is not, the triggered downtime should be later in the
27 + list than the untriggered one. This is so they are written to the
28 + retention.dat and status.dat in the correct order.
30 + Previously the triggered downtime always appeared before its
31 + triggering downtime in those files. When the downtimes were read
32 + from those files, either on a core restart or by the CGIs, the
33 + triggered downtime would be discarded because the triggering
34 + downtime did not yet exist.
36 + The most common case for this is when a downtime is created and
37 + the option is selected to create triggered downtimes on all child
38 + objects. This change in the sort order does NOT resolve the
39 + case where a manually created, triggered downtime is created with
40 + a start time earlier than the triggering downtime.
42 + This would need to be resolved by comparing the triggered_by value
43 + with the downtime ID regardless of the start time. However, this
44 + should be a relatively rare case and only caused by intentional
45 + scheduling by a human. This change was not implemented because it
46 + would cause the downtime list to be out of time order and the
47 + implications of this were not well understood.
50 + if(d1->start_time == d2->start_time) {
51 + if(( d1->triggered_by == 0 && d2->triggered_by != 0) ||
52 + ( d1->triggered_by != 0 && d2->triggered_by == 0)) {
53 + return d1->triggered_by == 0 ? -1 : 1;
56 return (d1->start_time < d2->start_time) ? -1 : (d1->start_time - d2->start_time);