Added GetProperty/SetTag/GetTag accessors to ConfigObjectAdapter.

This commit is contained in:
Gunnar Beutner 2012-07-09 16:44:46 +02:00
parent c7788f73c2
commit 16e8d131fb
5 changed files with 59 additions and 41 deletions

View File

@ -18,10 +18,28 @@ public:
ConfigObject::Ptr GetConfigObject() const; ConfigObject::Ptr GetConfigObject() const;
template<typename T>
bool GetProperty(const string& key, T *value) const
{
return GetConfigObject()->GetProperty(key, value);
}
template<typename T>
void SetTag(const string& key, const T& value)
{
GetConfigObject()->SetTag(key, value);
}
template<typename T>
bool GetTag(const string& key, T *value) const
{
return GetConfigObject()->GetTag(key, value);
}
private: private:
ConfigObject::Ptr m_ConfigObject; ConfigObject::Ptr m_ConfigObject;
}; };
} }
#endif /* CONFIGOBJECTADAPTER_H */ #endif /* CONFIGOBJECTADAPTER_H */

View File

@ -6,7 +6,7 @@ string Host::GetAlias(void) const
{ {
string value; string value;
if (GetConfigObject()->GetProperty("alias", &value)) if (GetProperty("alias", &value))
return value; return value;
return GetName(); return GetName();
@ -30,7 +30,7 @@ Host Host::GetByName(const string& name)
Dictionary::Ptr Host::GetGroups(void) const Dictionary::Ptr Host::GetGroups(void) const
{ {
Dictionary::Ptr value; Dictionary::Ptr value;
GetConfigObject()->GetProperty("hostgroups", &value); GetProperty("hostgroups", &value);
return value; return value;
} }
@ -40,7 +40,7 @@ set<string> Host::GetParents(void) const
Dictionary::Ptr dependencies; Dictionary::Ptr dependencies;
if (GetConfigObject()->GetProperty("dependencies", &dependencies)) { if (GetProperty("dependencies", &dependencies)) {
dependencies = Service::ResolveDependencies(*this, dependencies); dependencies = Service::ResolveDependencies(*this, dependencies);
Dictionary::Iterator it; Dictionary::Iterator it;
@ -63,7 +63,7 @@ set<string> Host::GetParents(void) const
bool Host::IsReachable(void) const bool Host::IsReachable(void) const
{ {
Dictionary::Ptr dependencies; Dictionary::Ptr dependencies;
if (GetConfigObject()->GetProperty("dependencies", &dependencies)) { if (GetProperty("dependencies", &dependencies)) {
dependencies = Service::ResolveDependencies(*this, dependencies); dependencies = Service::ResolveDependencies(*this, dependencies);
Dictionary::Iterator it; Dictionary::Iterator it;
@ -83,7 +83,7 @@ bool Host::IsReachable(void) const
bool Host::IsUp(void) const bool Host::IsUp(void) const
{ {
Dictionary::Ptr hostchecks; Dictionary::Ptr hostchecks;
if (GetConfigObject()->GetProperty("hostchecks", &hostchecks)) { if (GetProperty("hostchecks", &hostchecks)) {
hostchecks = Service::ResolveDependencies(*this, hostchecks); hostchecks = Service::ResolveDependencies(*this, hostchecks);
Dictionary::Iterator it; Dictionary::Iterator it;

View File

@ -6,7 +6,7 @@ string HostGroup::GetAlias(void) const
{ {
string value; string value;
if (GetConfigObject()->GetProperty("alias", &value)) if (GetProperty("alias", &value))
return value; return value;
return GetName(); return GetName();
@ -15,14 +15,14 @@ string HostGroup::GetAlias(void) const
string HostGroup::GetNotesUrl(void) const string HostGroup::GetNotesUrl(void) const
{ {
string value; string value;
GetConfigObject()->GetProperty("notes_url", &value); GetProperty("notes_url", &value);
return value; return value;
} }
string HostGroup::GetActionUrl(void) const string HostGroup::GetActionUrl(void) const
{ {
string value; string value;
GetConfigObject()->GetProperty("action_url", &value); GetProperty("action_url", &value);
return value; return value;
} }

View File

@ -8,7 +8,7 @@ string Service::GetAlias(void) const
{ {
string value; string value;
if (GetConfigObject()->GetProperty("alias", &value)) if (GetProperty("alias", &value))
return value; return value;
return GetName(); return GetName();
@ -32,7 +32,7 @@ Service Service::GetByName(const string& name)
Host Service::GetHost(void) const Host Service::GetHost(void) const
{ {
string hostname; string hostname;
if (!GetConfigObject()->GetProperty("host_name", &hostname)) if (!GetProperty("host_name", &hostname))
throw runtime_error("Service object is missing the 'host_name' property."); throw runtime_error("Service object is missing the 'host_name' property.");
return Host::GetByName(hostname); return Host::GetByName(hostname);
@ -41,35 +41,35 @@ Host Service::GetHost(void) const
Dictionary::Ptr Service::GetMacros(void) const Dictionary::Ptr Service::GetMacros(void) const
{ {
Dictionary::Ptr macros; Dictionary::Ptr macros;
GetConfigObject()->GetProperty("macros", &macros); GetProperty("macros", &macros);
return macros; return macros;
} }
string Service::GetCheckType(void) const string Service::GetCheckType(void) const
{ {
string value = "nagios"; string value = "nagios";
GetConfigObject()->GetProperty("check_type", &value); GetProperty("check_type", &value);
return value; return value;
} }
string Service::GetCheckCommand(void) const string Service::GetCheckCommand(void) const
{ {
string value; string value;
GetConfigObject()->GetProperty("check_command", &value); GetProperty("check_command", &value);
return value; return value;
} }
long Service::GetMaxCheckAttempts(void) const long Service::GetMaxCheckAttempts(void) const
{ {
long value = 3; long value = 3;
GetConfigObject()->GetProperty("max_check_attempts", &value); GetProperty("max_check_attempts", &value);
return value; return value;
} }
long Service::GetCheckInterval(void) const long Service::GetCheckInterval(void) const
{ {
long value = 300; long value = 300;
GetConfigObject()->GetProperty("check_interval", &value); GetProperty("check_interval", &value);
if (value < 15) if (value < 15)
value = 15; value = 15;
@ -80,7 +80,7 @@ long Service::GetCheckInterval(void) const
long Service::GetRetryInterval(void) const long Service::GetRetryInterval(void) const
{ {
long value; long value;
if (!GetConfigObject()->GetProperty("retry_interval", &value)) if (!GetProperty("retry_interval", &value))
value = GetCheckInterval() / 5; value = GetCheckInterval() / 5;
return value; return value;
@ -89,7 +89,7 @@ long Service::GetRetryInterval(void) const
Dictionary::Ptr Service::GetDependencies(void) const Dictionary::Ptr Service::GetDependencies(void) const
{ {
Dictionary::Ptr value; Dictionary::Ptr value;
GetConfigObject()->GetProperty("dependencies", &value); GetProperty("dependencies", &value);
return value; return value;
} }
@ -116,14 +116,14 @@ void Service::GetDependenciesRecursive(const Dictionary::Ptr& result) const {
Dictionary::Ptr Service::GetGroups(void) const Dictionary::Ptr Service::GetGroups(void) const
{ {
Dictionary::Ptr value; Dictionary::Ptr value;
GetConfigObject()->GetProperty("servicegroups", &value); GetProperty("servicegroups", &value);
return value; return value;
} }
Dictionary::Ptr Service::GetCheckers(void) const Dictionary::Ptr Service::GetCheckers(void) const
{ {
Dictionary::Ptr value; Dictionary::Ptr value;
GetConfigObject()->GetProperty("checkers", &value); GetProperty("checkers", &value);
return value; return value;
} }
@ -154,13 +154,13 @@ bool Service::IsReachable(void) const
void Service::SetNextCheck(time_t nextCheck) void Service::SetNextCheck(time_t nextCheck)
{ {
GetConfigObject()->SetTag("next_check", (long)nextCheck); SetTag("next_check", (long)nextCheck);
} }
time_t Service::GetNextCheck(void) time_t Service::GetNextCheck(void)
{ {
long value; long value;
if (!GetConfigObject()->GetTag("next_check", &value)) { if (!GetTag("next_check", &value)) {
value = time(NULL) + rand() % GetCheckInterval(); value = time(NULL) + rand() % GetCheckInterval();
SetNextCheck(value); SetNextCheck(value);
} }
@ -180,93 +180,93 @@ void Service::UpdateNextCheck(void)
void Service::SetChecker(const string& checker) void Service::SetChecker(const string& checker)
{ {
GetConfigObject()->SetTag("checker", checker); SetTag("checker", checker);
} }
string Service::GetChecker(void) const string Service::GetChecker(void) const
{ {
string value; string value;
GetConfigObject()->GetTag("checker", &value); GetTag("checker", &value);
return value; return value;
} }
void Service::SetCurrentCheckAttempt(long attempt) void Service::SetCurrentCheckAttempt(long attempt)
{ {
GetConfigObject()->SetTag("check_attempt", attempt); SetTag("check_attempt", attempt);
} }
long Service::GetCurrentCheckAttempt(void) const long Service::GetCurrentCheckAttempt(void) const
{ {
long value = 1; long value = 1;
GetConfigObject()->GetTag("check_attempt", &value); GetTag("check_attempt", &value);
return value; return value;
} }
void Service::SetState(ServiceState state) void Service::SetState(ServiceState state)
{ {
GetConfigObject()->SetTag("state", static_cast<long>(state)); SetTag("state", static_cast<long>(state));
} }
ServiceState Service::GetState(void) const ServiceState Service::GetState(void) const
{ {
long value = StateUnknown; long value = StateUnknown;
GetConfigObject()->GetTag("state", &value); GetTag("state", &value);
return static_cast<ServiceState>(value); return static_cast<ServiceState>(value);
} }
void Service::SetStateType(ServiceStateType type) void Service::SetStateType(ServiceStateType type)
{ {
GetConfigObject()->SetTag("state_type", static_cast<long>(type)); SetTag("state_type", static_cast<long>(type));
} }
ServiceStateType Service::GetStateType(void) const ServiceStateType Service::GetStateType(void) const
{ {
long value = StateTypeHard; long value = StateTypeHard;
GetConfigObject()->GetTag("state_type", &value); GetTag("state_type", &value);
return static_cast<ServiceStateType>(value); return static_cast<ServiceStateType>(value);
} }
void Service::SetLastCheckResult(const CheckResult& result) void Service::SetLastCheckResult(const CheckResult& result)
{ {
GetConfigObject()->SetTag("last_result", result.GetDictionary()); SetTag("last_result", result.GetDictionary());
} }
bool Service::HasLastCheckResult(void) const bool Service::HasLastCheckResult(void) const
{ {
Dictionary::Ptr value; Dictionary::Ptr value;
return GetConfigObject()->GetTag("last_result", &value) && value; return GetTag("last_result", &value) && value;
} }
CheckResult Service::GetLastCheckResult(void) const CheckResult Service::GetLastCheckResult(void) const
{ {
Dictionary::Ptr value; Dictionary::Ptr value;
if (!GetConfigObject()->GetTag("last_result", &value)) if (!GetTag("last_result", &value))
throw invalid_argument("Service has no last check result."); throw invalid_argument("Service has no last check result.");
return CheckResult(value); return CheckResult(value);
} }
void Service::SetLastStateChange(time_t ts) void Service::SetLastStateChange(time_t ts)
{ {
GetConfigObject()->SetTag("last_state_change", static_cast<long>(ts)); SetTag("last_state_change", static_cast<long>(ts));
} }
time_t Service::GetLastStateChange(void) const time_t Service::GetLastStateChange(void) const
{ {
long value; long value;
if (!GetConfigObject()->GetTag("last_state_change", &value)) if (!GetTag("last_state_change", &value))
value = IcingaApplication::GetInstance()->GetStartTime(); value = IcingaApplication::GetInstance()->GetStartTime();
return value; return value;
} }
void Service::SetLastHardStateChange(time_t ts) void Service::SetLastHardStateChange(time_t ts)
{ {
GetConfigObject()->SetTag("last_hard_state_change", static_cast<long>(ts)); SetTag("last_hard_state_change", static_cast<long>(ts));
} }
time_t Service::GetLastHardStateChange(void) const time_t Service::GetLastHardStateChange(void) const
{ {
long value; long value;
if (!GetConfigObject()->GetTag("last_hard_state_change", &value)) if (!GetTag("last_hard_state_change", &value))
value = IcingaApplication::GetInstance()->GetStartTime(); value = IcingaApplication::GetInstance()->GetStartTime();
return value; return value;
} }
@ -370,7 +370,7 @@ bool Service::IsAllowedChecker(const string& checker) const
Dictionary::Ptr Service::ResolveDependencies(Host host, const Dictionary::Ptr& dependencies) Dictionary::Ptr Service::ResolveDependencies(Host host, const Dictionary::Ptr& dependencies)
{ {
Dictionary::Ptr services; Dictionary::Ptr services;
host.GetConfigObject()->GetProperty("services", &services); host.GetProperty("services", &services);
Dictionary::Ptr result = boost::make_shared<Dictionary>(); Dictionary::Ptr result = boost::make_shared<Dictionary>();

View File

@ -6,7 +6,7 @@ string ServiceGroup::GetAlias(void) const
{ {
string value; string value;
if (GetConfigObject()->GetProperty("alias", &value)) if (GetProperty("alias", &value))
return value; return value;
return GetName(); return GetName();
@ -15,14 +15,14 @@ string ServiceGroup::GetAlias(void) const
string ServiceGroup::GetNotesUrl(void) const string ServiceGroup::GetNotesUrl(void) const
{ {
string value; string value;
GetConfigObject()->GetProperty("notes_url", &value); GetProperty("notes_url", &value);
return value; return value;
} }
string ServiceGroup::GetActionUrl(void) const string ServiceGroup::GetActionUrl(void) const
{ {
string value; string value;
GetConfigObject()->GetProperty("action_url", &value); GetProperty("action_url", &value);
return value; return value;
} }