0
0
Fork 0
mirror of https://github.com/alerta/alerta.git synced 2025-01-29 03:11:34 +00:00
alerta_alerta/tools/alertctl
2014-01-16 01:29:41 +00:00

197 lines
4.9 KiB
Python
Executable file

#!/usr/bin/env python
import os
import sys
import argparse
import subprocess
import urllib2
import json
import datetime
Version = '2.0.3'
DISABLE_DIR = '/var/run/alerta'
SERVICES = {
'alerta': 1,
'alert-cloudwatch': 1,
'alert-dynect': 1,
'alert-ircbot': 1,
'alert-logger': 1,
'alert-mailer': 1,
'alert-pagerduty': 1,
'alert-pinger': 1,
'alert-syslog': 1,
'alert-urlmon': 1,
'httpd': 1,
}
MGMT_ENDPOINT = 'http://monitoring/alerta/management'
API_ENDPOINT = 'http://monitoring/alerta/api/v2'
def start():
for service in SERVICES:
start = subprocess.Popen(["/etc/init.d/%s" % service, "start"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = start.communicate()[0].rstrip('\n')
rc = start.returncode
if rc:
print 'ERROR %s (rc=%s)' % (stdout, rc)
else:
print stdout
def stop():
for service in SERVICES:
stop = subprocess.Popen(["/etc/init.d/%s" % service, "stop"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = stop.communicate()[0].rstrip('\n')
rc = stop.returncode
if rc:
print 'ERROR %s (rc=%s)' % (stdout, rc)
else:
print stdout
def restart():
stop()
start()
def status():
for service in SERVICES:
status = subprocess.Popen(["/etc/init.d/%s" % service, "status"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = status.communicate()[0].rstrip('\n')
rc = status.returncode
if rc:
print 'ERROR %s (rc=%s)' % (stdout, rc)
else:
print stdout
def disables():
disabled = False
for service in SERVICES:
if os.path.exists("%s/%s.disable" % (DISABLE_DIR, service)):
print "DISABLED : %s" % service
disabled = True
if not disabled:
print 'DISABLED: No services have been administratively disabled.'
def pgrep():
for service in SERVICES:
pgrep = subprocess.Popen(["pgrep", "-lf", service], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = pgrep.communicate()[0].rstrip('\n')
rc = pgrep.returncode
if rc:
print 'ERROR %s (rc=%s)' % (stdout, rc)
else:
print stdout
def api():
try:
response = urllib2.urlopen('%s/healthcheck' % MGMT_ENDPOINT).read()
except urllib2.HTTPError, e:
print 'HEALTHCHECK: %s' % e
else:
print 'HEALTHCHECK: %s' % response
try:
response = json.loads(urllib2.urlopen('%s/heartbeats' % API_ENDPOINT).read())
except urllib2.HTTPError, e:
print 'HEARTBEATS: %s' % e
else:
for hb in response['heartbeats']:
create_time = datetime.datetime.strptime(hb['createTime'], '%Y-%m-%dT%H:%M:%S.%fZ')
receive_time = datetime.datetime.strptime(hb['receiveTime'], '%Y-%m-%dT%H:%M:%S.%fZ')
now = datetime.datetime.utcnow()
delta = receive_time - create_time
latency = int(delta.days * 24 * 60 * 60 * 1000 + delta.seconds * 1000 + delta.microseconds / 1000) # ms
delta = now - receive_time
freshness = int(delta.days * 24 * 60 * 60 + delta.seconds) # seconds
if freshness > 300:
print 'STALE: %s %s %s s %s ms' % (hb['origin'], hb['version'], freshness, latency)
elif latency > 200:
print 'SLOW: %s %s %s s %s ms' % (hb['origin'], hb['version'], freshness, latency)
def main(argv):
try:
parser = argparse.ArgumentParser(
description="Alerta daemon controller"
)
parser.add_argument(
"--start",
action="store_true",
default=False,
help='Start all daemons'
)
parser.add_argument(
"--stop",
action="store_true",
default=False,
help='Stop all daemons'
)
parser.add_argument(
"--restart",
action="store_true",
default=False,
help='Restart all daemons'
)
parser.add_argument(
"--status",
action="store_true",
default=False,
help='Status of all daemons'
)
parser.add_argument(
"--report",
action="store_true",
default=False,
help='Report on API status, disables, services and processes'
)
args = parser.parse_args(argv)
if args.start:
start()
elif args.stop:
stop()
elif args.restart:
restart()
elif args.status:
status()
elif args.report:
api()
print
disables()
print
status()
print
pgrep()
else:
parser.print_help()
except Exception, e:
print 'ERROR %s' % e
sys.exit(1)
if __name__ == '__main__':
main(sys.argv[1:])