Move some of the compat functionality to the CompatUtility class.

This commit is contained in:
Gunnar Beutner 2013-07-23 09:12:38 +02:00
parent cab2b41e56
commit 42cf9ea8d6
5 changed files with 257 additions and 130 deletions

View File

@ -27,6 +27,7 @@
#include "icinga/eventcommand.h" #include "icinga/eventcommand.h"
#include "icinga/timeperiod.h" #include "icinga/timeperiod.h"
#include "icinga/notificationcommand.h" #include "icinga/notificationcommand.h"
#include "icinga/compatutility.h"
#include "base/dynamictype.h" #include "base/dynamictype.h"
#include "base/objectlock.h" #include "base/objectlock.h"
#include "base/convert.h" #include "base/convert.h"
@ -280,10 +281,10 @@ void CompatComponent::DumpCommand(std::ostream& fp, const Command::Ptr& command)
String arg; String arg;
BOOST_FOREACH(arg, args) { BOOST_FOREACH(arg, args) {
// This is obviously incorrect for non-trivial cases. // This is obviously incorrect for non-trivial cases.
fp << " \"" << EscapeString(arg) << "\""; fp << " \"" << CompatUtility::EscapeString(arg) << "\"";
} }
} else if (!commandLine.IsEmpty()) { } else if (!commandLine.IsEmpty()) {
fp << EscapeString(Convert::ToString(commandLine)); fp << CompatUtility::EscapeString(Convert::ToString(commandLine));
} else { } else {
fp << "<internal>"; fp << "<internal>";
} }
@ -418,129 +419,48 @@ void CompatComponent::DumpHostObject(std::ostream& fp, const Host::Ptr& host)
<< "\n"; << "\n";
} }
String CompatComponent::EscapeString(const String& str)
{
String result = str;
boost::algorithm::replace_all(result, "\n", "\\n");
return result;
}
void CompatComponent::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type) void CompatComponent::DumpServiceStatusAttrs(std::ostream& fp, const Service::Ptr& service, CompatObjectType type)
{ {
ASSERT(service->OwnsLock()); Dictionary::Ptr attrs = CompatUtility::GetServiceStatusAttributes(service, type);
String raw_output; fp << "\t" << "check_command=" << attrs->Get("check_command") << "\n"
String output; << "\t" << "event_handler=" << attrs->Get("event_handler") << "\n"
String long_output; << "\t" << "check_period=" << attrs->Get("check_period") << "\n"
String perfdata; << "\t" << "check_interval=" << attrs->Get("check_interval") << "\n"
double schedule_end = -1; << "\t" << "retry_interval=" << attrs->Get("retry_interval") << "\n"
<< "\t" << "has_been_checked=" << attrs->Get("has_been_checked") << "\n"
String check_period_str; << "\t" << "should_be_scheduled=" << attrs->Get("should_be_scheduled") << "\n"
TimePeriod::Ptr check_period = service->GetCheckPeriod(); << "\t" << "check_execution_time=" << attrs->Get("check_execution_time") << "\n"
if (check_period) << "\t" << "check_latency=" << attrs->Get("check_latency") << "\n"
check_period_str = check_period->GetName(); << "\t" << "current_state=" << attrs->Get("current_state") << "\n"
else << "\t" << "state_type=" << attrs->Get("state_type") << "\n"
check_period_str = "24x7"; << "\t" << "plugin_output=" << attrs->Get("plugin_output") << "\n"
<< "\t" << "long_plugin_output=" << attrs->Get("long_plugin_output") << "\n"
Dictionary::Ptr cr = service->GetLastCheckResult(); << "\t" << "performance_data=" << attrs->Get("performance_data") << "\n"
<< "\t" << "last_check=" << attrs->Get("last_check") << "\n"
if (cr) { << "\t" << "next_check=" << attrs->Get("next_check") << "\n"
raw_output = cr->Get("output"); << "\t" << "current_attempt=" << attrs->Get("current_attempt") << "\n"
size_t line_end = raw_output.Find("\n"); << "\t" << "max_attempts=" << attrs->Get("max_attempts") << "\n"
<< "\t" << "last_state_change=" << attrs->Get("last_state_change") << "\n"
output = raw_output.SubStr(0, line_end); << "\t" << "last_hard_state_change=" << attrs->Get("last_hard_state_change") << "\n"
<< "\t" << "last_time_ok=" << attrs->Get("last_time_ok") << "\n"
if (line_end > 0 && line_end != String::NPos) { << "\t" << "last_time_warn=" << attrs->Get("last_time_warn") << "\n"
long_output = raw_output.SubStr(line_end+1, raw_output.GetLength()); << "\t" << "last_time_critical=" << attrs->Get("last_time_critical") << "\n"
long_output = EscapeString(long_output); << "\t" << "last_time_unknown=" << attrs->Get("last_time_unknown") << "\n"
} << "\t" << "last_update=" << attrs->Get("last_update") << "\n"
<< "\t" << "notifications_enabled=" << attrs->Get("notifications_enabled") << "\n"
boost::algorithm::replace_all(output, "\n", "\\n"); << "\t" << "active_checks_enabled=" << attrs->Get("active_checks_enabled") << "\n"
<< "\t" << "passive_checks_enabled=" << attrs->Get("passive_checks_enabled") << "\n"
schedule_end = cr->Get("schedule_end"); << "\t" << "flap_detection_enabled=" << attrs->Get("flap_detection_enabled") << "\n"
<< "\t" << "is_flapping=" << attrs->Get("is_flapping") << "\n"
perfdata = cr->Get("performance_data_raw"); << "\t" << "percent_state_change=" << attrs->Get("percent_state_change") << "\n"
boost::algorithm::replace_all(perfdata, "\n", "\\n"); << "\t" << "problem_has_been_acknowledged=" << attrs->Get("problem_has_been_acknowledged") << "\n"
} << "\t" << "acknowledgement_type=" << attrs->Get("acknowledgement_type") << "\n"
<< "\t" << "acknowledgement_end_time=" << attrs->Get("acknowledgement_end_time") << "\n"
int state = service->GetState(); << "\t" << "scheduled_downtime_depth=" << attrs->Get("scheduled_downtime_depth") << "\n"
<< "\t" << "last_notification=" << attrs->Get("last_notification") << "\n"
if (state > StateUnknown) << "\t" << "next_notification=" << attrs->Get("next_notification") << "\n"
state = StateUnknown; << "\t" << "current_notification_number=" << attrs->Get("current_notification_number") << "\n";
if (type == CompatTypeHost) {
if (state == StateOK || state == StateWarning)
state = 0; /* UP */
else
state = 1; /* DOWN */
Host::Ptr host = service->GetHost();
if (!host)
return;
if (!host->IsReachable())
state = 2; /* UNREACHABLE */
}
double last_notification = 0;
double next_notification = 0;
int notification_number = 0;
BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
if (notification->GetLastNotification() > last_notification)
last_notification = notification->GetLastNotification();
if (notification->GetNextNotification() < next_notification)
next_notification = notification->GetNextNotification();
if (notification->GetNotificationNumber() > notification_number)
notification_number = notification->GetNotificationNumber();
}
CheckCommand::Ptr checkcommand = service->GetCheckCommand();
if (checkcommand)
fp << "\t" << "check_command=check_" << checkcommand->GetName() << "\n";
EventCommand::Ptr eventcommand = service->GetEventCommand();
if (eventcommand)
fp << "\t" << "event_handler=event_" << eventcommand->GetName() << "\n";
fp << "\t" << "check_period=" << check_period_str << "\n"
<< "\t" << "check_interval=" << service->GetCheckInterval() / 60.0 << "\n"
<< "\t" << "retry_interval=" << service->GetRetryInterval() / 60.0 << "\n"
<< "\t" << "has_been_checked=" << (service->GetLastCheckResult() ? 1 : 0) << "\n"
<< "\t" << "should_be_scheduled=1" << "\n"
<< "\t" << "check_execution_time=" << Service::CalculateExecutionTime(cr) << "\n"
<< "\t" << "check_latency=" << Service::CalculateLatency(cr) << "\n"
<< "\t" << "current_state=" << state << "\n"
<< "\t" << "state_type=" << service->GetStateType() << "\n"
<< "\t" << "plugin_output=" << output << "\n"
<< "\t" << "long_plugin_output=" << long_output << "\n"
<< "\t" << "performance_data=" << perfdata << "\n"
<< "\t" << "last_check=" << schedule_end << "\n"
<< "\t" << "next_check=" << service->GetNextCheck() << "\n"
<< "\t" << "current_attempt=" << service->GetCurrentCheckAttempt() << "\n"
<< "\t" << "max_attempts=" << service->GetMaxCheckAttempts() << "\n"
<< "\t" << "last_state_change=" << service->GetLastStateChange() << "\n"
<< "\t" << "last_hard_state_change=" << service->GetLastHardStateChange() << "\n"
<< "\t" << "last_time_ok=" << service->GetLastStateOK() << "\n"
<< "\t" << "last_time_warn=" << service->GetLastStateWarning() << "\n"
<< "\t" << "last_time_critical=" << service->GetLastStateCritical() << "\n"
<< "\t" << "last_time_unknown=" << service->GetLastStateUnknown() << "\n"
<< "\t" << "last_update=" << time(NULL) << "\n"
<< "\t" << "notifications_enabled=" << (service->GetEnableNotifications() ? 1 : 0) << "\n"
<< "\t" << "active_checks_enabled=" << (service->GetEnableActiveChecks() ? 1 : 0) <<"\n"
<< "\t" << "passive_checks_enabled=" << (service->GetEnablePassiveChecks() ? 1 : 0) << "\n"
<< "\t" << "flap_detection_enabled=" << "\t" << (service->GetEnableFlapping() ? 1 : 0) << "\n"
<< "\t" << "is_flapping=" << "\t" << (service->IsFlapping() ? 1 : 0) << "\n"
<< "\t" << "percent_state_change=" << "\t" << Convert::ToString(service->GetFlappingCurrent()) << "\n"
<< "\t" << "problem_has_been_acknowledged=" << (service->GetAcknowledgement() != AcknowledgementNone ? 1 : 0) << "\n"
<< "\t" << "acknowledgement_type=" << static_cast<int>(service->GetAcknowledgement()) << "\n"
<< "\t" << "acknowledgement_end_time=" << service->GetAcknowledgementExpiry() << "\n"
<< "\t" << "scheduled_downtime_depth=" << (service->IsInDowntime() ? 1 : 0) << "\n"
<< "\t" << "last_notification=" << last_notification << "\n"
<< "\t" << "next_notification=" << next_notification << "\n"
<< "\t" << "current_notification_number=" << notification_number << "\n";
} }
void CompatComponent::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service) void CompatComponent::DumpServiceStatus(std::ostream& fp, const Service::Ptr& service)

