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