0
0
Fork 0
mirror of https://github.com/alerta/alerta.git synced 2025-01-24 17:29:39 +00:00
alerta_alerta/bin/alert-sender
2013-03-11 00:46:47 +00:00

179 lines
5.8 KiB
Python
Executable file

#!/usr/bin/env python
########################################
#
# alert-sender - Alert Command-line script
#
########################################
import os
import sys
import argparse
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
os.pardir,
os.pardir))
if os.path.exists(os.path.join(possible_topdir, 'alerta', '__init__.py')):
sys.path.insert(0, possible_topdir)
from alerta.common import config
from alerta.common import log as logging
from alerta.sender.client import SenderClient, Version
from alerta.alert import severity
LOG = logging.getLogger('alerta.sender')
CONF = config.CONF
def main(argv):
try:
parser = argparse.ArgumentParser(
add_help=False,
prog='alert-sender',
description='Alert Command-Line Tool - sends an alert to the alerting system. Alerts must have' +
' a resource (including service and environment), event name, value and text. A ' +
'severity of "Normal" is used if none given. Tags and group are optional.',
epilog='alert-sender.py --resource myCoolApp --event AppStatus --group Application --value Down ' +
'--severity critical --env PROD --svc MicroApp --tag release:134 --tag build:1005 ' +
'--text "Micro App X is down."'
)
parser.add_argument(
'-r', '--resource',
help='Resource under alarm eg. hostname, network device, application, web address.'
)
parser.add_argument(
'-e',
'--event',
help='Event name eg. NodeDown, QUEUE:LENGTH:EXCEEDED, coldStart, LOG_ERROR'
)
parser.add_argument(
'-C',
'--correlate',
help='Comma-separated list of events to correlate together eg. NodeUp,NodeDown'
)
parser.add_argument(
'-g',
'--group',
help='Event group eg. Application, Backup, Database, HA, Hardware, Job, Network, OS, Performance, Security'
)
parser.add_argument(
'-v',
'--value',
help='Event value eg. 100%%, Down, PingFail, 55tps, ORA-1664'
)
parser.add_argument(
'-s',
'--severity',
default='Normal',
help='Severity eg. Critical, Major, Minor, Warning, Normal, Inform (default: %(default)s)'
)
parser.add_argument(
'-E',
'--environment',
metavar='ENV',
action='append',
help='Environment eg. PROD, REL, QA, TEST, CODE, STAGE, DEV, LWP, INFRA'
)
parser.add_argument(
'-S',
'--svc',
'--service',
dest='service',
action='append',
help='Service eg. R1, R2, Discussion, Soulmates, ContentAPI, MicroApp, FlexibleContent, SharedSvcs'
)
parser.add_argument(
'-T',
'--tag',
action='append',
dest='tags',
default=list(),
help='Tag the event with anything and everything.'
)
parser.add_argument(
'-t',
'--text',
help='Freeform alert text eg. Host not responding to ping.'
)
parser.add_argument(
'-o',
'--timeout',
type=int,
default=CONF.alert_timeout,
help='Timeout in seconds that OPEN alert will persist in webapp.'
)
parser.add_argument(
'-H',
'--heartbeat',
action='store_true',
default=False,
help='Send heartbeat to server.'
)
parser.add_argument(
'-O',
'--origin',
help='Origin of heartbeat. Usually an application instance.'
)
parser.add_argument(
'-q',
'--quiet',
action='store_true',
default=False,
help='Do not display assigned alert id.'
)
parser.add_argument(
'-d',
'--dry-run',
action='store_true',
default=False,
help='Do not send alert.'
)
config.parse_args(argv, version=Version, cli_parser=parser, daemon=False)
logging.setup('alerta')
if not CONF.heartbeat:
if not CONF.resource:
parser.error("Must supply event resource using -r or --resource")
if not CONF.event:
parser.error("Must supply event name using -e or --event")
if not CONF.value:
parser.error("Must supply event value using -v or --value")
CONF.severity = severity.parse_severity(CONF.severity) or severity.NORMAL
if not CONF.severity:
CONF.severity = severity.NORMAL
else:
CONF.severity = severity.parse_severity(CONF.severity)
if CONF.severity not in severity.ALL:
parser.error("Severity must be one of %s" % ','.join(severity.ALL))
if not CONF.environment:
parser.error("Must supply one or more environments using -E or --environment")
else:
CONF.environment = [x.upper() for x in CONF.environment]
if not CONF.service:
parser.error("Must supply one or more service using -S or --service")
if not CONF.text:
parser.error("Must supply alert text using -t or --text.")
sender = SenderClient()
msg_id = sender.main()
if not CONF.quiet:
print msg_id
except Exception, e:
print >> sys.stderr, e
sys.exit(1)
if __name__ == '__main__':
main(sys.argv[1:])