Re-designed reachability detection.

This commit is contained in:
Gunnar Beutner 2012-07-03 15:11:11 +02:00
parent f9139a6f97
commit d26d8e7bb7
4 changed files with 18 additions and 100 deletions

View File

@ -186,74 +186,17 @@ void Service::InvalidateDependencyCache(void)
m_DependencyCacheValid = false; m_DependencyCacheValid = false;
} }
ServiceStatusMessage Service::CalculateCombinedStatus(Service *current, ServiceStatusMessage *input, const vector<Service>& parents) bool Service::IsReachable(void) const
{ {
vector<Service> failedServices; vector<Service> parents = GetParents();
time_t nextCheck = -1; vector<Service>::iterator it;
vector<Service>::const_iterator it;
for (it = parents.begin(); it != parents.end(); it++) { for (it = parents.begin(); it != parents.end(); it++) {
Service parent = *it; if (!it->IsReachable())
return false;
if (current && current->GetName() == parent.GetName())
continue;
if (!parent.HasLastCheckResult())
continue;
string svcname;
ServiceState state = StateUnknown;
ServiceStateType type = StateTypeHard;
if (input && input->GetService(&svcname) && svcname == parent.GetName()) {
input->GetState(&state);
input->GetStateType(&type);
} else {
state = parent.GetState();
type = parent.GetStateType();
}
if (state != StateOK && state != StateWarning && type == StateTypeHard)
failedServices.push_back(parent);
if (nextCheck == -1 || parent.GetNextCheck() < nextCheck)
nextCheck = parent.GetNextCheck();
} }
string message; return true;
ServiceState state;
if (failedServices.empty()) {
if (input)
return *input;
state = StateOK;
message = "Dependant services are available.";
} else {
state = StateUnreachable;
message = "One or more dependant services have failed.";
}
ServiceStatusMessage result;
result.SetState(state);
result.SetStateType(StateTypeHard);
result.SetCurrentCheckAttempt(1);
result.SetNextCheck(nextCheck);
time_t now;
time(&now);
CheckResult cr;
cr.SetScheduleStart(now);
cr.SetScheduleEnd(now);
cr.SetExecutionStart(now);
cr.SetExecutionEnd(now);
cr.SetOutput(message);
cr.SetState(state);
result.SetCheckResult(cr);
return result;
} }
void Service::SetNextCheck(time_t nextCheck) void Service::SetNextCheck(time_t nextCheck)
@ -407,7 +350,6 @@ ServiceState Service::StateFromString(const string& state)
stateLookup["ok"] = StateOK; stateLookup["ok"] = StateOK;
stateLookup["warning"] = StateWarning; stateLookup["warning"] = StateWarning;
stateLookup["critical"] = StateCritical; stateLookup["critical"] = StateCritical;
stateLookup["unreachable"] = StateUnreachable;
stateLookup["uncheckable"] = StateUncheckable; stateLookup["uncheckable"] = StateUncheckable;
stateLookup["unknown"] = StateUnknown; stateLookup["unknown"] = StateUnknown;
} }
@ -430,8 +372,6 @@ string Service::StateToString(ServiceState state)
return "warning"; return "warning";
case StateCritical: case StateCritical:
return "critical"; return "critical";
case StateUnreachable:
return "unreachable";
case StateUncheckable: case StateUncheckable:
return "uncheckable"; return "uncheckable";
case StateUnknown: case StateUnknown:

View File

@ -10,7 +10,6 @@ enum ServiceState
StateWarning, StateWarning,
StateCritical, StateCritical,
StateUnknown, StateUnknown,
StateUnreachable,
StateUncheckable, StateUncheckable,
}; };
@ -50,7 +49,7 @@ public:
static void UpdateDependencyCache(void); static void UpdateDependencyCache(void);
static void InvalidateDependencyCache(void); static void InvalidateDependencyCache(void);
static ServiceStatusMessage CalculateCombinedStatus(Service *current, ServiceStatusMessage *input, const vector<Service>& parents); bool IsReachable(void) const;
void SetNextCheck(time_t nextCheck); void SetNextCheck(time_t nextCheck);
time_t GetNextCheck(void); time_t GetNextCheck(void);

View File

@ -113,8 +113,16 @@ void CompatComponent::DumpServiceStatus(ofstream& fp, Service service)
int state = service.GetState(); int state = service.GetState();
if (state == StateUnreachable) if (!service.IsReachable()) {
state = StateCritical; state = StateCritical;
string text = "One or more parent services are unavailable.";
if (output.empty())
output = text;
else
output = text + " (" + output + ")";
}
if (state >= StateUnknown) if (state >= StateUnknown)
state = StateUnknown; state = StateUnknown;

View File

@ -306,39 +306,10 @@ void DelegationComponent::CheckResultRequestHandler(const Endpoint::Ptr& sender,
if (!service.IsAllowedChecker(sender->GetIdentity())) if (!service.IsAllowedChecker(sender->GetIdentity()))
return; return;
vector<Service> children = service.GetChildren();
vector<Service>::iterator it;
for (it = children.begin(); it != children.end(); it++) {
Service child = *it;
vector<Service> affectedServices = child.GetParents();
affectedServices.push_back(child);
ServiceStatusMessage statusmsg = Service::CalculateCombinedStatus(&child, NULL, affectedServices);
statusmsg.SetService(child.GetName());
ServiceState state = StateUnreachable;
statusmsg.GetState(&state);
if (child.GetState() == StateUnreachable || state == StateUnreachable) {
RequestMessage rm;
rm.SetMethod("delegation::ServiceStatus");
rm.SetParams(statusmsg);
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, rm);
}
}
/* send state update */ /* send state update */
RequestMessage rm; RequestMessage rm;
rm.SetMethod("delegation::ServiceStatus"); rm.SetMethod("delegation::ServiceStatus");
rm.SetParams(params);
vector<Service> parents = service.GetParents();
ServiceStatusMessage statusmsg = Service::CalculateCombinedStatus(&service, &params, parents);
statusmsg.SetService(service.GetName());
rm.SetParams(statusmsg);
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, rm); EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, rm);
} }