2012-07-09 20:32:02 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* 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. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
2012-07-02 12:34:54 +02:00
|
|
|
#include "i2-cib.h"
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
REGISTER_CLASS(Host);
|
2012-07-09 17:07:20 +02:00
|
|
|
|
2012-06-30 13:39:55 +02:00
|
|
|
string Host::GetAlias(void) const
|
2012-06-13 13:42:55 +02:00
|
|
|
{
|
|
|
|
string value;
|
|
|
|
|
2012-07-09 16:44:46 +02:00
|
|
|
if (GetProperty("alias", &value))
|
2012-06-13 13:42:55 +02:00
|
|
|
return value;
|
|
|
|
|
|
|
|
return GetName();
|
|
|
|
}
|
|
|
|
|
2012-06-30 13:39:55 +02:00
|
|
|
bool Host::Exists(const string& name)
|
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
return (DynamicObject::GetObject("Host", name));
|
2012-06-30 13:39:55 +02:00
|
|
|
}
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
Host::Ptr Host::GetByName(const string& name)
|
2012-06-13 13:42:55 +02:00
|
|
|
{
|
2012-07-30 10:17:29 +02:00
|
|
|
DynamicObject::Ptr configObject = DynamicObject::GetObject("Host", name);
|
2012-06-13 13:42:55 +02:00
|
|
|
|
|
|
|
if (!configObject)
|
2012-07-17 20:41:06 +02:00
|
|
|
throw_exception(invalid_argument("Host '" + name + "' does not exist."));
|
2012-06-13 13:42:55 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
return dynamic_pointer_cast<Host>(configObject);
|
2012-06-13 13:42:55 +02:00
|
|
|
}
|
2012-06-30 13:39:55 +02:00
|
|
|
|
|
|
|
Dictionary::Ptr Host::GetGroups(void) const
|
|
|
|
{
|
|
|
|
Dictionary::Ptr value;
|
2012-07-09 16:44:46 +02:00
|
|
|
GetProperty("hostgroups", &value);
|
2012-06-30 13:39:55 +02:00
|
|
|
return value;
|
|
|
|
}
|
2012-07-09 10:09:53 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
set<string> Host::GetParents(void)
|
2012-07-09 12:44:31 +02:00
|
|
|
{
|
|
|
|
set<string> parents;
|
|
|
|
|
|
|
|
Dictionary::Ptr dependencies;
|
|
|
|
|
2012-07-09 16:44:46 +02:00
|
|
|
if (GetProperty("dependencies", &dependencies)) {
|
2012-07-27 16:05:02 +02:00
|
|
|
dependencies = Service::ResolveDependencies(GetSelf(), dependencies);
|
2012-07-09 12:44:31 +02:00
|
|
|
|
2012-07-17 19:19:03 +02:00
|
|
|
Variant dependency;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) {
|
2012-07-27 16:05:02 +02:00
|
|
|
Service::Ptr service = Service::GetByName(dependency);
|
2012-07-09 12:44:31 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
string parent = service->GetHost()->GetName();
|
2012-07-09 12:44:31 +02:00
|
|
|
|
|
|
|
/* ignore ourselves */
|
|
|
|
if (parent == GetName())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
parents.insert(parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parents;
|
|
|
|
}
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2012-07-13 11:29:23 +02:00
|
|
|
Dictionary::Ptr Host::GetMacros(void) const
|
|
|
|
{
|
|
|
|
Dictionary::Ptr value;
|
|
|
|
GetProperty("macros", &value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
bool Host::IsReachable(void)
|
2012-07-09 13:27:02 +02:00
|
|
|
{
|
|
|
|
Dictionary::Ptr dependencies;
|
2012-07-09 16:44:46 +02:00
|
|
|
if (GetProperty("dependencies", &dependencies)) {
|
2012-07-27 16:05:02 +02:00
|
|
|
dependencies = Service::ResolveDependencies(GetSelf(), dependencies);
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2012-07-17 19:19:03 +02:00
|
|
|
Variant dependency;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, dependency), dependencies) {
|
2012-07-27 16:05:02 +02:00
|
|
|
Service::Ptr service = Service::GetByName(dependency);
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (!service->IsReachable() ||
|
|
|
|
(service->GetState() != StateOK && service->GetState() != StateWarning)) {
|
2012-07-09 13:27:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
bool Host::IsUp(void)
|
2012-07-09 13:27:02 +02:00
|
|
|
{
|
|
|
|
Dictionary::Ptr hostchecks;
|
2012-07-09 16:44:46 +02:00
|
|
|
if (GetProperty("hostchecks", &hostchecks)) {
|
2012-07-27 16:05:02 +02:00
|
|
|
hostchecks = Service::ResolveDependencies(GetSelf(), hostchecks);
|
2012-07-09 13:27:02 +02:00
|
|
|
|
2012-07-17 19:19:03 +02:00
|
|
|
Variant hostcheck;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, hostcheck), hostchecks) {
|
2012-07-27 16:05:02 +02:00
|
|
|
Service::Ptr service = Service::GetByName(hostcheck);
|
2012-07-16 22:00:50 +02:00
|
|
|
|
2012-07-27 16:05:02 +02:00
|
|
|
if (service->GetState() != StateOK && service->GetState() != StateWarning) {
|
2012-07-09 13:27:02 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|