1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "livestatus/logtable.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>
28 using namespace icinga;
30 LogTable::LogTable(const String& compat_log_path, time_t from, time_t until)
32 /* store attributes for FetchRows */
35 m_CompatLogPath = compat_log_path;
40 void LogTable::AddColumns(Table *table, const String& prefix,
41 const Column::ObjectAccessor& objectAccessor)
43 table->AddColumn(prefix + "time", Column(&LogTable::TimeAccessor, objectAccessor));
44 table->AddColumn(prefix + "lineno", Column(&LogTable::LinenoAccessor, objectAccessor));
45 table->AddColumn(prefix + "class", Column(&LogTable::ClassAccessor, objectAccessor));
46 table->AddColumn(prefix + "message", Column(&LogTable::MessageAccessor, objectAccessor));
47 table->AddColumn(prefix + "type", Column(&LogTable::TypeAccessor, objectAccessor));
48 table->AddColumn(prefix + "options", Column(&LogTable::OptionsAccessor, objectAccessor));
49 table->AddColumn(prefix + "comment", Column(&LogTable::CommentAccessor, objectAccessor));
50 table->AddColumn(prefix + "plugin_output", Column(&LogTable::PluginOutputAccessor, objectAccessor));
51 table->AddColumn(prefix + "state", Column(&LogTable::StateAccessor, objectAccessor));
52 table->AddColumn(prefix + "state_type", Column(&LogTable::StateTypeAccessor, objectAccessor));
53 table->AddColumn(prefix + "attempt", Column(&LogTable::AttemptAccessor, objectAccessor));
54 table->AddColumn(prefix + "service_description", Column(&LogTable::ServiceDescriptionAccessor, objectAccessor));
55 table->AddColumn(prefix + "host_name", Column(&LogTable::HostNameAccessor, objectAccessor));
56 table->AddColumn(prefix + "contact_name", Column(&LogTable::ContactNameAccessor, objectAccessor));
57 table->AddColumn(prefix + "command_name", Column(&LogTable::CommandNameAccessor, objectAccessor));
59 HostsTable::AddColumns(table, "current_host_", std::bind(&LogTable::HostAccessor, _1, objectAccessor));
60 ServicesTable::AddColumns(table, "current_service_", std::bind(&LogTable::ServiceAccessor, _1, objectAccessor));
61 ContactsTable::AddColumns(table, "current_contact_", std::bind(&LogTable::ContactAccessor, _1, objectAccessor));
62 CommandsTable::AddColumns(table, "current_command_", std::bind(&LogTable::CommandAccessor, _1, objectAccessor));
65 String LogTable::GetName() const
70 String LogTable::GetPrefix() const
75 void LogTable::FetchRows(const AddRowFunction& addRowFn)
77 Log(LogDebug, "LogTable")
78 << "Pre-selecting log file from " << m_TimeFrom << " until " << m_TimeUntil;
80 /* create log file index */
81 LivestatusLogUtility::CreateLogIndex(m_CompatLogPath, m_LogFileIndex);
83 /* generate log cache */
84 LivestatusLogUtility::CreateLogCache(m_LogFileIndex, this, m_TimeFrom, m_TimeUntil, addRowFn);
87 /* gets called in LivestatusLogUtility::CreateLogCache */
88 void LogTable::UpdateLogEntries(const Dictionary::Ptr& log_entry_attrs, int line_count, int lineno, const AddRowFunction& addRowFn)
90 /* additional attributes only for log table */
91 log_entry_attrs->Set("lineno", lineno);
93 addRowFn(log_entry_attrs, LivestatusGroupByNone, Empty);
96 Object::Ptr LogTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
98 String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
100 if (host_name.IsEmpty())
103 return Host::GetByName(host_name);
106 Object::Ptr LogTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
108 String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
109 String service_description = static_cast<Dictionary::Ptr>(row)->Get("service_description");
111 if (service_description.IsEmpty() || host_name.IsEmpty())
114 return Service::GetByNamePair(host_name, service_description);
117 Object::Ptr LogTable::ContactAccessor(const Value& row, const Column::ObjectAccessor&)
119 String contact_name = static_cast<Dictionary::Ptr>(row)->Get("contact_name");
121 if (contact_name.IsEmpty())
124 return User::GetByName(contact_name);
127 Object::Ptr LogTable::CommandAccessor(const Value& row, const Column::ObjectAccessor&)
129 String command_name = static_cast<Dictionary::Ptr>(row)->Get("command_name");
131 if (command_name.IsEmpty())
134 CheckCommand::Ptr check_command = CheckCommand::GetByName(command_name);
135 if (!check_command) {
136 EventCommand::Ptr event_command = EventCommand::GetByName(command_name);
137 if (!event_command) {
138 NotificationCommand::Ptr notification_command = NotificationCommand::GetByName(command_name);
139 if (!notification_command)
142 return notification_command;
144 return event_command;
146 return check_command;
149 Value LogTable::TimeAccessor(const Value& row)
151 return static_cast<Dictionary::Ptr>(row)->Get("time");
154 Value LogTable::LinenoAccessor(const Value& row)
156 return static_cast<Dictionary::Ptr>(row)->Get("lineno");
159 Value LogTable::ClassAccessor(const Value& row)
161 return static_cast<Dictionary::Ptr>(row)->Get("class");
164 Value LogTable::MessageAccessor(const Value& row)
166 return static_cast<Dictionary::Ptr>(row)->Get("message");
169 Value LogTable::TypeAccessor(const Value& row)
171 return static_cast<Dictionary::Ptr>(row)->Get("type");
174 Value LogTable::OptionsAccessor(const Value& row)
176 return static_cast<Dictionary::Ptr>(row)->Get("options");
179 Value LogTable::CommentAccessor(const Value& row)
181 return static_cast<Dictionary::Ptr>(row)->Get("comment");
184 Value LogTable::PluginOutputAccessor(const Value& row)
186 return static_cast<Dictionary::Ptr>(row)->Get("plugin_output");
189 Value LogTable::StateAccessor(const Value& row)
191 return static_cast<Dictionary::Ptr>(row)->Get("state");
194 Value LogTable::StateTypeAccessor(const Value& row)
196 return static_cast<Dictionary::Ptr>(row)->Get("state_type");
199 Value LogTable::AttemptAccessor(const Value& row)
201 return static_cast<Dictionary::Ptr>(row)->Get("attempt");
204 Value LogTable::ServiceDescriptionAccessor(const Value& row)
206 return static_cast<Dictionary::Ptr>(row)->Get("service_description");
209 Value LogTable::HostNameAccessor(const Value& row)
211 return static_cast<Dictionary::Ptr>(row)->Get("host_name");
214 Value LogTable::ContactNameAccessor(const Value& row)
216 return static_cast<Dictionary::Ptr>(row)->Get("contact_name");
219 Value LogTable::CommandNameAccessor(const Value& row)
221 return static_cast<Dictionary::Ptr>(row)->Get("command_name");