1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
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>
25 #include <fstream>
26
27 using namespace icinga;
28
LogTable(const String & compat_log_path,time_t from,time_t until)29 LogTable::LogTable(const String& compat_log_path, time_t from, time_t until)
30 {
31 /* store attributes for FetchRows */
32 m_TimeFrom = from;
33 m_TimeUntil = until;
34 m_CompatLogPath = compat_log_path;
35
36 AddColumns(this);
37 }
38
AddColumns(Table * table,const String & prefix,const Column::ObjectAccessor & objectAccessor)39 void LogTable::AddColumns(Table *table, const String& prefix,
40 const Column::ObjectAccessor& objectAccessor)
41 {
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));
57
58 HostsTable::AddColumns(table, "current_host_", [objectAccessor](const Value& row, LivestatusGroupByType, const Object::Ptr&) -> Value {
59 return HostAccessor(row, objectAccessor);
60 });
61 ServicesTable::AddColumns(table, "current_service_", [objectAccessor](const Value& row, LivestatusGroupByType, const Object::Ptr&) -> Value {
62 return ServiceAccessor(row, objectAccessor);
63 });
64 ContactsTable::AddColumns(table, "current_contact_", [objectAccessor](const Value& row, LivestatusGroupByType, const Object::Ptr&) -> Value {
65 return ContactAccessor(row, objectAccessor);
66 });
67 CommandsTable::AddColumns(table, "current_command_", [objectAccessor](const Value& row, LivestatusGroupByType, const Object::Ptr&) -> Value {
68 return CommandAccessor(row, objectAccessor);
69 });
70 }
71
GetName() const72 String LogTable::GetName() const
73 {
74 return "log";
75 }
76
GetPrefix() const77 String LogTable::GetPrefix() const
78 {
79 return "log";
80 }
81
FetchRows(const AddRowFunction & addRowFn)82 void LogTable::FetchRows(const AddRowFunction& addRowFn)
83 {
84 Log(LogDebug, "LogTable")
85 << "Pre-selecting log file from " << m_TimeFrom << " until " << m_TimeUntil;
86
87 /* create log file index */
88 LivestatusLogUtility::CreateLogIndex(m_CompatLogPath, m_LogFileIndex);
89
90 /* generate log cache */
91 LivestatusLogUtility::CreateLogCache(m_LogFileIndex, this, m_TimeFrom, m_TimeUntil, addRowFn);
92 }
93
94 /* gets called in LivestatusLogUtility::CreateLogCache */
UpdateLogEntries(const Dictionary::Ptr & log_entry_attrs,int line_count,int lineno,const AddRowFunction & addRowFn)95 void LogTable::UpdateLogEntries(const Dictionary::Ptr& log_entry_attrs, int line_count, int lineno, const AddRowFunction& addRowFn)
96 {
97 /* additional attributes only for log table */
98 log_entry_attrs->Set("lineno", lineno);
99
100 addRowFn(log_entry_attrs, LivestatusGroupByNone, Empty);
101 }
102
HostAccessor(const Value & row,const Column::ObjectAccessor &)103 Object::Ptr LogTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
104 {
105 String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
106
107 if (host_name.IsEmpty())
108 return nullptr;
109
110 return Host::GetByName(host_name);
111 }
112
ServiceAccessor(const Value & row,const Column::ObjectAccessor &)113 Object::Ptr LogTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
114 {
115 String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
116 String service_description = static_cast<Dictionary::Ptr>(row)->Get("service_description");
117
118 if (service_description.IsEmpty() || host_name.IsEmpty())
119 return nullptr;
120
121 return Service::GetByNamePair(host_name, service_description);
122 }
123
ContactAccessor(const Value & row,const Column::ObjectAccessor &)124 Object::Ptr LogTable::ContactAccessor(const Value& row, const Column::ObjectAccessor&)
125 {
126 String contact_name = static_cast<Dictionary::Ptr>(row)->Get("contact_name");
127
128 if (contact_name.IsEmpty())
129 return nullptr;
130
131 return User::GetByName(contact_name);
132 }
133
CommandAccessor(const Value & row,const Column::ObjectAccessor &)134 Object::Ptr LogTable::CommandAccessor(const Value& row, const Column::ObjectAccessor&)
135 {
136 String command_name = static_cast<Dictionary::Ptr>(row)->Get("command_name");
137
138 if (command_name.IsEmpty())
139 return nullptr;
140
141 CheckCommand::Ptr check_command = CheckCommand::GetByName(command_name);
142 if (!check_command) {
143 EventCommand::Ptr event_command = EventCommand::GetByName(command_name);
144 if (!event_command) {
145 NotificationCommand::Ptr notification_command = NotificationCommand::GetByName(command_name);
146 if (!notification_command)
147 return nullptr;
148 else
149 return notification_command;
150 } else
151 return event_command;
152 } else
153 return check_command;
154 }
155
TimeAccessor(const Value & row)156 Value LogTable::TimeAccessor(const Value& row)
157 {
158 return static_cast<Dictionary::Ptr>(row)->Get("time");
159 }
160
LinenoAccessor(const Value & row)161 Value LogTable::LinenoAccessor(const Value& row)
162 {
163 return static_cast<Dictionary::Ptr>(row)->Get("lineno");
164 }
165
ClassAccessor(const Value & row)166 Value LogTable::ClassAccessor(const Value& row)
167 {
168 return static_cast<Dictionary::Ptr>(row)->Get("class");
169 }
170
MessageAccessor(const Value & row)171 Value LogTable::MessageAccessor(const Value& row)
172 {
173 return static_cast<Dictionary::Ptr>(row)->Get("message");
174 }
175
TypeAccessor(const Value & row)176 Value LogTable::TypeAccessor(const Value& row)
177 {
178 return static_cast<Dictionary::Ptr>(row)->Get("type");
179 }
180
OptionsAccessor(const Value & row)181 Value LogTable::OptionsAccessor(const Value& row)
182 {
183 return static_cast<Dictionary::Ptr>(row)->Get("options");
184 }
185
CommentAccessor(const Value & row)186 Value LogTable::CommentAccessor(const Value& row)
187 {
188 return static_cast<Dictionary::Ptr>(row)->Get("comment");
189 }
190
PluginOutputAccessor(const Value & row)191 Value LogTable::PluginOutputAccessor(const Value& row)
192 {
193 return static_cast<Dictionary::Ptr>(row)->Get("plugin_output");
194 }
195
StateAccessor(const Value & row)196 Value LogTable::StateAccessor(const Value& row)
197 {
198 return static_cast<Dictionary::Ptr>(row)->Get("state");
199 }
200
StateTypeAccessor(const Value & row)201 Value LogTable::StateTypeAccessor(const Value& row)
202 {
203 return static_cast<Dictionary::Ptr>(row)->Get("state_type");
204 }
205
AttemptAccessor(const Value & row)206 Value LogTable::AttemptAccessor(const Value& row)
207 {
208 return static_cast<Dictionary::Ptr>(row)->Get("attempt");
209 }
210
ServiceDescriptionAccessor(const Value & row)211 Value LogTable::ServiceDescriptionAccessor(const Value& row)
212 {
213 return static_cast<Dictionary::Ptr>(row)->Get("service_description");
214 }
215
HostNameAccessor(const Value & row)216 Value LogTable::HostNameAccessor(const Value& row)
217 {
218 return static_cast<Dictionary::Ptr>(row)->Get("host_name");
219 }
220
ContactNameAccessor(const Value & row)221 Value LogTable::ContactNameAccessor(const Value& row)
222 {
223 return static_cast<Dictionary::Ptr>(row)->Get("contact_name");
224 }
225
CommandNameAccessor(const Value & row)226 Value LogTable::CommandNameAccessor(const Value& row)
227 {
228 return static_cast<Dictionary::Ptr>(row)->Get("command_name");
229 }
230