View File

@ -23,6 +23,7 @@
#include "icinga/host.h" #include "icinga/host.h"
#include "icinga/service.h" #include "icinga/service.h"
#include "icinga/command.h" #include "icinga/command.h"
#include "icinga/compatutility.h"
#include "base/dynamicobject.h" #include "base/dynamicobject.h"
#include "base/objectlock.h" #include "base/objectlock.h"
#include "base/timer.h" #include "base/timer.h"
@ -33,12 +34,6 @@
namespace icinga namespace icinga
{ {
enum CompatObjectType
{
CompatTypeService,
CompatTypeHost
};
/** /**
* @ingroup compat * @ingroup compat
*/ */
@ -114,8 +109,6 @@ private:
void DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object); void DumpCustomAttributes(std::ostream& fp, const DynamicObject::Ptr& object);
void StatusTimerHandler(void); void StatusTimerHandler(void);
static String EscapeString(const String& str);
}; };
} }

View File

@ -21,6 +21,8 @@ libicinga_la_SOURCES = \
cib.h \ cib.h \
command.cpp \ command.cpp \
command.h \ command.h \
compatutility.cpp \
compatutility.h \
downtimemessage.cpp \ downtimemessage.cpp \
downtimemessage.h \ downtimemessage.h \
eventcommand.cpp \ eventcommand.cpp \

