]> granicus.if.org Git - icinga2/blob - lib/db_ido/idochecktask.cpp
Build fix
[icinga2] / lib / db_ido / idochecktask.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org)    *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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>
33
34 using namespace icinga;
35
36 REGISTER_SCRIPTFUNCTION(IdoCheck, &IdoCheckTask::ScriptFunc);
37
38 void IdoCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
39     const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
40 {
41         CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
42         Value raw_command = commandObj->GetCommandLine();
43
44         Host::Ptr host;
45         Service::Ptr service;
46         tie(host, service) = GetHostService(checkable);
47
48         MacroProcessor::ResolverList resolvers;
49         if (service)
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()));
54
55         String idoType = MacroProcessor::ResolveMacros("$ido_type$", resolvers, checkable->GetLastCheckResult(),
56             NULL, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
57
58         if (resolvedMacros && !useResolvedMacros)
59                 return;
60
61         if (idoType.IsEmpty()) {
62                 cr->SetOutput("Macro 'ido_type' must be set.");
63                 cr->SetState(ServiceUnknown);
64                 checkable->ProcessCheckResult(cr);
65                 return;
66         }
67
68         String idoName = MacroProcessor::ResolveMacros("$ido_name$", resolvers, checkable->GetLastCheckResult(),
69             NULL, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
70
71         if (resolvedMacros && !useResolvedMacros)
72                 return;
73
74         if (idoName.IsEmpty()) {
75                 cr->SetOutput("Macro 'ido_name' must be set.");
76                 cr->SetState(ServiceUnknown);
77                 checkable->ProcessCheckResult(cr);
78                 return;
79         }
80
81         Type::Ptr type = Type::GetByName(idoType);
82
83         if (!type || !Type::GetByName("DbConnection")->IsAssignableFrom(type)) {
84                 cr->SetOutput("IDO type '" + idoType + "' is invalid.");
85                 cr->SetState(ServiceUnknown);
86                 checkable->ProcessCheckResult(cr);
87                 return;
88         }
89
90         DynamicType::Ptr dtype = DynamicType::GetByName(idoType);
91         VERIFY(dtype);
92
93         DbConnection::Ptr conn = static_pointer_cast<DbConnection>(dtype->GetObject(idoName));
94
95         double qps = conn->GetQueryCount(60) / 60.0;
96
97         if (!conn->GetConnected()) {
98                 if (conn->GetShouldConnect()) {
99                         cr->SetOutput("Could not connect to the database server.");
100                         cr->SetState(ServiceCritical);
101                 } else {
102                         cr->SetOutput("Not currently enabled: Another cluster instance is responsible for the IDO database.");
103                         cr->SetState(ServiceOK);
104                 }
105         } else {
106                 String schema_version = conn->GetSchemaVersion();
107
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);
111                 } else {
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);
116                 }
117         }
118
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);
125
126         checkable->ProcessCheckResult(cr);
127 }