]> granicus.if.org Git - icinga2/blob - lib/methods/icingachecktask.cpp
Move PerfdataValue() class into base library
[icinga2] / lib / methods / icingachecktask.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/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"
30
31 using namespace icinga;
32
33 REGISTER_SCRIPTFUNCTION_NS(Internal, IcingaCheck, &IcingaCheckTask::ScriptFunc, "checkable:cr:resolvedMacros:useResolvedMacros");
34
35 void IcingaCheckTask::ScriptFunc(const Checkable::Ptr& service, const CheckResult::Ptr& cr,
36     const Dictionary::Ptr& resolvedMacros, bool useResolvedMacros)
37 {
38         if (resolvedMacros && !useResolvedMacros)
39                 return;
40
41         double interval = Utility::GetTime() - Application::GetStartTime();
42
43         if (interval > 60)
44                 interval = 60;
45
46         Array::Ptr perfdata = new Array();
47
48         perfdata->Add(new PerfdataValue("active_host_checks", CIB::GetActiveHostChecksStatistics(interval) / interval));
49         perfdata->Add(new PerfdataValue("passive_host_checks", CIB::GetPassiveHostChecksStatistics(interval) / interval));
50         perfdata->Add(new PerfdataValue("active_host_checks_1min", CIB::GetActiveHostChecksStatistics(60)));
51         perfdata->Add(new PerfdataValue("passive_host_checks_1min", CIB::GetPassiveHostChecksStatistics(60)));
52         perfdata->Add(new PerfdataValue("active_host_checks_5min", CIB::GetActiveHostChecksStatistics(60 * 5)));
53         perfdata->Add(new PerfdataValue("passive_host_checks_5min", CIB::GetPassiveHostChecksStatistics(60 * 5)));
54         perfdata->Add(new PerfdataValue("active_host_checks_15min", CIB::GetActiveHostChecksStatistics(60 * 15)));
55         perfdata->Add(new PerfdataValue("passive_host_checks_15min", CIB::GetPassiveHostChecksStatistics(60 * 15)));
56
57         perfdata->Add(new PerfdataValue("active_service_checks", CIB::GetActiveServiceChecksStatistics(interval) / interval));
58         perfdata->Add(new PerfdataValue("passive_service_checks", CIB::GetPassiveServiceChecksStatistics(interval) / interval));
59         perfdata->Add(new PerfdataValue("active_service_checks_1min", CIB::GetActiveServiceChecksStatistics(60)));
60         perfdata->Add(new PerfdataValue("passive_service_checks_1min", CIB::GetPassiveServiceChecksStatistics(60)));
61         perfdata->Add(new PerfdataValue("active_service_checks_5min", CIB::GetActiveServiceChecksStatistics(60 * 5)));
62         perfdata->Add(new PerfdataValue("passive_service_checks_5min", CIB::GetPassiveServiceChecksStatistics(60 * 5)));
63         perfdata->Add(new PerfdataValue("active_service_checks_15min", CIB::GetActiveServiceChecksStatistics(60 * 15)));
64         perfdata->Add(new PerfdataValue("passive_service_checks_15min", CIB::GetPassiveServiceChecksStatistics(60 * 15)));
65
66         CheckableCheckStatistics scs = CIB::CalculateServiceCheckStats();
67
68         perfdata->Add(new PerfdataValue("min_latency", scs.min_latency));
69         perfdata->Add(new PerfdataValue("max_latency", scs.max_latency));
70         perfdata->Add(new PerfdataValue("avg_latency", scs.avg_latency));
71         perfdata->Add(new PerfdataValue("min_execution_time", scs.min_execution_time));
72         perfdata->Add(new PerfdataValue("max_execution_time", scs.max_execution_time));
73         perfdata->Add(new PerfdataValue("avg_execution_time", scs.avg_execution_time));
74
75         ServiceStatistics ss = CIB::CalculateServiceStats();
76
77         perfdata->Add(new PerfdataValue("num_services_ok", ss.services_ok));
78         perfdata->Add(new PerfdataValue("num_services_warning", ss.services_warning));
79         perfdata->Add(new PerfdataValue("num_services_critical", ss.services_critical));
80         perfdata->Add(new PerfdataValue("num_services_unknown", ss.services_unknown));
81         perfdata->Add(new PerfdataValue("num_services_pending", ss.services_pending));
82         perfdata->Add(new PerfdataValue("num_services_unreachable", ss.services_unreachable));
83         perfdata->Add(new PerfdataValue("num_services_flapping", ss.services_flapping));
84         perfdata->Add(new PerfdataValue("num_services_in_downtime", ss.services_in_downtime));
85         perfdata->Add(new PerfdataValue("num_services_acknowledged", ss.services_acknowledged));
86
87         double uptime = Utility::GetTime() - Application::GetStartTime();
88         perfdata->Add(new PerfdataValue("uptime", uptime));
89
90         HostStatistics hs = CIB::CalculateHostStats();
91
92         perfdata->Add(new PerfdataValue("num_hosts_up", hs.hosts_up));
93         perfdata->Add(new PerfdataValue("num_hosts_down", hs.hosts_down));
94         perfdata->Add(new PerfdataValue("num_hosts_pending", hs.hosts_pending));
95         perfdata->Add(new PerfdataValue("num_hosts_unreachable", hs.hosts_unreachable));
96         perfdata->Add(new PerfdataValue("num_hosts_flapping", hs.hosts_flapping));
97         perfdata->Add(new PerfdataValue("num_hosts_in_downtime", hs.hosts_in_downtime));
98         perfdata->Add(new PerfdataValue("num_hosts_acknowledged", hs.hosts_acknowledged));
99
100         cr->SetOutput("Icinga 2 has been running for " + Utility::FormatDuration(uptime) +
101             ". Version: " + Application::GetAppVersion());
102         cr->SetPerformanceData(perfdata);
103
104         double lastReloadFailed = Application::GetLastReloadFailed();
105
106         if (lastReloadFailed > 0) {
107                 cr->SetOutput(cr->GetOutput() + "; Last reload attempt failed at " + Utility::FormatDateTime("%Y-%m-%d %H:%M:%S %z", lastReloadFailed));
108                 cr->SetState(ServiceWarning);
109         } else
110                 cr->SetState(ServiceOK);
111
112         service->ProcessCheckResult(cr);
113 }