2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-11-12 12:04:52 +01:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "methods/icingachecktask.hpp"
|
|
|
|
#include "icinga/cib.hpp"
|
|
|
|
#include "icinga/service.hpp"
|
2018-01-12 11:03:49 +01:00
|
|
|
#include "icinga/checkcommand.hpp"
|
|
|
|
#include "icinga/macroprocessor.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/icingaapplication.hpp"
|
2018-01-18 15:22:16 +01:00
|
|
|
#include "icinga/clusterevents.hpp"
|
2018-03-01 15:01:47 +01:00
|
|
|
#include "icinga/checkable.hpp"
|
2018-10-23 17:23:34 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/utility.hpp"
|
2017-05-15 15:51:39 +02:00
|
|
|
#include "base/perfdatavalue.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.hpp"
|
2013-11-12 12:04:52 +01:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2018-08-07 13:55:41 +02:00
|
|
|
REGISTER_FUNCTION_NONCONST(Internal, IcingaCheck, &IcingaCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
|
2013-11-12 12:04:52 +01:00
|
|
|
|
2018-01-08 19:44:54 +01:00
|
|
|
void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& checkable, const CheckResult::Ptr& cr,
|
2017-12-19 15:50:05 +01:00
|
|
|
const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
|
2013-11-12 12:04:52 +01:00
|
|
|
{
|
2018-02-21 13:42:58 +01:00
|
|
|
REQUIRE_NOT_NULL(checkable);
|
|
|
|
REQUIRE_NOT_NULL(cr);
|
2018-01-30 11:26:07 +01:00
|
|
|
|
2019-07-10 10:15:15 +02:00
|
|
|
CheckCommand::Ptr command = checkable->GetCheckCommand();
|
2018-01-08 19:44:54 +01:00
|
|
|
|
|
|
|
Host::Ptr host;
|
|
|
|
Service::Ptr service;
|
|
|
|
tie(host, service) = GetHostService(checkable);
|
|
|
|
|
|
|
|
MacroProcessor::ResolverList resolvers;
|
|
|
|
if (service)
|
|
|
|
resolvers.emplace_back("service", service);
|
|
|
|
resolvers.emplace_back("host", host);
|
2019-07-10 10:15:15 +02:00
|
|
|
resolvers.emplace_back("command", command);
|
2018-01-08 19:44:54 +01:00
|
|
|
resolvers.emplace_back("icinga", IcingaApplication::GetInstance());
|
|
|
|
|
2018-05-18 15:11:56 +02:00
|
|
|
String missingIcingaMinVersion;
|
|
|
|
|
2018-01-08 19:44:54 +01:00
|
|
|
String icingaMinVersion = MacroProcessor::ResolveMacros("$icinga_min_version$", resolvers, checkable->GetLastCheckResult(),
|
2018-05-18 15:11:56 +02:00
|
|
|
&missingIcingaMinVersion, MacroProcessor::EscapeCallback(), resolvedMacros, useResolvedMacros);
|
2018-01-08 19:44:54 +01:00
|
|
|
|
2014-12-01 13:19:07 +01:00
|
|
|
if (resolvedMacros && !useResolvedMacros)
|
|
|
|
return;
|
|
|
|
|
2013-11-12 12:04:52 +01:00
|
|
|
double interval = Utility::GetTime() - Application::GetStartTime();
|
|
|
|
|
|
|
|
if (interval > 60)
|
|
|
|
interval = 60;
|
|
|
|
|
2017-05-23 16:13:56 +02:00
|
|
|
/* use feature stats perfdata */
|
|
|
|
std::pair<Dictionary::Ptr, Array::Ptr> feature_stats = CIB::GetFeatureStats();
|
|
|
|
|
|
|
|
Array::Ptr perfdata = feature_stats.second;
|
2014-11-08 21:17:16 +01:00
|
|
|
|
|
|
|
perfdata->Add(new PerfdataValue("active_host_checks", CIB::GetActiveHostChecksStatistics(interval) / interval));
|
|
|
|
perfdata->Add(new PerfdataValue("passive_host_checks", CIB::GetPassiveHostChecksStatistics(interval) / interval));
|
|
|
|
perfdata->Add(new PerfdataValue("active_host_checks_1min", CIB::GetActiveHostChecksStatistics(60)));
|
|
|
|
perfdata->Add(new PerfdataValue("passive_host_checks_1min", CIB::GetPassiveHostChecksStatistics(60)));
|
|
|
|
perfdata->Add(new PerfdataValue("active_host_checks_5min", CIB::GetActiveHostChecksStatistics(60 * 5)));
|
|
|
|
perfdata->Add(new PerfdataValue("passive_host_checks_5min", CIB::GetPassiveHostChecksStatistics(60 * 5)));
|
|
|
|
perfdata->Add(new PerfdataValue("active_host_checks_15min", CIB::GetActiveHostChecksStatistics(60 * 15)));
|
|
|
|
perfdata->Add(new PerfdataValue("passive_host_checks_15min", CIB::GetPassiveHostChecksStatistics(60 * 15)));
|
|
|
|
|
|
|
|
perfdata->Add(new PerfdataValue("active_service_checks", CIB::GetActiveServiceChecksStatistics(interval) / interval));
|
|
|
|
perfdata->Add(new PerfdataValue("passive_service_checks", CIB::GetPassiveServiceChecksStatistics(interval) / interval));
|
|
|
|
perfdata->Add(new PerfdataValue("active_service_checks_1min", CIB::GetActiveServiceChecksStatistics(60)));
|
|
|
|
perfdata->Add(new PerfdataValue("passive_service_checks_1min", CIB::GetPassiveServiceChecksStatistics(60)));
|
|
|
|
perfdata->Add(new PerfdataValue("active_service_checks_5min", CIB::GetActiveServiceChecksStatistics(60 * 5)));
|
|
|
|
perfdata->Add(new PerfdataValue("passive_service_checks_5min", CIB::GetPassiveServiceChecksStatistics(60 * 5)));
|
|
|
|
perfdata->Add(new PerfdataValue("active_service_checks_15min", CIB::GetActiveServiceChecksStatistics(60 * 15)));
|
|
|
|
perfdata->Add(new PerfdataValue("passive_service_checks_15min", CIB::GetPassiveServiceChecksStatistics(60 * 15)));
|
2014-05-26 20:56:34 +02:00
|
|
|
|
2019-08-14 17:22:27 +02:00
|
|
|
perfdata->Add(new PerfdataValue("current_pending_callbacks", Application::GetTP().GetPending()));
|
2019-08-14 17:43:06 +02:00
|
|
|
perfdata->Add(new PerfdataValue("current_concurrent_checks", Checkable::CurrentConcurrentChecks.load()));
|
2018-01-18 15:22:16 +01:00
|
|
|
perfdata->Add(new PerfdataValue("remote_check_queue", ClusterEvents::GetCheckRequestQueueSize()));
|
|
|
|
|
2014-05-26 20:56:34 +02:00
|
|
|
CheckableCheckStatistics scs = CIB::CalculateServiceCheckStats();
|
2014-02-10 14:56:39 +01:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
perfdata->Add(new PerfdataValue("min_latency", scs.min_latency));
|
|
|
|
perfdata->Add(new PerfdataValue("max_latency", scs.max_latency));
|
|
|
|
perfdata->Add(new PerfdataValue("avg_latency", scs.avg_latency));
|
2016-07-25 17:28:24 +02:00
|
|
|
perfdata->Add(new PerfdataValue("min_execution_time", scs.min_execution_time));
|
|
|
|
perfdata->Add(new PerfdataValue("max_execution_time", scs.max_execution_time));
|
2014-11-08 21:17:16 +01:00
|
|
|
perfdata->Add(new PerfdataValue("avg_execution_time", scs.avg_execution_time));
|
2014-02-10 14:56:39 +01:00
|
|
|
|
|
|
|
ServiceStatistics ss = CIB::CalculateServiceStats();
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
perfdata->Add(new PerfdataValue("num_services_ok", ss.services_ok));
|
|
|
|
perfdata->Add(new PerfdataValue("num_services_warning", ss.services_warning));
|
|
|
|
perfdata->Add(new PerfdataValue("num_services_critical", ss.services_critical));
|
|
|
|
perfdata->Add(new PerfdataValue("num_services_unknown", ss.services_unknown));
|
|
|
|
perfdata->Add(new PerfdataValue("num_services_pending", ss.services_pending));
|
|
|
|
perfdata->Add(new PerfdataValue("num_services_unreachable", ss.services_unreachable));
|
|
|
|
perfdata->Add(new PerfdataValue("num_services_flapping", ss.services_flapping));
|
|
|
|
perfdata->Add(new PerfdataValue("num_services_in_downtime", ss.services_in_downtime));
|
|
|
|
perfdata->Add(new PerfdataValue("num_services_acknowledged", ss.services_acknowledged));
|
2019-08-29 17:04:23 +02:00
|
|
|
perfdata->Add(new PerfdataValue("num_services_handled", ss.services_handled));
|
|
|
|
perfdata->Add(new PerfdataValue("num_services_problem", ss.services_problem));
|
2014-02-10 14:56:39 +01:00
|
|
|
|
2020-01-31 16:49:49 +01:00
|
|
|
double uptime = Application::GetUptime();
|
2014-11-08 21:17:16 +01:00
|
|
|
perfdata->Add(new PerfdataValue("uptime", uptime));
|
2014-04-28 09:16:27 +02:00
|
|
|
|
2014-02-10 14:56:39 +01:00
|
|
|
HostStatistics hs = CIB::CalculateHostStats();
|
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
perfdata->Add(new PerfdataValue("num_hosts_up", hs.hosts_up));
|
|
|
|
perfdata->Add(new PerfdataValue("num_hosts_down", hs.hosts_down));
|
2015-11-26 20:03:46 +01:00
|
|
|
perfdata->Add(new PerfdataValue("num_hosts_pending", hs.hosts_pending));
|
2014-11-08 21:17:16 +01:00
|
|
|
perfdata->Add(new PerfdataValue("num_hosts_unreachable", hs.hosts_unreachable));
|
|
|
|
perfdata->Add(new PerfdataValue("num_hosts_flapping", hs.hosts_flapping));
|
|
|
|
perfdata->Add(new PerfdataValue("num_hosts_in_downtime", hs.hosts_in_downtime));
|
|
|
|
perfdata->Add(new PerfdataValue("num_hosts_acknowledged", hs.hosts_acknowledged));
|
2019-08-29 17:04:23 +02:00
|
|
|
perfdata->Add(new PerfdataValue("num_hosts_handled", hs.hosts_handled));
|
|
|
|
perfdata->Add(new PerfdataValue("num_hosts_problem", hs.hosts_problem));
|
2013-12-13 15:09:17 +01:00
|
|
|
|
2017-11-13 16:30:29 +01:00
|
|
|
std::vector<Endpoint::Ptr> endpoints = ConfigType::GetObjectsByType<Endpoint>();
|
|
|
|
|
|
|
|
double lastMessageSent = 0;
|
|
|
|
double lastMessageReceived = 0;
|
|
|
|
double messagesSentPerSecond = 0;
|
|
|
|
double messagesReceivedPerSecond = 0;
|
|
|
|
double bytesSentPerSecond = 0;
|
|
|
|
double bytesReceivedPerSecond = 0;
|
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
for (const Endpoint::Ptr& endpoint : endpoints)
|
2017-11-13 16:30:29 +01:00
|
|
|
{
|
|
|
|
if (endpoint->GetLastMessageSent() > lastMessageSent)
|
|
|
|
lastMessageSent = endpoint->GetLastMessageSent();
|
|
|
|
|
|
|
|
if (endpoint->GetLastMessageReceived() > lastMessageReceived)
|
|
|
|
lastMessageReceived = endpoint->GetLastMessageReceived();
|
|
|
|
|
|
|
|
messagesSentPerSecond += endpoint->GetMessagesSentPerSecond();
|
|
|
|
messagesReceivedPerSecond += endpoint->GetMessagesReceivedPerSecond();
|
|
|
|
bytesSentPerSecond += endpoint->GetBytesSentPerSecond();
|
|
|
|
bytesReceivedPerSecond += endpoint->GetBytesReceivedPerSecond();
|
|
|
|
}
|
|
|
|
|
|
|
|
perfdata->Add(new PerfdataValue("last_messages_sent", lastMessageSent));
|
|
|
|
perfdata->Add(new PerfdataValue("last_messages_received", lastMessageReceived));
|
|
|
|
perfdata->Add(new PerfdataValue("sum_messages_sent_per_second", messagesSentPerSecond));
|
|
|
|
perfdata->Add(new PerfdataValue("sum_messages_received_per_second", messagesReceivedPerSecond));
|
|
|
|
perfdata->Add(new PerfdataValue("sum_bytes_sent_per_second", bytesSentPerSecond));
|
|
|
|
perfdata->Add(new PerfdataValue("sum_bytes_received_per_second", bytesReceivedPerSecond));
|
|
|
|
|
2013-11-12 12:04:52 +01:00
|
|
|
cr->SetPerformanceData(perfdata);
|
2018-01-08 19:44:54 +01:00
|
|
|
cr->SetState(ServiceOK);
|
|
|
|
|
|
|
|
String appVersion = Application::GetAppVersion();
|
|
|
|
|
|
|
|
String output = "Icinga 2 has been running for " + Utility::FormatDuration(uptime) +
|
|
|
|
". Version: " + appVersion;
|
2016-05-11 16:07:28 +02:00
|
|
|
|
2018-01-08 19:44:54 +01:00
|
|
|
/* Indicate a warning if the last reload failed. */
|
2016-05-11 16:07:28 +02:00
|
|
|
double lastReloadFailed = Application::GetLastReloadFailed();
|
|
|
|
|
|
|
|
if (lastReloadFailed > 0) {
|
2018-01-08 19:44:54 +01:00
|
|
|
output += "; Last reload attempt failed at " + Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", lastReloadFailed);
|
2016-05-11 16:07:28 +02:00
|
|
|
cr->SetState(ServiceWarning);
|
2018-01-08 19:44:54 +01:00
|
|
|
}
|
|
|
|
|
2018-10-23 17:23:34 +02:00
|
|
|
/* Indicate a warning when the last synced config caused a stage validation error. */
|
|
|
|
ApiListener::Ptr listener = ApiListener::GetInstance();
|
|
|
|
|
|
|
|
if (listener) {
|
|
|
|
Dictionary::Ptr validationResult = listener->GetLastFailedZonesStageValidation();
|
|
|
|
|
|
|
|
if (validationResult) {
|
|
|
|
output += "; Last zone sync stage validation failed at "
|
|
|
|
+ Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", validationResult->Get("ts"));
|
|
|
|
|
|
|
|
cr->SetState(ServiceWarning);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 13:15:05 +02:00
|
|
|
String parsedAppVersion = Utility::ParseVersion(appVersion);
|
2018-01-08 19:44:54 +01:00
|
|
|
|
2018-10-19 15:37:32 +02:00
|
|
|
/* Return an error if the version is less than specified (optional). */
|
2018-05-18 15:11:56 +02:00
|
|
|
if (missingIcingaMinVersion.IsEmpty() && !icingaMinVersion.IsEmpty() && Utility::CompareVersion(icingaMinVersion, parsedAppVersion) < 0) {
|
2018-01-08 19:44:54 +01:00
|
|
|
output += "; Minimum version " + icingaMinVersion + " is not installed.";
|
|
|
|
cr->SetState(ServiceCritical);
|
|
|
|
}
|
|
|
|
|
|
|
|
cr->SetOutput(output);
|
2019-07-10 10:15:15 +02:00
|
|
|
cr->SetCommand(command->GetName());
|
2013-11-12 12:04:52 +01:00
|
|
|
|
2018-01-08 19:44:54 +01:00
|
|
|
checkable->ProcessCheckResult(cr);
|
2013-11-12 12:04:52 +01:00
|
|
|
}
|