9e489865e39dbdd132f1708ada0caac0ac89a123
[doctor.git] / tests / monitor.py
1 ##############################################################################
2 # Copyright (c) 2016 NEC Corporation and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import argparse
11 import json
12 import requests
13 import socket
14 import time
15
16
17 # NOTE: icmp message with all zero data (checksum = 0xf7ff)
18 #       see https://tools.ietf.org/html/rfc792
19 ICMP_ECHO_MESSAGE = '\x08\x00\xf7\xff\x00\x00\x00\x00'
20
21
22 class DoctorMonitorSample(object):
23
24     interval = 0.1  # second
25     timeout = 0.1  # second
26     event_type = "compute.host.down"
27
28     def __init__(self, args):
29         self.hostname = args.hostname
30         self.inspector = args.inspector
31         self.ip_addr = args.ip or socket.gethostbyname(self.hostname)
32
33     def start_loop(self):
34         print "start ping to host %(h)s (ip=%(i)s)" % {'h': self.hostname,
35                                                        'i': self.ip_addr}
36         sock = socket.socket(socket.AF_INET, socket.SOCK_RAW,
37                              socket.IPPROTO_ICMP)
38         sock.settimeout(self.timeout)
39         while True:
40             try:
41                 sock.sendto(ICMP_ECHO_MESSAGE, (self.ip_addr, 0))
42                 data = sock.recv(4096)
43             except socket.timeout:
44                 print "doctor monitor detected at %s" % time.time()
45                 self.report_error()
46                 print "ping timeout, quit monitoring..."
47                 return
48             time.sleep(self.interval)
49
50     def report_error(self):
51         payload = {"type": self.event_type, "hostname": self.hostname}
52         data = json.dumps(payload)
53         headers = {'content-type': 'application/json'}
54         requests.post(self.inspector, data=data, headers=headers)
55
56
57 def get_args():
58     parser = argparse.ArgumentParser(description='Doctor Sample Monitor')
59     parser.add_argument('hostname', metavar='HOSTNAME', type=str, nargs='?',
60                         help='a hostname to monitor connectivity')
61     parser.add_argument('ip', metavar='IP', type=str, nargs='?',
62                         help='an IP address to monitor connectivity')
63     parser.add_argument('inspector', metavar='INSPECTOR', type=str, nargs='?',
64                         help='inspector url to report error',
65                         default='http://127.0.0.1:12345/events')
66     return parser.parse_args()
67
68
69 def main():
70     args = get_args()
71     monitor = DoctorMonitorSample(args)
72     monitor.start_loop()
73
74
75 if __name__ == '__main__':
76     main()