Value ServicesTable::CheckSourceAccessor(const Value& row) { Service::Ptr service = static_cast<Service::Ptr>(row); if (!service) return Empty; CheckResult::Ptr cr = service->GetLastCheckResult(); if (cr) return cr->GetCheckSource(); return Empty; }
void LogstashWriter::StateChangeHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, StateType type) { CONTEXT("Logstash Processing state change '" + checkable->GetName() + "'"); Log(LogDebug, "LogstashWriter") << "Processing state change for '" << checkable->GetName() << "'"; Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); Dictionary::Ptr fields = new Dictionary(); fields->Set("state", service ? Service::StateToString(service->GetState()) : Host::StateToString(host->GetState())); fields->Set("type", "StateChange"); fields->Set("current_check_attempt", checkable->GetCheckAttempt()); fields->Set("max_check_attempts", checkable->GetMaxCheckAttempts()); fields->Set("hostname", host->GetName()); 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()); } double ts = Utility::GetTime(); if (cr) { fields->Set("plugin_output", cr->GetOutput()); fields->Set("check_source", cr->GetCheckSource()); ts = cr->GetExecutionEnd(); } SendLogMessage(ComposeLogstashMessage(fields, GetSource(), ts)); }
void GelfWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { CONTEXT("GELF Processing check result for '" + checkable->GetName() + "'"); Log(LogDebug, "GelfWriter") << "GELF 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("_hostname", host->GetName()); fields->Set("_type", "CHECK RESULT"); 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()); if (cr) { fields->Set("short_message", CompatUtility::GetCheckResultOutput(cr)); fields->Set("full_message", CompatUtility::GetCheckResultLongOutput(cr)); fields->Set("_check_source", cr->GetCheckSource()); } SendLogMessage(ComposeGelfMessage(fields, GetSource())); }
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)); }
Dictionary::Ptr ServiceDbObject::GetStatusFields(void) const { Dictionary::Ptr fields = make_shared<Dictionary>(); Service::Ptr service = static_pointer_cast<Service>(GetObject()); CheckResult::Ptr cr = service->GetLastCheckResult(); if (cr) { fields->Set("output", CompatUtility::GetCheckResultOutput(cr)); fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr)); fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr)); fields->Set("check_source", cr->GetCheckSource()); } fields->Set("current_state", service->GetState()); fields->Set("has_been_checked", CompatUtility::GetCheckableHasBeenChecked(service)); fields->Set("should_be_scheduled", service->GetEnableActiveChecks()); fields->Set("current_check_attempt", service->GetCheckAttempt()); fields->Set("max_check_attempts", service->GetMaxCheckAttempts()); if (cr) fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd())); fields->Set("next_check", DbValue::FromTimestamp(service->GetNextCheck())); fields->Set("check_type", CompatUtility::GetCheckableCheckType(service)); fields->Set("last_state_change", DbValue::FromTimestamp(service->GetLastStateChange())); fields->Set("last_hard_state_change", DbValue::FromTimestamp(service->GetLastHardStateChange())); fields->Set("last_time_ok", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateOK()))); fields->Set("last_time_warning", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateWarning()))); fields->Set("last_time_critical", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateCritical()))); fields->Set("last_time_unknown", DbValue::FromTimestamp(static_cast<int>(service->GetLastStateUnknown()))); fields->Set("state_type", service->GetStateType()); fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationLastNotification(service))); fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationNextNotification(service))); fields->Set("no_more_notifications", Empty); fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(service)); fields->Set("problem_has_been_acknowledged", CompatUtility::GetCheckableProblemHasBeenAcknowledged(service)); fields->Set("acknowledgement_type", CompatUtility::GetCheckableAcknowledgementType(service)); fields->Set("current_notification_number", CompatUtility::GetCheckableNotificationNotificationNumber(service)); fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(service)); fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(service)); fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(service)); fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(service)); fields->Set("is_flapping", CompatUtility::GetCheckableIsFlapping(service)); fields->Set("percent_state_change", CompatUtility::GetCheckablePercentStateChange(service)); if (cr) { fields->Set("latency", Convert::ToString(Service::CalculateLatency(cr))); fields->Set("execution_time", Convert::ToString(Service::CalculateExecutionTime(cr))); } fields->Set("scheduled_downtime_depth", service->GetDowntimeDepth()); fields->Set("process_performance_data", CompatUtility::GetCheckableProcessPerformanceData(service)); fields->Set("event_handler", CompatUtility::GetCheckableEventHandler(service)); fields->Set("check_command", CompatUtility::GetCheckableCheckCommand(service)); fields->Set("normal_check_interval", CompatUtility::GetCheckableCheckInterval(service)); fields->Set("retry_check_interval", CompatUtility::GetCheckableRetryInterval(service)); fields->Set("check_timeperiod_object_id", service->GetCheckPeriod()); fields->Set("modified_service_attributes", service->GetModifiedAttributes()); return fields; }
Dictionary::Ptr HostDbObject::GetStatusFields(void) const { Dictionary::Ptr fields = new Dictionary(); Host::Ptr host = static_pointer_cast<Host>(GetObject()); CheckResult::Ptr cr = host->GetLastCheckResult(); if (cr) { fields->Set("output", CompatUtility::GetCheckResultOutput(cr)); fields->Set("long_output", CompatUtility::GetCheckResultLongOutput(cr)); fields->Set("perfdata", CompatUtility::GetCheckResultPerfdata(cr)); fields->Set("check_source", cr->GetCheckSource()); } fields->Set("current_state", host->IsReachable() ? host->GetState() : 2); fields->Set("has_been_checked", CompatUtility::GetCheckableHasBeenChecked(host)); fields->Set("should_be_scheduled", host->GetEnableActiveChecks()); fields->Set("current_check_attempt", host->GetCheckAttempt()); fields->Set("max_check_attempts", host->GetMaxCheckAttempts()); if (cr) fields->Set("last_check", DbValue::FromTimestamp(cr->GetScheduleEnd())); fields->Set("next_check", DbValue::FromTimestamp(host->GetNextCheck())); fields->Set("check_type", CompatUtility::GetCheckableCheckType(host)); fields->Set("last_state_change", DbValue::FromTimestamp(host->GetLastStateChange())); fields->Set("last_hard_state_change", DbValue::FromTimestamp(host->GetLastHardStateChange())); fields->Set("last_time_up", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUp()))); fields->Set("last_time_down", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateDown()))); fields->Set("last_time_unreachable", DbValue::FromTimestamp(static_cast<int>(host->GetLastStateUnreachable()))); fields->Set("state_type", host->GetStateType()); fields->Set("last_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationLastNotification(host))); fields->Set("next_notification", DbValue::FromTimestamp(CompatUtility::GetCheckableNotificationNextNotification(host))); fields->Set("no_more_notifications", Empty); fields->Set("notifications_enabled", CompatUtility::GetCheckableNotificationsEnabled(host)); { ObjectLock olock(host); fields->Set("problem_has_been_acknowledged", CompatUtility::GetCheckableProblemHasBeenAcknowledged(host)); fields->Set("acknowledgement_type", CompatUtility::GetCheckableAcknowledgementType(host)); } fields->Set("current_notification_number", CompatUtility::GetCheckableNotificationNotificationNumber(host)); fields->Set("passive_checks_enabled", CompatUtility::GetCheckablePassiveChecksEnabled(host)); fields->Set("active_checks_enabled", CompatUtility::GetCheckableActiveChecksEnabled(host)); fields->Set("event_handler_enabled", CompatUtility::GetCheckableEventHandlerEnabled(host)); fields->Set("flap_detection_enabled", CompatUtility::GetCheckableFlapDetectionEnabled(host)); fields->Set("is_flapping", CompatUtility::GetCheckableIsFlapping(host)); fields->Set("percent_state_change", CompatUtility::GetCheckablePercentStateChange(host)); if (cr) { fields->Set("latency", Convert::ToString(Service::CalculateLatency(cr))); fields->Set("execution_time", Convert::ToString(Service::CalculateExecutionTime(cr))); } fields->Set("scheduled_downtime_depth", host->GetDowntimeDepth()); fields->Set("failure_prediction_enabled", Empty); fields->Set("process_performance_data", 0); /* this is a host which does not process any perf data */ fields->Set("obsess_over_host", Empty); fields->Set("modified_host_attributes", host->GetModifiedAttributes()); fields->Set("event_handler", CompatUtility::GetCheckableEventHandler(host)); fields->Set("check_command", CompatUtility::GetCheckableCheckCommand(host)); fields->Set("normal_check_interval", CompatUtility::GetCheckableCheckInterval(host)); fields->Set("retry_check_interval", CompatUtility::GetCheckableRetryInterval(host)); fields->Set("check_timeperiod_object_id", host->GetCheckPeriod()); fields->Set("is_reachable", CompatUtility::GetCheckableIsReachable(host)); return fields; }
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()); } } }