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/algorithm/string.hpp>
23 #include <boost/algorithm/string/replace.hpp>
24 #include <boost/algorithm/string/predicate.hpp>
27 using namespace icinga;
29 LogTable::LogTable(const String& compat_log_path, time_t from, time_t until)
31 /* store attributes for FetchRows */
34 m_CompatLogPath = compat_log_path;
39 void LogTable::AddColumns(Table *table, const String& prefix,
40 const Column::ObjectAccessor& objectAccessor)
42 table->AddColumn(prefix + "time", Column(&LogTable::TimeAccessor, objectAccessor));
43 table->AddColumn(prefix + "lineno", Column(&LogTable::LinenoAccessor, objectAccessor));
44 table->AddColumn(prefix + "class", Column(&LogTable::ClassAccessor, objectAccessor));
45 table->AddColumn(prefix + "message", Column(&LogTable::MessageAccessor, objectAccessor));
46 table->AddColumn(prefix + "type", Column(&LogTable::TypeAccessor, objectAccessor));
47 table->AddColumn(prefix + "options", Column(&LogTable::OptionsAccessor, objectAccessor));
48 table->AddColumn(prefix + "comment", Column(&LogTable::CommentAccessor, objectAccessor));
49 table->AddColumn(prefix + "plugin_output", Column(&LogTable::PluginOutputAccessor, objectAccessor));
50 table->AddColumn(prefix + "state", Column(&LogTable::StateAccessor, objectAccessor));
51 table->AddColumn(prefix + "state_type", Column(&LogTable::StateTypeAccessor, objectAccessor));
52 table->AddColumn(prefix + "attempt", Column(&LogTable::AttemptAccessor, objectAccessor));
53 table->AddColumn(prefix + "service_description", Column(&LogTable::ServiceDescriptionAccessor, objectAccessor));
54 table->AddColumn(prefix + "host_name", Column(&LogTable::HostNameAccessor, objectAccessor));
55 table->AddColumn(prefix + "contact_name", Column(&LogTable::ContactNameAccessor, objectAccessor));
56 table->AddColumn(prefix + "command_name", Column(&LogTable::CommandNameAccessor, objectAccessor));
58 HostsTable::AddColumns(table, "current_host_", std::bind(&LogTable::HostAccessor, _1, objectAccessor));
59 ServicesTable::AddColumns(table, "current_service_", std::bind(&LogTable::ServiceAccessor, _1, objectAccessor));
60 ContactsTable::AddColumns(table, "current_contact_", std::bind(&LogTable::ContactAccessor, _1, objectAccessor));
61 CommandsTable::AddColumns(table, "current_command_", std::bind(&LogTable::CommandAccessor, _1, objectAccessor));
64 String LogTable::GetName() const
69 String LogTable::GetPrefix() const
74 void LogTable::FetchRows(const AddRowFunction& addRowFn)
76 Log(LogDebug, "LogTable")
77 << "Pre-selecting log file from " << m_TimeFrom << " until " << m_TimeUntil;
79 /* create log file index */
80 LivestatusLogUtility::CreateLogIndex(m_CompatLogPath, m_LogFileIndex);
82 /* generate log cache */
83 LivestatusLogUtility::CreateLogCache(m_LogFileIndex, this, m_TimeFrom, m_TimeUntil, addRowFn);
86 /* gets called in LivestatusLogUtility::CreateLogCache */
87 void LogTable::UpdateLogEntries(const Dictionary::Ptr& log_entry_attrs, int line_count, int lineno, const AddRowFunction& addRowFn)
89 /* additional attributes only for log table */
90 log_entry_attrs->Set("lineno", lineno);
92 addRowFn(log_entry_attrs, LivestatusGroupByNone, Empty);
95 Object::Ptr LogTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
97 String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
99 if (host_name.IsEmpty())
102 return Host::GetByName(host_name);
105 Object::Ptr LogTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
107 String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
108 String service_description = static_cast<Dictionary::Ptr>(row)->Get("service_description");
110 if (service_description.IsEmpty() || host_name.IsEmpty())
113 return Service::GetByNamePair(host_name, service_description);
116 Object::Ptr LogTable::ContactAccessor(const Value& row, const Column::ObjectAccessor&)
118 String contact_name = static_cast<Dictionary::Ptr>(row)->Get("contact_name");
120 if (contact_name.IsEmpty())
123 return User::GetByName(contact_name);
126 Object::Ptr LogTable::CommandAccessor(const Value& row, const Column::ObjectAccessor&)
128 String command_name = static_cast<Dictionary::Ptr>(row)->Get("command_name");
130 if (command_name.IsEmpty())
133 CheckCommand::Ptr check_command = CheckCommand::GetByName(command_name);
134 if (!check_command) {
135 EventCommand::Ptr event_command = EventCommand::GetByName(command_name);
136 if (!event_command) {
137 NotificationCommand::Ptr notification_command = NotificationCommand::GetByName(command_name);
138 if (!notification_command)
141 return notification_command;
143 return event_command;
145 return check_command;
148 Value LogTable::TimeAccessor(const Value& row)
150 return static_cast<Dictionary::Ptr>(row)->Get("time");
153 Value LogTable::LinenoAccessor(const Value& row)
155 return static_cast<Dictionary::Ptr>(row)->Get("lineno");
158 Value LogTable::ClassAccessor(const Value& row)
160 return static_cast<Dictionary::Ptr>(row)->Get("class");
163 Value LogTable::MessageAccessor(const Value& row)
165 return static_cast<Dictionary::Ptr>(row)->Get("message");
168 Value LogTable::TypeAccessor(const Value& row)
170 return static_cast<Dictionary::Ptr>(row)->Get("type");
173 Value LogTable::OptionsAccessor(const Value& row)
175 return static_cast<Dictionary::Ptr>(row)->Get("options");
178 Value LogTable::CommentAccessor(const Value& row)
180 return static_cast<Dictionary::Ptr>(row)->Get("comment");
183 Value LogTable::PluginOutputAccessor(const Value& row)
185 return static_cast<Dictionary::Ptr>(row)->Get("plugin_output");
188 Value LogTable::StateAccessor(const Value& row)
190 return static_cast<Dictionary::Ptr>(row)->Get("state");
193 Value LogTable::StateTypeAccessor(const Value& row)
195 return static_cast<Dictionary::Ptr>(row)->Get("state_type");
198 Value LogTable::AttemptAccessor(const Value& row)
200 return static_cast<Dictionary::Ptr>(row)->Get("attempt");
203 Value LogTable::ServiceDescriptionAccessor(const Value& row)
205 return static_cast<Dictionary::Ptr>(row)->Get("service_description");
208 Value LogTable::HostNameAccessor(const Value& row)
210 return static_cast<Dictionary::Ptr>(row)->Get("host_name");
213 Value LogTable::ContactNameAccessor(const Value& row)
215 return static_cast<Dictionary::Ptr>(row)->Get("contact_name");
218 Value LogTable::CommandNameAccessor(const Value& row)
220 return static_cast<Dictionary::Ptr>(row)->Get("command_name");