]> granicus.if.org Git - icinga2/blob - lib/livestatus/logtable.cpp
Update copyright headers for 2016
[icinga2] / lib / livestatus / logtable.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/)  *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "livestatus/logtable.hpp"
21 #include "livestatus/livestatuslogutility.hpp"
22 #include "livestatus/hoststable.hpp"
23 #include "livestatus/servicestable.hpp"
24 #include "livestatus/contactstable.hpp"
25 #include "livestatus/commandstable.hpp"
26 #include "icinga/icingaapplication.hpp"
27 #include "icinga/cib.hpp"
28 #include "icinga/service.hpp"
29 #include "icinga/host.hpp"
30 #include "icinga/user.hpp"
31 #include "icinga/checkcommand.hpp"
32 #include "icinga/eventcommand.hpp"
33 #include "icinga/notificationcommand.hpp"
34 #include "base/convert.hpp"
35 #include "base/utility.hpp"
36 #include "base/logger.hpp"
37 #include "base/application.hpp"
38 #include "base/objectlock.hpp"
39 #include <boost/foreach.hpp>
40 #include <boost/tuple/tuple.hpp>
41 #include <boost/algorithm/string.hpp>
42 #include <boost/algorithm/string/split.hpp>
43 #include <boost/algorithm/string/classification.hpp>
44 #include <boost/algorithm/string/replace.hpp>
45 #include <boost/algorithm/string/predicate.hpp>
46 #include <fstream>
47
48 using namespace icinga;
49
50 LogTable::LogTable(const String& compat_log_path, time_t from, time_t until)
51 {
52         /* store attributes for FetchRows */
53         m_TimeFrom = from;
54         m_TimeUntil = until;
55         m_CompatLogPath = compat_log_path;
56
57         AddColumns(this);
58 }
59
60 void LogTable::AddColumns(Table *table, const String& prefix,
61     const Column::ObjectAccessor& objectAccessor)
62 {
63         table->AddColumn(prefix + "time", Column(&LogTable::TimeAccessor, objectAccessor));
64         table->AddColumn(prefix + "lineno", Column(&LogTable::LinenoAccessor, objectAccessor));
65         table->AddColumn(prefix + "class", Column(&LogTable::ClassAccessor, objectAccessor));
66         table->AddColumn(prefix + "message", Column(&LogTable::MessageAccessor, objectAccessor));
67         table->AddColumn(prefix + "type", Column(&LogTable::TypeAccessor, objectAccessor));
68         table->AddColumn(prefix + "options", Column(&LogTable::OptionsAccessor, objectAccessor));
69         table->AddColumn(prefix + "comment", Column(&LogTable::CommentAccessor, objectAccessor));
70         table->AddColumn(prefix + "plugin_output", Column(&LogTable::PluginOutputAccessor, objectAccessor));
71         table->AddColumn(prefix + "state", Column(&LogTable::StateAccessor, objectAccessor));
72         table->AddColumn(prefix + "state_type", Column(&LogTable::StateTypeAccessor, objectAccessor));
73         table->AddColumn(prefix + "attempt", Column(&LogTable::AttemptAccessor, objectAccessor));
74         table->AddColumn(prefix + "service_description", Column(&LogTable::ServiceDescriptionAccessor, objectAccessor));
75         table->AddColumn(prefix + "host_name", Column(&LogTable::HostNameAccessor, objectAccessor));
76         table->AddColumn(prefix + "contact_name", Column(&LogTable::ContactNameAccessor, objectAccessor));
77         table->AddColumn(prefix + "command_name", Column(&LogTable::CommandNameAccessor, objectAccessor));
78
79         HostsTable::AddColumns(table, "current_host_", boost::bind(&LogTable::HostAccessor, _1, objectAccessor));
80         ServicesTable::AddColumns(table, "current_service_", boost::bind(&LogTable::ServiceAccessor, _1, objectAccessor));
81         ContactsTable::AddColumns(table, "current_contact_", boost::bind(&LogTable::ContactAccessor, _1, objectAccessor));
82         CommandsTable::AddColumns(table, "current_command_", boost::bind(&LogTable::CommandAccessor, _1, objectAccessor));
83 }
84
85 String LogTable::GetName(void) const
86 {
87         return "log";
88 }
89
90 String LogTable::GetPrefix(void) const
91 {
92         return "log";
93 }
94
95 void LogTable::FetchRows(const AddRowFunction& addRowFn)
96 {
97         Log(LogDebug, "LogTable")
98             << "Pre-selecting log file from " << m_TimeFrom << " until " << m_TimeUntil;
99
100         /* create log file index */
101         LivestatusLogUtility::CreateLogIndex(m_CompatLogPath, m_LogFileIndex);
102
103         /* generate log cache */
104         LivestatusLogUtility::CreateLogCache(m_LogFileIndex, this, m_TimeFrom, m_TimeUntil, addRowFn);
105 }
106
107 /* gets called in LivestatusLogUtility::CreateLogCache */
108 void LogTable::UpdateLogEntries(const Dictionary::Ptr& log_entry_attrs, int line_count, int lineno, const AddRowFunction& addRowFn)
109 {
110         /* additional attributes only for log table */
111         log_entry_attrs->Set("lineno", lineno);
112
113         addRowFn(log_entry_attrs, LivestatusGroupByNone, Empty);
114 }
115
116 Object::Ptr LogTable::HostAccessor(const Value& row, const Column::ObjectAccessor&)
117 {
118         String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
119
120         if (host_name.IsEmpty())
121                 return Object::Ptr();
122
123         return Host::GetByName(host_name);
124 }
125
126 Object::Ptr LogTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor&)
127 {
128         String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
129         String service_description = static_cast<Dictionary::Ptr>(row)->Get("service_description");
130
131         if (service_description.IsEmpty() || host_name.IsEmpty())
132                 return Object::Ptr();
133
134         return Service::GetByNamePair(host_name, service_description);
135 }
136
137 Object::Ptr LogTable::ContactAccessor(const Value& row, const Column::ObjectAccessor&)
138 {
139         String contact_name = static_cast<Dictionary::Ptr>(row)->Get("contact_name");
140
141         if (contact_name.IsEmpty())
142                 return Object::Ptr();
143
144         return User::GetByName(contact_name);
145 }
146
147 Object::Ptr LogTable::CommandAccessor(const Value& row, const Column::ObjectAccessor&)
148 {
149         String command_name = static_cast<Dictionary::Ptr>(row)->Get("command_name");
150
151         if (command_name.IsEmpty())
152                 return Object::Ptr();
153
154         CheckCommand::Ptr check_command = CheckCommand::GetByName(command_name);
155         if (!check_command) {
156                 EventCommand::Ptr event_command = EventCommand::GetByName(command_name);
157                 if (!event_command) {
158                         NotificationCommand::Ptr notification_command = NotificationCommand::GetByName(command_name);
159                         if (!notification_command)
160                                 return Object::Ptr();
161                         else
162                                 return notification_command;
163                 } else
164                         return event_command;
165         } else
166                 return check_command;
167 }
168
169 Value LogTable::TimeAccessor(const Value& row)
170 {
171         return static_cast<Dictionary::Ptr>(row)->Get("time");
172 }
173
174 Value LogTable::LinenoAccessor(const Value& row)
175 {
176         return static_cast<Dictionary::Ptr>(row)->Get("lineno");
177 }
178
179 Value LogTable::ClassAccessor(const Value& row)
180 {
181         return static_cast<Dictionary::Ptr>(row)->Get("class");
182 }
183
184 Value LogTable::MessageAccessor(const Value& row)
185 {
186         return static_cast<Dictionary::Ptr>(row)->Get("message");
187 }
188
189 Value LogTable::TypeAccessor(const Value& row)
190 {
191         return static_cast<Dictionary::Ptr>(row)->Get("type");
192 }
193
194 Value LogTable::OptionsAccessor(const Value& row)
195 {
196         return static_cast<Dictionary::Ptr>(row)->Get("options");
197 }
198
199 Value LogTable::CommentAccessor(const Value& row)
200 {
201         return static_cast<Dictionary::Ptr>(row)->Get("comment");
202 }
203
204 Value LogTable::PluginOutputAccessor(const Value& row)
205 {
206         return static_cast<Dictionary::Ptr>(row)->Get("plugin_output");
207 }
208
209 Value LogTable::StateAccessor(const Value& row)
210 {
211         return static_cast<Dictionary::Ptr>(row)->Get("state");
212 }
213
214 Value LogTable::StateTypeAccessor(const Value& row)
215 {
216         return static_cast<Dictionary::Ptr>(row)->Get("state_type");
217 }
218
219 Value LogTable::AttemptAccessor(const Value& row)
220 {
221         return static_cast<Dictionary::Ptr>(row)->Get("attempt");
222 }
223
224 Value LogTable::ServiceDescriptionAccessor(const Value& row)
225 {
226         return static_cast<Dictionary::Ptr>(row)->Get("service_description");
227 }
228
229 Value LogTable::HostNameAccessor(const Value& row)
230 {
231         return static_cast<Dictionary::Ptr>(row)->Get("host_name");
232 }
233
234 Value LogTable::ContactNameAccessor(const Value& row)
235 {
236         return static_cast<Dictionary::Ptr>(row)->Get("contact_name");
237 }
238
239 Value LogTable::CommandNameAccessor(const Value& row)
240 {
241         return static_cast<Dictionary::Ptr>(row)->Get("command_name");
242 }