4dc5e60389ce72326de55e0876a3cf7a12693d3c
[doctor.git] / doctor_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 doctor_tests.common.constants import Inspector
17 from doctor_tests.identity_auth import get_session
18 from doctor_tests.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.pinger = None
28
29     def start(self, host):
30         self.log.info('sample monitor start......')
31         self.pinger = Pinger(host.name, host.ip, self, self.log)
32         self.pinger.start()
33
34     def stop(self):
35         self.log.info('sample monitor stop......')
36         if self.pinger is not None:
37             self.pinger.stop()
38             self.pinger.join()
39
40     def report_error(self, hostname):
41         self.log.info('sample monitor report error......')
42         data = {
43             'time': datetime.now().isoformat(),
44             'type': self.event_type,
45             'details': {
46                 'hostname': hostname,
47                 'status': 'down',
48                 'monitor': 'monitor_sample',
49                 'monitor_event_id': 'monitor_sample_event1'
50             },
51         }
52
53         auth_token = self.session.get_token() if \
54                      self.conf.inspector.type != 'sample' else None
55         headers = {
56             'Content-Type': 'application/json',
57             'Accept': 'application/json',
58             'X-Auth-Token': auth_token,
59         }
60         if self.conf.inspector.type != Inspector.VITRAGE:
61             requests.put(self.inspector_url, data=json.dumps([data]), headers=headers)
62         else:
63             requests.post(self.inspector_url, data=json.dumps(data), headers=headers)
64
65
66 class Pinger(Thread):
67     interval = 0.1  # second
68     timeout = 0.1   # second
69     ICMP_ECHO_MESSAGE = bytes([0x08, 0x00, 0xf7, 0xff, 0x00, 0x00, 0x00, 0x00])
70
71     def __init__(self, host_name, host_ip, monitor, log):
72         Thread.__init__(self)
73         self.monitor = monitor
74         self.hostname = host_name
75         self.ip_addr = host_ip or socket.gethostbyname(self.hostname)
76         self.log = log
77         self._stopped = False
78
79     def run(self):
80         self.log.info("Starting Pinger host_name(%s), host_ip(%s)"
81                       % (self.hostname, self.ip_addr))
82
83         sock = socket.socket(socket.AF_INET, socket.SOCK_RAW,
84                              socket.IPPROTO_ICMP)
85         sock.settimeout(self.timeout)
86         while True:
87             if self._stopped:
88                 return
89             try:
90                 sock.sendto(self.ICMP_ECHO_MESSAGE, (self.ip_addr, 0))
91                 sock.recv(4096)
92             except socket.timeout:
93                 self.log.info("doctor monitor detected at %s" % time.time())
94                 self.monitor.report_error(self.hostname)
95                 self.log.info("ping timeout, quit monitoring...")
96                 self._stopped = True
97                 return
98             time.sleep(self.interval)
99
100     def stop(self):
101         self.log.info("Stopping Pinger host_name(%s), host_ip(%s)"
102                       % (self.hostname, self.ip_addr))
103         self._stopped = True