void InfluxdbWriter::SendPerfdata(const Dictionary::Ptr& tmpl, const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, double ts) { Array::Ptr perfdata = cr->GetPerformanceData(); if (!perfdata) return; ObjectLock olock(perfdata); for (const Value& val : perfdata) { PerfdataValue::Ptr pdv; if (val.IsObjectType<PerfdataValue>()) pdv = val; else { try { pdv = PerfdataValue::Parse(val); } catch (const std::exception&) { Log(LogWarning, "InfluxdbWriter") << "Ignoring invalid perfdata value: " << val; continue; } } Dictionary::Ptr fields = new Dictionary(); fields->Set(String("value"), pdv->GetValue()); if (GetEnableSendThresholds()) { if (pdv->GetCrit()) fields->Set(String("crit"), pdv->GetCrit()); if (pdv->GetWarn()) fields->Set(String("warn"), pdv->GetWarn()); if (pdv->GetMin()) fields->Set(String("min"), pdv->GetMin()); if (pdv->GetMax()) fields->Set(String("max"), pdv->GetMax()); } if (GetEnableSendMetadata()) { Host::Ptr host; Service::Ptr service; boost::tie(host, service) = GetHostService(checkable); if (service) fields->Set(String("state"), FormatInteger(service->GetState())); else fields->Set(String("state"), FormatInteger(host->GetState())); fields->Set(String("current_attempt"), FormatInteger(checkable->GetCheckAttempt())); fields->Set(String("max_check_attempts"), FormatInteger(checkable->GetMaxCheckAttempts())); fields->Set(String("state_type"), FormatInteger(checkable->GetStateType())); fields->Set(String("reachable"), FormatBoolean(checkable->IsReachable())); fields->Set(String("downtime_depth"), FormatInteger(checkable->GetDowntimeDepth())); fields->Set(String("acknowledgement"), FormatInteger(checkable->GetAcknowledgement())); fields->Set(String("latency"), cr->CalculateLatency()); fields->Set(String("execution_time"), cr->CalculateExecutionTime()); } SendMetric(tmpl, pdv->GetLabel(), fields, ts); } }
String CompatUtility::GetCheckResultPerfdata(const CheckResult::Ptr& cr) { if (!cr) return String(); return PluginUtility::FormatPerfdata(cr->GetPerformanceData()); }
Value ServicesTable::PerfDataAccessor(const Value& row) { Service::Ptr service = static_cast<Service::Ptr>(row); if (!service) return Empty; String perfdata; CheckResult::Ptr cr = service->GetLastCheckResult(); if (!cr) return Empty; return PluginUtility::FormatPerfdata(cr->GetPerformanceData()); }
void GraphiteWriter::CheckResultHandler(const Service::Ptr& service, const CheckResult::Ptr& cr) { if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !service->GetEnablePerfdata()) return; /* TODO: sanitize host and service names */ String hostName = service->GetHost()->GetName(); String serviceName = service->GetShortName(); SanitizeMetric(hostName); SanitizeMetric(serviceName); String prefix = "icinga." + hostName + "." + serviceName; /* basic metrics */ SendMetric(prefix, "current_attempt", service->GetCheckAttempt()); SendMetric(prefix, "max_check_attempts", service->GetMaxCheckAttempts()); SendMetric(prefix, "state_type", service->GetStateType()); SendMetric(prefix, "state", service->GetState()); SendMetric(prefix, "latency", Service::CalculateLatency(cr)); SendMetric(prefix, "execution_time", Service::CalculateExecutionTime(cr)); Value pdv = cr->GetPerformanceData(); if (!pdv.IsObjectType<Dictionary>()) return; Dictionary::Ptr perfdata = pdv; ObjectLock olock(perfdata); BOOST_FOREACH(const Dictionary::Pair& kv, perfdata) { double valueNum; if (!kv.second.IsObjectType<PerfdataValue>()) valueNum = kv.second; else valueNum = static_cast<PerfdataValue::Ptr>(kv.second)->GetValue(); String escaped_key = kv.first; SanitizeMetric(escaped_key); boost::algorithm::replace_all(escaped_key, "::", "."); SendMetric(prefix, escaped_key, valueNum); }
void OpenTsdbWriter::SendPerfdata(const String& metric, const std::map<String, String>& tags, const CheckResult::Ptr& cr, double ts) { Array::Ptr perfdata = cr->GetPerformanceData(); if (!perfdata) return; ObjectLock olock(perfdata); for (const Value& val : perfdata) { PerfdataValue::Ptr pdv; if (val.IsObjectType<PerfdataValue>()) pdv = val; else { try { pdv = PerfdataValue::Parse(val); } catch (const std::exception&) { Log(LogWarning, "OpenTsdbWriter") << "Ignoring invalid perfdata value: " << val; continue; } } String escaped_key = EscapeMetric(pdv->GetLabel()); boost::algorithm::replace_all(escaped_key, "::", "."); SendMetric(metric + "." + escaped_key, tags, pdv->GetValue(), ts); if (pdv->GetCrit()) SendMetric(metric + "." + escaped_key + "_crit", tags, pdv->GetCrit(), ts); if (pdv->GetWarn()) SendMetric(metric + "." + escaped_key + "_warn", tags, pdv->GetWarn(), ts); if (pdv->GetMin()) SendMetric(metric + "." + escaped_key + "_min", tags, pdv->GetMin(), ts); if (pdv->GetMax()) SendMetric(metric + "." + escaped_key + "_max", tags, pdv->GetMax(), ts); } }
void LogstashWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { CONTEXT("LOGSTASH Processing check result for '" + checkable->GetName() + "'"); Log(LogDebug, "LogstashWriter") << "Processing check result for '" << checkable->GetName() << "'"; Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); Dictionary::Ptr fields = new Dictionary(); if (service) { fields->Set("service_name", service->GetShortName()); fields->Set("service_state", Service::StateToString(service->GetState())); fields->Set("last_state", service->GetLastState()); fields->Set("last_hard_state", service->GetLastHardState()); } else { fields->Set("last_state", host->GetLastState()); fields->Set("last_hard_state", host->GetLastHardState()); } fields->Set("host_name", host->GetName()); fields->Set("type", "CheckResult"); fields->Set("state", service ? Service::StateToString(service->GetState()) : Host::StateToString(host->GetState())); fields->Set("current_check_attempt", checkable->GetCheckAttempt()); fields->Set("max_check_attempts", checkable->GetMaxCheckAttempts()); fields->Set("latency", cr->CalculateLatency()); fields->Set("execution_time", cr->CalculateExecutionTime()); fields->Set("reachable", checkable->IsReachable()); double ts = Utility::GetTime(); if (cr) { fields->Set("plugin_output", cr->GetOutput()); fields->Set("check_source", cr->GetCheckSource()); ts = cr->GetExecutionEnd(); } Array::Ptr perfdata = cr->GetPerformanceData(); if (perfdata) { Dictionary::Ptr perfdataItems = new Dictionary(); ObjectLock olock(perfdata); for (const Value& val : perfdata) { PerfdataValue::Ptr pdv; if (val.IsObjectType<PerfdataValue>()) pdv = val; else { try { pdv = PerfdataValue::Parse(val); } catch (const std::exception&) { Log(LogWarning, "LogstashWriter") << "Ignoring invalid perfdata value: '" << val << "' for object '" << checkable->GetName() << "'."; continue; } } Dictionary::Ptr perfdataItem = new Dictionary(); perfdataItem->Set("value", pdv->GetValue()); if (pdv->GetMin()) perfdataItem->Set("min", pdv->GetMin()); if (pdv->GetMax()) perfdataItem->Set("max", pdv->GetMax()); if (pdv->GetWarn()) perfdataItem->Set("warn", pdv->GetWarn()); if (pdv->GetCrit()) perfdataItem->Set("crit", pdv->GetCrit()); String escaped_key = EscapeMetricLabel(pdv->GetLabel()); perfdataItems->Set(escaped_key, perfdataItem); } fields->Set("performance_data", perfdataItems); } SendLogMessage(ComposeLogstashMessage(fields, GetSource(), ts)); }
void StatusDataWriter::DumpCheckableStatusAttrs(std::ostream& fp, const Checkable::Ptr& checkable) { CheckResult::Ptr cr = checkable->GetLastCheckResult(); EventCommand::Ptr eventcommand = checkable->GetEventCommand(); CheckCommand::Ptr checkcommand = checkable->GetCheckCommand(); fp << "\t" << "check_command=" << CompatUtility::GetCommandName(checkcommand) << "!" << CompatUtility::GetCheckableCommandArgs(checkable) << "\n" "\t" "event_handler=" << CompatUtility::GetCommandName(eventcommand) << "\n" "\t" "check_interval=" << (checkable->GetCheckInterval() / 60.0) << "\n" "\t" "retry_interval=" << (checkable->GetRetryInterval() / 60.0) << "\n" "\t" "has_been_checked=" << Convert::ToLong(checkable->HasBeenChecked()) << "\n" "\t" "should_be_scheduled=" << checkable->GetEnableActiveChecks() << "\n" "\t" "event_handler_enabled=" << Convert::ToLong(checkable->GetEnableEventHandler()) << "\n"; TimePeriod::Ptr checkPeriod = checkable->GetCheckPeriod(); if (checkPeriod) fp << "\t" "check_period" "\t" << checkPeriod->GetName() << "\n"; if (cr) { fp << "\t" << "check_execution_time=" << Convert::ToString(cr->CalculateExecutionTime()) << "\n" "\t" "check_latency=" << Convert::ToString(cr->CalculateLatency()) << "\n"; } Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); if (service) { fp << "\t" "current_state=" << service->GetState() << "\n" "\t" "last_hard_state=" << service->GetLastHardState() << "\n" "\t" "last_time_ok=" << static_cast<int>(service->GetLastStateOK()) << "\n" "\t" "last_time_warn=" << static_cast<int>(service->GetLastStateWarning()) << "\n" "\t" "last_time_critical=" << static_cast<int>(service->GetLastStateCritical()) << "\n" "\t" "last_time_unknown=" << static_cast<int>(service->GetLastStateUnknown()) << "\n"; } else { int currentState = host->GetState(); if (currentState != HostUp && !host->IsReachable()) currentState = 2; /* hardcoded compat state */ fp << "\t" "current_state=" << currentState << "\n" "\t" "last_hard_state=" << host->GetLastHardState() << "\n" "\t" "last_time_up=" << static_cast<int>(host->GetLastStateUp()) << "\n" "\t" "last_time_down=" << static_cast<int>(host->GetLastStateDown()) << "\n"; } fp << "\t" "state_type=" << checkable->GetStateType() << "\n" "\t" "last_check=" << static_cast<long>(host->GetLastCheck()) << "\n"; if (cr) { fp << "\t" "plugin_output=" << CompatUtility::GetCheckResultOutput(cr) << "\n" "\t" "long_plugin_output=" << CompatUtility::GetCheckResultLongOutput(cr) << "\n" "\t" "performance_data=" << PluginUtility::FormatPerfdata(cr->GetPerformanceData()) << "\n"; } fp << "\t" << "next_check=" << static_cast<long>(checkable->GetNextCheck()) << "\n" "\t" "current_attempt=" << checkable->GetCheckAttempt() << "\n" "\t" "max_attempts=" << checkable->GetMaxCheckAttempts() << "\n" "\t" "last_state_change=" << static_cast<long>(checkable->GetLastStateChange()) << "\n" "\t" "last_hard_state_change=" << static_cast<long>(checkable->GetLastHardStateChange()) << "\n" "\t" "last_update=" << static_cast<long>(Utility::GetTime()) << "\n" "\t" "notifications_enabled=" << Convert::ToLong(checkable->GetEnableNotifications()) << "\n" "\t" "active_checks_enabled=" << Convert::ToLong(checkable->GetEnableActiveChecks()) << "\n" "\t" "passive_checks_enabled=" << Convert::ToLong(checkable->GetEnablePassiveChecks()) << "\n" "\t" "flap_detection_enabled=" << Convert::ToLong(checkable->GetEnableFlapping()) << "\n" "\t" "is_flapping=" << Convert::ToLong(checkable->IsFlapping()) << "\n" "\t" "percent_state_change=" << checkable->GetFlappingCurrent() << "\n" "\t" "problem_has_been_acknowledged=" << (checkable->GetAcknowledgement() != AcknowledgementNone ? 1 : 0) << "\n" "\t" "acknowledgement_type=" << checkable->GetAcknowledgement() << "\n" "\t" "acknowledgement_end_time=" << checkable->GetAcknowledgementExpiry() << "\n" "\t" "scheduled_downtime_depth=" << checkable->GetDowntimeDepth() << "\n" "\t" "last_notification=" << CompatUtility::GetCheckableNotificationLastNotification(checkable) << "\n" "\t" "next_notification=" << CompatUtility::GetCheckableNotificationNextNotification(checkable) << "\n" "\t" "current_notification_number=" << CompatUtility::GetCheckableNotificationNotificationNumber(checkable) << "\n" "\t" "is_reachable=" << Convert::ToLong(checkable->IsReachable()) << "\n"; }
void InfluxdbWriter::CheckResultHandlerWQ(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { AssertOnWorkQueue(); CONTEXT("Processing check result for '" + checkable->GetName() + "'"); if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata()) return; Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); MacroProcessor::ResolverList resolvers; if (service) resolvers.emplace_back("service", service); resolvers.emplace_back("host", host); resolvers.emplace_back("icinga", IcingaApplication::GetInstance()); String prefix; double ts = cr->GetExecutionEnd(); // Clone the template and perform an in-place macro expansion of measurement and tag values Dictionary::Ptr tmpl_clean = service ? GetServiceTemplate() : GetHostTemplate(); Dictionary::Ptr tmpl = static_pointer_cast<Dictionary>(tmpl_clean->Clone()); tmpl->Set("measurement", MacroProcessor::ResolveMacros(tmpl->Get("measurement"), resolvers, cr)); Dictionary::Ptr tags = tmpl->Get("tags"); if (tags) { ObjectLock olock(tags); for (const Dictionary::Pair& pair : tags) { String missing_macro; Value value = MacroProcessor::ResolveMacros(pair.second, resolvers, cr, &missing_macro); if (!missing_macro.IsEmpty()) continue; tags->Set(pair.first, value); } } Array::Ptr perfdata = cr->GetPerformanceData(); if (perfdata) { ObjectLock olock(perfdata); for (const Value& val : perfdata) { PerfdataValue::Ptr pdv; if (val.IsObjectType<PerfdataValue>()) pdv = val; else { try { pdv = PerfdataValue::Parse(val); } catch (const std::exception&) { Log(LogWarning, "InfluxdbWriter") << "Ignoring invalid perfdata value: " << val; continue; } } Dictionary::Ptr fields = new Dictionary(); fields->Set("value", pdv->GetValue()); if (GetEnableSendThresholds()) { if (pdv->GetCrit()) fields->Set("crit", pdv->GetCrit()); if (pdv->GetWarn()) fields->Set("warn", pdv->GetWarn()); if (pdv->GetMin()) fields->Set("min", pdv->GetMin()); if (pdv->GetMax()) fields->Set("max", pdv->GetMax()); } if (!pdv->GetUnit().IsEmpty()) { fields->Set("unit", pdv->GetUnit()); } SendMetric(tmpl, pdv->GetLabel(), fields, ts); } } if (GetEnableSendMetadata()) { Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); Dictionary::Ptr fields = new Dictionary(); if (service) fields->Set("state", new InfluxdbInteger(service->GetState())); else fields->Set("state", new InfluxdbInteger(host->GetState())); fields->Set("current_attempt", new InfluxdbInteger(checkable->GetCheckAttempt())); fields->Set("max_check_attempts", new InfluxdbInteger(checkable->GetMaxCheckAttempts())); fields->Set("state_type", new InfluxdbInteger(checkable->GetStateType())); fields->Set("reachable", checkable->IsReachable()); fields->Set("downtime_depth", new InfluxdbInteger(checkable->GetDowntimeDepth())); fields->Set("acknowledgement", new InfluxdbInteger(checkable->GetAcknowledgement())); fields->Set("latency", cr->CalculateLatency()); fields->Set("execution_time", cr->CalculateExecutionTime()); SendMetric(tmpl, Empty, fields, ts); } }
void ElasticsearchWriter::AddCheckResult(const Dictionary::Ptr& fields, const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { String prefix = "check_result."; fields->Set(prefix + "output", cr->GetOutput()); fields->Set(prefix + "check_source", cr->GetCheckSource()); fields->Set(prefix + "exit_status", cr->GetExitStatus()); fields->Set(prefix + "command", cr->GetCommand()); fields->Set(prefix + "state", cr->GetState()); fields->Set(prefix + "vars_before", cr->GetVarsBefore()); fields->Set(prefix + "vars_after", cr->GetVarsAfter()); fields->Set(prefix + "execution_start", FormatTimestamp(cr->GetExecutionStart())); fields->Set(prefix + "execution_end", FormatTimestamp(cr->GetExecutionEnd())); fields->Set(prefix + "schedule_start", FormatTimestamp(cr->GetScheduleStart())); fields->Set(prefix + "schedule_end", FormatTimestamp(cr->GetScheduleEnd())); /* Add extra calculated field. */ fields->Set(prefix + "latency", cr->CalculateLatency()); fields->Set(prefix + "execution_time", cr->CalculateExecutionTime()); if (!GetEnableSendPerfdata()) return; Array::Ptr perfdata = cr->GetPerformanceData(); CheckCommand::Ptr checkCommand = checkable->GetCheckCommand(); if (perfdata) { ObjectLock olock(perfdata); for (const Value& val : perfdata) { PerfdataValue::Ptr pdv; if (val.IsObjectType<PerfdataValue>()) pdv = val; else { try { pdv = PerfdataValue::Parse(val); } catch (const std::exception&) { Log(LogWarning, "ElasticsearchWriter") << "Ignoring invalid perfdata for checkable '" << checkable->GetName() << "' and command '" << checkCommand->GetName() << "' with value: " << val; continue; } } String escapedKey = pdv->GetLabel(); boost::replace_all(escapedKey, " ", "_"); boost::replace_all(escapedKey, ".", "_"); boost::replace_all(escapedKey, "\\", "_"); boost::algorithm::replace_all(escapedKey, "::", "."); String perfdataPrefix = prefix + "perfdata." + escapedKey; fields->Set(perfdataPrefix + ".value", pdv->GetValue()); if (pdv->GetMin()) fields->Set(perfdataPrefix + ".min", pdv->GetMin()); if (pdv->GetMax()) fields->Set(perfdataPrefix + ".max", pdv->GetMax()); if (pdv->GetWarn()) fields->Set(perfdataPrefix + ".warn", pdv->GetWarn()); if (pdv->GetCrit()) fields->Set(perfdataPrefix + ".crit", pdv->GetCrit()); if (!pdv->GetUnit().IsEmpty()) fields->Set(perfdataPrefix + ".unit", pdv->GetUnit()); } } }