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