diff options
author | dongwenjuan <dong.wenjuan@zte.com.cn> | 2017-12-21 16:11:31 +0800 |
---|---|---|
committer | dongwenjuan <dong.wenjuan@zte.com.cn> | 2018-01-09 09:37:39 +0800 |
commit | 754b94bba6df6e7aafd5f1ffb8432a40cb815649 (patch) | |
tree | 4bbe2f725e5b378bbd8f1b12cbbbb173afb203b2 /doctor_tests/monitor | |
parent | bdbd22904733af311225a3315555bb39ea12ed55 (diff) |
support pep8 check
Change-Id: I3c4358694c57d27a207ae60712c44f717c8895f7
Signed-off-by: dongwenjuan <dong.wenjuan@zte.com.cn>
Diffstat (limited to 'doctor_tests/monitor')
-rw-r--r-- | doctor_tests/monitor/__init__.py | 1 | ||||
-rw-r--r-- | doctor_tests/monitor/collectd.py | 56 | ||||
-rw-r--r-- | doctor_tests/monitor/collectd_plugin.py | 78 | ||||
-rw-r--r-- | doctor_tests/monitor/sample.py | 10 |
4 files changed, 86 insertions, 59 deletions
diff --git a/doctor_tests/monitor/__init__.py b/doctor_tests/monitor/__init__.py index 7e30c9f8..76839720 100644 --- a/doctor_tests/monitor/__init__.py +++ b/doctor_tests/monitor/__init__.py @@ -23,6 +23,7 @@ _monitor_name_class_mapping = { 'collectd': 'doctor_tests.monitor.collectd.CollectdMonitor' } + def get_monitor(conf, inspector_url, log): monitor_class = _monitor_name_class_mapping.get(conf.monitor.type) return importutils.import_object(monitor_class, conf, diff --git a/doctor_tests/monitor/collectd.py b/doctor_tests/monitor/collectd.py index 4e9329c2..60150b12 100644 --- a/doctor_tests/monitor/collectd.py +++ b/doctor_tests/monitor/collectd.py @@ -9,7 +9,6 @@ import os import socket import getpass -import sys from doctor_tests.monitor.base import BaseMonitor @@ -22,11 +21,11 @@ class CollectdMonitor(BaseMonitor): tmp_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) tmp_sock.connect(("8.8.8.8", 80)) - ## control_ip is the IP of primary interface of control node i.e. - ## eth0, eno1. It is used by collectd monitor to communicate with - ## sample inspector. - ## TODO (umar) see if mgmt IP of control is a better option. Also - ## primary interface may not be the right option + # control_ip is the IP of primary interface of control node i.e. + # eth0, eno1. It is used by collectd monitor to communicate with + # sample inspector. + # TODO (umar) see if mgmt IP of control is a better option. Also + # primary interface may not be the right option self.control_ip = tmp_sock.getsockname()[0] self.compute_user = getpass.getuser() self.interface_name = os.environ.get('INTERFACE_NAME') or '' @@ -35,18 +34,21 @@ class CollectdMonitor(BaseMonitor): self.username = os.environ.get('OS_USERNAME') self.password = os.environ.get('OS_PASSWORD') self.project_name = os.environ.get('OS_PROJECT_NAME') - self.user_domain_name = os.environ.get('OS_USER_DOMAIN_NAME') or 'default' + self.user_domain_name = \ + os.environ.get('OS_USER_DOMAIN_NAME') or 'default' self.user_domain_id = os.environ.get('OS_USER_DOMAIN_ID') - self.project_domain_name = os.environ.get('OS_PROJECT_DOMAIN_NAME') or 'default' + self.project_domain_name = \ + os.environ.get('OS_PROJECT_DOMAIN_NAME') or 'default' self.project_domain_id = os.environ.get('OS_PROJECT_DOMAIN_ID') - self.ssh_opts_cpu = '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' + self.ssh_opts_cpu = \ + '-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' def start(self, host): self.log.info("Collectd monitor start.........") self.compute_host = host.name self.compute_ip = host.ip f = open("%s/collectd.conf" % self.top_dir, 'w') - collectd_conf_file = """ + collectd_conf_file = """ Hostname %s FQDNLookup false Interval 1 @@ -93,16 +95,21 @@ LoadPlugin logfile SendNotification true DispatchValues false </Plugin> - """ % (self.compute_host, self.compute_user, self.control_ip, self.compute_ip, self.compute_host, self.compute_user, - self.inspector_type, self.auth_url, self.username, self.password, self.project_name, self.user_domain_name, - self.user_domain_id, self.project_domain_name, self.project_domain_id) + """ % (self.compute_host, self.compute_user, self.control_ip, + self.compute_ip, self.compute_host, self.compute_user, + self.inspector_type, self.auth_url, self.username, + self.password, self.project_name, self.user_domain_name, + self.user_domain_id, self.project_domain_name, + self.project_domain_id) f.write(collectd_conf_file) f.close() - os.system(" scp %s %s/collectd.conf %s@%s: " % (self.ssh_opts_cpu, self.top_dir, self.compute_user, self.compute_ip)) + os.system("scp %s %s/collectd.conf %s@%s: " + % (self.ssh_opts_cpu, self.top_dir, + self.compute_user, self.compute_ip)) self.log.info("after first scp") - ## @TODO (umar) Always assuming that the interface is assigned an IP if - ## interface name is not provided. See if there is a better approach + # @TODO (umar) Always assuming that the interface is assigned an IP if + # interface name is not provided. See if there is a better approach os.system(""" ssh %s %s@%s \"if [ -n \"%s\" ]; then dev=%s else @@ -115,15 +122,24 @@ LoadPlugin logfile else sudo touch \${collectd_conf}-doctor-created fi - sudo mv collectd.conf /opt/collectd/etc/collectd.conf\" """ % (self.ssh_opts_cpu, self.compute_user, self.compute_ip, self.interface_name, self.interface_name, self.compute_ip)) + sudo mv collectd.conf /opt/collectd/etc/collectd.conf\" """ + % (self.ssh_opts_cpu, self.compute_user, + self.compute_ip, self.interface_name, + self.interface_name, self.compute_ip)) self.log.info("after first ssh") - os.system(" scp %s %s/monitor/collectd_plugin.py %s@%s:collectd_plugin.py " % (self.ssh_opts_cpu, self.top_dir, self.compute_user, self.compute_ip)) + os.system("scp %s %s/monitor/collectd_plugin.py" + "%s@%s:collectd_plugin.py" + % (self.ssh_opts_cpu, self.top_dir, + self.compute_user, self.compute_ip)) self.log.info("after sec scp") - os.system(" ssh %s %s@%s \"sudo pkill collectd; sudo /opt/collectd/sbin/collectd\" " % (self.ssh_opts_cpu, self.compute_user, self.compute_ip)) + os.system("ssh %s %s@%s \"sudo pkill collectd;" + "sudo /opt/collectd/sbin/collectd\" " + % (self.ssh_opts_cpu, self.compute_user, self.compute_ip)) self.log.info("after sec ssh") def stop(self): - os.system(" ssh %s %s@%s \"sudo pkill collectd\" " % (self.ssh_opts_cpu, self.compute_user, self.compute_ip)) + os.system(" ssh %s %s@%s \"sudo pkill collectd\" " + % (self.ssh_opts_cpu, self.compute_user, self.compute_ip)) def cleanup(self): os.system(""" ssh %s %s@%s \" diff --git a/doctor_tests/monitor/collectd_plugin.py b/doctor_tests/monitor/collectd_plugin.py index 57105f33..f4341020 100644 --- a/doctor_tests/monitor/collectd_plugin.py +++ b/doctor_tests/monitor/collectd_plugin.py @@ -23,7 +23,7 @@ from congressclient.v1 import client def write_debug(str_write, write_type, compute_user): file_name = ('/home/%s/monitor.log' % compute_user) file_tmp = open(file_name, write_type) - file_tmp.write( "%s" % str_write) + file_tmp.write("%s" % str_write) file_tmp.close() @@ -84,48 +84,51 @@ class DoctorMonitorCollectd(object): collectd.info('Unknown config key "%s"' % key) def init_collectd(self): - write_debug("Compute node collectd monitor start at %s\n\n" % datetime.now().isoformat(), "w", self.compute_user) + write_debug("Compute node collectd monitor start at %s\n\n" + % datetime.now().isoformat(), "w", self.compute_user) if self.inspector_type == 'sample': self.inspector_url = ('http://%s:12345/events' % self.control_ip) elif self.inspector_type == 'congress': loader = loading.get_plugin_loader('password') - self.auth = loader.load_from_options(auth_url=self.os_auth_url, - username=self.os_username, - password=self.os_password, - project_name=self.os_project_name, - user_domain_name=self.os_user_domain_name, - user_domain_id=self.os_user_domain_id, - project_domain_name=self.os_project_domain_name, - project_domain_id=self.os_project_domain_id) - self.sess=session.Session(auth=self.auth) + self.auth = loader.load_from_options( + auth_url=self.os_auth_url, + username=self.os_username, + password=self.os_password, + project_name=self.os_project_name, + user_domain_name=self.os_user_domain_name, + user_domain_id=self.os_user_domain_id, + project_domain_name=self.os_project_domain_name, + project_domain_id=self.os_project_domain_id) + self.sess = session.Session(auth=self.auth) congress = client.Client(session=self.sess, service_type='policy') ds = congress.list_datasources()['results'] - doctor_ds = next((item for item in ds if item['driver'] == 'doctor'), - None) + doctor_ds = next( + (item for item in ds if item['driver'] == 'doctor'), None) - congress_endpoint = congress.httpclient.get_endpoint(auth=self.auth) - self.inspector_url = ('%s/v1/data-sources/%s/tables/events/rows' % - (congress_endpoint, doctor_ds['id'])) + congress_endpoint = \ + congress.httpclient.get_endpoint(auth=self.auth) + self.inspector_url = ('%s/v1/data-sources/%s/tables/events/rows' + % (congress_endpoint, doctor_ds['id'])) else: sys.exit() self.start_notifications = 1 - def notify_inspector(self): event_type = "compute.host.down" payload = [ { - 'id': ("monitor_%s_id1" % self.monitor_type), - 'time': datetime.now().isoformat(), - 'type': event_type, - 'details': { - 'hostname': self.host_name, - 'status': 'down', - 'monitor': ("monitor_%s" % self.monitor_type), - 'monitor_event_id': ("monitor_%s_event1" % self.monitor_type) - }, - }, + 'id': ("monitor_%s_id1" % self.monitor_type), + 'time': datetime.now().isoformat(), + 'type': event_type, + 'details': { + 'hostname': self.host_name, + 'status': 'down', + 'monitor': ("monitor_%s" % self.monitor_type), + 'monitor_event_id': + ("monitor_%s_event1" % self.monitor_type) + }, + }, ] data = json.dumps(payload) self.inspector_notified = 1 @@ -135,7 +138,7 @@ class DoctorMonitorCollectd(object): try: requests.post(self.inspector_url, data=data, headers=headers) except ConnectionError as err: - print err + print(err) elif self.inspector_type == 'congress': # TODO(umar) enhance for token expiry case headers = { @@ -145,22 +148,25 @@ class DoctorMonitorCollectd(object): } requests.put(self.inspector_url, data=data, headers=headers) - - def handle_notif(self, notification, data=None): - if (notification.severity == collectd.NOTIF_FAILURE or - notification.severity == collectd.NOTIF_WARNING): - if (self.start_notifications == 1 and self.inspector_notified == 0): - write_debug("Received down notification: doctor monitor detected at %s\n" % time.time(), "a", self.compute_user) + def handle_notify(self, notification, data=None): + if (notification.seerity == collectd.NOTIF_FAILURE or + notification.severity == collectd.NOTIF_WARNING): + if (self.start_notifications == 1 and + self.inspector_notified == 0): + write_debug("Received down notification:" + "doctor monitor detected at %s\n" + % time.time(), "a", self.compute_user) self.notify_inspector() elif notification.severity == collectd.NOTIF_OKAY: collectd.info("Interface status: UP again %s\n" % time.time()) else: - collectd.info("Unknown notification severity %s\n" % notification.severity) + collectd.info("Unknown notification severity %s\n" + % notification.severity) monitor = DoctorMonitorCollectd() collectd.register_config(monitor.config_func) collectd.register_init(monitor.init_collectd) -collectd.register_notification(monitor.handle_notif) +collectd.register_notification(monitor.handle_notify) diff --git a/doctor_tests/monitor/sample.py b/doctor_tests/monitor/sample.py index 4dc5e603..c207cd9f 100644 --- a/doctor_tests/monitor/sample.py +++ b/doctor_tests/monitor/sample.py @@ -51,16 +51,20 @@ class SampleMonitor(BaseMonitor): } auth_token = self.session.get_token() if \ - self.conf.inspector.type != 'sample' else None + self.conf.inspector.type != 'sample' else None headers = { 'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Auth-Token': auth_token, } if self.conf.inspector.type != Inspector.VITRAGE: - requests.put(self.inspector_url, data=json.dumps([data]), headers=headers) + requests.put(self.inspector_url, + data=json.dumps([data]), + headers=headers) else: - requests.post(self.inspector_url, data=json.dumps(data), headers=headers) + requests.post(self.inspector_url, + data=json.dumps(data), + headers=headers) class Pinger(Thread): |