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;
}
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>::const_iterator it;
vector<Service>::iterator it;
for (it = parents.begin(); it != parents.end(); it++) {
Service parent = *it;
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 (!it->IsReachable())
return false;
}
if (state != StateOK && state != StateWarning && type == StateTypeHard)
failedServices.push_back(parent);
if (nextCheck == -1 || parent.GetNextCheck() < nextCheck)
nextCheck = parent.GetNextCheck();
}
string message;
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;
return true;
}
void Service::SetNextCheck(time_t nextCheck)
@ -407,7 +350,6 @@ ServiceState Service::StateFromString(const string& state)
stateLookup["ok"] = StateOK;
stateLookup["warning"] = StateWarning;
stateLookup["critical"] = StateCritical;
stateLookup["unreachable"] = StateUnreachable;
stateLookup["uncheckable"] = StateUncheckable;
stateLookup["unknown"] = StateUnknown;
}
@ -430,8 +372,6 @@ string Service::StateToString(ServiceState state)
return "warning";
case StateCritical:
return "critical";
case StateUnreachable:
return "unreachable";
case StateUncheckable:
return "uncheckable";
case StateUnknown:

View File

@ -10,7 +10,6 @@ enum ServiceState
StateWarning,
StateCritical,
StateUnknown,
StateUnreachable,
StateUncheckable,
};
@ -50,7 +49,7 @@ public:
static void UpdateDependencyCache(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);
time_t GetNextCheck(void);

View File

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

View File

@ -306,39 +306,10 @@ void DelegationComponent::CheckResultRequestHandler(const Endpoint::Ptr& sender,
if (!service.IsAllowedChecker(sender->GetIdentity()))
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 */
RequestMessage rm;
rm.SetMethod("delegation::ServiceStatus");
vector<Service> parents = service.GetParents();
ServiceStatusMessage statusmsg = Service::CalculateCombinedStatus(&service, &params, parents);
statusmsg.SetService(service.GetName());
rm.SetParams(statusmsg);
rm.SetParams(params);
EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, rm);
}