1 /******************************************************************************
3 * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "db_ido/idochecktask.hpp"
21 #include "icinga/host.hpp"
22 #include "icinga/checkcommand.hpp"
23 #include "icinga/macroprocessor.hpp"
24 #include "icinga/perfdatavalue.hpp"
25 #include "remote/apilistener.hpp"
26 #include "remote/endpoint.hpp"
27 #include "remote/zone.hpp"
28 #include "base/function.hpp"
29 #include "base/utility.hpp"
30 #include "base/dynamictype.hpp"
31 #include "base/convert.hpp"
32 #include <boost/foreach.hpp>
34 using namespace icinga;
36 REGISTER_SCRIPTFUNCTION(IdoCheck, &IdoCheckTask::ScriptFunc);
38 void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
39 const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
41 CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
42 Value raw_command = commandObj->GetCommandLine();
46 tie(host, service) = GetHostService(checkable);
48 MacroProcessor::ResolverList resolvers;
50 resolvers.push_back(std::make_pair("service", service));
51 resolvers.push_back(std::make_pair("host", host));
52 resolvers.push_back(std::make_pair("command", commandObj));
53 resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
55 String idoType = MacroProcessor::ResolveMacros("$ido_type$", resolvers, checkable->GetLastCheckResult(),
56 NULL, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
58 if (resolvedMacros && !useResolvedMacros)
61 if (idoType.IsEmpty()) {
62 cr->SetOutput("Macro 'ido_type' must be set.");
63 cr->SetState(ServiceUnknown);
64 checkable->ProcessCheckResult(cr);
68 String idoName = MacroProcessor::ResolveMacros("$ido_name$", resolvers, checkable->GetLastCheckResult(),
69 NULL, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
71 if (resolvedMacros && !useResolvedMacros)
74 if (idoName.IsEmpty()) {
75 cr->SetOutput("Macro 'ido_name' must be set.");
76 cr->SetState(ServiceUnknown);
77 checkable->ProcessCheckResult(cr);
81 Type::Ptr type = Type::GetByName(idoType);
83 if (!type || !Type::GetByName("DbConnection")->IsAssignableFrom(type)) {
84 cr->SetOutput("IDO type '" + idoType + "' is invalid.");
85 cr->SetState(ServiceUnknown);
86 checkable->ProcessCheckResult(cr);
90 DynamicType::Ptr dtype = DynamicType::GetByName(idoType);
93 DbConnection::Ptr conn = static_pointer_cast<DbConnection>(dtype->GetObject(idoName));
95 double qps = conn->GetQueryCount(60) / 60.0;
97 if (!conn->GetConnected()) {
98 if (conn->GetShouldConnect()) {
99 cr->SetOutput("Could not connect to the database server.");
100 cr->SetState(ServiceCritical);
102 cr->SetOutput("Not currently enabled: Another cluster instance is responsible for the IDO database.");
103 cr->SetState(ServiceOK);
106 String schema_version = conn->GetSchemaVersion();
108 if (Utility::CompareVersion(IDO_CURRENT_SCHEMA_VERSION, schema_version) < 0) {
109 cr->SetOutput("Outdated schema version: " + schema_version + "; Latest version: " IDO_CURRENT_SCHEMA_VERSION);
110 cr->SetState(ServiceWarning);
112 std::ostringstream msgbuf;
113 msgbuf << "Connected to the database server; queries per second: " << std::fixed << std::setprecision(3) << qps;
114 cr->SetOutput(msgbuf.str());
115 cr->SetState(ServiceOK);
119 Array::Ptr perfdata = new Array();
120 perfdata->Add(new PerfdataValue("queries", qps));
121 perfdata->Add(new PerfdataValue("queries_1min", conn->GetQueryCount(60)));
122 perfdata->Add(new PerfdataValue("queries_5mins", conn->GetQueryCount(5 * 60)));
123 perfdata->Add(new PerfdataValue("queries_15mins", conn->GetQueryCount(15 * 60)));
124 cr->SetPerformanceData(perfdata);
126 checkable->ProcessCheckResult(cr);