]> granicus.if.org Git - icinga2/blob - lib/livestatus/logtable.cpp
Merge pull request #7210 from Icinga/bugfix/boost-asio-deprecated
[icinga2] / lib / livestatus / logtable.cpp
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
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
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_", 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));
62 }
63
64 String LogTable::GetName() const
65 {
66         return "log";
67 }
68
69 String LogTable::GetPrefix() const
70 {
71         return "log";
72 }
73
74 void LogTable::FetchRows(const AddRowFunction& addRowFn)
75 {
76         Log(LogDebug, "LogTable")
77                 << "Pre-selecting log file from " << m_TimeFrom << " until " << m_TimeUntil;
78
79         /* create log file index */
80         LivestatusLogUtility::CreateLogIndex(m_CompatLogPath, m_LogFileIndex);
81
82         /* generate log cache */
83         LivestatusLogUtility::CreateLogCache(m_LogFileIndex, this, m_TimeFrom, m_TimeUntil, addRowFn);
84 }
85
86 /* gets called in LivestatusLogUtility::CreateLogCache */
87 void LogTable::UpdateLogEntries(const Dictionary::Ptr& log_entry_attrs, int line_count, int lineno, const AddRowFunction& addRowFn)
88 {
89         /* additional attributes only for log table */
90         log_entry_attrs->Set("lineno", lineno);
91
92         addRowFn(log_entry_attrs, LivestatusGroupByNone, Empty);
93 }
94
95 Object::Ptr LogTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
96 {
97         String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
98
99         if (host_name.IsEmpty())
100                 return nullptr;
101
102         return Host::GetByName(host_name);
103 }
104
105 Object::Ptr LogTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
106 {
107         String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
108         String service_description = static_cast<Dictionary::Ptr>(row)->Get("service_description");
109
110         if (service_description.IsEmpty() || host_name.IsEmpty())
111                 return nullptr;
112
113         return Service::GetByNamePair(host_name, service_description);
114 }
115
116 Object::Ptr LogTable::ContactAccessor(const Value& row, const Column::ObjectAccessor&)
117 {
118         String contact_name = static_cast<Dictionary::Ptr>(row)->Get("contact_name");
119
120         if (contact_name.IsEmpty())
121                 return nullptr;
122
123         return User::GetByName(contact_name);
124 }
125
126 Object::Ptr LogTable::CommandAccessor(const Value& row, const Column::ObjectAccessor&)
127 {
128         String command_name = static_cast<Dictionary::Ptr>(row)->Get("command_name");
129
130         if (command_name.IsEmpty())
131                 return nullptr;
132
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)
139                                 return nullptr;
140                         else
141                                 return notification_command;
142                 } else
143                         return event_command;
144         } else
145                 return check_command;
146 }
147
148 Value LogTable::TimeAccessor(const Value& row)
149 {
150         return static_cast<Dictionary::Ptr>(row)->Get("time");
151 }
152
153 Value LogTable::LinenoAccessor(const Value& row)
154 {
155         return static_cast<Dictionary::Ptr>(row)->Get("lineno");
156 }
157
158 Value LogTable::ClassAccessor(const Value& row)
159 {
160         return static_cast<Dictionary::Ptr>(row)->Get("class");
161 }
162
163 Value LogTable::MessageAccessor(const Value& row)
164 {
165         return static_cast<Dictionary::Ptr>(row)->Get("message");
166 }
167
168 Value LogTable::TypeAccessor(const Value& row)
169 {
170         return static_cast<Dictionary::Ptr>(row)->Get("type");
171 }
172
173 Value LogTable::OptionsAccessor(const Value& row)
174 {
175         return static_cast<Dictionary::Ptr>(row)->Get("options");
176 }
177
178 Value LogTable::CommentAccessor(const Value& row)
179 {
180         return static_cast<Dictionary::Ptr>(row)->Get("comment");
181 }
182
183 Value LogTable::PluginOutputAccessor(const Value& row)
184 {
185         return static_cast<Dictionary::Ptr>(row)->Get("plugin_output");
186 }
187
188 Value LogTable::StateAccessor(const Value& row)
189 {
190         return static_cast<Dictionary::Ptr>(row)->Get("state");
191 }
192
193 Value LogTable::StateTypeAccessor(const Value& row)
194 {
195         return static_cast<Dictionary::Ptr>(row)->Get("state_type");
196 }
197
198 Value LogTable::AttemptAccessor(const Value& row)
199 {
200         return static_cast<Dictionary::Ptr>(row)->Get("attempt");
201 }
202
203 Value LogTable::ServiceDescriptionAccessor(const Value& row)
204 {
205         return static_cast<Dictionary::Ptr>(row)->Get("service_description");
206 }
207
208 Value LogTable::HostNameAccessor(const Value& row)
209 {
210         return static_cast<Dictionary::Ptr>(row)->Get("host_name");
211 }
212
213 Value LogTable::ContactNameAccessor(const Value& row)
214 {
215         return static_cast<Dictionary::Ptr>(row)->Get("contact_name");
216 }
217
218 Value LogTable::CommandNameAccessor(const Value& row)
219 {
220         return static_cast<Dictionary::Ptr>(row)->Get("command_name");
221 }