2015-03-12 09:39:53 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-10-18 09:27:04 +02:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/) *
|
2015-03-12 09:39:53 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "db_ido/idochecktask.hpp"
|
|
|
|
#include "icinga/host.hpp"
|
|
|
|
#include "icinga/checkcommand.hpp"
|
|
|
|
#include "icinga/macroprocessor.hpp"
|
|
|
|
#include "remote/apilistener.hpp"
|
|
|
|
#include "remote/endpoint.hpp"
|
|
|
|
#include "remote/zone.hpp"
|
|
|
|
#include "base/function.hpp"
|
|
|
|
#include "base/utility.hpp"
|
2017-05-15 15:51:39 +02:00
|
|
|
#include "base/perfdatavalue.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2015-03-12 09:39:53 +01:00
|
|
|
#include "base/convert.hpp"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
REGISTER_FUNCTION_NONCONST(Internal, IdoCheck, &IdoCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
|
2015-03-12 09:39:53 +01:00
|
|
|
|
|
|
|
void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
|
2017-12-19 15:50:05 +01:00
|
|
|
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
|
2015-03-12 09:39:53 +01:00
|
|
|
{
|
|
|
|
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)
|
2017-11-30 08:19:58 +01:00
|
|
|
resolvers.emplace_back("service", service);
|
|
|
|
resolvers.emplace_back("host", host);
|
|
|
|
resolvers.emplace_back("command", commandObj);
|
|
|
|
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
|
2015-03-12 09:39:53 +01:00
|
|
|
|
|
|
|
String idoType = MacroProcessor::ResolveMacros("$ido_type$", resolvers, checkable->GetLastCheckResult(),
|
2017-12-19 15:50:05 +01:00
|
|
|
nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
|
2015-03-12 09:39:53 +01:00
|
|
|
|
2016-08-14 19:27:35 +02:00
|
|
|
String idoName = MacroProcessor::ResolveMacros("$ido_name$", resolvers, checkable->GetLastCheckResult(),
|
2017-12-19 15:50:05 +01:00
|
|
|
nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
|
2016-08-14 19:27:35 +02:00
|
|
|
|
2018-01-15 15:13:49 +01:00
|
|
|
String missingQueriesWarning;
|
|
|
|
String missingQueriesCritical;
|
|
|
|
String missingPendingQueriesWarning;
|
|
|
|
String missingPendingQueriesCritical;
|
|
|
|
|
|
|
|
double queriesWarning = MacroProcessor::ResolveMacros("$ido_queries_warning$", resolvers, checkable->GetLastCheckResult(),
|
|
|
|
&missingQueriesWarning, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
|
|
|
|
|
|
|
|
double queriesCritical = MacroProcessor::ResolveMacros("$ido_queries_critical$", resolvers, checkable->GetLastCheckResult(),
|
|
|
|
&missingQueriesCritical, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
|
|
|
|
|
|
|
|
double pendingQueriesWarning = MacroProcessor::ResolveMacros("$ido_pending_queries_warning$", resolvers, checkable->GetLastCheckResult(),
|
|
|
|
&missingPendingQueriesWarning, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
|
|
|
|
|
|
|
|
double pendingQueriesCritical = MacroProcessor::ResolveMacros("$ido_pending_queries_critical$", resolvers, checkable->GetLastCheckResult(),
|
|
|
|
&missingPendingQueriesCritical, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
|
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
if (resolvedMacros && !useResolvedMacros)
|
|
|
|
return;
|
|
|
|
|
2015-03-12 11:51:02 +01:00
|
|
|
if (idoType.IsEmpty()) {
|
2018-01-15 15:13:49 +01:00
|
|
|
cr->SetOutput("Attribute 'ido_type' must be set.");
|
2015-03-12 11:51:02 +01:00
|
|
|
cr->SetState(ServiceUnknown);
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
if (idoName.IsEmpty()) {
|
2018-01-15 15:13:49 +01:00
|
|
|
cr->SetOutput("Attribute 'ido_name' must be set.");
|
2015-03-12 09:39:53 +01:00
|
|
|
cr->SetState(ServiceUnknown);
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Type::Ptr type = Type::GetByName(idoType);
|
|
|
|
|
2015-03-20 08:15:07 +01:00
|
|
|
if (!type || !DbConnection::TypeInstance->IsAssignableFrom(type)) {
|
2018-01-15 15:13:49 +01:00
|
|
|
cr->SetOutput("DB IDO type '" + idoType + "' is invalid.");
|
2015-03-12 09:39:53 +01:00
|
|
|
cr->SetState(ServiceUnknown);
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *dtype = dynamic_cast<ConfigType *>(type.get());
|
2015-03-12 09:39:53 +01:00
|
|
|
VERIFY(dtype);
|
|
|
|
|
|
|
|
DbConnection::Ptr conn = static_pointer_cast<DbConnection>(dtype->GetObject(idoName));
|
|
|
|
|
2016-02-02 12:03:12 +01:00
|
|
|
if (!conn) {
|
2018-01-15 15:13:49 +01:00
|
|
|
cr->SetOutput("DB IDO connection '" + idoName + "' does not exist.");
|
2016-02-02 12:03:12 +01:00
|
|
|
cr->SetState(ServiceUnknown);
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
double qps = conn->GetQueryCount(60) / 60.0;
|
|
|
|
|
2016-08-14 20:17:14 +02:00
|
|
|
if (conn->IsPaused()) {
|
2018-01-15 15:13:49 +01:00
|
|
|
cr->SetOutput("DB IDO connection is temporarily disabled on this cluster instance.");
|
2016-08-14 20:17:14 +02:00
|
|
|
cr->SetState(ServiceOK);
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-15 15:13:49 +01:00
|
|
|
double pendingQueries = conn->GetPendingQueryCount();
|
|
|
|
|
2015-03-12 11:51:02 +01:00
|
|
|
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.");
|
2015-03-12 13:44:05 +01:00
|
|
|
cr->SetState(ServiceOK);
|
2015-03-12 11:51:02 +01:00
|
|
|
}
|
2018-01-15 15:13:49 +01:00
|
|
|
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Schema versions. */
|
|
|
|
String schema_version = conn->GetSchemaVersion();
|
|
|
|
std::ostringstream msgbuf;
|
|
|
|
|
|
|
|
if (Utility::CompareVersion(IDO_CURRENT_SCHEMA_VERSION, schema_version) < 0) {
|
|
|
|
msgbuf << "Outdated schema version: '" << schema_version << "'. Latest version: '"
|
|
|
|
<< IDO_CURRENT_SCHEMA_VERSION << "'."
|
|
|
|
<< " Queries per second: " << std::fixed << std::setprecision(3) << qps
|
|
|
|
<< " Pending queries: " << std::fixed << std::setprecision(3) << pendingQueries << ".";
|
|
|
|
|
|
|
|
cr->SetState(ServiceWarning);
|
2015-03-12 09:39:53 +01:00
|
|
|
} else {
|
2018-01-15 15:13:49 +01:00
|
|
|
msgbuf << "Connected to the database server (Schema version: '" << schema_version << "')."
|
|
|
|
<< " Queries per second: " << std::fixed << std::setprecision(3) << qps
|
|
|
|
<< " Pending queries: " << std::fixed << std::setprecision(3) << pendingQueries << ".";
|
2015-03-12 09:39:53 +01:00
|
|
|
|
2018-01-15 15:13:49 +01:00
|
|
|
cr->SetState(ServiceOK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check whether the thresholds have been defined and match. */
|
|
|
|
if (missingQueriesCritical.IsEmpty() && qps < queriesCritical) {
|
|
|
|
msgbuf << " " << qps << " queries/s lower than critical threshold (" << queriesCritical << " queries/s).";
|
2016-06-17 12:43:05 +02:00
|
|
|
|
2018-01-15 15:13:49 +01:00
|
|
|
cr->SetState(ServiceCritical);
|
|
|
|
} else if (missingQueriesWarning.IsEmpty() && qps < queriesWarning) {
|
|
|
|
msgbuf << " " << qps << " queries/s lower than warning threshold (" << queriesWarning << " queries/s).";
|
2016-06-17 12:43:05 +02:00
|
|
|
|
2018-01-15 15:13:49 +01:00
|
|
|
cr->SetState(ServiceWarning);
|
2015-03-12 09:39:53 +01:00
|
|
|
}
|
|
|
|
|
2018-01-15 15:13:49 +01:00
|
|
|
if (missingPendingQueriesCritical.IsEmpty() && pendingQueries > pendingQueriesCritical) {
|
|
|
|
msgbuf << " " << pendingQueries << " pending queries greater than critical threshold ("
|
|
|
|
<< pendingQueriesCritical << " queries).";
|
|
|
|
|
|
|
|
cr->SetState(ServiceCritical);
|
|
|
|
} else if (missingPendingQueriesWarning.IsEmpty() && pendingQueries > pendingQueriesWarning) {
|
|
|
|
msgbuf << " " << pendingQueries << " pending queries greater than warning threshold ("
|
|
|
|
<< pendingQueriesWarning << " queries).";
|
|
|
|
|
|
|
|
cr->SetState(ServiceWarning);
|
|
|
|
}
|
|
|
|
|
|
|
|
cr->SetOutput(msgbuf.str());
|
|
|
|
|
2018-01-11 11:17:38 +01:00
|
|
|
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) }
|
|
|
|
}));
|
2015-03-12 09:39:53 +01:00
|
|
|
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
}
|