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