]> granicus.if.org Git - icinga2/blob - lib/livestatus/statehisttable.cpp
Docs: Explain across midnight time periods with an overlapping range
[icinga2] / lib / livestatus / statehisttable.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
3 #include "livestatus/statehisttable.hpp"
4 #include "livestatus/livestatuslogutility.hpp"
5 #include "livestatus/hoststable.hpp"
6 #include "livestatus/servicestable.hpp"
7 #include "livestatus/contactstable.hpp"
8 #include "livestatus/commandstable.hpp"
9 #include "icinga/icingaapplication.hpp"
10 #include "icinga/cib.hpp"
11 #include "icinga/service.hpp"
12 #include "icinga/host.hpp"
13 #include "icinga/user.hpp"
14 #include "icinga/checkcommand.hpp"
15 #include "icinga/eventcommand.hpp"
16 #include "icinga/notificationcommand.hpp"
17 #include "base/convert.hpp"
18 #include "base/utility.hpp"
19 #include "base/logger.hpp"
20 #include "base/application.hpp"
21 #include "base/objectlock.hpp"
22 #include <boost/algorithm/string.hpp>
23 #include <boost/algorithm/string/replace.hpp>
24 #include <boost/algorithm/string/predicate.hpp>
25 #include <fstream>
26
27 using namespace icinga;
28
29 StateHistTable::StateHistTable(const String& compat_log_path, time_t from, time_t until)
30 {
31         /* store attributes for FetchRows */
32         m_TimeFrom = from;
33         m_TimeUntil = until;
34         m_CompatLogPath = compat_log_path;
35
36         AddColumns(this);
37 }
38
39 void StateHistTable::UpdateLogEntries(const Dictionary::Ptr& log_entry_attrs, int line_count, int lineno, const AddRowFunction& addRowFn)
40 {
41         unsigned int time = log_entry_attrs->Get("time");
42         String host_name = log_entry_attrs->Get("host_name");
43         String service_description = log_entry_attrs->Get("service_description");
44         unsigned long state = log_entry_attrs->Get("state");
45         int log_type = log_entry_attrs->Get("log_type");
46         String state_type = log_entry_attrs->Get("state_type"); //SOFT, HARD, STARTED, STOPPED, ...
47         String log_line = log_entry_attrs->Get("message"); /* use message from log table */
48
49         Checkable::Ptr checkable;
50
51         if (service_description.IsEmpty())
52                 checkable = Host::GetByName(host_name);
53         else
54                 checkable = Service::GetByNamePair(host_name, service_description);
55
56         /* invalid log line for state history */
57         if (!checkable)
58                 return;
59
60         Array::Ptr state_hist_service_states;
61         Dictionary::Ptr state_hist_bag;
62         unsigned long query_part = m_TimeUntil - m_TimeFrom;
63
64         /* insert new service states array with values if not existing */
65         if (m_CheckablesCache.find(checkable) == m_CheckablesCache.end()) {
66
67                 /* create new values */
68                 state_hist_service_states = new Array();
69                 state_hist_bag = new Dictionary();
70
71                 Service::Ptr service = dynamic_pointer_cast<Service>(checkable);
72                 Host::Ptr host;
73
74                 if (service)
75                         host = service->GetHost();
76                 else
77                         host = static_pointer_cast<Host>(checkable);
78
79                 state_hist_bag->Set("host_name", host->GetName());
80
81                 if (service)
82                         state_hist_bag->Set("service_description", service->GetShortName());
83
84                 state_hist_bag->Set("state", state);
85                 state_hist_bag->Set("in_downtime", 0);
86                 state_hist_bag->Set("in_host_downtime", 0);
87                 state_hist_bag->Set("in_notification_period", 1); // assume "always"
88                 state_hist_bag->Set("is_flapping", 0);
89                 state_hist_bag->Set("time", time);
90                 state_hist_bag->Set("lineno", lineno);
91                 state_hist_bag->Set("log_output", log_line); /* complete line */
92                 state_hist_bag->Set("from", time); /* starting at current timestamp */
93                 state_hist_bag->Set("until", time); /* will be updated later on state change */
94                 state_hist_bag->Set("query_part", query_part); /* required for _part calculations */
95
96                 state_hist_service_states->Add(state_hist_bag);
97
98                 Log(LogDebug, "StateHistTable")
99                         << "statehist: Adding new object '" << checkable->GetName() << "' to services cache.";
100         } else {
101                 state_hist_service_states = m_CheckablesCache[checkable];
102                 state_hist_bag = state_hist_service_states->Get(state_hist_service_states->GetLength()-1); /* fetch latest state from history */
103
104                 /* state duration */
105
106                 /* determine service notifications notification_period and compare against current timestamp */
107                 bool in_notification_period = true;
108                 String notification_period_name;
109                 for (const Notification::Ptr& notification : checkable->GetNotifications()) {
110                         TimePeriod::Ptr notification_period = notification->GetPeriod();
111
112                         if (notification_period) {
113                                 if (notification_period->IsInside(static_cast<double>(time)))
114                                         in_notification_period = true;
115                                 else
116                                         in_notification_period = false;
117
118                                 notification_period_name = notification_period->GetName(); // last one wins
119                         } else
120                                 in_notification_period = true; // assume "always"
121                 }
122
123                 /* check for state changes, flapping & downtime start/end */
124                 switch (log_type) {
125                         case LogEntryTypeHostAlert:
126                         case LogEntryTypeHostInitialState:
127                         case LogEntryTypeHostCurrentState:
128                         case LogEntryTypeServiceAlert:
129                         case LogEntryTypeServiceInitialState:
130                         case LogEntryTypeServiceCurrentState:
131                                 if (state != state_hist_bag->Get("state")) {
132                                         /* 1. seal old state_hist_bag */
133                                         state_hist_bag->Set("until", time); /* add until record for duration calculation */
134
135                                         /* 2. add new state_hist_bag */
136                                         Dictionary::Ptr state_hist_bag_new = new Dictionary();
137
138                                         state_hist_bag_new->Set("host_name", state_hist_bag->Get("host_name"));
139                                         state_hist_bag_new->Set("service_description", state_hist_bag->Get("service_description"));
140                                         state_hist_bag_new->Set("state", state);
141                                         state_hist_bag_new->Set("in_downtime", state_hist_bag->Get("in_downtime")); // keep value from previous state!
142                                         state_hist_bag_new->Set("in_host_downtime", state_hist_bag->Get("in_host_downtime")); // keep value from previous state!
143                                         state_hist_bag_new->Set("in_notification_period", (in_notification_period ? 1 : 0));
144                                         state_hist_bag_new->Set("notification_period", notification_period_name);
145                                         state_hist_bag_new->Set("is_flapping", state_hist_bag->Get("is_flapping")); // keep value from previous state!
146                                         state_hist_bag_new->Set("time", time);
147                                         state_hist_bag_new->Set("lineno", lineno);
148                                         state_hist_bag_new->Set("log_output", log_line); /* complete line */
149                                         state_hist_bag_new->Set("from", time); /* starting at current timestamp */
150                                         state_hist_bag_new->Set("until", time + 1); /* will be updated later */
151                                         state_hist_bag_new->Set("query_part", query_part);
152
153                                         state_hist_service_states->Add(state_hist_bag_new);
154
155                                         Log(LogDebug, "StateHistTable")
156                                                 << "statehist: State change detected for object '" << checkable->GetName() << "' in '" << log_line << "'.";
157                                 }
158                                 break;
159                         case LogEntryTypeHostFlapping:
160                         case LogEntryTypeServiceFlapping:
161                                 if (state_type == "STARTED")
162                                         state_hist_bag->Set("is_flapping", 1);
163                                 else if (state_type == "STOPPED" || state_type == "DISABLED")
164                                         state_hist_bag->Set("is_flapping", 0);
165                                 break;
166                                 break;
167                         case LogEntryTypeHostDowntimeAlert:
168                         case LogEntryTypeServiceDowntimeAlert:
169                                 if (state_type == "STARTED") {
170                                         state_hist_bag->Set("in_downtime", 1);
171                                         if (log_type == LogEntryTypeHostDowntimeAlert)
172                                                 state_hist_bag->Set("in_host_downtime", 1);
173                                 }
174                                 else if (state_type == "STOPPED" || state_type == "CANCELLED") {
175                                         state_hist_bag->Set("in_downtime", 0);
176                                         if (log_type == LogEntryTypeHostDowntimeAlert)
177                                                 state_hist_bag->Set("in_host_downtime", 0);
178                                 }
179                                 break;
180                         default:
181                                 //nothing to update
182                                 break;
183                 }
184
185         }
186
187         m_CheckablesCache[checkable] = state_hist_service_states;
188
189         /* TODO find a way to directly call addRowFn() - right now m_ServicesCache depends on historical lines ("already seen service") */
190 }
191
192 void StateHistTable::AddColumns(Table *table, const String& prefix,
193         const Column::ObjectAccessor& objectAccessor)
194 {
195         table->AddColumn(prefix + "time", Column(&StateHistTable::TimeAccessor, objectAccessor));
196         table->AddColumn(prefix + "lineno", Column(&StateHistTable::LinenoAccessor, objectAccessor));
197         table->AddColumn(prefix + "from", Column(&StateHistTable::FromAccessor, objectAccessor));
198         table->AddColumn(prefix + "until", Column(&StateHistTable::UntilAccessor, objectAccessor));
199         table->AddColumn(prefix + "duration", Column(&StateHistTable::DurationAccessor, objectAccessor));
200         table->AddColumn(prefix + "duration_part", Column(&StateHistTable::DurationPartAccessor, objectAccessor));
201         table->AddColumn(prefix + "state", Column(&StateHistTable::StateAccessor, objectAccessor));
202         table->AddColumn(prefix + "host_down", Column(&StateHistTable::HostDownAccessor, objectAccessor));
203         table->AddColumn(prefix + "in_downtime", Column(&StateHistTable::InDowntimeAccessor, objectAccessor));
204         table->AddColumn(prefix + "in_host_downtime", Column(&StateHistTable::InHostDowntimeAccessor, objectAccessor));
205         table->AddColumn(prefix + "is_flapping", Column(&StateHistTable::IsFlappingAccessor, objectAccessor));
206         table->AddColumn(prefix + "in_notification_period", Column(&StateHistTable::InNotificationPeriodAccessor, objectAccessor));
207         table->AddColumn(prefix + "notification_period", Column(&StateHistTable::NotificationPeriodAccessor, objectAccessor));
208         table->AddColumn(prefix + "debug_info", Column(&Table::EmptyStringAccessor, objectAccessor));
209         table->AddColumn(prefix + "host_name", Column(&StateHistTable::HostNameAccessor, objectAccessor));
210         table->AddColumn(prefix + "service_description", Column(&StateHistTable::ServiceDescriptionAccessor, objectAccessor));
211         table->AddColumn(prefix + "log_output", Column(&StateHistTable::LogOutputAccessor, objectAccessor));
212         table->AddColumn(prefix + "duration_ok", Column(&StateHistTable::DurationOkAccessor, objectAccessor));
213         table->AddColumn(prefix + "duration_part_ok", Column(&StateHistTable::DurationPartOkAccessor, objectAccessor));
214         table->AddColumn(prefix + "duration_warning", Column(&StateHistTable::DurationWarningAccessor, objectAccessor));
215         table->AddColumn(prefix + "duration_part_warning", Column(&StateHistTable::DurationPartWarningAccessor, objectAccessor));
216         table->AddColumn(prefix + "duration_critical", Column(&StateHistTable::DurationCriticalAccessor, objectAccessor));
217         table->AddColumn(prefix + "duration_part_critical", Column(&StateHistTable::DurationPartCriticalAccessor, objectAccessor));
218         table->AddColumn(prefix + "duration_unknown", Column(&StateHistTable::DurationUnknownAccessor, objectAccessor));
219         table->AddColumn(prefix + "duration_part_unknown", Column(&StateHistTable::DurationPartUnknownAccessor, objectAccessor));
220         table->AddColumn(prefix + "duration_unmonitored", Column(&StateHistTable::DurationUnmonitoredAccessor, objectAccessor));
221         table->AddColumn(prefix + "duration_part_unmonitored", Column(&StateHistTable::DurationPartUnmonitoredAccessor, objectAccessor));
222
223         HostsTable::AddColumns(table, "current_host_", std::bind(&StateHistTable::HostAccessor, _1, objectAccessor));
224         ServicesTable::AddColumns(table, "current_service_", std::bind(&StateHistTable::ServiceAccessor, _1, objectAccessor));
225 }
226
227 String StateHistTable::GetName() const
228 {
229         return "log";
230 }
231
232 String StateHistTable::GetPrefix() const
233 {
234         return "log";
235 }
236
237 void StateHistTable::FetchRows(const AddRowFunction& addRowFn)
238 {
239         Log(LogDebug, "StateHistTable")
240                 << "Pre-selecting log file from " << m_TimeFrom << " until " << m_TimeUntil;
241
242         /* create log file index */
243         LivestatusLogUtility::CreateLogIndex(m_CompatLogPath, m_LogFileIndex);
244
245         /* generate log cache */
246         LivestatusLogUtility::CreateLogCache(m_LogFileIndex, this, m_TimeFrom, m_TimeUntil, addRowFn);
247
248         Checkable::Ptr checkable;
249
250         for (const auto& kv : m_CheckablesCache) {
251                 for (const Dictionary::Ptr& state_hist_bag : kv.second) {
252                         /* pass a dictionary from state history array */
253                         if (!addRowFn(state_hist_bag, LivestatusGroupByNone, Empty))
254                                 return;
255                 }
256         }
257 }
258
259 Object::Ptr StateHistTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
260 {
261         String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
262
263         if (host_name.IsEmpty())
264                 return nullptr;
265
266         return Host::GetByName(host_name);
267 }
268
269 Object::Ptr StateHistTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
270 {
271         String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
272         String service_description = static_cast<Dictionary::Ptr>(row)->Get("service_description");
273
274         if (service_description.IsEmpty() || host_name.IsEmpty())
275                 return nullptr;
276
277         return Service::GetByNamePair(host_name, service_description);
278 }
279
280 Value StateHistTable::TimeAccessor(const Value& row)
281 {
282         return static_cast<Dictionary::Ptr>(row)->Get("time");
283 }
284
285 Value StateHistTable::LinenoAccessor(const Value& row)
286 {
287         return static_cast<Dictionary::Ptr>(row)->Get("lineno");
288 }
289
290 Value StateHistTable::FromAccessor(const Value& row)
291 {
292         return static_cast<Dictionary::Ptr>(row)->Get("from");
293 }
294
295 Value StateHistTable::UntilAccessor(const Value& row)
296 {
297         return static_cast<Dictionary::Ptr>(row)->Get("until");
298 }
299
300 Value StateHistTable::DurationAccessor(const Value& row)
301 {
302         Dictionary::Ptr state_hist_bag = static_cast<Dictionary::Ptr>(row);
303
304         return (state_hist_bag->Get("until") - state_hist_bag->Get("from"));
305 }
306
307 Value StateHistTable::DurationPartAccessor(const Value& row)
308 {
309         Dictionary::Ptr state_hist_bag = static_cast<Dictionary::Ptr>(row);
310
311         return (state_hist_bag->Get("until") - state_hist_bag->Get("from")) / state_hist_bag->Get("query_part");
312 }
313
314 Value StateHistTable::StateAccessor(const Value& row)
315 {
316         return static_cast<Dictionary::Ptr>(row)->Get("state");
317 }
318
319 Value StateHistTable::HostDownAccessor(const Value& row)
320 {
321         return static_cast<Dictionary::Ptr>(row)->Get("host_down");
322 }
323
324 Value StateHistTable::InDowntimeAccessor(const Value& row)
325 {
326         return static_cast<Dictionary::Ptr>(row)->Get("in_downtime");
327 }
328
329 Value StateHistTable::InHostDowntimeAccessor(const Value& row)
330 {
331         return static_cast<Dictionary::Ptr>(row)->Get("in_host_downtime");
332 }
333
334 Value StateHistTable::IsFlappingAccessor(const Value& row)
335 {
336         return static_cast<Dictionary::Ptr>(row)->Get("is_flapping");
337 }
338
339 Value StateHistTable::InNotificationPeriodAccessor(const Value& row)
340 {
341         return static_cast<Dictionary::Ptr>(row)->Get("in_notification_period");
342 }
343
344 Value StateHistTable::NotificationPeriodAccessor(const Value& row)
345 {
346         return static_cast<Dictionary::Ptr>(row)->Get("notification_period");
347 }
348
349 Value StateHistTable::HostNameAccessor(const Value& row)
350 {
351         return static_cast<Dictionary::Ptr>(row)->Get("host_name");
352 }
353
354 Value StateHistTable::ServiceDescriptionAccessor(const Value& row)
355 {
356         return static_cast<Dictionary::Ptr>(row)->Get("service_description");
357 }
358
359 Value StateHistTable::LogOutputAccessor(const Value& row)
360 {
361         return static_cast<Dictionary::Ptr>(row)->Get("log_output");
362 }
363
364 Value StateHistTable::DurationOkAccessor(const Value& row)
365 {
366         Dictionary::Ptr state_hist_bag = static_cast<Dictionary::Ptr>(row);
367
368         if (state_hist_bag->Get("state") == ServiceOK)
369                 return (state_hist_bag->Get("until") - state_hist_bag->Get("from"));
370
371         return 0;
372 }
373
374 Value StateHistTable::DurationPartOkAccessor(const Value& row)
375 {
376         Dictionary::Ptr state_hist_bag = static_cast<Dictionary::Ptr>(row);
377
378         if (state_hist_bag->Get("state") == ServiceOK)
379                 return (state_hist_bag->Get("until") - state_hist_bag->Get("from")) / state_hist_bag->Get("query_part");
380
381         return 0;
382 }
383
384 Value StateHistTable::DurationWarningAccessor(const Value& row)
385 {
386         Dictionary::Ptr state_hist_bag = static_cast<Dictionary::Ptr>(row);
387
388         if (state_hist_bag->Get("state") == ServiceWarning)
389                 return (state_hist_bag->Get("until") - state_hist_bag->Get("from"));
390
391         return 0;
392 }
393
394 Value StateHistTable::DurationPartWarningAccessor(const Value& row)
395 {
396         Dictionary::Ptr state_hist_bag = static_cast<Dictionary::Ptr>(row);
397
398         if (state_hist_bag->Get("state") == ServiceWarning)
399                 return (state_hist_bag->Get("until") - state_hist_bag->Get("from")) / state_hist_bag->Get("query_part");
400
401         return 0;
402 }
403
404 Value StateHistTable::DurationCriticalAccessor(const Value& row)
405 {
406         Dictionary::Ptr state_hist_bag = static_cast<Dictionary::Ptr>(row);
407
408         if (state_hist_bag->Get("state") == ServiceCritical)
409                 return (state_hist_bag->Get("until") - state_hist_bag->Get("from"));
410
411         return 0;
412 }
413
414 Value StateHistTable::DurationPartCriticalAccessor(const Value& row)
415 {
416         Dictionary::Ptr state_hist_bag = static_cast<Dictionary::Ptr>(row);
417
418         if (state_hist_bag->Get("state") == ServiceCritical)
419                 return (state_hist_bag->Get("until") - state_hist_bag->Get("from")) / state_hist_bag->Get("query_part");
420
421         return 0;
422 }
423
424 Value StateHistTable::DurationUnknownAccessor(const Value& row)
425 {
426         Dictionary::Ptr state_hist_bag = static_cast<Dictionary::Ptr>(row);
427
428         if (state_hist_bag->Get("state") == ServiceUnknown)
429                 return (state_hist_bag->Get("until") - state_hist_bag->Get("from"));
430
431         return 0;
432 }
433
434 Value StateHistTable::DurationPartUnknownAccessor(const Value& row)
435 {
436         Dictionary::Ptr state_hist_bag = static_cast<Dictionary::Ptr>(row);
437
438         if (state_hist_bag->Get("state") == ServiceUnknown)
439                 return (state_hist_bag->Get("until") - state_hist_bag->Get("from")) / state_hist_bag->Get("query_part");
440
441         return 0;
442 }
443
444 Value StateHistTable::DurationUnmonitoredAccessor(const Value& row)
445 {
446         Dictionary::Ptr state_hist_bag = static_cast<Dictionary::Ptr>(row);
447
448         if (state_hist_bag->Get("state") == -1)
449                 return (state_hist_bag->Get("until") - state_hist_bag->Get("from"));
450
451         return 0;
452 }
453
454 Value StateHistTable::DurationPartUnmonitoredAccessor(const Value& row)
455 {
456         Dictionary::Ptr state_hist_bag = static_cast<Dictionary::Ptr>(row);
457
458         if (state_hist_bag->Get("state") == -1)
459                 return (state_hist_bag->Get("until") - state_hist_bag->Get("from")) / state_hist_bag->Get("query_part");
460
461         return 0;
462 }