137
137
# But it's likely to change once we understand what the Best-Match
138
138
# code will need better.
139
139
combined = {} # { <service>_<cap> : (min, max), ... }
140
stale_host_services = {} # { host1 : [svc1, svc2], host2 :[svc1]}
140
141
for host, host_dict in hosts_dict.iteritems():
141
142
for service_name, service_dict in host_dict.iteritems():
142
143
if not service_dict.get("enabled", True):
143
144
# Service is disabled; do no include it
147
#Check if the service capabilities became stale
148
if self.host_service_caps_stale(host, service_name):
149
if host not in stale_host_services:
150
stale_host_services[host] = [] # Adding host key once
151
stale_host_services[host].append(service_name)
145
153
for cap, value in service_dict.iteritems():
154
if cap == "timestamp": # Timestamp is not needed
146
156
key = "%s_%s" % (service_name, cap)
147
157
min_value, max_value = combined.get(key, (value, value))
148
158
min_value = min(min_value, value)
149
159
max_value = max(max_value, value)
150
160
combined[key] = (min_value, max_value)
162
# Delete the expired host services
163
self.delete_expired_host_services(stale_host_services)
153
166
def _refresh_from_db(self, context):
186
199
logging.debug(_("Received %(service_name)s service update from "
187
200
"%(host)s: %(capabilities)s") % locals())
188
201
service_caps = self.service_states.get(host, {})
202
capabilities["timestamp"] = utils.utcnow() # Reported time
189
203
service_caps[service_name] = capabilities
190
204
self.service_states[host] = service_caps
206
def host_service_caps_stale(self, host, service):
207
"""Check if host service capabilites are not recent enough."""
208
allowed_time_diff = FLAGS.periodic_interval * 3
209
caps = self.service_states[host][service]
210
if (utils.utcnow() - caps["timestamp"]) <= \
211
datetime.timedelta(seconds=allowed_time_diff):
215
def delete_expired_host_services(self, host_services_dict):
216
"""Delete all the inactive host services information."""
217
for host, services in host_services_dict.iteritems():
218
service_caps = self.service_states[host]
219
for service in services:
220
del service_caps[service]
221
if len(service_caps) == 0: # Delete host if no services
222
del self.service_states[host]