1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://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/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"
35 using namespace icinga;
37 REGISTER_FUNCTION_NONCONST(Internal, IcingaCheck, &IcingaCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
39 void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
40 const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
42 REQUIRE_NOT_NULL(checkable);
45 CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
49 tie(host, service) = GetHostService(checkable);
51 MacroProcessor::ResolverList resolvers;
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());
58 String missingIcingaMinVersion;
60 String icingaMinVersion = MacroProcessor::ResolveMacros("$icinga_min_version$", resolvers, checkable->GetLastCheckResult(),
61 &missingIcingaMinVersion, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
63 if (resolvedMacros && !useResolvedMacros)
66 double interval = Utility::GetTime() - Application::GetStartTime();
71 /* use feature stats perfdata */
72 std::pair<Dictionary::Ptr, Array::Ptr> feature_stats = CIB::GetFeatureStats();
74 Array::Ptr perfdata = feature_stats.second;
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)));
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)));
94 perfdata->Add(new PerfdataValue("current_concurrent_checks", Checkable::GetPendingChecks()));
95 perfdata->Add(new PerfdataValue("remote_check_queue", ClusterEvents::GetCheckRequestQueueSize()));
97 CheckableCheckStatistics scs = CIB::CalculateServiceCheckStats();
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));
106 ServiceStatistics ss = CIB::CalculateServiceStats();
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));
118 double uptime = Utility::GetTime() - Application::GetStartTime();
119 perfdata->Add(new PerfdataValue("uptime", uptime));
121 HostStatistics hs = CIB::CalculateHostStats();
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));
131 std::vector<Endpoint::Ptr> endpoints = ConfigType::GetObjectsByType<Endpoint>();
133 double lastMessageSent = 0;
134 double lastMessageReceived = 0;
135 double messagesSentPerSecond = 0;
136 double messagesReceivedPerSecond = 0;
137 double bytesSentPerSecond = 0;
138 double bytesReceivedPerSecond = 0;
140 for (const Endpoint::Ptr& endpoint : endpoints)
142 if (endpoint->GetLastMessageSent() > lastMessageSent)
143 lastMessageSent = endpoint->GetLastMessageSent();
145 if (endpoint->GetLastMessageReceived() > lastMessageReceived)
146 lastMessageReceived = endpoint->GetLastMessageReceived();
148 messagesSentPerSecond += endpoint->GetMessagesSentPerSecond();
149 messagesReceivedPerSecond += endpoint->GetMessagesReceivedPerSecond();
150 bytesSentPerSecond += endpoint->GetBytesSentPerSecond();
151 bytesReceivedPerSecond += endpoint->GetBytesReceivedPerSecond();
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));
161 cr->SetPerformanceData(perfdata);
162 cr->SetState(ServiceOK);
164 String appVersion = Application::GetAppVersion();
166 String output = "Icinga 2 has been running for " + Utility::FormatDuration(uptime) +
167 ". Version: " + appVersion;
169 /* Indicate a warning if the last reload failed. */
170 double lastReloadFailed = Application::GetLastReloadFailed();
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);
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'.
180 int endOfVersionNumber = appVersion.FindFirstOf("-") - 1;
181 String parsedAppVersion = appVersion.SubStr(1, endOfVersionNumber);
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);
189 cr->SetOutput(output);
191 checkable->ProcessCheckResult(cr);