2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2014-05-28 14:25:12 +02:00
|
|
|
#include "livestatus/livestatuslogutility.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#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"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2013-12-17 12:58:04 +01:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2014-05-28 14:25:12 +02:00
|
|
|
void LivestatusLogUtility::CreateLogIndex(const String& path, std::map<time_t, String>& index)
|
2013-12-17 12:58:04 +01:00
|
|
|
{
|
2021-01-18 14:29:05 +01:00
|
|
|
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);
|
2013-12-17 12:58:04 +01:00
|
|
|
}
|
|
|
|
|
2014-05-28 14:25:12 +02:00
|
|
|
void LivestatusLogUtility::CreateLogIndexFileHandler(const String& path, std::map<time_t, String>& index)
|
2013-12-17 12:58:04 +01:00
|
|
|
{
|
|
|
|
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;
|
2013-12-18 16:06:39 +01:00
|
|
|
time_t ts_start = atoi(buffer+1);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
|
|
|
stream.close();
|
|
|
|
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogDebug, "LivestatusLogUtility")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Indexing log file: '" << path << "' with timestamp start: '" << ts_start << "'.";
|
2013-12-17 12:58:04 +01:00
|
|
|
|
|
|
|
index[ts_start] = path;
|
|
|
|
}
|
|
|
|
|
2014-05-28 14:25:12 +02:00
|
|
|
void LivestatusLogUtility::CreateLogCache(std::map<time_t, String> index, HistoryTable *table,
|
2017-12-19 15:50:05 +01:00
|
|
|
time_t from, time_t until, const AddRowFunction& addRowFn)
|
2013-12-17 12:58:04 +01:00
|
|
|
{
|
2013-12-18 16:06:39 +01:00
|
|
|
ASSERT(table);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
|
|
|
/* m_LogFileIndex map tells which log files are involved ordered by their start timestamp */
|
|
|
|
unsigned long line_count = 0;
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const auto& kv : index) {
|
|
|
|
unsigned int ts = kv.first;
|
|
|
|
|
2013-12-17 12:58:04 +01:00
|
|
|
/* 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 */
|
|
|
|
|
2014-05-28 14:25:12 +02:00
|
|
|
Dictionary::Ptr log_entry_attrs = LivestatusLogUtility::GetAttributes(line);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
|
|
|
/* no attributes available - invalid log line */
|
2013-12-18 16:06:39 +01:00
|
|
|
if (!log_entry_attrs) {
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogDebug, "LivestatusLogUtility")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Skipping invalid log line: '" << line << "'.";
|
2013-12-17 12:58:04 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-12-18 17:19:16 +01:00
|
|
|
table->UpdateLogEntries(log_entry_attrs, line_count, lineno, addRowFn);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
|
|
|
line_count++;
|
|
|
|
lineno++;
|
|
|
|
}
|
|
|
|
|
|
|
|
fp.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 14:25:12 +02:00
|
|
|
Dictionary::Ptr LivestatusLogUtility::GetAttributes(const String& text)
|
2013-12-17 12:58:04 +01:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr bag = new Dictionary();
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2014-08-17 17:57:20 +02:00
|
|
|
/*
|
|
|
|
* [1379025342] SERVICE NOTIFICATION: contactname;hostname;servicedesc;WARNING;true;foo output
|
|
|
|
*/
|
|
|
|
unsigned long time = atoi(text.SubStr(1, 11).CStr());
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogDebug, "LivestatusLogUtility")
|
2017-12-19 15:50:05 +01:00
|
|
|
<< "Processing log line: '" << text << "'.";
|
2014-08-17 17:57:20 +02:00
|
|
|
bag->Set("time", time);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2014-08-17 17:57:20 +02:00
|
|
|
size_t colon = text.FindFirstOf(':');
|
|
|
|
size_t colon_offset = colon - 13;
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2015-08-27 18:06:20 +02:00
|
|
|
String type = String(text.SubStr(13, colon_offset)).Trim();
|
|
|
|
String options = String(text.SubStr(colon + 1)).Trim();
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2014-08-17 17:57:20 +02:00
|
|
|
bag->Set("type", type);
|
|
|
|
bag->Set("options", options);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2018-01-04 18:24:45 +01:00
|
|
|
std::vector<String> tokens = options.Split(";");
|
2013-12-17 12:58:04 +01:00
|
|
|
|
|
|
|
/* set default values */
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassInfo);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", 0);
|
2013-12-17 12:58:04 +01:00
|
|
|
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") ||
|
2017-12-19 15:50:05 +01:00
|
|
|
type.Contains("CURRENT HOST STATE") ||
|
|
|
|
type.Contains("HOST ALERT")) {
|
2013-12-17 12:58:04 +01:00
|
|
|
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")) {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassState);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeHostInitialState);
|
2013-12-17 12:58:04 +01:00
|
|
|
}
|
|
|
|
else if (type.Contains("CURRENT HOST STATE")) {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassState);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeHostCurrentState);
|
2013-12-17 12:58:04 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassAlert);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeHostAlert);
|
2013-12-17 12:58:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return bag;
|
2017-12-19 15:50:05 +01:00
|
|
|
} else if (type.Contains("HOST DOWNTIME ALERT") || type.Contains("HOST FLAPPING ALERT")) {
|
2013-12-17 12:58:04 +01:00
|
|
|
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")) {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassAlert);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeHostFlapping);
|
2013-12-17 12:58:04 +01:00
|
|
|
} else {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassAlert);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeHostDowntimeAlert);
|
2013-12-17 12:58:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return bag;
|
2013-12-18 16:06:39 +01:00
|
|
|
} else if (type.Contains("INITIAL SERVICE STATE") ||
|
2017-12-19 15:50:05 +01:00
|
|
|
type.Contains("CURRENT SERVICE STATE") ||
|
|
|
|
type.Contains("SERVICE ALERT")) {
|
2013-12-17 12:58:04 +01:00
|
|
|
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")) {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassState);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeServiceInitialState);
|
2013-12-17 12:58:04 +01:00
|
|
|
}
|
|
|
|
else if (type.Contains("CURRENT SERVICE STATE")) {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassState);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeServiceCurrentState);
|
2013-12-17 12:58:04 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassAlert);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeServiceAlert);
|
2013-12-17 12:58:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return bag;
|
2013-12-18 16:06:39 +01:00
|
|
|
} else if (type.Contains("SERVICE DOWNTIME ALERT") ||
|
2017-12-19 15:50:05 +01:00
|
|
|
type.Contains("SERVICE FLAPPING ALERT")) {
|
2013-12-17 12:58:04 +01:00
|
|
|
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")) {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassAlert);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeServiceFlapping);
|
2013-12-17 12:58:04 +01:00
|
|
|
} else {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassAlert);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeServiceDowntimeAlert);
|
2013-12-17 12:58:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return bag;
|
2013-12-18 16:06:39 +01:00
|
|
|
} else if (type.Contains("TIMEPERIOD TRANSITION")) {
|
2013-12-17 12:58:04 +01:00
|
|
|
if (tokens.size() < 4)
|
|
|
|
return bag;
|
|
|
|
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassState);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeTimeperiodTransition);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
|
|
|
bag->Set("host_name", tokens[0]);
|
|
|
|
bag->Set("service_description", tokens[1]);
|
|
|
|
bag->Set("state_type", tokens[2]);
|
|
|
|
bag->Set("comment", tokens[3]);
|
2013-12-18 16:06:39 +01:00
|
|
|
} else if (type.Contains("HOST NOTIFICATION")) {
|
2013-12-17 12:58:04 +01:00
|
|
|
if (tokens.size() < 6)
|
|
|
|
return bag;
|
|
|
|
|
|
|
|
bag->Set("contact_name", tokens[0]);
|
|
|
|
bag->Set("host_name", tokens[1]);
|
2013-12-18 17:19:16 +01:00
|
|
|
bag->Set("state_type", tokens[2].CStr());
|
2014-08-17 17:57:20 +02:00
|
|
|
bag->Set("state", Service::StateFromString(tokens[3]));
|
2013-12-18 17:19:16 +01:00
|
|
|
bag->Set("command_name", tokens[4]);
|
2013-12-17 12:58:04 +01:00
|
|
|
bag->Set("plugin_output", tokens[5]);
|
|
|
|
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassNotification);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeHostNotification);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
|
|
|
return bag;
|
2013-12-18 16:06:39 +01:00
|
|
|
} else if (type.Contains("SERVICE NOTIFICATION")) {
|
2013-12-17 12:58:04 +01:00
|
|
|
if (tokens.size() < 7)
|
|
|
|
return bag;
|
|
|
|
|
|
|
|
bag->Set("contact_name", tokens[0]);
|
|
|
|
bag->Set("host_name", tokens[1]);
|
2014-08-17 17:57:20 +02:00
|
|
|
bag->Set("service_description", tokens[2]);
|
2013-12-18 17:19:16 +01:00
|
|
|
bag->Set("state_type", tokens[3].CStr());
|
2014-08-17 17:57:20 +02:00
|
|
|
bag->Set("state", Service::StateFromString(tokens[4]));
|
2013-12-18 17:19:16 +01:00
|
|
|
bag->Set("command_name", tokens[5]);
|
2013-12-17 12:58:04 +01:00
|
|
|
bag->Set("plugin_output", tokens[6]);
|
|
|
|
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassNotification);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeServiceNotification);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
|
|
|
return bag;
|
2013-12-18 16:06:39 +01:00
|
|
|
} else if (type.Contains("PASSIVE HOST CHECK")) {
|
2013-12-17 12:58:04 +01:00
|
|
|
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]);
|
|
|
|
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassPassive);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2014-08-17 17:57:20 +02:00
|
|
|
return bag;
|
2013-12-18 16:06:39 +01:00
|
|
|
} else if (type.Contains("PASSIVE SERVICE CHECK")) {
|
2013-12-17 12:58:04 +01:00
|
|
|
if (tokens.size() < 4)
|
|
|
|
return bag;
|
|
|
|
|
|
|
|
bag->Set("host_name", tokens[0]);
|
2014-08-17 17:57:20 +02:00
|
|
|
bag->Set("service_description", tokens[1]);
|
2013-12-17 12:58:04 +01:00
|
|
|
bag->Set("state", Host::StateFromString(tokens[2]));
|
|
|
|
bag->Set("plugin_output", tokens[3]);
|
|
|
|
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassPassive);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2014-08-17 17:57:20 +02:00
|
|
|
return bag;
|
2013-12-18 16:06:39 +01:00
|
|
|
} else if (type.Contains("EXTERNAL COMMAND")) {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassCommand);
|
2013-12-17 12:58:04 +01:00
|
|
|
/* string processing not implemented in 1.x */
|
|
|
|
|
2014-08-17 17:57:20 +02:00
|
|
|
return bag;
|
2013-12-18 16:06:39 +01:00
|
|
|
} else if (type.Contains("LOG VERSION")) {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassProgram);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeVersion);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2014-08-17 17:57:20 +02:00
|
|
|
return bag;
|
2013-12-18 16:06:39 +01:00
|
|
|
} else if (type.Contains("logging initial states")) {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassProgram);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeInitialStates);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2014-08-17 17:57:20 +02:00
|
|
|
return bag;
|
2013-12-18 16:06:39 +01:00
|
|
|
} else if (type.Contains("starting... (PID=")) {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassProgram);
|
2015-12-02 09:55:49 +01:00
|
|
|
bag->Set("log_type", LogEntryTypeProgramStarting);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2014-08-17 17:57:20 +02:00
|
|
|
return bag;
|
2013-12-17 12:58:04 +01:00
|
|
|
}
|
|
|
|
/* program */
|
|
|
|
else if (type.Contains("restarting...") ||
|
2017-12-19 15:50:05 +01:00
|
|
|
type.Contains("shutting down...") ||
|
|
|
|
type.Contains("Bailing out") ||
|
|
|
|
type.Contains("active mode...") ||
|
|
|
|
type.Contains("standby mode...")) {
|
2015-11-26 19:15:54 +01:00
|
|
|
bag->Set("class", LogEntryClassProgram);
|
2013-12-17 12:58:04 +01:00
|
|
|
|
2014-08-17 17:57:20 +02:00
|
|
|
return bag;
|
2013-12-17 12:58:04 +01:00
|
|
|
}
|
|
|
|
|
2014-08-17 17:57:20 +02:00
|
|
|
return bag;
|
2013-12-17 12:58:04 +01:00
|
|
|
}
|