2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2012-07-09 20:32:02 +02:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/host.hpp"
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "icinga/host.tcpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "icinga/service.hpp"
|
|
|
|
#include "icinga/hostgroup.hpp"
|
|
|
|
#include "icinga/pluginutility.hpp"
|
2014-11-16 16:20:39 +01:00
|
|
|
#include "icinga/scheduleddowntime.hpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/convert.hpp"
|
|
|
|
#include "base/utility.hpp"
|
|
|
|
#include "base/debug.hpp"
|
2014-10-26 19:59:49 +01:00
|
|
|
#include "base/json.hpp"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
REGISTER_TYPE(Host);
|
2012-07-09 17:07:20 +02:00
|
|
|
|
2014-11-21 18:31:37 +01:00
|
|
|
void Host::OnAllConfigLoaded(void)
|
2013-02-26 10:13:54 +01:00
|
|
|
{
|
2015-08-25 13:53:43 +02:00
|
|
|
ObjectImpl<Host>::OnAllConfigLoaded();
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2016-01-14 15:33:28 +01:00
|
|
|
String zoneName = GetZoneName();
|
|
|
|
|
|
|
|
if (!zoneName.IsEmpty()) {
|
|
|
|
Zone::Ptr zone = Zone::GetByName(zoneName);
|
|
|
|
|
|
|
|
if (zone && zone->IsGlobal())
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Host '" + GetName() + "' cannot be put into global zone '" + zone->GetName() + "'."));
|
|
|
|
}
|
|
|
|
|
2014-11-21 18:31:37 +01:00
|
|
|
HostGroup::EvaluateObjectRules(this);
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Array::Ptr groups = GetGroups();
|
|
|
|
|
|
|
|
if (groups) {
|
2014-10-28 18:58:22 +01:00
|
|
|
groups = groups->ShallowClone();
|
|
|
|
|
2013-09-09 13:52:37 +02:00
|
|
|
ObjectLock olock(groups);
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(const String& name, groups) {
|
|
|
|
HostGroup::Ptr hg = HostGroup::GetByName(name);
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (hg)
|
2014-11-08 21:17:16 +01:00
|
|
|
hg->ResolveGroupMembership(this, true);
|
2013-02-08 21:05:08 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-19 15:47:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Host::CreateChildObjects(const Type::Ptr& childType)
|
|
|
|
{
|
|
|
|
if (childType->GetName() == "ScheduledDowntime")
|
|
|
|
ScheduledDowntime::EvaluateApplyRules(this);
|
|
|
|
|
|
|
|
if (childType->GetName() == "Notification")
|
|
|
|
Notification::EvaluateApplyRules(this);
|
|
|
|
|
|
|
|
if (childType->GetName() == "Dependency")
|
|
|
|
Dependency::EvaluateApplyRules(this);
|
2014-11-16 16:20:39 +01:00
|
|
|
|
2015-03-19 15:47:46 +01:00
|
|
|
if (childType->GetName() == "Service")
|
|
|
|
Service::EvaluateApplyRules(this);
|
2013-01-24 15:01:06 +01:00
|
|
|
}
|
|
|
|
|
2015-08-20 17:18:48 +02:00
|
|
|
void Host::Stop(bool runtimeRemoved)
|
2013-02-27 12:44:51 +01:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<Host>::Stop(runtimeRemoved);
|
2013-03-02 09:07:47 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Array::Ptr groups = GetGroups();
|
2013-02-27 12:44:51 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (groups) {
|
2013-09-09 13:52:37 +02:00
|
|
|
ObjectLock olock(groups);
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
BOOST_FOREACH(const String& name, groups) {
|
|
|
|
HostGroup::Ptr hg = HostGroup::GetByName(name);
|
|
|
|
|
|
|
|
if (hg)
|
2014-11-08 21:17:16 +01:00
|
|
|
hg->ResolveGroupMembership(this, false);
|
2013-08-20 11:06:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: unregister slave services/notifications?
|
2013-02-27 12:44:51 +01:00
|
|
|
}
|
|
|
|
|
2015-10-22 08:28:40 +02:00
|
|
|
std::vector<Service::Ptr> Host::GetServices(void) const
|
2013-01-24 15:10:17 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ServicesMutex);
|
2013-01-24 15:10:17 +01:00
|
|
|
|
2015-10-22 08:28:40 +02:00
|
|
|
std::vector<Service::Ptr> services;
|
|
|
|
services.reserve(m_Services.size());
|
2013-11-30 17:42:50 +01:00
|
|
|
typedef std::pair<String, Service::Ptr> ServicePair;
|
|
|
|
BOOST_FOREACH(const ServicePair& kv, m_Services) {
|
2015-10-22 08:28:40 +02:00
|
|
|
services.push_back(kv.second);
|
2013-01-24 15:10:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return services;
|
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void Host::AddService(const Service::Ptr& service)
|
2013-07-05 09:35:49 +02:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ServicesMutex);
|
|
|
|
|
|
|
|
m_Services[service->GetShortName()] = service;
|
2013-07-05 09:35:49 +02:00
|
|
|
}
|
2013-07-09 16:59:31 +02:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void Host::RemoveService(const Service::Ptr& service)
|
2013-02-27 15:23:25 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ServicesMutex);
|
2013-02-27 16:04:49 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
m_Services.erase(service->GetShortName());
|
2013-02-27 15:23:25 +01:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
int Host::GetTotalServices(void) const
|
2013-01-24 15:10:17 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
return GetServices().size();
|
2013-01-24 15:10:17 +01:00
|
|
|
}
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
Service::Ptr Host::GetServiceByShortName(const Value& name)
|
2013-02-07 20:29:35 +01:00
|
|
|
{
|
2014-04-03 15:36:13 +02:00
|
|
|
if (name.IsScalar()) {
|
2013-02-27 12:44:51 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
boost::mutex::scoped_lock lock(m_ServicesMutex);
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
std::map<String, Service::Ptr>::const_iterator it = m_Services.find(name);
|
2013-02-08 15:38:22 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (it != m_Services.end())
|
|
|
|
return it->second;
|
2013-02-08 21:30:14 +01:00
|
|
|
}
|
2013-02-08 15:38:22 +01:00
|
|
|
|
2013-02-27 15:23:25 +01:00
|
|
|
return Service::Ptr();
|
2013-02-08 21:30:14 +01:00
|
|
|
} else if (name.IsObjectType<Dictionary>()) {
|
|
|
|
Dictionary::Ptr dict = name;
|
2013-02-27 15:23:25 +01:00
|
|
|
String short_name;
|
|
|
|
|
2013-03-02 09:07:47 +01:00
|
|
|
return Service::GetByNamePair(dict->Get("host"), dict->Get("service"));
|
2013-02-08 21:30:14 +01:00
|
|
|
} else {
|
2014-10-26 19:59:49 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + JsonEncode(name)));
|
2013-02-08 21:30:14 +01:00
|
|
|
}
|
2013-02-07 20:29:35 +01:00
|
|
|
}
|
|
|
|
|
2014-04-08 08:54:49 +02:00
|
|
|
HostState Host::CalculateState(ServiceState state)
|
2013-03-07 12:04:20 +01:00
|
|
|
{
|
2013-03-19 13:04:30 +01:00
|
|
|
switch (state) {
|
2014-04-08 09:11:54 +02:00
|
|
|
case ServiceOK:
|
|
|
|
case ServiceWarning:
|
2013-03-07 12:04:20 +01:00
|
|
|
return HostUp;
|
|
|
|
default:
|
|
|
|
return HostDown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:35:49 +02:00
|
|
|
HostState Host::GetState(void) const
|
|
|
|
{
|
2014-04-08 08:54:49 +02:00
|
|
|
return CalculateState(GetStateRaw());
|
2013-07-05 09:35:49 +02:00
|
|
|
}
|
|
|
|
|
2013-03-07 12:04:20 +01:00
|
|
|
HostState Host::GetLastState(void) const
|
|
|
|
{
|
2014-04-08 08:54:49 +02:00
|
|
|
return CalculateState(GetLastStateRaw());
|
2013-03-07 12:04:20 +01:00
|
|
|
}
|
|
|
|
|
2013-07-05 09:35:49 +02:00
|
|
|
HostState Host::GetLastHardState(void) const
|
|
|
|
{
|
2014-04-08 08:54:49 +02:00
|
|
|
return CalculateState(GetLastHardStateRaw());
|
2013-07-05 09:35:49 +02:00
|
|
|
}
|
|
|
|
|
2016-03-15 09:46:20 +01:00
|
|
|
bool Host::IsStateOK(ServiceState state)
|
|
|
|
{
|
|
|
|
return Host::CalculateState(state) == HostUp;
|
|
|
|
}
|
|
|
|
|
2015-11-02 14:10:44 +01:00
|
|
|
void Host::SaveLastState(ServiceState state, double timestamp)
|
2013-07-18 18:16:39 +02:00
|
|
|
{
|
2015-11-02 14:10:44 +01:00
|
|
|
if (state == ServiceOK || state == ServiceWarning)
|
|
|
|
SetLastStateUp(timestamp);
|
|
|
|
else if (state == ServiceCritical)
|
|
|
|
SetLastStateDown(timestamp);
|
2013-07-05 09:35:49 +02:00
|
|
|
}
|
|
|
|
|
2013-10-29 13:44:43 +01:00
|
|
|
HostState Host::StateFromString(const String& state)
|
|
|
|
{
|
|
|
|
if (state == "UP")
|
|
|
|
return HostUp;
|
|
|
|
else
|
2014-04-08 08:54:49 +02:00
|
|
|
return HostDown;
|
2013-10-29 13:44:43 +01:00
|
|
|
}
|
|
|
|
|
2013-03-19 13:04:30 +01:00
|
|
|
String Host::StateToString(HostState state)
|
2013-03-07 12:04:20 +01:00
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case HostUp:
|
|
|
|
return "UP";
|
|
|
|
case HostDown:
|
|
|
|
return "DOWN";
|
|
|
|
default:
|
|
|
|
return "INVALID";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-29 13:44:43 +01:00
|
|
|
StateType Host::StateTypeFromString(const String& type)
|
|
|
|
{
|
|
|
|
if (type == "SOFT")
|
|
|
|
return StateTypeSoft;
|
|
|
|
else
|
|
|
|
return StateTypeHard;
|
|
|
|
}
|
|
|
|
|
|
|
|
String Host::StateTypeToString(StateType type)
|
|
|
|
{
|
|
|
|
if (type == StateTypeSoft)
|
|
|
|
return "SOFT";
|
|
|
|
else
|
|
|
|
return "HARD";
|
|
|
|
}
|
|
|
|
|
2014-11-26 20:43:42 +01:00
|
|
|
bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, Value *result) const
|
2013-02-09 18:39:43 +01:00
|
|
|
{
|
2014-04-08 13:23:24 +02:00
|
|
|
if (macro == "state") {
|
|
|
|
*result = StateToString(GetState());
|
|
|
|
return true;
|
|
|
|
} else if (macro == "state_id") {
|
2016-06-16 15:14:35 +02:00
|
|
|
*result = GetState();
|
2014-04-08 13:23:24 +02:00
|
|
|
return true;
|
|
|
|
} else if (macro == "state_type") {
|
|
|
|
*result = StateTypeToString(GetStateType());
|
|
|
|
return true;
|
|
|
|
} else if (macro == "last_state") {
|
|
|
|
*result = StateToString(GetLastState());
|
|
|
|
return true;
|
|
|
|
} else if (macro == "last_state_id") {
|
2016-06-16 15:14:35 +02:00
|
|
|
*result = GetLastState();
|
2014-04-08 13:23:24 +02:00
|
|
|
return true;
|
|
|
|
} else if (macro == "last_state_type") {
|
|
|
|
*result = StateTypeToString(GetLastStateType());
|
|
|
|
return true;
|
|
|
|
} else if (macro == "last_state_change") {
|
2016-06-21 11:29:12 +02:00
|
|
|
*result = static_cast<long>(GetLastStateChange());
|
2014-04-08 13:23:24 +02:00
|
|
|
return true;
|
2015-02-08 00:15:38 +01:00
|
|
|
} else if (macro == "downtime_depth") {
|
2016-06-16 15:14:35 +02:00
|
|
|
*result = GetDowntimeDepth();
|
2015-02-08 00:15:38 +01:00
|
|
|
return true;
|
2014-04-08 13:23:24 +02:00
|
|
|
} else if (macro == "duration_sec") {
|
2016-06-16 15:14:35 +02:00
|
|
|
*result = Utility::GetTime() - GetLastStateChange();
|
2014-04-08 13:23:24 +02:00
|
|
|
return true;
|
2014-05-11 15:08:32 +02:00
|
|
|
} else if (macro == "num_services" || macro == "num_services_ok" || macro == "num_services_warning"
|
|
|
|
|| macro == "num_services_unknown" || macro == "num_services_critical") {
|
2014-04-08 13:23:24 +02:00
|
|
|
int filter = -1;
|
|
|
|
int count = 0;
|
|
|
|
|
2014-05-11 15:08:32 +02:00
|
|
|
if (macro == "num_services_ok")
|
2014-04-08 13:23:24 +02:00
|
|
|
filter = ServiceOK;
|
2014-05-11 15:08:32 +02:00
|
|
|
else if (macro == "num_services_warning")
|
2014-04-08 13:23:24 +02:00
|
|
|
filter = ServiceWarning;
|
2014-05-11 15:08:32 +02:00
|
|
|
else if (macro == "num_services_unknown")
|
2014-04-08 13:23:24 +02:00
|
|
|
filter = ServiceUnknown;
|
2014-05-11 15:08:32 +02:00
|
|
|
else if (macro == "num_services_critical")
|
2014-04-08 13:23:24 +02:00
|
|
|
filter = ServiceCritical;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Service::Ptr& service, GetServices()) {
|
|
|
|
if (filter != -1 && service->GetState() != filter)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
2014-04-05 17:45:28 +02:00
|
|
|
|
2016-06-16 15:14:35 +02:00
|
|
|
*result = count;
|
2014-04-05 17:45:28 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-04-08 13:23:24 +02:00
|
|
|
CheckResult::Ptr cr = GetLastCheckResult();
|
2014-04-04 20:09:23 +02:00
|
|
|
|
2014-04-08 13:23:24 +02:00
|
|
|
if (cr) {
|
|
|
|
if (macro == "latency") {
|
2016-06-16 15:14:35 +02:00
|
|
|
*result = cr->CalculateLatency();
|
2014-04-04 20:09:23 +02:00
|
|
|
return true;
|
2014-04-08 13:23:24 +02:00
|
|
|
} else if (macro == "execution_time") {
|
2016-06-16 15:14:35 +02:00
|
|
|
*result = cr->CalculateExecutionTime();
|
2014-04-04 20:09:23 +02:00
|
|
|
return true;
|
2014-04-08 13:23:24 +02:00
|
|
|
} else if (macro == "output") {
|
|
|
|
*result = cr->GetOutput();
|
2014-04-04 20:09:23 +02:00
|
|
|
return true;
|
2014-04-08 13:23:24 +02:00
|
|
|
} else if (macro == "perfdata") {
|
|
|
|
*result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
|
2014-04-04 20:09:23 +02:00
|
|
|
return true;
|
2015-01-25 14:30:05 +01:00
|
|
|
} else if (macro == "check_source") {
|
|
|
|
*result = cr->GetCheckSource();
|
|
|
|
return true;
|
2014-04-05 17:13:17 +02:00
|
|
|
}
|
2013-02-24 01:10:34 +01:00
|
|
|
}
|
|
|
|
|
2013-03-22 14:40:55 +01:00
|
|
|
return false;
|
2013-02-09 18:39:43 +01:00
|
|
|
}
|