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