void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification, const User::Ptr& user, const CheckResult::Ptr& cr, int itype, const String& author, const String& comment) { NotificationCommand::Ptr commandObj = notification->GetCommand(); NotificationType type = static_cast<NotificationType>(itype); Checkable::Ptr checkable = notification->GetCheckable(); Dictionary::Ptr notificationExtra = make_shared<Dictionary>(); notificationExtra->Set("type", Notification::NotificationTypeToString(type)); notificationExtra->Set("author", author); notificationExtra->Set("comment", comment); Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); MacroProcessor::ResolverList resolvers; resolvers.push_back(std::make_pair("user", user)); resolvers.push_back(std::make_pair("notification", notificationExtra)); resolvers.push_back(std::make_pair("notification", notification)); if (service) resolvers.push_back(std::make_pair("service", service)); resolvers.push_back(std::make_pair("host", host)); resolvers.push_back(std::make_pair("command", commandObj)); resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance())); PluginUtility::ExecuteCommand(commandObj, checkable, cr, resolvers, boost::bind(&PluginNotificationTask::ProcessFinishedHandler, checkable, _1, _2)); }
void PluginCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { CheckCommand::Ptr commandObj = checkable->GetCheckCommand(); Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); MacroProcessor::ResolverList resolvers; if (service) resolvers.push_back(std::make_pair("service", service)); resolvers.push_back(std::make_pair("host", host)); resolvers.push_back(std::make_pair("command", commandObj)); resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance())); PluginUtility::ExecuteCommand(commandObj, checkable, checkable->GetLastCheckResult(), resolvers, boost::bind(&PluginCheckTask::ProcessFinishedHandler, checkable, cr, _1, _2)); }
void PerfdataWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { CONTEXT("Writing performance data for object '" + checkable->GetName() + "'"); if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata()) return; Service::Ptr service = dynamic_pointer_cast<Service>(checkable); Host::Ptr host; if (service) host = service->GetHost(); else host = static_pointer_cast<Host>(checkable); MacroProcessor::ResolverList resolvers; if (service) resolvers.push_back(std::make_pair("service", service)); resolvers.push_back(std::make_pair("host", host)); resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance())); if (service) { String line = MacroProcessor::ResolveMacros(GetServiceFormatTemplate(), resolvers, cr, NULL, &PerfdataWriter::EscapeMacroMetric); { ObjectLock olock(this); if (!m_ServiceOutputFile.good()) return; m_ServiceOutputFile << line << "\n"; } } else { String line = MacroProcessor::ResolveMacros(GetHostFormatTemplate(), resolvers, cr, NULL, &PerfdataWriter::EscapeMacroMetric); { ObjectLock olock(this); if (!m_HostOutputFile.good()) return; m_HostOutputFile << line << "\n"; } } }
void DummyCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) { REQUIRE_NOT_NULL(checkable); REQUIRE_NOT_NULL(cr); CheckCommand::Ptr commandObj = checkable->GetCheckCommand(); 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("command", commandObj); resolvers.emplace_back("icinga", IcingaApplication::GetInstance()); int dummyState = MacroProcessor::ResolveMacros("$dummy_state$", resolvers, checkable->GetLastCheckResult(), nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros); String dummyText = MacroProcessor::ResolveMacros("$dummy_text$", resolvers, checkable->GetLastCheckResult(), nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros); if (resolvedMacros && !useResolvedMacros) return; /* Parse output and performance data. */ std::pair<String, String> co = PluginUtility::ParseCheckOutput(dummyText); double now = Utility::GetTime(); cr->SetOutput(co.first); cr->SetPerformanceData(PluginUtility::SplitPerfdata(co.second)); cr->SetState(PluginUtility::ExitStatusToState(dummyState)); cr->SetExitStatus(dummyState); cr->SetExecutionStart(now); cr->SetExecutionEnd(now); checkable->ProcessCheckResult(cr); }
void InfluxdbWriter::CheckResultHandler(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr) { CONTEXT("Processing check result for '" + checkable->GetName() + "'"); if (!IcingaApplication::GetInstance()->GetEnablePerfdata() || !checkable->GetEnablePerfdata()) return; Host::Ptr host; Service::Ptr service; boost::tie(host, service) = GetHostService(checkable); MacroProcessor::ResolverList resolvers; if (service) resolvers.push_back(std::make_pair("service", service)); resolvers.push_back(std::make_pair("host", host)); resolvers.push_back(std::make_pair("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) { // Prevent missing macros from warning; will return an empty value // which will be filtered out in SendMetric() String missing_macro; tags->Set(pair.first, MacroProcessor::ResolveMacros(pair.second, resolvers, cr, &missing_macro)); } } SendPerfdata(tmpl, checkable, cr, ts); }
void PluginNotificationTask::ScriptFunc(const Notification::Ptr& notification, const User::Ptr& user, const CheckResult::Ptr& cr, int itype, const String& author, const String& comment, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) { REQUIRE_NOT_NULL(notification); REQUIRE_NOT_NULL(user); NotificationCommand::Ptr commandObj = notification->GetCommand(); auto type = static_cast<NotificationType>(itype); Checkable::Ptr checkable = notification->GetCheckable(); Dictionary::Ptr notificationExtra = new Dictionary({ { "type", Notification::NotificationTypeToString(type) }, { "author", author }, { "comment", comment } }); Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); MacroProcessor::ResolverList resolvers; resolvers.emplace_back("user", user); resolvers.emplace_back("notification", notificationExtra); resolvers.emplace_back("notification", notification); if (service) resolvers.emplace_back("service", service); resolvers.emplace_back("host", host); resolvers.emplace_back("command", commandObj); resolvers.emplace_back("icinga", IcingaApplication::GetInstance()); int timeout = commandObj->GetTimeout(); PluginUtility::ExecuteCommand(commandObj, checkable, cr, resolvers, resolvedMacros, useResolvedMacros, timeout, std::bind(&PluginNotificationTask::ProcessFinishedHandler, checkable, _1, _2)); }
void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) { CheckCommand::Ptr commandObj = checkable->GetCheckCommand(); Value raw_command = commandObj->GetCommandLine(); Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); MacroProcessor::ResolverList resolvers; if (service) resolvers.push_back(std::make_pair("service", service)); resolvers.push_back(std::make_pair("host", host)); resolvers.push_back(std::make_pair("command", commandObj)); resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance())); String idoType = MacroProcessor::ResolveMacros("$ido_type$", resolvers, checkable->GetLastCheckResult(), NULL, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros); if (resolvedMacros && !useResolvedMacros) return; if (idoType.IsEmpty()) { cr->SetOutput("Macro 'ido_type' must be set."); cr->SetState(ServiceUnknown); checkable->ProcessCheckResult(cr); return; } String idoName = MacroProcessor::ResolveMacros("$ido_name$", resolvers, checkable->GetLastCheckResult(), NULL, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros); if (resolvedMacros && !useResolvedMacros) return; if (idoName.IsEmpty()) { cr->SetOutput("Macro 'ido_name' must be set."); cr->SetState(ServiceUnknown); checkable->ProcessCheckResult(cr); return; } Type::Ptr type = Type::GetByName(idoType); if (!type || !DbConnection::TypeInstance->IsAssignableFrom(type)) { cr->SetOutput("IDO type '" + idoType + "' is invalid."); cr->SetState(ServiceUnknown); checkable->ProcessCheckResult(cr); return; } ConfigType::Ptr dtype = ConfigType::GetByName(idoType); VERIFY(dtype); DbConnection::Ptr conn = static_pointer_cast<DbConnection>(dtype->GetObject(idoName)); double qps = conn->GetQueryCount(60) / 60.0; if (!conn->GetConnected()) { if (conn->GetShouldConnect()) { cr->SetOutput("Could not connect to the database server."); cr->SetState(ServiceCritical); } else { cr->SetOutput("Not currently enabled: Another cluster instance is responsible for the IDO database."); cr->SetState(ServiceOK); } } else { String schema_version = conn->GetSchemaVersion(); if (Utility::CompareVersion(IDO_CURRENT_SCHEMA_VERSION, schema_version) < 0) { cr->SetOutput("Outdated schema version: " + schema_version + "; Latest version: " IDO_CURRENT_SCHEMA_VERSION); cr->SetState(ServiceWarning); } else { std::ostringstream msgbuf; msgbuf << "Connected to the database server; queries per second: " << std::fixed << std::setprecision(3) << qps; cr->SetOutput(msgbuf.str()); cr->SetState(ServiceOK); } } Array::Ptr perfdata = new Array(); perfdata->Add(new PerfdataValue("queries", qps)); perfdata->Add(new PerfdataValue("queries_1min", conn->GetQueryCount(60))); perfdata->Add(new PerfdataValue("queries_5mins", conn->GetQueryCount(5 * 60))); perfdata->Add(new PerfdataValue("queries_15mins", conn->GetQueryCount(15 * 60))); perfdata->Add(new PerfdataValue("pending_queries", conn->GetPendingQueryCount())); cr->SetPerformanceData(perfdata); checkable->ProcessCheckResult(cr); }
void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) { REQUIRE_NOT_NULL(checkable); REQUIRE_NOT_NULL(cr); ApiListener::Ptr listener = ApiListener::GetInstance(); if (!listener) { cr->SetOutput("No API listener is configured for this instance."); cr->SetState(ServiceUnknown); checkable->ProcessCheckResult(cr); return; } CheckCommand::Ptr commandObj = checkable->GetCheckCommand(); Value raw_command = commandObj->GetCommandLine(); 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("command", commandObj); resolvers.emplace_back("icinga", IcingaApplication::GetInstance()); String zoneName = MacroProcessor::ResolveMacros("$cluster_zone$", resolvers, checkable->GetLastCheckResult(), nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros); String missingLagWarning; String missingLagCritical; double lagWarning = MacroProcessor::ResolveMacros("$cluster_lag_warning$", resolvers, checkable->GetLastCheckResult(), &missingLagWarning, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros); double lagCritical = MacroProcessor::ResolveMacros("$cluster_lag_critical$", resolvers, checkable->GetLastCheckResult(), &missingLagCritical, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros); if (resolvedMacros && !useResolvedMacros) return; if (zoneName.IsEmpty()) { cr->SetOutput("Macro 'cluster_zone' must be set."); cr->SetState(ServiceUnknown); checkable->ProcessCheckResult(cr); return; } Zone::Ptr zone = Zone::GetByName(zoneName); if (!zone) { cr->SetOutput("Zone '" + zoneName + "' does not exist."); cr->SetState(ServiceUnknown); checkable->ProcessCheckResult(cr); return; } bool connected = false; double zoneLag = 0; double lastMessageSent = 0; double lastMessageReceived = 0; double messagesSentPerSecond = 0; double messagesReceivedPerSecond = 0; double bytesSentPerSecond = 0; double bytesReceivedPerSecond = 0; for (const Endpoint::Ptr& endpoint : zone->GetEndpoints()) { if (endpoint->GetConnected()) connected = true; double eplag = ApiListener::CalculateZoneLag(endpoint); if (eplag > 0 && eplag > zoneLag) zoneLag = eplag; if (endpoint->GetLastMessageSent() > lastMessageSent) lastMessageSent = endpoint->GetLastMessageSent(); if (endpoint->GetLastMessageReceived() > lastMessageReceived) lastMessageReceived = endpoint->GetLastMessageReceived(); messagesSentPerSecond += endpoint->GetMessagesSentPerSecond(); messagesReceivedPerSecond += endpoint->GetMessagesReceivedPerSecond(); bytesSentPerSecond += endpoint->GetBytesSentPerSecond(); bytesReceivedPerSecond += endpoint->GetBytesReceivedPerSecond(); } if (connected) { cr->SetState(ServiceOK); cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag)); /* Check whether the thresholds have been resolved and compare them */ if (missingLagCritical.IsEmpty() && zoneLag > lagCritical) { cr->SetState(ServiceCritical); cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag) + " greater than critical threshold: " + Utility::FormatDuration(lagCritical)); } else if (missingLagWarning.IsEmpty() && zoneLag > lagWarning) { cr->SetState(ServiceWarning); cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag) + " greater than warning threshold: " + Utility::FormatDuration(lagWarning)); } } else { cr->SetState(ServiceCritical); cr->SetOutput("Zone '" + zoneName + "' is not connected. Log lag: " + Utility::FormatDuration(zoneLag)); } cr->SetPerformanceData(new Array({ new PerfdataValue("slave_lag", zoneLag, false, "s", lagWarning, lagCritical), new PerfdataValue("last_messages_sent", lastMessageSent), new PerfdataValue("last_messages_received", lastMessageReceived), new PerfdataValue("sum_messages_sent_per_second", messagesSentPerSecond), new PerfdataValue("sum_messages_received_per_second", messagesReceivedPerSecond), new PerfdataValue("sum_bytes_sent_per_second", bytesSentPerSecond), new PerfdataValue("sum_bytes_received_per_second", bytesReceivedPerSecond) })); checkable->ProcessCheckResult(cr); }
void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) { ApiListener::Ptr listener = ApiListener::GetInstance(); if (!listener) { cr->SetOutput("No API listener is configured for this instance."); cr->SetState(ServiceUnknown); checkable->ProcessCheckResult(cr); return; } CheckCommand::Ptr commandObj = checkable->GetCheckCommand(); Value raw_command = commandObj->GetCommandLine(); Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); MacroProcessor::ResolverList resolvers; if (service) resolvers.push_back(std::make_pair("service", service)); resolvers.push_back(std::make_pair("host", host)); resolvers.push_back(std::make_pair("command", commandObj)); resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance())); String zoneName = MacroProcessor::ResolveMacros("$cluster_zone$", resolvers, checkable->GetLastCheckResult(), NULL, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros); if (resolvedMacros && !useResolvedMacros) return; if (zoneName.IsEmpty()) { cr->SetOutput("Macro 'cluster_zone' must be set."); cr->SetState(ServiceUnknown); checkable->ProcessCheckResult(cr); return; } Zone::Ptr zone = Zone::GetByName(zoneName); if (!zone) { cr->SetOutput("Zone '" + zoneName + "' does not exist."); cr->SetState(ServiceUnknown); checkable->ProcessCheckResult(cr); return; } bool connected = false; double zoneLag = 0; BOOST_FOREACH(const Endpoint::Ptr& endpoint, zone->GetEndpoints()) { if (endpoint->GetConnected()) connected = true; double eplag = ApiListener::CalculateZoneLag(endpoint); if (eplag > 0 && eplag > zoneLag) zoneLag = eplag; } if (!connected) { cr->SetState(ServiceCritical); cr->SetOutput("Zone '" + zoneName + "' is not connected. Log lag: " + Utility::FormatDuration(zoneLag)); } else { cr->SetState(ServiceOK); cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag)); } Array::Ptr perfdata = new Array(); perfdata->Add(new PerfdataValue("slave_lag", zoneLag)); cr->SetPerformanceData(perfdata); checkable->ProcessCheckResult(cr); }
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 ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) { ApiListener::Ptr listener = ApiListener::GetInstance(); if (!listener) { cr->SetOutput("No API listener is configured for this instance."); cr->SetState(ServiceUnknown); checkable->ProcessCheckResult(cr); return; } CheckCommand::Ptr commandObj = checkable->GetCheckCommand(); Value raw_command = commandObj->GetCommandLine(); Host::Ptr host; Service::Ptr service; tie(host, service) = GetHostService(checkable); MacroProcessor::ResolverList resolvers; if (service) resolvers.push_back(std::make_pair("service", service)); resolvers.push_back(std::make_pair("host", host)); resolvers.push_back(std::make_pair("command", commandObj)); resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance())); String zoneName = MacroProcessor::ResolveMacros("$cluster_zone$", resolvers, checkable->GetLastCheckResult()); if (zoneName.IsEmpty()) { cr->SetOutput("Macro 'cluster_zone' must be set."); cr->SetState(ServiceUnknown); checkable->ProcessCheckResult(cr); return; } Zone::Ptr zone = Zone::GetByName(zoneName); if (!zone) { cr->SetOutput("Zone '" + zoneName + "' does not exist."); cr->SetState(ServiceUnknown); checkable->ProcessCheckResult(cr); return; } bool connected = false; BOOST_FOREACH(const Endpoint::Ptr& endpoint, zone->GetEndpoints()) { if (endpoint->IsConnected()) { connected = true; break; } } if (!connected) { cr->SetState(ServiceCritical); cr->SetOutput("Zone '" + zoneName + "' is not connected."); } else { cr->SetState(ServiceOK); cr->SetOutput("Zone '" + zoneName + "' is connected."); } checkable->ProcessCheckResult(cr); }