class Surveyor(threading.Thread): def __init__(self, args, manifest, monitored_devices, check_period, notifier): super(Surveyor, self).__init__() self._args = args self._manifest = manifest self._monitored_devices = monitored_devices self._notifer = notifier self._roll_call = SharedVarCollection({}) self._devices_poll = Periodic(check_period, self.poll_devices, "poll_devices") return def poll_devices(self): for name in self._monitored_devices: self._notifer.diagnostic("pinging %s" % name) found = self.ping(self._manifest.address(name)) if found: self._roll_call.set(name, True) self._notifer.note("%s found" % name) else: self._roll_call.set(name, False) self._notifer.note("%s missing" % name) return def ping(self, ip_address): # NOTE: ping requires root access. Fake it during development with a random# if hasattr(args, 'test') and args.test: found = (random.randint(0, 3) == 3) else: response = pyping.ping(ip_address) found = (response.ret_code == 0) return found def roll_call(self): return self._roll_call.get() def check(self): self._devices_poll.check() return def run(self): while gRunningFlag: self._devices_poll.check() return
class Tracker(threading.Thread): def __init__(self, args, config, notifier): super(Tracker, self).__init__() self._args = args self._config = config self._monitored_devices = config.devices_details()["monitored_devices"] self._notifer = notifier positive_poll_period = config.general_details()["positive_poll_period"] negative_poll_period = config.general_details()["negative_poll_period"] self._poll = Periodic(negative_poll_period, self.poll_devices, "poll_devices", notifier=notifier) redis_info = config.redis_details() self._rdb = redis.StrictRedis(host=redis_info["host"], port=redis_info["port"], db=redis_info["db_no"]) self._roll_call = {} return def any_detected(self): for thisDevice, presence in self._roll_call.items(): if presence: return True return False def poll_devices(self): roll_call = {} redis_info = config.redis_details() previously_detected = self.any_detected() any_detected = False for name, address in self._monitored_devices.items(): self._notifer.diagnostic("pinging %s at %s" % (name, address)) found = self.ping(address) if found: any_detected = True roll_call[name] = True self._notifer.note("%s found" % name) else: roll_call[name] = False self._notifer.note("%s missing" % name) if previously_detected: self._poll.reset() # set up next poll if any_detected and not previously_detected: self._poll.set_period( config.general_details()["positive_poll_period"]) elif not any_detected and previously_detected: self._poll.set_period( config.general_details()["negative_poll_period"]) self._roll_call = roll_call self._rdb.set(redis_info["key_detail"], json.dumps(roll_call)) self._rdb.set(redis_info["key_summary"], json.dumps(any_detected)) return def ping(self, ip_address): # NOTE: ping requires root access. Fake it during development with a random# if hasattr(args, 'test') and args.test: found = (random.randint(0, 3) == 3) elif USE_PYPING: response = pyping.ping(ip_address) found = (response.ret_code == 0) else: response = os.system("ping -c i %s" % ip_address) found = (response == 0) return found def roll_call(self): return self._roll_call def check(self): self._poll.check() return def run(self): while gRunningFlag: self.check() return