refactor the monitor
[doctor.git] / tests / monitor / sample.py
1 ##############################################################################
2 # Copyright (c) 2017 ZTE 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 from datetime import datetime
10 import json
11 import requests
12 import socket
13 from threading import Thread
14 import time
15
16 from identity_auth import get_session
17 from os_clients import nova_client
18 from monitor.base import BaseMonitor
19
20
21 class SampleMonitor(BaseMonitor):
22     event_type = "compute.host.down"
23
24     def __init__(self, conf, inspector_url, log):
25         super(SampleMonitor, self).__init__(conf, inspector_url, log)
26         self.session = get_session()
27         self.nova = nova_client(conf.nova_version, self.session)
28         self.hosts = self.nova.hypervisors.list(detailed=True)
29         self.pingers = []
30
31     def start(self):
32         self.log.info('sample monitor start......')
33         for host in self.hosts:
34             host_dict = host.__dict__
35             host_name = host_dict['hypervisor_hostname']
36             host_ip = host_dict['host_ip']
37             pinger = Pinger(host_name, host_ip, self, self.log)
38             pinger.start()
39             self.pingers.append(pinger)
40
41     def stop(self):
42         self.log.info('sample monitor stop......')
43         for pinger in self.pingers:
44             pinger.stop()
45             pinger.join()
46         del self.pingers
47
48     def report_error(self, hostname):
49         self.log.info('sample monitor report error......')
50         data = [
51             {
52                 'id': 'monitor_sample_id1',
53                 'time': datetime.now().isoformat(),
54                 'type': self.event_type,
55                 'details': {
56                     'hostname': hostname,
57                     'status': 'down',
58                     'monitor': 'monitor_sample',
59                     'monitor_event_id': 'monitor_sample_event1'
60                 },
61             },
62         ]
63
64         auth_token = self.session.get_token() if \
65                      self.conf.inspector.type != 'sample' else None
66         headers = {
67             'Content-Type': 'application/json',
68             'Accept': 'application/json',
69             'X-Auth-Token': auth_token,
70         }
71
72         url = '%s%s' % (self.inspector_url, 'events') \
73             if self.inspector_url.endswith('/') else \
74             '%s%s' % (self.inspector_url, '/events')
75         requests.put(url, data=json.dumps(data), headers=headers)
76
77
78 class Pinger(Thread):
79     interval = 0.1  # second
80     timeout = 0.1   # second
81     ICMP_ECHO_MESSAGE = '\x08\x00\xf7\xff\x00\x00\x00\x00'
82
83     def __init__(self, host_name, host_ip, monitor, log):
84         Thread.__init__(self)
85         self.monitor = monitor
86         self.hostname = host_name
87         self.ip_addr = host_ip or socket.gethostbyname(self.hostname)
88         self.log = log
89         self._stopped = False
90
91     def run(self):
92         while True:
93             if self._stopped:
94                 return
95             self._run()
96             time.sleep(self.interval)
97
98     def stop(self):
99         self.log.info("Stopping Pinger host_name(%s), host_ip(%s)"
100                       % (self.hostname, self.ip_addr))
101         self._stopped = True
102
103     def _run(self):
104         self.log.info("Starting Pinger host_name(%s), host_ip(%s)"
105                       % (self.hostname, self.ip_addr))
106
107         sock = socket.socket(socket.AF_INET, socket.SOCK_RAW,
108                              socket.IPPROTO_ICMP)
109         sock.settimeout(self.timeout)
110         while True:
111             try:
112                 sock.sendto(self.ICMP_ECHO_MESSAGE.encode(), (self.ip_addr, 0))
113                 sock.recv(4096)
114             except socket.timeout:
115                 self.log.info("doctor monitor detected at %s" % time.time())
116                 self.monitor.report_error(self.hostname)
117                 self.log.info("ping timeout, quit monitoring...")
118                 self._stopped = True
119                 return