]> granicus.if.org Git - icinga2/blob - lib/methods/clusterzonechecktask.cpp
Merge branch 'support/2.3'
[icinga2] / lib / methods / clusterzonechecktask.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 "methods/clusterzonechecktask.hpp"
21 #include "icinga/checkcommand.hpp"
22 #include "icinga/macroprocessor.hpp"
23 #include "icinga/perfdatavalue.hpp"
24 #include "remote/apilistener.hpp"
25 #include "remote/endpoint.hpp"
26 #include "remote/zone.hpp"
27 #include "base/function.hpp"
28 #include "base/utility.hpp"
29 #include <boost/foreach.hpp>
30
31 using namespace icinga;
32
33 REGISTER_SCRIPTFUNCTION(ClusterZoneCheck, &ClusterZoneCheckTask::ScriptFunc);
34
35 void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
36     const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
37 {
38         ApiListener::Ptr listener = ApiListener::GetInstance();
39
40         if (!listener) {
41                 cr->SetOutput("No API listener is configured for this instance.");
42                 cr->SetState(ServiceUnknown);
43                 checkable->ProcessCheckResult(cr);
44                 return;
45         }
46
47         CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
48         Value raw_command = commandObj->GetCommandLine();
49
50         Host::Ptr host;
51         Service::Ptr service;
52         tie(host, service) = GetHostService(checkable);
53
54         MacroProcessor::ResolverList resolvers;
55         if (service)
56                 resolvers.push_back(std::make_pair("service", service));
57         resolvers.push_back(std::make_pair("host", host));
58         resolvers.push_back(std::make_pair("command", commandObj));
59         resolvers.push_back(std::make_pair("icinga", IcingaApplication::GetInstance()));
60
61         String zoneName = MacroProcessor::ResolveMacros("$cluster_zone$", resolvers, checkable->GetLastCheckResult(),
62             NULL, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
63
64         if (resolvedMacros && !useResolvedMacros)
65                 return;
66
67         if (zoneName.IsEmpty()) {
68                 cr->SetOutput("Macro 'cluster_zone' must be set.");
69                 cr->SetState(ServiceUnknown);
70                 checkable->ProcessCheckResult(cr);
71                 return;
72         }
73
74         Zone::Ptr zone = Zone::GetByName(zoneName);
75
76         if (!zone) {
77                 cr->SetOutput("Zone '" + zoneName + "' does not exist.");
78                 cr->SetState(ServiceUnknown);
79                 checkable->ProcessCheckResult(cr);
80                 return;
81         }
82
83         bool connected = false;
84         double zoneLag = 0;
85
86         BOOST_FOREACH(const Endpoint::Ptr& endpoint, zone->GetEndpoints()) {
87                 if (endpoint->GetConnected())
88                         connected = true;
89
90                 double eplag = ApiListener::CalculateZoneLag(endpoint);
91
92                 if (eplag > 0 && eplag > zoneLag)
93                         zoneLag = eplag;
94         }
95
96         if (!connected) {
97                 cr->SetState(ServiceCritical);
98                 cr->SetOutput("Zone '" + zoneName + "' is not connected. Log lag: " + Utility::FormatDuration(zoneLag));
99         } else {
100                 cr->SetState(ServiceOK);
101                 cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag));
102         }
103
104         Array::Ptr perfdata = new Array();
105         perfdata->Add(new PerfdataValue("slave_lag", zoneLag));
106         cr->SetPerformanceData(perfdata);
107
108         checkable->ProcessCheckResult(cr);
109 }