]> granicus.if.org Git - icinga2/blob - lib/methods/clusterzonechecktask.cpp
Merge pull request #5861 from Icinga/fix/invalid-memory-access
[icinga2] / lib / methods / clusterzonechecktask.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2017 Icinga Development Team (https://www.icinga.com/)  *
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 "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"
29
30 using namespace icinga;
31
32 REGISTER_SCRIPTFUNCTION_NS(Internal, ClusterZoneCheck, &ClusterZoneCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
33
34 void ClusterZoneCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
35     const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
36 {
37         ApiListener::Ptr listener = ApiListener::GetInstance();
38
39         if (!listener) {
40                 cr->SetOutput("No API listener is configured for this instance.");
41                 cr->SetState(ServiceUnknown);
42                 checkable->ProcessCheckResult(cr);
43                 return;
44         }
45
46         CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
47         Value raw_command = commandObj->GetCommandLine();
48
49         Host::Ptr host;
50         Service::Ptr service;
51         tie(host, service) = GetHostService(checkable);
52
53         MacroProcessor::ResolverList resolvers;
54         if (service)
55                 resolvers.emplace_back("service", service);
56         resolvers.emplace_back("host", host);
57         resolvers.emplace_back("command", commandObj);
58         resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
59
60         String zoneName = MacroProcessor::ResolveMacros("$cluster_zone$", resolvers, checkable->GetLastCheckResult(),
61             nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
62
63         String missingLagWarning;
64         String missingLagCritical;
65
66         double lagWarning = MacroProcessor::ResolveMacros("$cluster_lag_warning$", resolvers, checkable->GetLastCheckResult(),
67             &missingLagWarning, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
68
69         double lagCritical = MacroProcessor::ResolveMacros("$cluster_lag_critical$", resolvers, checkable->GetLastCheckResult(),
70             &missingLagCritical, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
71
72         if (resolvedMacros && !useResolvedMacros)
73                 return;
74
75         if (zoneName.IsEmpty()) {
76                 cr->SetOutput("Macro 'cluster_zone' must be set.");
77                 cr->SetState(ServiceUnknown);
78                 checkable->ProcessCheckResult(cr);
79                 return;
80         }
81
82         Zone::Ptr zone = Zone::GetByName(zoneName);
83
84         if (!zone) {
85                 cr->SetOutput("Zone '" + zoneName + "' does not exist.");
86                 cr->SetState(ServiceUnknown);
87                 checkable->ProcessCheckResult(cr);
88                 return;
89         }
90
91         bool connected = false;
92         double zoneLag = 0;
93
94         double lastMessageSent = 0;
95         double lastMessageReceived = 0;
96         double messagesSentPerSecond = 0;
97         double messagesReceivedPerSecond = 0;
98         double bytesSentPerSecond = 0;
99         double bytesReceivedPerSecond = 0;
100
101         for (const Endpoint::Ptr& endpoint : zone->GetEndpoints()) {
102                 if (endpoint->GetConnected())
103                         connected = true;
104
105                 double eplag = ApiListener::CalculateZoneLag(endpoint);
106
107                 if (eplag > 0 && eplag > zoneLag)
108                         zoneLag = eplag;
109
110                 if (endpoint->GetLastMessageSent() > lastMessageSent)
111                         lastMessageSent = endpoint->GetLastMessageSent();
112
113                 if (endpoint->GetLastMessageReceived() > lastMessageReceived)
114                         lastMessageReceived = endpoint->GetLastMessageReceived();
115
116                 messagesSentPerSecond += endpoint->GetMessagesSentPerSecond();
117                 messagesReceivedPerSecond += endpoint->GetMessagesReceivedPerSecond();
118                 bytesSentPerSecond += endpoint->GetBytesSentPerSecond();
119                 bytesReceivedPerSecond += endpoint->GetBytesReceivedPerSecond();
120         }
121
122         if (!connected) {
123                 cr->SetState(ServiceCritical);
124                 cr->SetOutput("Zone '" + zoneName + "' is not connected. Log lag: " + Utility::FormatDuration(zoneLag));
125         } else {
126                 cr->SetState(ServiceOK);
127                 cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag));
128         }
129
130         /* Check whether the thresholds have been resolved and compare them */
131         if (missingLagCritical.IsEmpty() && zoneLag > lagCritical) {
132                 cr->SetState(ServiceCritical);
133                 cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag)
134                     + " greater than critical threshold: " + Utility::FormatDuration(lagCritical));
135         } else if (missingLagWarning.IsEmpty() && zoneLag > lagWarning) {
136                 cr->SetState(ServiceWarning);
137                 cr->SetOutput("Zone '" + zoneName + "' is connected. Log lag: " + Utility::FormatDuration(zoneLag)
138                     + " greater than warning threshold: " + Utility::FormatDuration(lagWarning));
139         }
140
141         Array::Ptr perfdata = new Array();
142         perfdata->Add(new PerfdataValue("slave_lag", zoneLag, false, "s", lagWarning, lagCritical));
143         perfdata->Add(new PerfdataValue("last_messages_sent", lastMessageSent));
144         perfdata->Add(new PerfdataValue("last_messages_received", lastMessageReceived));
145         perfdata->Add(new PerfdataValue("sum_messages_sent_per_second", messagesSentPerSecond));
146         perfdata->Add(new PerfdataValue("sum_messages_received_per_second", messagesReceivedPerSecond));
147         perfdata->Add(new PerfdataValue("sum_bytes_sent_per_second", bytesSentPerSecond));
148         perfdata->Add(new PerfdataValue("sum_bytes_received_per_second", bytesReceivedPerSecond));
149         cr->SetPerformanceData(perfdata);
150
151         checkable->ProcessCheckResult(cr);
152 }