1 /******************************************************************************
3 * Copyright (C) 2012-2018 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/icingachecktask.hpp"
21 #include "icinga/cib.hpp"
22 #include "icinga/service.hpp"
23 #include "icinga/icingaapplication.hpp"
24 #include "base/application.hpp"
25 #include "base/objectlock.hpp"
26 #include "base/utility.hpp"
27 #include "base/perfdatavalue.hpp"
28 #include "base/function.hpp"
29 #include "base/configtype.hpp"
31 using namespace icinga;
33 REGISTER_SCRIPTFUNCTION_NS(Internal, IcingaCheck, &IcingaCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
35 void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
36 const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
38 CheckCommand::Ptr commandObj = checkable->GetCheckCommand();
42 tie(host, service) = GetHostService(checkable);
44 MacroProcessor::ResolverList resolvers;
46 resolvers.emplace_back("service", service);
47 resolvers.emplace_back("host", host);
48 resolvers.emplace_back("command", commandObj);
49 resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
51 String icingaMinVersion = MacroProcessor::ResolveMacros("$icinga_min_version$", resolvers, checkable->GetLastCheckResult(),
52 nullptr, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
54 if (resolvedMacros && !useResolvedMacros)
57 double interval = Utility::GetTime() - Application::GetStartTime();
62 /* use feature stats perfdata */
63 std::pair<Dictionary::Ptr, Array::Ptr> feature_stats = CIB::GetFeatureStats();
65 Array::Ptr perfdata = feature_stats.second;
67 perfdata->Add(new PerfdataValue("active_host_checks", CIB::GetActiveHostChecksStatistics(interval) / interval));
68 perfdata->Add(new PerfdataValue("passive_host_checks", CIB::GetPassiveHostChecksStatistics(interval) / interval));
69 perfdata->Add(new PerfdataValue("active_host_checks_1min", CIB::GetActiveHostChecksStatistics(60)));
70 perfdata->Add(new PerfdataValue("passive_host_checks_1min", CIB::GetPassiveHostChecksStatistics(60)));
71 perfdata->Add(new PerfdataValue("active_host_checks_5min", CIB::GetActiveHostChecksStatistics(60 * 5)));
72 perfdata->Add(new PerfdataValue("passive_host_checks_5min", CIB::GetPassiveHostChecksStatistics(60 * 5)));
73 perfdata->Add(new PerfdataValue("active_host_checks_15min", CIB::GetActiveHostChecksStatistics(60 * 15)));
74 perfdata->Add(new PerfdataValue("passive_host_checks_15min", CIB::GetPassiveHostChecksStatistics(60 * 15)));
76 perfdata->Add(new PerfdataValue("active_service_checks", CIB::GetActiveServiceChecksStatistics(interval) / interval));
77 perfdata->Add(new PerfdataValue("passive_service_checks", CIB::GetPassiveServiceChecksStatistics(interval) / interval));
78 perfdata->Add(new PerfdataValue("active_service_checks_1min", CIB::GetActiveServiceChecksStatistics(60)));
79 perfdata->Add(new PerfdataValue("passive_service_checks_1min", CIB::GetPassiveServiceChecksStatistics(60)));
80 perfdata->Add(new PerfdataValue("active_service_checks_5min", CIB::GetActiveServiceChecksStatistics(60 * 5)));
81 perfdata->Add(new PerfdataValue("passive_service_checks_5min", CIB::GetPassiveServiceChecksStatistics(60 * 5)));
82 perfdata->Add(new PerfdataValue("active_service_checks_15min", CIB::GetActiveServiceChecksStatistics(60 * 15)));
83 perfdata->Add(new PerfdataValue("passive_service_checks_15min", CIB::GetPassiveServiceChecksStatistics(60 * 15)));
85 CheckableCheckStatistics scs = CIB::CalculateServiceCheckStats();
87 perfdata->Add(new PerfdataValue("min_latency", scs.min_latency));
88 perfdata->Add(new PerfdataValue("max_latency", scs.max_latency));
89 perfdata->Add(new PerfdataValue("avg_latency", scs.avg_latency));
90 perfdata->Add(new PerfdataValue("min_execution_time", scs.min_execution_time));
91 perfdata->Add(new PerfdataValue("max_execution_time", scs.max_execution_time));
92 perfdata->Add(new PerfdataValue("avg_execution_time", scs.avg_execution_time));
94 ServiceStatistics ss = CIB::CalculateServiceStats();
96 perfdata->Add(new PerfdataValue("num_services_ok", ss.services_ok));
97 perfdata->Add(new PerfdataValue("num_services_warning", ss.services_warning));
98 perfdata->Add(new PerfdataValue("num_services_critical", ss.services_critical));
99 perfdata->Add(new PerfdataValue("num_services_unknown", ss.services_unknown));
100 perfdata->Add(new PerfdataValue("num_services_pending", ss.services_pending));
101 perfdata->Add(new PerfdataValue("num_services_unreachable", ss.services_unreachable));
102 perfdata->Add(new PerfdataValue("num_services_flapping", ss.services_flapping));
103 perfdata->Add(new PerfdataValue("num_services_in_downtime", ss.services_in_downtime));
104 perfdata->Add(new PerfdataValue("num_services_acknowledged", ss.services_acknowledged));
106 double uptime = Utility::GetTime() - Application::GetStartTime();
107 perfdata->Add(new PerfdataValue("uptime", uptime));
109 HostStatistics hs = CIB::CalculateHostStats();
111 perfdata->Add(new PerfdataValue("num_hosts_up", hs.hosts_up));
112 perfdata->Add(new PerfdataValue("num_hosts_down", hs.hosts_down));
113 perfdata->Add(new PerfdataValue("num_hosts_pending", hs.hosts_pending));
114 perfdata->Add(new PerfdataValue("num_hosts_unreachable", hs.hosts_unreachable));
115 perfdata->Add(new PerfdataValue("num_hosts_flapping", hs.hosts_flapping));
116 perfdata->Add(new PerfdataValue("num_hosts_in_downtime", hs.hosts_in_downtime));
117 perfdata->Add(new PerfdataValue("num_hosts_acknowledged", hs.hosts_acknowledged));
119 std::vector<Endpoint::Ptr> endpoints = ConfigType::GetObjectsByType<Endpoint>();
121 double lastMessageSent = 0;
122 double lastMessageReceived = 0;
123 double messagesSentPerSecond = 0;
124 double messagesReceivedPerSecond = 0;
125 double bytesSentPerSecond = 0;
126 double bytesReceivedPerSecond = 0;
128 for (const Endpoint::Ptr& endpoint : endpoints)
130 if (endpoint->GetLastMessageSent() > lastMessageSent)
131 lastMessageSent = endpoint->GetLastMessageSent();
133 if (endpoint->GetLastMessageReceived() > lastMessageReceived)
134 lastMessageReceived = endpoint->GetLastMessageReceived();
136 messagesSentPerSecond += endpoint->GetMessagesSentPerSecond();
137 messagesReceivedPerSecond += endpoint->GetMessagesReceivedPerSecond();
138 bytesSentPerSecond += endpoint->GetBytesSentPerSecond();
139 bytesReceivedPerSecond += endpoint->GetBytesReceivedPerSecond();
142 perfdata->Add(new PerfdataValue("last_messages_sent", lastMessageSent));
143 perfdata->Add(new PerfdataValue("last_messages_received", lastMessageReceived));
144 perfdata->Add(new PerfdataValue("sum_messages_sent_per_second", messagesSentPerSecond));
145 perfdata->Add(new PerfdataValue("sum_messages_received_per_second", messagesReceivedPerSecond));
146 perfdata->Add(new PerfdataValue("sum_bytes_sent_per_second", bytesSentPerSecond));
147 perfdata->Add(new PerfdataValue("sum_bytes_received_per_second", bytesReceivedPerSecond));
149 cr->SetPerformanceData(perfdata);
150 cr->SetState(ServiceOK);
152 String appVersion = Application::GetAppVersion();
154 String output = "Icinga 2 has been running for " + Utility::FormatDuration(uptime) +
155 ". Version: " + appVersion;
157 /* Indicate a warning if the last reload failed. */
158 double lastReloadFailed = Application::GetLastReloadFailed();
160 if (lastReloadFailed > 0) {
161 output += "; Last reload attempt failed at " + Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", lastReloadFailed);
162 cr->SetState(ServiceWarning);
165 /* Return an error if the version is less than specified (optional). */
166 String parsedAppVersion = appVersion.SubStr(1,5);
168 if (!icingaMinVersion.IsEmpty() && Utility::CompareVersion(icingaMinVersion, parsedAppVersion) < 0) {
169 output += "; Minimum version " + icingaMinVersion + " is not installed.";
170 cr->SetState(ServiceCritical);
173 cr->SetOutput(output);
175 checkable->ProcessCheckResult(cr);