Auto-detect host parents.

This commit is contained in:
Gunnar Beutner 2012-07-09 12:44:31 +02:00
parent 9f56ce6c46
commit c8261fa8f2
7 changed files with 63 additions and 18 deletions

View File

@ -34,3 +34,28 @@ Dictionary::Ptr Host::GetGroups(void) const
return value;
}
set<string> Host::GetParents(void) const
{
set<string> parents;
Dictionary::Ptr dependencies;
if (GetConfigObject()->GetProperty("dependencies", &dependencies)) {
dependencies = Service::ResolveDependencies(*this, dependencies);
Dictionary::Iterator it;
for (it = dependencies->Begin(); it != dependencies->End(); it++) {
Service service = Service::GetByName(it->second);
string parent = service.GetHost().GetName();
/* ignore ourselves */
if (parent == GetName())
continue;
parents.insert(parent);
}
}
return parents;
}

View File

@ -16,6 +16,7 @@ public:
string GetAlias(void) const;
Dictionary::Ptr GetGroups(void) const;
set<string> GetParents(void) const;
};
}

View File

@ -140,6 +140,10 @@ bool Service::IsReachable(void) const
if (service.GetName() == GetName())
continue;
/* ignore pending services */
if (!service.HasLastCheckResult())
continue;
if (service.GetStateType() == StateTypeHard && service.GetState() != StateOK &&
service.GetState() != StateWarning)
return false;

View File

@ -116,12 +116,22 @@ void CompatComponent::DumpHostObject(ofstream& fp, Host host)
{
fp << "define host {" << "\n"
<< "\t" << "host_name" << "\t" << host.GetName() << "\n"
<< "\t" << "alias" << "\t" << host.GetAlias() << "\n"
<< "\t" << "check_interval" << "\t" << 1 << "\n"
<< "\t" << "retry_interval" << "\t" << 1 << "\n"
<< "\t" << "max_check_attempts" << "\t" << 1 << "\n"
<< "\t" << "active_checks_enabled" << "\t" << 1 << "\n"
<< "\t" << "passive_checks_enabled" << "\t" << 1 << "\n"
<< "\t" << "}" << "\n"
<< "\t" << "passive_checks_enabled" << "\t" << 1 << "\n";
set<string> parents = host.GetParents();
if (!parents.empty()) {
fp << "\t" << "parents" << "\t";
DumpStringList(fp, parents);
fp << "\n";
}
fp << "\t" << "}" << "\n"
<< "\n";
}
@ -201,20 +211,6 @@ void CompatComponent::DumpServiceObject(ofstream& fp, Service service)
<< "\n";
}
void CompatComponent::DumpStringList(ofstream& fp, const vector<string>& list)
{
vector<string>::const_iterator it;
bool first = true;
for (it = list.begin(); it != list.end(); it++) {
if (!first)
fp << ",";
else
first = false;
fp << *it;
}
}
/**
* Periodically writes the status.dat and objects.cache files.
*/

View File

@ -39,7 +39,21 @@ private:
void DumpHostStatus(ofstream& fp, Host host);
void DumpHostObject(ofstream& fp, Host host);
void DumpStringList(ofstream& fp, const vector<string>& list);
template<typename T>
void DumpStringList(ofstream& fp, const T& list)
{
typename T::const_iterator it;
bool first = true;
for (it = list.begin(); it != list.end(); it++) {
if (!first)
fp << ",";
else
first = false;
fp << *it;
}
}
void DumpServiceStatus(ofstream& fp, Service service);
void DumpServiceObject(ofstream& fp, Service service);

View File

@ -80,6 +80,11 @@ void ConvenienceComponent::CopyServiceAttributes(const ConfigObject::Ptr& host,
if (service->GetProperty("dependencies", &dependencies))
builder->AddExpression("dependencies", OperatorPlus,
Service::ResolveDependencies(host, dependencies));
Dictionary::Ptr hostchecks;
if (service->GetProperty("hostchecks", &hostchecks))
builder->AddExpression("dependencies", OperatorPlus,
Service::ResolveDependencies(host, hostchecks));
}
void ConvenienceComponent::HostCommittedHandler(const ConfigItem::Ptr& item)

View File

@ -211,7 +211,7 @@ int yylex(YYSTYPE *lvalp, YYLTYPE *llocp, void *scanner);
void yyerror(YYLTYPE *locp, ConfigCompiler *context, const char *err)
{
stringstream message;
message << *locp;
message << *locp << ": " << err;
throw runtime_error(message.str());
}