2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2014-03-19 01:02:29 +01:00
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "icinga/host.h"
|
|
|
|
#include "icinga/service.h"
|
|
|
|
#include "icinga/hostgroup.h"
|
2013-07-05 09:35:49 +02:00
|
|
|
#include "icinga/icingaapplication.h"
|
2013-11-07 14:38:37 +01:00
|
|
|
#include "icinga/pluginutility.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/logger_fwd.h"
|
2013-03-18 11:02:18 +01:00
|
|
|
#include "base/timer.h"
|
2013-03-22 14:40:55 +01:00
|
|
|
#include "base/convert.h"
|
2013-10-18 11:30:55 +02:00
|
|
|
#include "base/utility.h"
|
2013-03-25 18:36:15 +01:00
|
|
|
#include "base/scriptfunction.h"
|
2013-08-28 08:18:58 +02:00
|
|
|
#include "base/debug.h"
|
2013-11-08 11:17:46 +01:00
|
|
|
#include "base/serializer.h"
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "config/configitembuilder.h"
|
|
|
|
#include "config/configcompilercontext.h"
|
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-03-05 12:07:53 +01:00
|
|
|
void Host::OnConfigLoaded(void)
|
2013-02-26 10:13:54 +01:00
|
|
|
{
|
2014-03-05 12:07:53 +01:00
|
|
|
DynamicObject::OnConfigLoaded();
|
2013-02-26 10:13:54 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
ASSERT(!OwnsLock());
|
2013-02-20 19:52:25 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
Array::Ptr groups = GetGroups();
|
|
|
|
|
|
|
|
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);
|
2013-02-08 21:05:08 +01:00
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
if (hg)
|
|
|
|
hg->AddMember(GetSelf());
|
2013-02-08 21:05:08 +01:00
|
|
|
}
|
|
|
|
}
|
2013-01-24 15:01:06 +01:00
|
|
|
}
|
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
void Host::Stop(void)
|
2013-02-27 12:44:51 +01:00
|
|
|
{
|
2013-08-20 11:06:04 +02:00
|
|
|
DynamicObject::Stop();
|
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)
|
|
|
|
hg->RemoveMember(GetSelf());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: unregister slave services/notifications?
|
2013-02-27 12:44:51 +01:00
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::set<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
|
|
|
|
2013-08-20 11:06:04 +02:00
|
|
|
std::set<Service::Ptr> services;
|
2013-11-30 17:42:50 +01:00
|
|
|
typedef std::pair<String, Service::Ptr> ServicePair;
|
|
|
|
BOOST_FOREACH(const ServicePair& kv, m_Services) {
|
|
|
|
services.insert(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 {
|
2013-11-08 11:17:46 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Host/Service name pair is invalid: " + JsonSerialize(name)));
|
2013-02-08 21:30:14 +01:00
|
|
|
}
|
2013-02-07 20:29:35 +01:00
|
|
|
}
|
|
|
|
|
2013-03-19 13:04:30 +01:00
|
|
|
HostState Host::CalculateState(ServiceState state, bool reachable)
|
2013-03-07 12:04:20 +01:00
|
|
|
{
|
2013-03-19 13:04:30 +01:00
|
|
|
if (!reachable)
|
2013-03-07 12:04:20 +01:00
|
|
|
return HostUnreachable;
|
|
|
|
|
2013-03-19 13:04:30 +01:00
|
|
|
switch (state) {
|
2013-03-07 12:04:20 +01:00
|
|
|
case StateOK:
|
|
|
|
case StateWarning:
|
|
|
|
return HostUp;
|
|
|
|
default:
|
|
|
|
return HostDown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:35:49 +02:00
|
|
|
HostState Host::GetState(void) const
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
|
|
|
if (!IsReachable())
|
|
|
|
return HostUnreachable;
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
switch (GetStateRaw()) {
|
2013-07-05 09:35:49 +02:00
|
|
|
case StateOK:
|
|
|
|
case StateWarning:
|
|
|
|
return HostUp;
|
|
|
|
default:
|
|
|
|
return HostDown;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-07 12:04:20 +01:00
|
|
|
HostState Host::GetLastState(void) const
|
|
|
|
{
|
2013-03-07 16:00:10 +01:00
|
|
|
ASSERT(!OwnsLock());
|
2013-03-07 12:04:20 +01:00
|
|
|
|
|
|
|
if (!IsReachable())
|
|
|
|
return HostUnreachable;
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
switch (GetLastStateRaw()) {
|
2013-03-07 12:04:20 +01:00
|
|
|
case StateOK:
|
|
|
|
case StateWarning:
|
|
|
|
return HostUp;
|
|
|
|
default:
|
|
|
|
return HostDown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:35:49 +02:00
|
|
|
HostState Host::GetLastHardState(void) const
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
|
|
|
if (!IsReachable())
|
|
|
|
return HostUnreachable;
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
switch (GetLastHardStateRaw()) {
|
2013-07-05 09:35:49 +02:00
|
|
|
case StateOK:
|
|
|
|
case StateWarning:
|
|
|
|
return HostUp;
|
|
|
|
default:
|
|
|
|
return HostDown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-18 18:16:39 +02:00
|
|
|
double Host::GetLastStateUp(void) const
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
if (GetLastStateOK() > GetLastStateWarning())
|
|
|
|
return GetLastStateOK();
|
2013-07-18 18:16:39 +02:00
|
|
|
else
|
2014-04-03 15:36:13 +02:00
|
|
|
return GetLastStateWarning();
|
2013-07-18 18:16:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double Host::GetLastStateDown(void) const
|
|
|
|
{
|
|
|
|
ASSERT(!OwnsLock());
|
|
|
|
|
2014-04-03 15:36:13 +02:00
|
|
|
return GetLastStateCritical();
|
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 if (state == "DOWN")
|
|
|
|
return HostDown;
|
|
|
|
else if (state == "UNREACHABLE")
|
|
|
|
return HostUnreachable;
|
|
|
|
else
|
|
|
|
return HostUnreachable;
|
|
|
|
}
|
|
|
|
|
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";
|
|
|
|
case HostUnreachable:
|
|
|
|
return "UNREACHABLE";
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2013-11-09 14:22:38 +01:00
|
|
|
bool Host::ResolveMacro(const String& macro, const CheckResult::Ptr&, String *result) const
|
2013-02-09 18:39:43 +01:00
|
|
|
{
|
2014-04-05 17:13:17 +02:00
|
|
|
String key;
|
|
|
|
Dictionary::Ptr vars;
|
|
|
|
|
2014-04-04 20:09:23 +02:00
|
|
|
/* require prefix for object macros */
|
|
|
|
if (macro.SubStr(0, 5) == "host.") {
|
2014-04-05 17:13:17 +02:00
|
|
|
key = macro.SubStr(5);
|
|
|
|
|
|
|
|
if (key.SubStr(0, 5) == "vars.") {
|
|
|
|
vars = GetVars();
|
|
|
|
String vars_key = key.SubStr(5);
|
2013-02-24 01:10:34 +01:00
|
|
|
|
2014-04-05 17:13:17 +02:00
|
|
|
if (vars && vars->Contains(vars_key)) {
|
|
|
|
*result = vars->Get(vars_key);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (key == "name") {
|
2014-04-04 20:09:23 +02:00
|
|
|
*result = GetName();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (key == "displaymane") {
|
|
|
|
*result = GetDisplayName();
|
|
|
|
return true;
|
2013-03-22 14:40:55 +01:00
|
|
|
}
|
2013-03-07 12:04:20 +01:00
|
|
|
|
2014-04-04 20:09:23 +02:00
|
|
|
CheckResult::Ptr cr = GetLastCheckResult();
|
|
|
|
|
|
|
|
if (key == "state") {
|
|
|
|
switch (GetState()) {
|
|
|
|
case HostUnreachable:
|
|
|
|
*result = "UNREACHABLE";
|
|
|
|
break;
|
|
|
|
case HostUp:
|
|
|
|
*result = "UP";
|
|
|
|
break;
|
|
|
|
case HostDown:
|
|
|
|
*result = "DOWN";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT(0);
|
|
|
|
}
|
2014-04-03 15:36:13 +02:00
|
|
|
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
2014-04-04 20:09:23 +02:00
|
|
|
} else if (key == "stateid") {
|
|
|
|
*result = Convert::ToString(GetState());
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
2014-04-04 20:09:23 +02:00
|
|
|
} else if (key == "statetype") {
|
|
|
|
*result = StateTypeToString(GetStateType());
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
2014-04-04 20:09:23 +02:00
|
|
|
} else if (key == "attempt") {
|
|
|
|
*result = Convert::ToString(GetCheckAttempt());
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
2014-04-04 20:09:23 +02:00
|
|
|
} else if (key == "maxattempt") {
|
|
|
|
*result = Convert::ToString(GetMaxCheckAttempts());
|
2013-03-22 14:40:55 +01:00
|
|
|
return true;
|
2014-04-04 20:09:23 +02:00
|
|
|
} else if (key == "laststate") {
|
|
|
|
*result = StateToString(GetLastState());
|
|
|
|
return true;
|
|
|
|
} else if (key == "laststateid") {
|
|
|
|
*result = Convert::ToString(GetLastState());
|
|
|
|
return true;
|
|
|
|
} else if (key == "laststatetype") {
|
|
|
|
*result = StateTypeToString(GetLastStateType());
|
|
|
|
return true;
|
|
|
|
} else if (key == "laststatechange") {
|
|
|
|
*result = Convert::ToString((long)GetLastStateChange());
|
|
|
|
return true;
|
|
|
|
} else if (key == "durationsec") {
|
|
|
|
*result = Convert::ToString((long)(Utility::GetTime() - GetLastStateChange()));
|
|
|
|
return true;
|
|
|
|
} else if (key == "checkcommand") {
|
|
|
|
CheckCommand::Ptr commandObj = GetCheckCommand();
|
2013-12-18 10:53:26 +01:00
|
|
|
|
2014-04-04 20:09:23 +02:00
|
|
|
if (commandObj)
|
|
|
|
*result = commandObj->GetName();
|
|
|
|
else
|
|
|
|
*result = "";
|
2013-07-02 09:47:31 +02:00
|
|
|
|
2014-04-04 20:09:23 +02:00
|
|
|
return true;
|
|
|
|
} else if (key == "totalservices" || key == "totalservicesok" || key == "totalserviceswarning"
|
|
|
|
|| key == "totalservicesunknown" || key == "totalservicescritical") {
|
|
|
|
int filter = -1;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (key == "totalservicesok")
|
|
|
|
filter = StateOK;
|
|
|
|
else if (key == "totalserviceswarning")
|
|
|
|
filter = StateWarning;
|
|
|
|
else if (key == "totalservicesunknown")
|
|
|
|
filter = StateUnknown;
|
|
|
|
else if (key == "totalservicescritical")
|
|
|
|
filter = StateCritical;
|
|
|
|
|
|
|
|
BOOST_FOREACH(const Service::Ptr& service, GetServices()) {
|
|
|
|
if (filter != -1 && service->GetState() != filter)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*result = Convert::ToString(count);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (cr) {
|
|
|
|
if (key == "latency") {
|
|
|
|
*result = Convert::ToString(Service::CalculateLatency(cr));
|
|
|
|
return true;
|
|
|
|
} else if (key == "executiontime") {
|
|
|
|
*result = Convert::ToString(Service::CalculateExecutionTime(cr));
|
|
|
|
return true;
|
|
|
|
} else if (key == "output") {
|
|
|
|
*result = cr->GetOutput();
|
|
|
|
return true;
|
|
|
|
} else if (key == "perfdata") {
|
|
|
|
*result = PluginUtility::FormatPerfdata(cr->GetPerformanceData());
|
|
|
|
return true;
|
|
|
|
} else if (key == "lastcheck") {
|
|
|
|
*result = Convert::ToString((long)cr->GetScheduleStart());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-04-05 17:13:17 +02:00
|
|
|
} else {
|
|
|
|
vars = GetVars();
|
2013-07-02 10:06:08 +02:00
|
|
|
|
2014-04-05 17:13:17 +02:00
|
|
|
if (vars && vars->Contains(macro)) {
|
|
|
|
*result = vars->Get(macro);
|
|
|
|
return true;
|
|
|
|
}
|
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
|
|
|
}
|