1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
#include "livestatus/livestatuslogutility.hpp"
#include "icinga/service.hpp"
#include "icinga/host.hpp"
#include "icinga/user.hpp"
#include "icinga/checkcommand.hpp"
#include "icinga/eventcommand.hpp"
#include "icinga/notificationcommand.hpp"
#include "base/utility.hpp"
#include "base/convert.hpp"
#include "base/logger.hpp"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <fstream>
using namespace icinga;
void LivestatusLogUtility::CreateLogIndex(const String& path, std::map<time_t, String>& index)
{
Utility::Glob(path + "/icinga.log", [&index](const String& newPath) { CreateLogIndexFileHandler(newPath, index); }, GlobFile);
Utility::Glob(path + "/archives/*.log", [&index](const String& newPath) { CreateLogIndexFileHandler(newPath, index); }, GlobFile);
}
void LivestatusLogUtility::CreateLogIndexFileHandler(const String& path, std::map<time_t, String>& index)
{
std::ifstream stream;
stream.open(path.CStr(), std::ifstream::in);
if (!stream)
BOOST_THROW_EXCEPTION(std::runtime_error("Could not open log file: " + path));
/* read the first bytes to get the timestamp: [123456789] */
char buffer[12];
stream.read(buffer, 12);
if (buffer[0] != '[' || buffer[11] != ']') {
/* this can happen for directories too, silently ignore them */
return;
}
/* extract timestamp */
buffer[11] = 0;
time_t ts_start = atoi(buffer+1);
stream.close();
Log(LogDebug, "LivestatusLogUtility")
<< "Indexing log file: '" << path << "' with timestamp start: '" << ts_start << "'.";
index[ts_start] = path;
}
void LivestatusLogUtility::CreateLogCache(std::map<time_t, String> index, HistoryTable *table,
time_t from, time_t until, const AddRowFunction& addRowFn)
{
ASSERT(table);
/* m_LogFileIndex map tells which log files are involved ordered by their start timestamp */
unsigned long line_count = 0;
for (const auto& kv : index) {
unsigned int ts = kv.first;
/* skip log files not in range (performance optimization) */
if (ts < from || ts > until)
continue;
String log_file = index[ts];
int lineno = 0;
std::ifstream fp;
fp.exceptions(std::ifstream::badbit);
fp.open(log_file.CStr(), std::ifstream::in);
while (fp.good()) {
std::string line;
std::getline(fp, line);
if (line.empty())
continue; /* Ignore empty lines */
Dictionary::Ptr log_entry_attrs = LivestatusLogUtility::GetAttributes(line);
/* no attributes available - invalid log line */
if (!log_entry_attrs) {
Log(LogDebug, "LivestatusLogUtility")
<< "Skipping invalid log line: '" << line << "'.";
continue;
}
table->UpdateLogEntries(log_entry_attrs, line_count, lineno, addRowFn);
line_count++;
lineno++;
}
fp.close();
}
}
Dictionary::Ptr LivestatusLogUtility::GetAttributes(const String& text)
{
Dictionary::Ptr bag = new Dictionary();
/*
* [1379025342] SERVICE NOTIFICATION: contactname;hostname;servicedesc;WARNING;true;foo output
*/
unsigned long time = atoi(text.SubStr(1, 11).CStr());
Log(LogDebug, "LivestatusLogUtility")
<< "Processing log line: '" << text << "'.";
bag->Set("time", time);
size_t colon = text.FindFirstOf(':');
size_t colon_offset = colon - 13;
String type = String(text.SubStr(13, colon_offset)).Trim();
String options = String(text.SubStr(colon + 1)).Trim();
bag->Set("type", type);
bag->Set("options", options);
std::vector<String> tokens = options.Split(";");
/* set default values */
bag->Set("class", LogEntryClassInfo);
bag->Set("log_type", 0);
bag->Set("state", 0);
bag->Set("attempt", 0);
bag->Set("message", text); /* used as 'message' in log table, and 'log_output' in statehist table */
if (type.Contains("INITIAL HOST STATE") ||
type.Contains("CURRENT HOST STATE") ||
type.Contains("HOST ALERT")) {
if (tokens.size() < 5)
return bag;
bag->Set("host_name", tokens[0]);
bag->Set("state", Host::StateFromString(tokens[1]));
bag->Set("state_type", tokens[2]);
bag->Set("attempt", atoi(tokens[3].CStr()));
bag->Set("plugin_output", tokens[4]);
if (type.Contains("INITIAL HOST STATE")) {
bag->Set("class", LogEntryClassState);
bag->Set("log_type", LogEntryTypeHostInitialState);
}
else if (type.Contains("CURRENT HOST STATE")) {
bag->Set("class", LogEntryClassState);
bag->Set("log_type", LogEntryTypeHostCurrentState);
}
else {
bag->Set("class", LogEntryClassAlert);
bag->Set("log_type", LogEntryTypeHostAlert);
}
return bag;
} else if (type.Contains("HOST DOWNTIME ALERT") || type.Contains("HOST FLAPPING ALERT")) {
if (tokens.size() < 3)
return bag;
bag->Set("host_name", tokens[0]);
bag->Set("state_type", tokens[1]);
bag->Set("comment", tokens[2]);
if (type.Contains("HOST FLAPPING ALERT")) {
bag->Set("class", LogEntryClassAlert);
bag->Set("log_type", LogEntryTypeHostFlapping);
} else {
bag->Set("class", LogEntryClassAlert);
bag->Set("log_type", LogEntryTypeHostDowntimeAlert);
}
return bag;
} else if (type.Contains("INITIAL SERVICE STATE") ||
type.Contains("CURRENT SERVICE STATE") ||
type.Contains("SERVICE ALERT")) {
if (tokens.size() < 6)
return bag;
bag->Set("host_name", tokens[0]);
bag->Set("service_description", tokens[1]);
bag->Set("state", Service::StateFromString(tokens[2]));
bag->Set("state_type", tokens[3]);
bag->Set("attempt", atoi(tokens[4].CStr()));
bag->Set("plugin_output", tokens[5]);
if (type.Contains("INITIAL SERVICE STATE")) {
bag->Set("class", LogEntryClassState);
bag->Set("log_type", LogEntryTypeServiceInitialState);
}
else if (type.Contains("CURRENT SERVICE STATE")) {
bag->Set("class", LogEntryClassState);
bag->Set("log_type", LogEntryTypeServiceCurrentState);
}
else {
bag->Set("class", LogEntryClassAlert);
bag->Set("log_type", LogEntryTypeServiceAlert);
}
return bag;
} else if (type.Contains("SERVICE DOWNTIME ALERT") ||
type.Contains("SERVICE FLAPPING ALERT")) {
if (tokens.size() < 4)
return bag;
bag->Set("host_name", tokens[0]);
bag->Set("service_description", tokens[1]);
bag->Set("state_type", tokens[2]);
bag->Set("comment", tokens[3]);
if (type.Contains("SERVICE FLAPPING ALERT")) {
bag->Set("class", LogEntryClassAlert);
bag->Set("log_type", LogEntryTypeServiceFlapping);
} else {
bag->Set("class", LogEntryClassAlert);
bag->Set("log_type", LogEntryTypeServiceDowntimeAlert);
}
return bag;
} else if (type.Contains("TIMEPERIOD TRANSITION")) {
if (tokens.size() < 4)
return bag;
bag->Set("class", LogEntryClassState);
bag->Set("log_type", LogEntryTypeTimeperiodTransition);
bag->Set("host_name", tokens[0]);
bag->Set("service_description", tokens[1]);
bag->Set("state_type", tokens[2]);
bag->Set("comment", tokens[3]);
} else if (type.Contains("HOST NOTIFICATION")) {
if (tokens.size() < 6)
return bag;
bag->Set("contact_name", tokens[0]);
bag->Set("host_name", tokens[1]);
bag->Set("state_type", tokens[2].CStr());
bag->Set("state", Service::StateFromString(tokens[3]));
bag->Set("command_name", tokens[4]);
bag->Set("plugin_output", tokens[5]);
bag->Set("class", LogEntryClassNotification);
bag->Set("log_type", LogEntryTypeHostNotification);
return bag;
} else if (type.Contains("SERVICE NOTIFICATION")) {
if (tokens.size() < 7)
return bag;
bag->Set("contact_name", tokens[0]);
bag->Set("host_name", tokens[1]);
bag->Set("service_description", tokens[2]);
bag->Set("state_type", tokens[3].CStr());
bag->Set("state", Service::StateFromString(tokens[4]));
bag->Set("command_name", tokens[5]);
bag->Set("plugin_output", tokens[6]);
bag->Set("class", LogEntryClassNotification);
bag->Set("log_type", LogEntryTypeServiceNotification);
return bag;
} else if (type.Contains("PASSIVE HOST CHECK")) {
if (tokens.size() < 3)
return bag;
bag->Set("host_name", tokens[0]);
bag->Set("state", Host::StateFromString(tokens[1]));
bag->Set("plugin_output", tokens[2]);
bag->Set("class", LogEntryClassPassive);
return bag;
} else if (type.Contains("PASSIVE SERVICE CHECK")) {
if (tokens.size() < 4)
return bag;
bag->Set("host_name", tokens[0]);
bag->Set("service_description", tokens[1]);
bag->Set("state", Host::StateFromString(tokens[2]));
bag->Set("plugin_output", tokens[3]);
bag->Set("class", LogEntryClassPassive);
return bag;
} else if (type.Contains("EXTERNAL COMMAND")) {
bag->Set("class", LogEntryClassCommand);
/* string processing not implemented in 1.x */
return bag;
} else if (type.Contains("LOG VERSION")) {
bag->Set("class", LogEntryClassProgram);
bag->Set("log_type", LogEntryTypeVersion);
return bag;
} else if (type.Contains("logging initial states")) {
bag->Set("class", LogEntryClassProgram);
bag->Set("log_type", LogEntryTypeInitialStates);
return bag;
} else if (type.Contains("starting... (PID=")) {
bag->Set("class", LogEntryClassProgram);
bag->Set("log_type", LogEntryTypeProgramStarting);
return bag;
}
/* program */
else if (type.Contains("restarting...") ||
type.Contains("shutting down...") ||
type.Contains("Bailing out") ||
type.Contains("active mode...") ||
type.Contains("standby mode...")) {
bag->Set("class", LogEntryClassProgram);
return bag;
}
return bag;
}
|