1 /******************************************************************************
3 * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/) *
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 "methods/clusterzonechecktask.hpp"
21 #include "icinga/checkcommand.hpp"
22 #include "icinga/macroprocessor.hpp"
23 #include "remote/apilistener.hpp"
24 #include "remote/endpoint.hpp"
25 #include "remote/zone.hpp"
26 #include "base/function.hpp"
27 #include "base/utility.hpp"
28 #include "base/perfdatavalue.hpp"
30 using namespace icinga;
32 REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterZoneCheck, &ClusterZoneCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
34 void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
35 const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
37 ApiListener::Ptr listener = ApiListener::GetInstance();
40 cr->SetOutput("No API listener is configured for this instance.");
41 cr->SetState(ServiceUnknown);
42 checkable->ProcessCheckResult(cr);
46 CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
47 Value raw_command = commandObj->GetCommandLine();
51 tie(host, service) = GetHostService(checkable);
53 MacroProcessor::ResolverList resolvers;
55 resolvers.push_back(std::make_pair("service", service));
56 resolvers.push_back(std::make_pair("host", host));
57 resolvers.push_back(std::make_pair("command", commandObj));
58 resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
60 String zoneName = MacroProcessor::ResolveMacros("$cluster_zone$", resolvers, checkable->GetLastCheckResult(),
61 NULL, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
63 String missingLagWarning;
64 String missingLagCritical;
66 double lagWarning = MacroProcessor::ResolveMacros("$cluster_lag_warning$", resolvers, checkable->GetLastCheckResult(),
67 &missingLagWarning, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
69 double lagCritical = MacroProcessor::ResolveMacros("$cluster_lag_critical$", resolvers, checkable->GetLastCheckResult(),
70 &missingLagCritical, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
72 if (resolvedMacros && !useResolvedMacros)
75 if (zoneName.IsEmpty()) {
76 cr->SetOutput("Macro 'cluster_zone' must be set.");
77 cr->SetState(ServiceUnknown);
78 checkable->ProcessCheckResult(cr);
82 Zone::Ptr zone = Zone::GetByName(zoneName);
85 cr->SetOutput("Zone '" + zoneName + "' does not exist.");
86 cr->SetState(ServiceUnknown);
87 checkable->ProcessCheckResult(cr);
91 bool connected = false;
94 for (const Endpoint::Ptr& endpoint : zone->GetEndpoints()) {
95 if (endpoint->GetConnected())
98 double eplag = ApiListener::CalculateZoneLag(endpoint);
100 if (eplag > 0 && eplag > zoneLag)
105 cr->SetState(ServiceCritical);
106 cr->SetOutput("Zone '" + zoneName + "' is not connected. Log lag: " + Utility::FormatDuration(zoneLag));
108 cr->SetState(ServiceOK);
109 cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag));
112 /* Check whether the thresholds have been resolved and compare them */
113 if (missingLagCritical.IsEmpty() && zoneLag > lagCritical) {
114 cr->SetState(ServiceCritical);
115 cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag)
116 + " greater than critical threshold: " + Utility::FormatDuration(lagCritical));
117 } else if (missingLagWarning.IsEmpty() && zoneLag > lagWarning) {
118 cr->SetState(ServiceWarning);
119 cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag)
120 + " greater than warning threshold: " + Utility::FormatDuration(lagWarning));
123 Array::Ptr perfdata = new Array();
124 perfdata->Add(new PerfdataValue("slave_lag", zoneLag, false, "s", lagWarning, lagCritical));
125 cr->SetPerformanceData(perfdata);
127 checkable->ProcessCheckResult(cr);