2013-07-09 17:46:48 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-01-09 00:32:11 +01:00
|
|
|
* Copyright (C) 2012-present Icinga Development Team (http://www.icinga.org) *
|
2013-07-09 17:46:48 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "livestatus/logtable.h"
|
2013-12-17 12:58:04 +01:00
|
|
|
#include "livestatus/logutility.h"
|
2013-10-29 13:44:43 +01:00
|
|
|
#include "livestatus/hoststable.h"
|
|
|
|
#include "livestatus/servicestable.h"
|
|
|
|
#include "livestatus/contactstable.h"
|
|
|
|
#include "livestatus/commandstable.h"
|
2013-07-09 17:46:48 +02:00
|
|
|
#include "icinga/icingaapplication.h"
|
|
|
|
#include "icinga/cib.h"
|
2013-10-29 13:44:43 +01:00
|
|
|
#include "icinga/service.h"
|
|
|
|
#include "icinga/host.h"
|
|
|
|
#include "icinga/user.h"
|
|
|
|
#include "icinga/checkcommand.h"
|
|
|
|
#include "icinga/eventcommand.h"
|
|
|
|
#include "icinga/notificationcommand.h"
|
|
|
|
#include "base/convert.h"
|
|
|
|
#include "base/utility.h"
|
|
|
|
#include "base/logger_fwd.h"
|
|
|
|
#include "base/application.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/split.hpp>
|
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <fstream>
|
2013-07-09 17:46:48 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-12-18 16:06:39 +01:00
|
|
|
LogTable::LogTable(const String& compat_log_path, time_t from, time_t until)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-12-18 16:28:33 +01:00
|
|
|
/* store attributes for FetchRows */
|
2013-10-29 13:44:43 +01:00
|
|
|
m_TimeFrom = from;
|
|
|
|
m_TimeUntil = until;
|
2013-12-18 16:28:33 +01:00
|
|
|
m_CompatLogPath = compat_log_path;
|
2013-10-29 13:44:43 +01:00
|
|
|
|
2013-07-09 17:46:48 +02:00
|
|
|
AddColumns(this);
|
|
|
|
}
|
|
|
|
|
2013-12-17 12:58:04 +01:00
|
|
|
|
|
|
|
|
2013-07-09 17:46:48 +02:00
|
|
|
void LogTable::AddColumns(Table *table, const String& prefix,
|
|
|
|
const Column::ObjectAccessor& objectAccessor)
|
|
|
|
{
|
|
|
|
table->AddColumn(prefix + "time", Column(&LogTable::TimeAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "lineno", Column(&LogTable::LinenoAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "class", Column(&LogTable::ClassAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "message", Column(&LogTable::MessageAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "type", Column(&LogTable::TypeAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "options", Column(&LogTable::OptionsAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "comment", Column(&LogTable::CommentAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "plugin_output", Column(&LogTable::PluginOutputAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "state", Column(&LogTable::StateAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "state_type", Column(&LogTable::StateTypeAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "attempt", Column(&LogTable::AttemptAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "service_description", Column(&LogTable::ServiceDescriptionAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "host_name", Column(&LogTable::HostNameAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "contact_name", Column(&LogTable::ContactNameAccessor, objectAccessor));
|
|
|
|
table->AddColumn(prefix + "command_name", Column(&LogTable::CommandNameAccessor, objectAccessor));
|
|
|
|
|
2013-10-29 13:44:43 +01:00
|
|
|
HostsTable::AddColumns(table, "current_host_", boost::bind(&LogTable::HostAccessor, _1, objectAccessor));
|
|
|
|
ServicesTable::AddColumns(table, "current_service_", boost::bind(&LogTable::ServiceAccessor, _1, objectAccessor));
|
|
|
|
ContactsTable::AddColumns(table, "current_contact_", boost::bind(&LogTable::ContactAccessor, _1, objectAccessor));
|
|
|
|
CommandsTable::AddColumns(table, "current_command_", boost::bind(&LogTable::CommandAccessor, _1, objectAccessor));
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String LogTable::GetName(void) const
|
|
|
|
{
|
2013-07-09 18:05:47 +02:00
|
|
|
return "log";
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogTable::FetchRows(const AddRowFunction& addRowFn)
|
|
|
|
{
|
2013-12-18 16:28:33 +01:00
|
|
|
Log(LogInformation, "livestatus", "Pre-selecting log file from " + Convert::ToString(m_TimeFrom) + " until " + Convert::ToString(m_TimeUntil));
|
|
|
|
|
|
|
|
/* create log file index */
|
|
|
|
LogUtility::CreateLogIndex(m_CompatLogPath, m_LogFileIndex);
|
|
|
|
|
|
|
|
/* generate log cache */
|
2013-12-18 17:19:16 +01:00
|
|
|
LogUtility::CreateLogCache(m_LogFileIndex, this, m_TimeFrom, m_TimeUntil, addRowFn);
|
|
|
|
}
|
2013-12-18 16:28:33 +01:00
|
|
|
|
2013-12-18 17:19:16 +01:00
|
|
|
/* gets called in LogUtility::CreateLogCache */
|
|
|
|
void LogTable::UpdateLogEntries(const Dictionary::Ptr& log_entry_attrs, int line_count, int lineno, const AddRowFunction& addRowFn)
|
|
|
|
{
|
|
|
|
/* additional attributes only for log table */
|
|
|
|
log_entry_attrs->Set("lineno", lineno);
|
2013-10-29 13:44:43 +01:00
|
|
|
|
2013-12-18 17:19:16 +01:00
|
|
|
addRowFn(log_entry_attrs);
|
2013-10-29 13:44:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Object::Ptr LogTable::HostAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor)
|
|
|
|
{
|
|
|
|
String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
|
2013-07-09 17:46:48 +02:00
|
|
|
|
2013-10-29 13:44:43 +01:00
|
|
|
if (host_name.IsEmpty())
|
|
|
|
return Object::Ptr();
|
|
|
|
|
|
|
|
return Host::GetByName(host_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object::Ptr LogTable::ServiceAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor)
|
|
|
|
{
|
|
|
|
String host_name = static_cast<Dictionary::Ptr>(row)->Get("host_name");
|
|
|
|
String service_description = static_cast<Dictionary::Ptr>(row)->Get("service_description");
|
|
|
|
|
|
|
|
if (service_description.IsEmpty() || host_name.IsEmpty())
|
|
|
|
return Object::Ptr();
|
|
|
|
|
|
|
|
return Service::GetByNamePair(host_name, service_description);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object::Ptr LogTable::ContactAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor)
|
|
|
|
{
|
|
|
|
String contact_name = static_cast<Dictionary::Ptr>(row)->Get("contact_name");
|
|
|
|
|
|
|
|
if (contact_name.IsEmpty())
|
|
|
|
return Object::Ptr();
|
|
|
|
|
|
|
|
return User::GetByName(contact_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object::Ptr LogTable::CommandAccessor(const Value& row, const Column::ObjectAccessor& parentObjectAccessor)
|
|
|
|
{
|
|
|
|
String command_name = static_cast<Dictionary::Ptr>(row)->Get("command_name");
|
|
|
|
|
|
|
|
if (command_name.IsEmpty())
|
|
|
|
return Object::Ptr();
|
|
|
|
|
|
|
|
CheckCommand::Ptr check_command = CheckCommand::GetByName(command_name);
|
|
|
|
if (!check_command) {
|
|
|
|
EventCommand::Ptr event_command = EventCommand::GetByName(command_name);
|
|
|
|
if (!event_command) {
|
|
|
|
NotificationCommand::Ptr notification_command = NotificationCommand::GetByName(command_name);
|
|
|
|
if (!notification_command)
|
|
|
|
return Object::Ptr();
|
|
|
|
else
|
|
|
|
return notification_command;
|
|
|
|
} else
|
|
|
|
return event_command;
|
|
|
|
} else
|
|
|
|
return check_command;
|
|
|
|
|
|
|
|
return Object::Ptr();
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::TimeAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("time");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::LinenoAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("lineno");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::ClassAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("class");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::MessageAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("message");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::TypeAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("type");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::OptionsAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("options");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::CommentAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("comment");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::PluginOutputAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("plugin_output");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::StateAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("state");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::StateTypeAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("state_type");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::AttemptAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("attempt");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::ServiceDescriptionAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("service_description");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::HostNameAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("host_name");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::ContactNameAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("contact_name");
|
2013-07-09 17:46:48 +02:00
|
|
|
}
|
|
|
|
|
2013-07-10 14:06:46 +02:00
|
|
|
Value LogTable::CommandNameAccessor(const Value& row)
|
2013-07-09 17:46:48 +02:00
|
|
|
{
|
2013-10-29 13:44:43 +01:00
|
|
|
return static_cast<Dictionary::Ptr>(row)->Get("command_name");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|