View File

@ -0,0 +1,155 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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 "base/convert.h"
#include "icinga/compatutility.h"
#include "icinga/checkcommand.h"
#include "icinga/eventcommand.h"
#include <boost/smart_ptr/make_shared.hpp>
#include <boost/foreach.hpp>
using namespace icinga;
Dictionary::Ptr CompatUtility::GetServiceStatusAttributes(const Service::Ptr& service, CompatObjectType type)
{
Dictionary::Ptr attr = boost::make_shared<Dictionary>();
ASSERT(service->OwnsLock());
String raw_output;
String output;
String long_output;
String perfdata;
double schedule_end = -1;
String check_period_str;
TimePeriod::Ptr check_period = service->GetCheckPeriod();
if (check_period)
check_period_str = check_period->GetName();
else
check_period_str = "24x7";
Dictionary::Ptr cr = service->GetLastCheckResult();
if (cr) {
raw_output = cr->Get("output");
size_t line_end = raw_output.Find("\n");
output = raw_output.SubStr(0, line_end);
if (line_end > 0 && line_end != String::NPos) {
long_output = raw_output.SubStr(line_end+1, raw_output.GetLength());
long_output = EscapeString(long_output);
}
boost::algorithm::replace_all(output, "\n", "\\n");
schedule_end = cr->Get("schedule_end");
perfdata = cr->Get("performance_data_raw");
boost::algorithm::replace_all(perfdata, "\n", "\\n");
}
int state = service->GetState();
if (state > StateUnknown)
state = StateUnknown;
if (type == CompatTypeHost) {
if (state == StateOK || state == StateWarning)
state = 0; /* UP */
else
state = 1; /* DOWN */
Host::Ptr host = service->GetHost();
ASSERT(host);
if (!host->IsReachable())
state = 2; /* UNREACHABLE */
}
double last_notification = 0;
double next_notification = 0;
int notification_number = 0;
BOOST_FOREACH(const Notification::Ptr& notification, service->GetNotifications()) {
if (notification->GetLastNotification() > last_notification)
last_notification = notification->GetLastNotification();
if (notification->GetNextNotification() < next_notification)
next_notification = notification->GetNextNotification();
if (notification->GetNotificationNumber() > notification_number)
notification_number = notification->GetNotificationNumber();
}
CheckCommand::Ptr checkcommand = service->GetCheckCommand();
if (checkcommand)
attr->Set("check_command", "check_" + checkcommand->GetName());
EventCommand::Ptr eventcommand = service->GetEventCommand();
if (eventcommand)
attr->Set("event_handler", "event_" + eventcommand->GetName());
attr->Set("check_period", check_period_str);
attr->Set("check_interval", service->GetCheckInterval() / 60.0);
attr->Set("retry_interval", service->GetRetryInterval() / 60.0);
attr->Set("has_been_checked", (service->GetLastCheckResult() ? 1 : 0));
attr->Set("should_be_scheduled", 1);
attr->Set("check_execution_time", Service::CalculateExecutionTime(cr));
attr->Set("check_latency", Service::CalculateLatency(cr));
attr->Set("current_state", state);
attr->Set("state_type", service->GetStateType());
attr->Set("plugin_output", output);
attr->Set("long_plugin_output", long_output);
attr->Set("performance_data", perfdata);
attr->Set("last_check", schedule_end);
attr->Set("next_check", service->GetNextCheck());
attr->Set("current_attempt", service->GetCurrentCheckAttempt());
attr->Set("max_attempts", service->GetMaxCheckAttempts());
attr->Set("last_state_change", service->GetLastStateChange());
attr->Set("last_hard_state_change", service->GetLastHardStateChange());
attr->Set("last_time_ok", service->GetLastStateOK());
attr->Set("last_time_warn", service->GetLastStateWarning());
attr->Set("last_time_critical", service->GetLastStateCritical());
attr->Set("last_time_unknown", service->GetLastStateUnknown());
attr->Set("last_update", time(NULL));
attr->Set("notifications_enabled", (service->GetEnableNotifications() ? 1 : 0));
attr->Set("active_checks_enabled", (service->GetEnableActiveChecks() ? 1 : 0));
attr->Set("passive_checks_enabled", (service->GetEnablePassiveChecks() ? 1 : 0));
attr->Set("flap_detection_enabled", (service->GetEnableFlapping() ? 1 : 0));
attr->Set("is_flapping", (service->IsFlapping() ? 1 : 0));
attr->Set("percent_state_change", Convert::ToString(service->GetFlappingCurrent()));
attr->Set("problem_has_been_acknowledged", (service->GetAcknowledgement() != AcknowledgementNone ? 1 : 0));
attr->Set("acknowledgement_type", static_cast<int>(service->GetAcknowledgement()));
attr->Set("acknowledgement_end_time", service->GetAcknowledgementExpiry());
attr->Set("scheduled_downtime_depth", (service->IsInDowntime() ? 1 : 0));
attr->Set("last_notification", last_notification);
attr->Set("next_notification", next_notification);
attr->Set("current_notification_number", notification_number);;
return attr;
}
String CompatUtility::EscapeString(const String& str)
{
String result = str;
boost::algorithm::replace_all(result, "\n", "\\n");
return result;
}

View File

@ -0,0 +1,57 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* 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. *
******************************************************************************/
#ifndef COMPATUTILITY_H
#define COMPATUTILITY_H
#include "icinga/i2-icinga.h"
#include "icinga/service.h"
#include "base/dictionary.h"
#include <vector>
namespace icinga
{
/**
* @ingroup icinga
*/
enum CompatObjectType
{
CompatTypeService,
CompatTypeHost
};
/**
* Compatibility utility functions.
*
* @ingroup icinga
*/
class I2_ICINGA_API CompatUtility
{
public:
static Dictionary::Ptr GetServiceStatusAttributes(const Service::Ptr& service, CompatObjectType type);
static String EscapeString(const String& str);
private:
CompatUtility(void);
};
}
#endif /* COMPATUTILITY_H */