2015-03-12 09:39:53 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
|
|
|
|
* *
|
|
|
|
* 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 "icinga/perfdatavalue.hpp"
|
|
|
|
#include "remote/apilistener.hpp"
|
|
|
|
#include "remote/endpoint.hpp"
|
|
|
|
#include "remote/zone.hpp"
|
|
|
|
#include "base/function.hpp"
|
|
|
|
#include "base/utility.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"
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
REGISTER_SCRIPTFUNCTION(IdoCheck, &IdoCheckTask::ScriptFunc);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2015-03-12 11:51:02 +01:00
|
|
|
if (idoType.IsEmpty()) {
|
|
|
|
cr->SetOutput("Macro 'ido_type' must be set.");
|
|
|
|
cr->SetState(ServiceUnknown);
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-12 09:39:53 +01:00
|
|
|
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);
|
|
|
|
|
2015-03-20 08:15:07 +01:00
|
|
|
if (!type || !DbConnection::TypeInstance->IsAssignableFrom(type)) {
|
2015-03-12 09:39:53 +01:00
|
|
|
cr->SetOutput("IDO type '" + idoType + "' is invalid.");
|
|
|
|
cr->SetState(ServiceUnknown);
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-15 20:28:05 +02:00
|
|
|
ConfigType::Ptr dtype = ConfigType::GetByName(idoType);
|
2015-03-12 09:39:53 +01:00
|
|
|
VERIFY(dtype);
|
|
|
|
|
|
|
|
DbConnection::Ptr conn = static_pointer_cast<DbConnection>(dtype->GetObject(idoName));
|
|
|
|
|
|
|
|
double qps = conn->GetQueryCount(60) / 60.0;
|
|
|
|
|
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
|
|
|
}
|
2015-03-12 09:39:53 +01:00
|
|
|
} 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)));
|
2015-03-13 12:00:30 +01:00
|
|
|
perfdata->Add(new PerfdataValue("pending_queries", conn->GetPendingQueryCount()));
|
2015-03-12 09:39:53 +01:00
|
|
|
cr->SetPerformanceData(perfdata);
|
|
|
|
|
|
|
|
checkable->ProcessCheckResult(cr);
|
|
|
|
}
|