|
| 1 | +import sys |
| 2 | +import requests |
| 3 | +import click |
| 4 | +import logging |
| 5 | +import time |
| 6 | +import json |
| 7 | + |
| 8 | +try: |
| 9 | + from urllib.parse import urlparse |
| 10 | +except ImportError: |
| 11 | + from urlparse import urlparse |
| 12 | + |
| 13 | +@click.command() |
| 14 | +@click.option('-u', '--uri', 'annotate_uri', |
| 15 | + default='http://localhost:3000/api/annotations', help='uri to send annotation to') |
| 16 | +@click.option('-T', '--title', 'title', default='event', help='event title') |
| 17 | +@click.option('-t', '--tag', 'tags', multiple=True, help='event tags') |
| 18 | +@click.option('-d', '--description', 'description', help='event description') |
| 19 | +@click.option('-s', '--start', 'start_time', default=int(round(time.time() * 1000)), help='event start timestamp (unix secs)') |
| 20 | +@click.option('-e', '--end', 'end_time', default=0, help='event end timestamp (unix secs)') |
| 21 | + |
| 22 | +def main(annotate_uri, title, tags, description, start_time, end_time): |
| 23 | + """ Send Grafana annotations """ |
| 24 | + |
| 25 | + logging.basicConfig(format='[%(levelname)s] %(message)s', level=logging.INFO) |
| 26 | + |
| 27 | + if description is None: |
| 28 | + description = "".join([line for line in iter(sys.stdin.readline, '')]) |
| 29 | + |
| 30 | + tags_field = ';'.join(tags) |
| 31 | + |
| 32 | + try: |
| 33 | + url_parts = urlparse(annotate_uri) |
| 34 | + event_data = {} |
| 35 | + |
| 36 | + if 'http' in url_parts.scheme: |
| 37 | + event_data['text'] = '<b>%s</b>\n\n%s' % (title, description) |
| 38 | + event_data['tags'] = tags |
| 39 | + event_data['time'] = start_time |
| 40 | + if end_time > 0: |
| 41 | + if end_time < start_time: |
| 42 | + raise Exception('Event end time cannot be before start time') |
| 43 | + event_data['isRegion'] = True |
| 44 | + event_data['timeEnd'] = end_time |
| 45 | + |
| 46 | + send_web_annotation(url_parts, event_data) |
| 47 | + |
| 48 | + elif 'influx' in url_parts.scheme: |
| 49 | + event_data['name'] = 'events' |
| 50 | + event_data['columns'] = ['tags', 'text', 'title'] |
| 51 | + event_data['points'] = [[tags_field, description, title]] |
| 52 | + |
| 53 | + send_influx_annotation(url_parts, event_data) |
| 54 | + |
| 55 | + else: |
| 56 | + raise NotImplementedError('Scheme %s not recognised in uri %s' % |
| 57 | + (url_parts.scheme, annotate_uri)) |
| 58 | + |
| 59 | + except Exception as e: |
| 60 | + logging.fatal(e) |
| 61 | + # We could exit 1 here but we really don't want to cause a job to |
| 62 | + # fail just because we couldn't send an event. |
| 63 | + |
| 64 | + sys.exit(0) |
| 65 | + |
| 66 | +def send_web_annotation(url_parts, event_data): |
| 67 | + """ POST event to an endpoint in Grafana Annotations API format """ |
| 68 | + logging.info('Sending web event to %s' % url_parts.hostname) |
| 69 | + #logging.debug(json.dumps(event_data)) |
| 70 | + |
| 71 | + url = url_parts.geturl() |
| 72 | + auth_tuple = None |
| 73 | + |
| 74 | + if url_parts.username and url_parts.password: |
| 75 | + auth_tuple = (url_parts.username, url_parts.password) |
| 76 | + url_host_port = url_parts.netloc.split('@')[1] |
| 77 | + url = '%s://%s%s' % (url_parts.scheme, url_host_port, url_parts.path) |
| 78 | + |
| 79 | + post_result = requests.post(url, json=json.dumps(event_data), |
| 80 | + auth=auth_tuple, timeout=5) |
| 81 | + |
| 82 | + if post_result.status_code > 299: |
| 83 | + logging.error('Received %s response, sending event failed' % post_result.status_code) |
| 84 | + |
| 85 | + if 'message' in post_result: |
| 86 | + logging.info(post_result.message) |
| 87 | + |
| 88 | +def send_influx_annotation(url_parts, event_data): |
| 89 | + raise NotImplementedError('Influx annotations not yet implemented, check back soon.') |
| 90 | + logging.info('Sending influx event to %s' % url_parts.hostname) |
| 91 | + logging.debug(json.dumps(event_data)) |
0 commit comments