From 282369b6fd58a78e6a7c91f21b331363d4ed0fb3 Mon Sep 17 00:00:00 2001 From: Umar Farooq Date: Thu, 13 Jul 2017 12:20:56 +0200 Subject: Add Collectd as a Monitor Type A plugin for collectd is added to use collectd on compute as a monitor type. Monitor files are updated accordingly. The inspector now listens on all interfaces instead of only localhost to enable it to communicate with compute node. JIRA: DOCTOR-86 JIRA: DOCTOR-101 Change-Id: Idc834d428152e4687020eff7d8db36a652b1bf86 Signed-off-by: Umar Farooq --- tests/lib/monitors/sample/monitor.py | 124 +++++++++++++++++++++++++++++++++++ tests/lib/monitors/sample/sample | 18 +++++ 2 files changed, 142 insertions(+) create mode 100644 tests/lib/monitors/sample/monitor.py create mode 100644 tests/lib/monitors/sample/sample (limited to 'tests/lib/monitors/sample') diff --git a/tests/lib/monitors/sample/monitor.py b/tests/lib/monitors/sample/monitor.py new file mode 100644 index 00000000..7450c534 --- /dev/null +++ b/tests/lib/monitors/sample/monitor.py @@ -0,0 +1,124 @@ +############################################################################## +# Copyright (c) 2016 NEC Corporation and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +import argparse +from datetime import datetime +import json +import logger as doctor_log +import requests +import socket +import time + +from keystoneauth1 import session +from congressclient.v1 import client + +import identity_auth + +# NOTE: icmp message with all zero data (checksum = 0xf7ff) +# see https://tools.ietf.org/html/rfc792 +ICMP_ECHO_MESSAGE = '\x08\x00\xf7\xff\x00\x00\x00\x00' + +SUPPORTED_INSPECTOR_TYPES = ['sample', 'congress'] + +LOG = doctor_log.Logger('doctor_monitor').getLogger() + + +class DoctorMonitorSample(object): + + interval = 0.1 # second + timeout = 0.1 # second + event_type = "compute.host.down" + + def __init__(self, args): + if args.inspector_type not in SUPPORTED_INSPECTOR_TYPES: + raise Exception("Inspector type '%s' not supported", args.inspector_type) + + self.hostname = args.hostname + self.inspector_type = args.inspector_type + self.ip_addr = args.ip or socket.gethostbyname(self.hostname) + + if self.inspector_type == 'sample': + self.inspector_url = 'http://127.0.0.1:12345/events' + elif self.inspector_type == 'congress': + auth=identity_auth.get_identity_auth() + self.session=session.Session(auth=auth) + congress = client.Client(session=self.session, service_type='policy') + ds = congress.list_datasources()['results'] + doctor_ds = next((item for item in ds if item['driver'] == 'doctor'), + None) + + congress_endpoint = congress.httpclient.get_endpoint(auth=auth) + self.inspector_url = ('%s/v1/data-sources/%s/tables/events/rows' % + (congress_endpoint, doctor_ds['id'])) + + def start_loop(self): + LOG.debug("start ping to host %(h)s (ip=%(i)s)" % {'h': self.hostname, + 'i': self.ip_addr}) + sock = socket.socket(socket.AF_INET, socket.SOCK_RAW, + socket.IPPROTO_ICMP) + sock.settimeout(self.timeout) + while True: + try: + sock.sendto(ICMP_ECHO_MESSAGE, (self.ip_addr, 0)) + data = sock.recv(4096) + except socket.timeout: + LOG.info("doctor monitor detected at %s" % time.time()) + self.report_error() + LOG.info("ping timeout, quit monitoring...") + return + time.sleep(self.interval) + + def report_error(self): + payload = [ + { + 'id': 'monitor_sample_id1', + 'time': datetime.now().isoformat(), + 'type': self.event_type, + 'details': { + 'hostname': self.hostname, + 'status': 'down', + 'monitor': 'monitor_sample', + 'monitor_event_id': 'monitor_sample_event1' + }, + }, + ] + data = json.dumps(payload) + + if self.inspector_type == 'sample': + headers = {'content-type': 'application/json'} + requests.post(self.inspector_url, data=data, headers=headers) + elif self.inspector_type == 'congress': + headers = { + 'Content-Type': 'application/json', + 'Accept': 'application/json', + 'X-Auth-Token':self.session.get_token(), + } + requests.put(self.inspector_url, data=data, headers=headers) + + +def get_args(): + parser = argparse.ArgumentParser(description='Doctor Sample Monitor') + parser.add_argument('hostname', metavar='HOSTNAME', type=str, nargs='?', + help='a hostname to monitor connectivity') + parser.add_argument('ip', metavar='IP', type=str, nargs='?', + help='an IP address to monitor connectivity') + parser.add_argument('inspector_type', metavar='INSPECTOR_TYPE', type=str, nargs='?', + help='inspector to report', + default='sample') + return parser.parse_args() + + +def main(): + args = get_args() + monitor = DoctorMonitorSample(args) + monitor.start_loop() + + +if __name__ == '__main__': + main() diff --git a/tests/lib/monitors/sample/sample b/tests/lib/monitors/sample/sample new file mode 100644 index 00000000..1d310333 --- /dev/null +++ b/tests/lib/monitors/sample/sample @@ -0,0 +1,18 @@ +#!/bin/bash + +function start_monitor_sample { + cp $TOP_DIR/lib/monitors/sample/monitor.py $TOP_DIR/monitor.py + pgrep -f "python monitor.py" && return 0 + sudo -E python monitor.py "$COMPUTE_HOST" "$COMPUTE_IP" "$INSPECTOR_TYPE" \ + > monitor.log 2>&1 & +} + +function stop_monitor_sample { + pgrep -f "python monitor.py" || return 0 + sudo kill $(pgrep -f "python monitor.py") +} + +function cleanup_monitor_sample { + rm monitor.py + return +} -- cgit 1.2.3-korg