]> granicus.if.org Git - icinga2/blob - lib/methods/icingachecktask.cpp
Minor styleguide
[icinga2] / lib / methods / icingachecktask.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://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/icingachecktask.hpp"
21 #include "icinga/cib.hpp"
22 #include "icinga/service.hpp"
23 #include "icinga/checkcommand.hpp"
24 #include "icinga/macroprocessor.hpp"
25 #include "icinga/icingaapplication.hpp"
26 #include "icinga/clusterevents.hpp"
27 #include "icinga/checkable.hpp"
28 #include "base/application.hpp"
29 #include "base/objectlock.hpp"
30 #include "base/utility.hpp"
31 #include "base/perfdatavalue.hpp"
32 #include "base/function.hpp"
33 #include "base/configtype.hpp"
34
35 using namespace icinga;
36
37 REGISTER_FUNCTION_NONCONST(Internal, IcingaCheck, &IcingaCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
38
39 void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
40         const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
41 {
42         REQUIRE_NOT_NULL(checkable);
43         REQUIRE_NOT_NULL(cr);
44
45         CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
46
47         Host::Ptr host;
48         Service::Ptr service;
49         tie(host, service) = GetHostService(checkable);
50
51         MacroProcessor::ResolverList resolvers;
52         if (service)
53                 resolvers.emplace_back("service", service);
54         resolvers.emplace_back("host", host);
55         resolvers.emplace_back("command", commandObj);
56         resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
57
58         String missingIcingaMinVersion;
59
60         String icingaMinVersion = MacroProcessor::ResolveMacros("$icinga_min_version$", resolvers, checkable->GetLastCheckResult(),
61                 &missingIcingaMinVersion, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
62
63         if (resolvedMacros && !useResolvedMacros)
64                 return;
65
66         double interval = Utility::GetTime() - Application::GetStartTime();
67
68         if (interval > 60)
69                 interval = 60;
70
71         /* use feature stats perfdata */
72         std::pair<Dictionary::Ptr, Array::Ptr> feature_stats = CIB::GetFeatureStats();
73
74         Array::Ptr perfdata = feature_stats.second;
75
76         perfdata->Add(new PerfdataValue("active_host_checks", CIB::GetActiveHostChecksStatistics(interval) / interval));
77         perfdata->Add(new PerfdataValue("passive_host_checks", CIB::GetPassiveHostChecksStatistics(interval) / interval));
78         perfdata->Add(new PerfdataValue("active_host_checks_1min", CIB::GetActiveHostChecksStatistics(60)));
79         perfdata->Add(new PerfdataValue("passive_host_checks_1min", CIB::GetPassiveHostChecksStatistics(60)));
80         perfdata->Add(new PerfdataValue("active_host_checks_5min", CIB::GetActiveHostChecksStatistics(60 * 5)));
81         perfdata->Add(new PerfdataValue("passive_host_checks_5min", CIB::GetPassiveHostChecksStatistics(60 * 5)));
82         perfdata->Add(new PerfdataValue("active_host_checks_15min", CIB::GetActiveHostChecksStatistics(60 * 15)));
83         perfdata->Add(new PerfdataValue("passive_host_checks_15min", CIB::GetPassiveHostChecksStatistics(60 * 15)));
84
85         perfdata->Add(new PerfdataValue("active_service_checks", CIB::GetActiveServiceChecksStatistics(interval) / interval));
86         perfdata->Add(new PerfdataValue("passive_service_checks", CIB::GetPassiveServiceChecksStatistics(interval) / interval));
87         perfdata->Add(new PerfdataValue("active_service_checks_1min", CIB::GetActiveServiceChecksStatistics(60)));
88         perfdata->Add(new PerfdataValue("passive_service_checks_1min", CIB::GetPassiveServiceChecksStatistics(60)));
89         perfdata->Add(new PerfdataValue("active_service_checks_5min", CIB::GetActiveServiceChecksStatistics(60 * 5)));
90         perfdata->Add(new PerfdataValue("passive_service_checks_5min", CIB::GetPassiveServiceChecksStatistics(60 * 5)));
91         perfdata->Add(new PerfdataValue("active_service_checks_15min", CIB::GetActiveServiceChecksStatistics(60 * 15)));
92         perfdata->Add(new PerfdataValue("passive_service_checks_15min", CIB::GetPassiveServiceChecksStatistics(60 * 15)));
93
94         perfdata->Add(new PerfdataValue("current_concurrent_checks", Checkable::GetPendingChecks()));
95         perfdata->Add(new PerfdataValue("remote_check_queue", ClusterEvents::GetCheckRequestQueueSize()));
96
97         CheckableCheckStatistics scs = CIB::CalculateServiceCheckStats();
98
99         perfdata->Add(new PerfdataValue("min_latency", scs.min_latency));
100         perfdata->Add(new PerfdataValue("max_latency", scs.max_latency));
101         perfdata->Add(new PerfdataValue("avg_latency", scs.avg_latency));
102         perfdata->Add(new PerfdataValue("min_execution_time", scs.min_execution_time));
103         perfdata->Add(new PerfdataValue("max_execution_time", scs.max_execution_time));
104         perfdata->Add(new PerfdataValue("avg_execution_time", scs.avg_execution_time));
105
106         ServiceStatistics ss = CIB::CalculateServiceStats();
107
108         perfdata->Add(new PerfdataValue("num_services_ok", ss.services_ok));
109         perfdata->Add(new PerfdataValue("num_services_warning", ss.services_warning));
110         perfdata->Add(new PerfdataValue("num_services_critical", ss.services_critical));
111         perfdata->Add(new PerfdataValue("num_services_unknown", ss.services_unknown));
112         perfdata->Add(new PerfdataValue("num_services_pending", ss.services_pending));
113         perfdata->Add(new PerfdataValue("num_services_unreachable", ss.services_unreachable));
114         perfdata->Add(new PerfdataValue("num_services_flapping", ss.services_flapping));
115         perfdata->Add(new PerfdataValue("num_services_in_downtime", ss.services_in_downtime));
116         perfdata->Add(new PerfdataValue("num_services_acknowledged", ss.services_acknowledged));
117
118         double uptime = Utility::GetTime() - Application::GetStartTime();
119         perfdata->Add(new PerfdataValue("uptime", uptime));
120
121         HostStatistics hs = CIB::CalculateHostStats();
122
123         perfdata->Add(new PerfdataValue("num_hosts_up", hs.hosts_up));
124         perfdata->Add(new PerfdataValue("num_hosts_down", hs.hosts_down));
125         perfdata->Add(new PerfdataValue("num_hosts_pending", hs.hosts_pending));
126         perfdata->Add(new PerfdataValue("num_hosts_unreachable", hs.hosts_unreachable));
127         perfdata->Add(new PerfdataValue("num_hosts_flapping", hs.hosts_flapping));
128         perfdata->Add(new PerfdataValue("num_hosts_in_downtime", hs.hosts_in_downtime));
129         perfdata->Add(new PerfdataValue("num_hosts_acknowledged", hs.hosts_acknowledged));
130
131         std::vector<Endpoint::Ptr> endpoints = ConfigType::GetObjectsByType<Endpoint>();
132
133         double lastMessageSent = 0;
134         double lastMessageReceived = 0;
135         double messagesSentPerSecond = 0;
136         double messagesReceivedPerSecond = 0;
137         double bytesSentPerSecond = 0;
138         double bytesReceivedPerSecond = 0;
139
140         for (const Endpoint::Ptr& endpoint : endpoints)
141         {
142                 if (endpoint->GetLastMessageSent() > lastMessageSent)
143                         lastMessageSent = endpoint->GetLastMessageSent();
144
145                 if (endpoint->GetLastMessageReceived() > lastMessageReceived)
146                         lastMessageReceived = endpoint->GetLastMessageReceived();
147
148                 messagesSentPerSecond += endpoint->GetMessagesSentPerSecond();
149                 messagesReceivedPerSecond += endpoint->GetMessagesReceivedPerSecond();
150                 bytesSentPerSecond += endpoint->GetBytesSentPerSecond();
151                 bytesReceivedPerSecond += endpoint->GetBytesReceivedPerSecond();
152         }
153
154         perfdata->Add(new PerfdataValue("last_messages_sent", lastMessageSent));
155         perfdata->Add(new PerfdataValue("last_messages_received", lastMessageReceived));
156         perfdata->Add(new PerfdataValue("sum_messages_sent_per_second", messagesSentPerSecond));
157         perfdata->Add(new PerfdataValue("sum_messages_received_per_second", messagesReceivedPerSecond));
158         perfdata->Add(new PerfdataValue("sum_bytes_sent_per_second", bytesSentPerSecond));
159         perfdata->Add(new PerfdataValue("sum_bytes_received_per_second", bytesReceivedPerSecond));
160
161         cr->SetPerformanceData(perfdata);
162         cr->SetState(ServiceOK);
163
164         String appVersion = Application::GetAppVersion();
165
166         String output = "Icinga 2 has been running for " + Utility::FormatDuration(uptime) +
167                 ". Version: " + appVersion;
168
169         /* Indicate a warning if the last reload failed. */
170         double lastReloadFailed = Application::GetLastReloadFailed();
171
172         if (lastReloadFailed > 0) {
173                 output += "; Last reload attempt failed at " + Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", lastReloadFailed);
174                 cr->SetState(ServiceWarning);
175         }
176
177         /* Extract the version number of the running Icinga2 instance.
178          * We assume that appVersion will allways be something like 'v2.10.1-8-gaebe6da' and we want to extract '2.10.1'.
179          */
180         int endOfVersionNumber = appVersion.FindFirstOf("-") - 1;
181         String parsedAppVersion = appVersion.SubStr(1, endOfVersionNumber);
182
183         /* Return an error if the version is less than specified (optional). */
184         if (missingIcingaMinVersion.IsEmpty() && !icingaMinVersion.IsEmpty() && Utility::CompareVersion(icingaMinVersion, parsedAppVersion) < 0) {
185                 output += "; Minimum version " + icingaMinVersion + " is not installed.";
186                 cr->SetState(ServiceCritical);
187         }
188
189         cr->SetOutput(output);
190
191         checkable->ProcessCheckResult(cr);
192 }