From 80bec5fcd43d0a1c1548c3c41176f841c5fc045d Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Fri, 29 Jun 2012 14:14:51 +0200 Subject: [PATCH] Performance fixes for the compat module. Split plugin output/perfdata. --- components/compat/compatcomponent.cpp | 193 ++++++++++++++------------ icinga/checkresult.cpp | 12 ++ icinga/checkresult.h | 3 + icinga/i2-icinga.h | 5 +- icinga/nagioschecktask.cpp | 35 ++++- icinga/nagioschecktask.h | 1 + icinga/service.cpp | 10 +- icinga/service.h | 10 +- 8 files changed, 163 insertions(+), 106 deletions(-) diff --git a/components/compat/compatcomponent.cpp b/components/compat/compatcomponent.cpp index 4ac947d81..f356de4e8 100644 --- a/components/compat/compatcomponent.cpp +++ b/components/compat/compatcomponent.cpp @@ -21,6 +21,12 @@ using namespace icinga; +/** + * Hint: The reason why we're using "\n" rather than std::endl is because + * std::endl also _flushes_ the output stream which severely degrades + * performance (see http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html). + */ + /** * Returns the name of the component. * @@ -54,36 +60,36 @@ void CompatComponent::Stop(void) void CompatComponent::DumpHostStatus(ofstream& fp, Host host) { - fp << "hoststatus {" << endl - << "\t" << "host_name=" << host.GetName() << endl - << "\t" << "has_been_checked=1" << endl - << "\t" << "should_be_scheduled=1" << endl - << "\t" << "check_execution_time=0" << endl - << "\t" << "check_latency=0" << endl - << "\t" << "current_state=0" << endl - << "\t" << "state_type=1" << endl - << "\t" << "last_check=" << time(NULL) << endl - << "\t" << "next_check=" << time(NULL) << endl - << "\t" << "current_attempt=1" << endl - << "\t" << "max_attempts=1" << endl - << "\t" << "active_checks_enabled=1" << endl - << "\t" << "passive_checks_enabled=1" << endl - << "\t" << "}" << endl - << endl; + fp << "hoststatus {" << "\n" + << "\t" << "host_name=" << host.GetName() << "\n" + << "\t" << "has_been_checked=1" << "\n" + << "\t" << "should_be_scheduled=1" << "\n" + << "\t" << "check_execution_time=0" << "\n" + << "\t" << "check_latency=0" << "\n" + << "\t" << "current_state=0" << "\n" + << "\t" << "state_type=1" << "\n" + << "\t" << "last_check=" << time(NULL) << "\n" + << "\t" << "next_check=" << time(NULL) << "\n" + << "\t" << "current_attempt=1" << "\n" + << "\t" << "max_attempts=1" << "\n" + << "\t" << "active_checks_enabled=1" << "\n" + << "\t" << "passive_checks_enabled=1" << "\n" + << "\t" << "}" << "\n" + << "\n"; } void CompatComponent::DumpHostObject(ofstream& fp, Host host) { - fp << "define host {" << endl - << "\t" << "host_name" << "\t" << host.GetName() << endl - << "\t" << "hostgroups" << "\t" << "all-hosts" << endl - << "\t" << "check_interval" << "\t" << 1 << endl - << "\t" << "retry_interval" << "\t" << 1 << endl - << "\t" << "max_check_attempts" << "\t" << 1 << endl - << "\t" << "active_checks_enabled" << "\t" << 1 << endl - << "\t" << "passive_checks_enabled" << "\t" << 1 << endl - << "\t" << "}" << endl - << endl; + fp << "define host {" << "\n" + << "\t" << "host_name" << "\t" << host.GetName() << "\n" + << "\t" << "hostgroups" << "\t" << "all-hosts" << "\n" + << "\t" << "check_interval" << "\t" << 1 << "\n" + << "\t" << "retry_interval" << "\t" << 1 << "\n" + << "\t" << "max_check_attempts" << "\t" << 1 << "\n" + << "\t" << "active_checks_enabled" << "\t" << 1 << "\n" + << "\t" << "passive_checks_enabled" << "\t" << 1 << "\n" + << "\t" << "}" << "\n" + << "\n"; } void CompatComponent::DumpServiceStatus(ofstream& fp, Service service) @@ -91,58 +97,61 @@ void CompatComponent::DumpServiceStatus(ofstream& fp, Service service) Dictionary::Ptr cr; cr = service.GetLastCheckResult(); - string plugin_output; + string output; + string perfdata; long schedule_start = -1, schedule_end = -1; long execution_start = -1, execution_end = -1; if (cr) { - cr->GetProperty("output", &plugin_output); + cr->GetProperty("output", &output); cr->GetProperty("schedule_start", &schedule_start); cr->GetProperty("schedule_end", &schedule_end); cr->GetProperty("execution_start", &execution_start); cr->GetProperty("execution_end", &execution_end); + cr->GetProperty("performance_data_raw", &perfdata); } long execution_time = (execution_end - execution_start); long latency = (schedule_end - schedule_start) - execution_time; - fp << "servicestatus {" << endl - << "\t" << "host_name=" << service.GetHost().GetName() << endl - << "\t" << "service_description=" << service.GetAlias() << endl - << "\t" << "check_interval=" << service.GetCheckInterval() / 60.0 << endl - << "\t" << "retry_interval=" << service.GetRetryInterval() / 60.0 << endl - << "\t" << "has_been_checked=" << (cr ? 1 : 0) << endl - << "\t" << "should_be_scheduled=1" << endl - << "\t" << "check_execution_time=" << execution_time << endl - << "\t" << "check_latency=" << latency << endl - << "\t" << "current_state=" << service.GetState() << endl - << "\t" << "state_type=" << service.GetStateType() << endl - << "\t" << "plugin_output=" << plugin_output << endl - << "\t" << "last_check=" << schedule_end << endl - << "\t" << "next_check=" << service.GetNextCheck() << endl - << "\t" << "current_attempt=" << service.GetCurrentCheckAttempt() << endl - << "\t" << "max_attempts=" << service.GetMaxCheckAttempts() << endl - << "\t" << "last_state_change=" << service.GetLastStateChange() << endl - << "\t" << "last_hard_state_change=" << service.GetLastHardStateChange() << endl - << "\t" << "last_update=" << time(NULL) << endl - << "\t" << "active_checks_enabled=1" << endl - << "\t" << "passive_checks_enabled=1" << endl - << "\t" << "}" << endl - << endl; + fp << "servicestatus {" << "\n" + << "\t" << "host_name=" << service.GetHost().GetName() << "\n" + << "\t" << "service_description=" << service.GetAlias() << "\n" + << "\t" << "check_interval=" << service.GetCheckInterval() / 60.0 << "\n" + << "\t" << "retry_interval=" << service.GetRetryInterval() / 60.0 << "\n" + << "\t" << "has_been_checked=" << (cr ? 1 : 0) << "\n" + << "\t" << "should_be_scheduled=1" << "\n" + << "\t" << "check_execution_time=" << execution_time << "\n" + << "\t" << "check_latency=" << latency << "\n" + << "\t" << "current_state=" << service.GetState() << "\n" + << "\t" << "state_type=" << service.GetStateType() << "\n" + << "\t" << "plugin_output=" << 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_update=" << time(NULL) << "\n" + << "\t" << "active_checks_enabled=1" << "\n" + << "\t" << "passive_checks_enabled=1" << "\n" + << "\t" << "}" << "\n" + << "\n"; } void CompatComponent::DumpServiceObject(ofstream& fp, Service service) { - fp << "define service {" << endl - << "\t" << "host_name" << "\t" << service.GetHost().GetName() << endl - << "\t" << "service_description" << "\t" << service.GetAlias() << endl - << "\t" << "check_command" << "\t" << "check_i2" << endl - << "\t" << "check_interval" << "\t" << service.GetCheckInterval() / 60.0 << endl - << "\t" << "retry_interval" << "\t" << service.GetRetryInterval() / 60.0 << endl - << "\t" << "max_check_attempts" << "\t" << 1 << endl - << "\t" << "active_checks_enabled" << "\t" << 1 << endl - << "\t" << "passive_checks_enabled" << "\t" << 1 << endl - << "\t" << "}" << endl - << endl; + fp << "define service {" << "\n" + << "\t" << "host_name" << "\t" << service.GetHost().GetName() << "\n" + << "\t" << "service_description" << "\t" << service.GetAlias() << "\n" + << "\t" << "check_command" << "\t" << "check_i2" << "\n" + << "\t" << "check_interval" << "\t" << service.GetCheckInterval() / 60.0 << "\n" + << "\t" << "retry_interval" << "\t" << service.GetRetryInterval() / 60.0 << "\n" + << "\t" << "max_check_attempts" << "\t" << 1 << "\n" + << "\t" << "active_checks_enabled" << "\t" << 1 << "\n" + << "\t" << "passive_checks_enabled" << "\t" << 1 << "\n" + << "\t" << "}" << "\n" + << "\n"; } /** @@ -153,40 +162,40 @@ void CompatComponent::StatusTimerHandler(void) ofstream statusfp; statusfp.open("status.dat.tmp", ofstream::out | ofstream::trunc); - statusfp << "# Icinga status file" << endl - << "# This file is auto-generated. Do not modify this file." << endl - << endl; + statusfp << "# Icinga status file" << "\n" + << "# This file is auto-generated. Do not modify this file." << "\n" + << "\n"; - statusfp << "info {" << endl - << "\t" << "created=" << time(NULL) << endl - << "\t" << "version=2.0" << endl - << "\t" << "}" << endl - << endl; + statusfp << "info {" << "\n" + << "\t" << "created=" << time(NULL) << "\n" + << "\t" << "version=2.0" << "\n" + << "\t" << "}" << "\n" + << "\n"; - statusfp << "programstatus {" << endl - << "\t" << "daemon_mode=1" << endl - << "\t" << "program_start=" << IcingaApplication::GetInstance()->GetStartTime() << endl - << "\t" << "active_service_checks_enabled=1" << endl - << "\t" << "passive_service_checks_enabled=1" << endl - << "\t" << "active_host_checks_enabled=0" << endl - << "\t" << "passive_host_checks_enabled=0" << endl - << "\t" << "check_service_freshness=1" << endl - << "\t" << "check_host_freshness=0" << endl - << "\t" << "enable_flap_detection=1" << endl - << "\t" << "enable_failure_prediction=0" << endl - << "\t" << "active_scheduled_service_check_stats=" << CIB::GetTaskStatistics(60) << "," << CIB::GetTaskStatistics(5 * 60) << "," << CIB::GetTaskStatistics(15 * 60) << endl - << "\t" << "}" << endl - << endl; + statusfp << "programstatus {" << "\n" + << "\t" << "daemon_mode=1" << "\n" + << "\t" << "program_start=" << IcingaApplication::GetInstance()->GetStartTime() << "\n" + << "\t" << "active_service_checks_enabled=1" << "\n" + << "\t" << "passive_service_checks_enabled=1" << "\n" + << "\t" << "active_host_checks_enabled=0" << "\n" + << "\t" << "passive_host_checks_enabled=0" << "\n" + << "\t" << "check_service_freshness=1" << "\n" + << "\t" << "check_host_freshness=0" << "\n" + << "\t" << "enable_flap_detection=1" << "\n" + << "\t" << "enable_failure_prediction=0" << "\n" + << "\t" << "active_scheduled_service_check_stats=" << CIB::GetTaskStatistics(60) << "," << CIB::GetTaskStatistics(5 * 60) << "," << CIB::GetTaskStatistics(15 * 60) << "\n" + << "\t" << "}" << "\n" + << "\n"; ofstream objectfp; objectfp.open("objects.cache.tmp", ofstream::out | ofstream::trunc); - objectfp << "# Icinga object cache file" << endl - << "# This file is auto-generated. Do not modify this file." << endl - << endl; + objectfp << "# Icinga object cache file" << "\n" + << "# This file is auto-generated. Do not modify this file." << "\n" + << "\n"; - objectfp << "define hostgroup {" << endl - << "\t" << "hostgroup_name" << "\t" << "all-hosts" << endl; + objectfp << "define hostgroup {" << "\n" + << "\t" << "hostgroup_name" << "\t" << "all-hosts" << "\n"; ConfigObject::TMap::Range range; range = ConfigObject::GetObjects("host"); @@ -203,11 +212,11 @@ void CompatComponent::StatusTimerHandler(void) if (distance(it, range.second) != 1) objectfp << ","; } - objectfp << endl; + objectfp << "\n"; } - objectfp << "\t" << "}" << endl - << endl; + objectfp << "\t" << "}" << "\n" + << "\n"; for (it = range.first; it != range.second; it++) { DumpHostStatus(statusfp, it->second); diff --git a/icinga/checkresult.cpp b/icinga/checkresult.cpp index dd365687a..9377dd1a6 100644 --- a/icinga/checkresult.cpp +++ b/icinga/checkresult.cpp @@ -74,6 +74,18 @@ string CheckResult::GetOutput(void) const return value; } +void CheckResult::SetPerformanceDataRaw(const string& pd) +{ + SetProperty("performance_data_raw", pd); +} + +string CheckResult::GetPerformanceDataRaw(void) const +{ + string value; + GetProperty("performance_data_raw", &value); + return value; +} + void CheckResult::SetPerformanceData(const Dictionary::Ptr& pd) { SetProperty("performance_data", pd); diff --git a/icinga/checkresult.h b/icinga/checkresult.h index 049e01cdf..44abdbc76 100644 --- a/icinga/checkresult.h +++ b/icinga/checkresult.h @@ -28,6 +28,9 @@ public: void SetOutput(string output); string GetOutput(void) const; + void SetPerformanceDataRaw(const string& pd); + string GetPerformanceDataRaw(void) const; + void SetPerformanceData(const Dictionary::Ptr& pd); Dictionary::Ptr GetPerformanceData(void) const; }; diff --git a/icinga/i2-icinga.h b/icinga/i2-icinga.h index 690d40e72..35dd1bf1c 100644 --- a/icinga/i2-icinga.h +++ b/icinga/i2-icinga.h @@ -31,9 +31,8 @@ #include #include -#include -using boost::packaged_task; -using boost::unique_future; +using boost::iterator_range; +using boost::algorithm::is_any_of; #ifdef I2_ICINGA_BUILD # define I2_ICINGA_API I2_EXPORT diff --git a/icinga/nagioschecktask.cpp b/icinga/nagioschecktask.cpp index 92857dfdd..30c3de97d 100644 --- a/icinga/nagioschecktask.cpp +++ b/icinga/nagioschecktask.cpp @@ -149,6 +149,39 @@ bool NagiosCheckTask::InitTask(void) return true; } +void NagiosCheckTask::ProcessCheckOutput(const string& output) +{ + string text; + string perfdata; + + vector lines; + boost::algorithm::split(lines, output, is_any_of("\r\n")); + + vector::iterator it; + for (it = lines.begin(); it != lines.end(); it++) { + const string& line = *it; + + string::size_type delim = line.find('|'); + + if (!text.empty()) + text.append("\n"); + + if (delim != string::npos) { + text.append(line, 0, delim - 1); + + if (!perfdata.empty()) + perfdata.append(" "); + + perfdata.append(line, delim + 1, line.size()); + } else { + text.append(line); + } + } + + GetResult().SetOutput(text); + GetResult().SetPerformanceDataRaw(perfdata); +} + bool NagiosCheckTask::RunTask(void) { char buffer[512]; @@ -162,7 +195,7 @@ bool NagiosCheckTask::RunTask(void) string output = m_OutputStream.str(); boost::algorithm::trim(output); - GetResult().SetOutput(output); + ProcessCheckOutput(output); int status, exitcode; #ifdef _MSC_VER diff --git a/icinga/nagioschecktask.h b/icinga/nagioschecktask.h index 422e90181..824f99df1 100644 --- a/icinga/nagioschecktask.h +++ b/icinga/nagioschecktask.h @@ -39,6 +39,7 @@ private: static void CheckThreadProc(void); bool InitTask(void); + void ProcessCheckOutput(const string& output); bool RunTask(void); int GetFD(void) const; }; diff --git a/icinga/service.cpp b/icinga/service.cpp index c8b8dd20a..5f498fe47 100644 --- a/icinga/service.cpp +++ b/icinga/service.cpp @@ -12,7 +12,7 @@ string Service::GetAlias(void) const return GetName(); } -Service Service::GetByName(string name) +Service Service::GetByName(const string& name) { ConfigObject::Ptr configObject = ConfigObject::GetObject("service", name); @@ -112,7 +112,7 @@ void Service::UpdateNextCheck(void) SetNextCheck(now + GetCheckInterval()); } -void Service::SetChecker(string checker) +void Service::SetChecker(const string& checker) { GetConfigObject()->SetTag("checker", checker); } @@ -221,7 +221,7 @@ void Service::ApplyCheckResult(const CheckResult& cr) SetState(cr.GetState()); } -ServiceState Service::StateFromString(string state) +ServiceState Service::StateFromString(const string& state) { /* TODO: make this thread-safe */ static map stateLookup; @@ -263,7 +263,7 @@ string Service::StateToString(ServiceState state) } } -ServiceStateType Service::StateTypeFromString(string type) +ServiceStateType Service::StateTypeFromString(const string& type) { if (type == "soft") return StateTypeSoft; @@ -279,7 +279,7 @@ string Service::StateTypeToString(ServiceStateType type) return "hard"; } -bool Service::IsAllowedChecker(string checker) const +bool Service::IsAllowedChecker(const string& checker) const { /* TODO: check config */ return true; diff --git a/icinga/service.h b/icinga/service.h index d7e116112..2bbe6663d 100644 --- a/icinga/service.h +++ b/icinga/service.h @@ -29,7 +29,7 @@ public: : ConfigObjectAdapter(configObject) { } - static Service GetByName(string name); + static Service GetByName(const string& name); string GetAlias(void) const; Host GetHost(void) const; @@ -45,10 +45,10 @@ public: time_t GetNextCheck(void); void UpdateNextCheck(void); - void SetChecker(string checker); + void SetChecker(const string& checker); string GetChecker(void) const; - bool IsAllowedChecker(string checker) const; + bool IsAllowedChecker(const string& checker) const; void SetCurrentCheckAttempt(long attempt); long GetCurrentCheckAttempt(void) const; @@ -70,10 +70,10 @@ public: void ApplyCheckResult(const CheckResult& cr); - static ServiceState StateFromString(string state); + static ServiceState StateFromString(const string& state); static string StateToString(ServiceState state); - static ServiceStateType StateTypeFromString(string state); + static ServiceStateType StateTypeFromString(const string& state); static string StateTypeToString(ServiceStateType state); };