27
29
print "No Password or Passfile in client config file"
28
30
# raise RuntimeError XXX
29
31
self.password = "gazonk"
32
self.fqdn = fqdn # string
31
33
self.created = datetime.datetime.now()
34
self.last_seen = None # datetime.datetime()
33
35
if timeout is None:
34
36
timeout = options.timeout
35
self.timeout = timeout
37
self.timeout = timeout # datetime.timedelta()
37
39
interval = options.interval
38
self.interval = interval
39
self.next_check = datetime.datetime.now()
40
self.interval = interval # datetime.timedelta()
41
self.next_check = datetime.datetime.now() # datetime.datetime()
42
self.checker = None # or a subprocess.Popen()
43
def check_action(self, now=None):
44
"""The checker said something and might have completed.
45
Check if is has, and take appropriate actions."""
46
if self.checker.poll() is None:
47
# False alarm, no result yet
51
now = datetime.datetime.now()
52
if self.checker.returncode == 0:
54
while self.next_check <= now:
55
self.next_check += self.interval
56
handle_request = check_action
57
def start_checker(self):
60
self.checker = subprocess.Popen("sleep 1; fping -q -- %s"
61
% re.escape(self.fqdn),
62
stdout=subprocess.PIPE,
65
except subprocess.OSError, e:
66
print "Failed to start subprocess:", e
67
def stop_checker(self):
68
if self.checker is None:
70
os.kill(self.checker.pid, signal.SIGTERM)
71
if self.checker.poll() is None:
72
os.kill(self.checker.pid, signal.SIGKILL)
74
__del__ = stop_checker
76
if self.checker is None:
78
return self.checker.stdout.fileno()
80
"""The time when something must be done about this client"""
81
return min(self.last_seen + self.timeout, self.next_check)
82
def still_valid(self, now=None):
83
"""Has this client's timeout not passed?"""
85
now = datetime.datetime.now()
86
return now < (self.last_seen + timeout)
87
def it_is_time_to_check(self, now=None):
89
now = datetime.datetime.now()
90
return self.next_check <= now
42
93
class server_metaclass(type):
229
280
credentials=cred)
232
in_, out, err = select.select((udp_server,
235
server.handle_request()
284
input, out, err = select.select((udp_server,
291
except KeyboardInterrupt:
295
for client in clients:
296
client.stop_checker()
238
299
if __name__ == "__main__":