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