0
0
Fork 0
mirror of https://github.com/alerta/alerta.git synced 2025-01-29 03:11:34 +00:00
alerta_alerta/bin/alert
2014-03-18 23:54:17 +00:00

108 lines
No EOL
2.3 KiB
Python
Executable file

#!/usr/bin/env python
import sys
import json
import argparse
import requests
import urllib
from datetime import datetime
__version__ = '0.0.1'
class AlertaApi(object):
ALERTA_URL = 'http://localhost:8080/api'
# ALERTA_URL = 'http://api.alerta.io'
def __init__(self):
self.last_updated = None
def get_alerts(self, **kwargs):
response = self._query('/alerts', kwargs)
return response['alerts']
def get_heartbeats(self):
response = self._query('/heartbeats')
return response['heartbeats']
def _query(self, path, filter=[]):
url = self.ALERTA_URL + path + '?' + urllib.urlencode(filter)
response = requests.get(url).json()
# self.last_updated = response['lastUpdated']
return response
class Client(object):
def __init__(self):
self.api = AlertaApi()
def alerts(self, args):
filter = dict([x.split('=') for x in args.filter if '=' in x])
alerts = self.api.get_alerts(**filter)
if args.json:
print json.dumps(alerts)
return
for alert in alerts:
print alert
def heartbeats(self, args):
heartbeats = self.api.get_heartbeats()
if args.json:
print json.dumps(heartbeats)
return
for hb in heartbeats:
print hb
if __name__ == '__main__':
alerta = Client()
parser = argparse.ArgumentParser(
prog='alert'
)
parser.add_argument(
'-json',
'-j',
action='store_true',
help='Output raw JSON'
)
subparsers = parser.add_subparsers(metavar='QUERY', help='Valid queries: alerts,heartbeats')
parser_alerts = subparsers.add_parser('alerts', help='List alerts optionally filtered by any attribute')
parser_alerts.add_argument(
'-s',
'--short',
action='store_true',
help='Only return hostnames'
)
parser_alerts.add_argument(
'filter',
nargs='*',
metavar='KEY=VALUE',
help='eg. stage=PROD stack=identity'
)
parser_alerts.set_defaults(func=alerta.alerts)
parser_heartbeats = subparsers.add_parser('heartbeats', help='List all heartbeats')
parser_heartbeats.set_defaults(func=alerta.heartbeats)
args = parser.parse_args()
args.func(args)