diff --git a/lib/db_ido/idochecktask.cpp b/lib/db_ido/idochecktask.cpp index aaff48f7b..d16c91b0c 100644 --- a/lib/db_ido/idochecktask.cpp +++ b/lib/db_ido/idochecktask.cpp @@ -12,11 +12,34 @@ #include "base/perfdatavalue.hpp" #include "base/configtype.hpp" #include "base/convert.hpp" +#include using namespace icinga; REGISTER_FUNCTION_NONCONST(Internal, IdoCheck, &IdoCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros"); +static void ReportIdoCheck( + const Checkable::Ptr& checkable, const CheckCommand::Ptr& commandObj, + const CheckResult::Ptr& cr, String output, ServiceState state = ServiceUnknown +) +{ + if (Checkable::ExecuteCommandProcessFinishedHandler) { + double now = Utility::GetTime(); + ProcessResult pr; + pr.PID = -1; + pr.Output = std::move(output); + pr.ExecutionStart = now; + pr.ExecutionEnd = now; + pr.ExitStatus = state; + + Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr); + } else { + cr->SetState(state); + cr->SetOutput(output); + checkable->ProcessCheckResult(cr); + } +} + void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr, const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros) { @@ -66,70 +89,19 @@ void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult return; if (idoType.IsEmpty()) { - String output = "Attribute 'ido_type' must be set."; - state = ServiceUnknown; - - if (Checkable::ExecuteCommandProcessFinishedHandler) { - double now = Utility::GetTime(); - ProcessResult pr; - pr.PID = -1; - pr.Output = output; - pr.ExecutionStart = now; - pr.ExecutionEnd = now; - pr.ExitStatus = state; - - Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr); - } else { - cr->SetState(state); - cr->SetOutput(output); - checkable->ProcessCheckResult(cr); - } + ReportIdoCheck(checkable, commandObj, cr, "Attribute 'ido_type' must be set."); return; } if (idoName.IsEmpty()) { - String output = "Attribute 'ido_name' must be set."; - state = ServiceUnknown; - - if (Checkable::ExecuteCommandProcessFinishedHandler) { - double now = Utility::GetTime(); - ProcessResult pr; - pr.PID = -1; - pr.Output = output; - pr.ExecutionStart = now; - pr.ExecutionEnd = now; - pr.ExitStatus = state; - - Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr); - } else { - cr->SetState(state); - cr->SetOutput(output); - checkable->ProcessCheckResult(cr); - } + ReportIdoCheck(checkable, commandObj, cr, "Attribute 'ido_name' must be set."); return; } Type::Ptr type = Type::GetByName(idoType); if (!type || !DbConnection::TypeInstance->IsAssignableFrom(type)) { - String output = "DB IDO type '" + idoType + "' is invalid."; - state = ServiceUnknown; - - if (Checkable::ExecuteCommandProcessFinishedHandler) { - double now = Utility::GetTime(); - ProcessResult pr; - pr.PID = -1; - pr.Output = output; - pr.ExecutionStart = now; - pr.ExecutionEnd = now; - pr.ExitStatus = state; - - Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr); - } else { - cr->SetState(state); - cr->SetOutput(output); - checkable->ProcessCheckResult(cr); - } + ReportIdoCheck(checkable, commandObj, cr, "DB IDO type '" + idoType + "' is invalid."); return; } @@ -139,77 +111,27 @@ void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult DbConnection::Ptr conn = static_pointer_cast(dtype->GetObject(idoName)); if (!conn) { - String output = "DB IDO connection '" + idoName + "' does not exist."; - state = ServiceUnknown; - - if (Checkable::ExecuteCommandProcessFinishedHandler) { - double now = Utility::GetTime(); - ProcessResult pr; - pr.PID = -1; - pr.Output = output; - pr.ExecutionStart = now; - pr.ExecutionEnd = now; - pr.ExitStatus = state; - - Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr); - } else { - cr->SetState(state); - cr->SetOutput(output); - checkable->ProcessCheckResult(cr); - } + ReportIdoCheck(checkable, commandObj, cr, "DB IDO connection '" + idoName + "' does not exist."); return; } double qps = conn->GetQueryCount(60) / 60.0; if (conn->IsPaused()) { - String output = "DB IDO connection is temporarily disabled on this cluster instance."; - state = ServiceOK; - - if (Checkable::ExecuteCommandProcessFinishedHandler) { - double now = Utility::GetTime(); - ProcessResult pr; - pr.PID = -1; - pr.Output = output; - pr.ExecutionStart = now; - pr.ExecutionEnd = now; - pr.ExitStatus = state; - - Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr); - } else { - cr->SetState(state); - cr->SetOutput(output); - checkable->ProcessCheckResult(cr); - } + ReportIdoCheck(checkable, commandObj, cr, "DB IDO connection is temporarily disabled on this cluster instance.", ServiceOK); return; } double pendingQueries = conn->GetPendingQueryCount(); if (!conn->GetConnected()) { - String output; if (conn->GetShouldConnect()) { - output ="Could not connect to the database server."; - state = ServiceCritical; + ReportIdoCheck(checkable, commandObj, cr, "Could not connect to the database server.", ServiceCritical); } else { - output = "Not currently enabled: Another cluster instance is responsible for the IDO database."; - state = ServiceOK; - } - - if (Checkable::ExecuteCommandProcessFinishedHandler) { - double now = Utility::GetTime(); - ProcessResult pr; - pr.PID = -1; - pr.Output = output; - pr.ExecutionStart = now; - pr.ExecutionEnd = now; - pr.ExitStatus = state; - - Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr); - } else { - cr->SetState(state); - cr->SetOutput(output); - checkable->ProcessCheckResult(cr); + ReportIdoCheck( + checkable, commandObj, cr, + "Not currently enabled: Another cluster instance is responsible for the IDO database.", ServiceOK + ); } return; } @@ -262,30 +184,13 @@ void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult state = ServiceWarning; } - String output = msgbuf.str(); + cr->SetPerformanceData(new Array({ + { new PerfdataValue("queries", qps, false, "", queriesWarning, queriesCritical) }, + { new PerfdataValue("queries_1min", conn->GetQueryCount(60)) }, + { new PerfdataValue("queries_5mins", conn->GetQueryCount(5 * 60)) }, + { new PerfdataValue("queries_15mins", conn->GetQueryCount(15 * 60)) }, + { new PerfdataValue("pending_queries", pendingQueries, false, "", pendingQueriesWarning, pendingQueriesCritical) } + })); - if (Checkable::ExecuteCommandProcessFinishedHandler) { - double now = Utility::GetTime(); - ProcessResult pr; - pr.PID = -1; - pr.Output = output; - pr.ExecutionStart = now; - pr.ExecutionEnd = now; - pr.ExitStatus = state; - - Checkable::ExecuteCommandProcessFinishedHandler(commandObj->GetName(), pr); - } else { - cr->SetState(state); - cr->SetOutput(output); - - cr->SetPerformanceData(new Array({ - { new PerfdataValue("queries", qps, false, "", queriesWarning, queriesCritical) }, - { new PerfdataValue("queries_1min", conn->GetQueryCount(60)) }, - { new PerfdataValue("queries_5mins", conn->GetQueryCount(5 * 60)) }, - { new PerfdataValue("queries_15mins", conn->GetQueryCount(15 * 60)) }, - { new PerfdataValue("pending_queries", pendingQueries, false, "", pendingQueriesWarning, pendingQueriesCritical) } - })); - - checkable->ProcessCheckResult(cr); - } + ReportIdoCheck(checkable, commandObj, cr, msgbuf.str(), state); }