1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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. *
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. *
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 ******************************************************************************/
20 #include "livestatus/logtable.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>
45 using namespace icinga;
47 LogTable::LogTable(const String& compat_log_path, time_t from, time_t until)
49 /* store attributes for FetchRows */
52 m_CompatLogPath = compat_log_path;
57 void LogTable::AddColumns(Table *table, const String& prefix,
58 const Column::ObjectAccessor& objectAccessor)
60 table->AddColumn(prefix + "time", Column(&LogTable::TimeAccessor, objectAccessor));
61 table->AddColumn(prefix + "lineno", Column(&LogTable::LinenoAccessor, objectAccessor));
62 table->AddColumn(prefix + "class", Column(&LogTable::ClassAccessor, objectAccessor));
63 table->AddColumn(prefix + "message", Column(&LogTable::MessageAccessor, objectAccessor));
64 table->AddColumn(prefix + "type", Column(&LogTable::TypeAccessor, objectAccessor));
65 table->AddColumn(prefix + "options", Column(&LogTable::OptionsAccessor, objectAccessor));
66 table->AddColumn(prefix + "comment", Column(&LogTable::CommentAccessor, objectAccessor));
67 table->AddColumn(prefix + "plugin_output", Column(&LogTable::PluginOutputAccessor, objectAccessor));
68 table->AddColumn(prefix + "state", Column(&LogTable::StateAccessor, objectAccessor));
69 table->AddColumn(prefix + "state_type", Column(&LogTable::StateTypeAccessor, objectAccessor));
70 table->AddColumn(prefix + "attempt", Column(&LogTable::AttemptAccessor, objectAccessor));
71 table->AddColumn(prefix + "service_description", Column(&LogTable::ServiceDescriptionAccessor, objectAccessor));
72 table->AddColumn(prefix + "host_name", Column(&LogTable::HostNameAccessor, objectAccessor));
73 table->AddColumn(prefix + "contact_name", Column(&LogTable::ContactNameAccessor, objectAccessor));
74 table->AddColumn(prefix + "command_name", Column(&LogTable::CommandNameAccessor, objectAccessor));
76 HostsTable::AddColumns(table, "current_host_", std::bind(&LogTable::HostAccessor, _1, objectAccessor));
77 ServicesTable::AddColumns(table, "current_service_", std::bind(&LogTable::ServiceAccessor, _1, objectAccessor));
78 ContactsTable::AddColumns(table, "current_contact_", std::bind(&LogTable::ContactAccessor, _1, objectAccessor));
79 CommandsTable::AddColumns(table, "current_command_", std::bind(&LogTable::CommandAccessor, _1, objectAccessor));
82 String LogTable::GetName() const
87 String LogTable::GetPrefix() const
92 void LogTable::FetchRows(const AddRowFunction& addRowFn)
94 Log(LogDebug, "LogTable")
95 << "Pre-selecting log file from " << m_TimeFrom << " until " << m_TimeUntil;
97 /* create log file index */
98 LivestatusLogUtility::CreateLogIndex(m_CompatLogPath, m_LogFileIndex);
100 /* generate log cache */
101 LivestatusLogUtility::CreateLogCache(m_LogFileIndex, this, m_TimeFrom, m_TimeUntil, addRowFn);
104 /* gets called in LivestatusLogUtility::CreateLogCache */
105 void LogTable::UpdateLogEntries(const Dictionary::Ptr& log_entry_attrs, int line_count, int lineno, const AddRowFunction& addRowFn)
107 /* additional attributes only for log table */
108 log_entry_attrs->Set("lineno", lineno);
110 addRowFn(log_entry_attrs, LivestatusGroupByNone, Empty);
113 Object::Ptr LogTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
115 String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
117 if (host_name.IsEmpty())
120 return Host::GetByName(host_name);
123 Object::Ptr LogTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
125 String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
126 String service_description = static_cast<Dictionary::Ptr>(row)->Get("service_description");
128 if (service_description.IsEmpty() || host_name.IsEmpty())
131 return Service::GetByNamePair(host_name, service_description);
134 Object::Ptr LogTable::ContactAccessor(const Value& row, const Column::ObjectAccessor&)
136 String contact_name = static_cast<Dictionary::Ptr>(row)->Get("contact_name");
138 if (contact_name.IsEmpty())
141 return User::GetByName(contact_name);
144 Object::Ptr LogTable::CommandAccessor(const Value& row, const Column::ObjectAccessor&)
146 String command_name = static_cast<Dictionary::Ptr>(row)->Get("command_name");
148 if (command_name.IsEmpty())
151 CheckCommand::Ptr check_command = CheckCommand::GetByName(command_name);
152 if (!check_command) {
153 EventCommand::Ptr event_command = EventCommand::GetByName(command_name);
154 if (!event_command) {
155 NotificationCommand::Ptr notification_command = NotificationCommand::GetByName(command_name);
156 if (!notification_command)
159 return notification_command;
161 return event_command;
163 return check_command;
166 Value LogTable::TimeAccessor(const Value& row)
168 return static_cast<Dictionary::Ptr>(row)->Get("time");
171 Value LogTable::LinenoAccessor(const Value& row)
173 return static_cast<Dictionary::Ptr>(row)->Get("lineno");
176 Value LogTable::ClassAccessor(const Value& row)
178 return static_cast<Dictionary::Ptr>(row)->Get("class");
181 Value LogTable::MessageAccessor(const Value& row)
183 return static_cast<Dictionary::Ptr>(row)->Get("message");
186 Value LogTable::TypeAccessor(const Value& row)
188 return static_cast<Dictionary::Ptr>(row)->Get("type");
191 Value LogTable::OptionsAccessor(const Value& row)
193 return static_cast<Dictionary::Ptr>(row)->Get("options");
196 Value LogTable::CommentAccessor(const Value& row)
198 return static_cast<Dictionary::Ptr>(row)->Get("comment");
201 Value LogTable::PluginOutputAccessor(const Value& row)
203 return static_cast<Dictionary::Ptr>(row)->Get("plugin_output");
206 Value LogTable::StateAccessor(const Value& row)
208 return static_cast<Dictionary::Ptr>(row)->Get("state");
211 Value LogTable::StateTypeAccessor(const Value& row)
213 return static_cast<Dictionary::Ptr>(row)->Get("state_type");
216 Value LogTable::AttemptAccessor(const Value& row)
218 return static_cast<Dictionary::Ptr>(row)->Get("attempt");
221 Value LogTable::ServiceDescriptionAccessor(const Value& row)
223 return static_cast<Dictionary::Ptr>(row)->Get("service_description");
226 Value LogTable::HostNameAccessor(const Value& row)
228 return static_cast<Dictionary::Ptr>(row)->Get("host_name");
231 Value LogTable::ContactNameAccessor(const Value& row)
233 return static_cast<Dictionary::Ptr>(row)->Get("contact_name");
236 Value LogTable::CommandNameAccessor(const Value& row)
238 return static_cast<Dictionary::Ptr>(row)->Get("command_name");