#NEWLINE# def run( self ) : #NEWLINE# #INDENT# root . setLevel ( logging . ERROR ) #NEWLINE# LOG . setLevel ( logging . ERROR ) #NEWLINE# config_file = ( os . environ . get ( 'ALERTA_CONF_FILE' ) or OPTIONS [ 'config_file' ] ) #NEWLINE# config = configparser . RawConfigParser ( defaults = OPTIONS ) #NEWLINE# try : #NEWLINE# #INDENT# config . read ( os . path . expanduser ( config_file ) ) #NEWLINE# #UNINDENT# except Exception : #NEWLINE# #INDENT# LOG . warning ( 'Problem reading configuration file %s - is this an ini file?' , config_file ) #NEWLINE# sys . exit ( 1 ) #NEWLINE# #UNINDENT# profile_parser = argparse . ArgumentParser ( add_help = False ) #NEWLINE# profile_parser . add_argument ( '--profile' , help = ( 'Profile to apply from %s' % config_file ) ) #NEWLINE# ( args , left ) = profile_parser . parse_known_args ( ) #NEWLINE# want_profile = ( args . profile or os . environ . get ( 'ALERTA_DEFAULT_PROFILE' ) or config . defaults ( ) . get ( 'profile' ) ) #NEWLINE# if ( want_profile and config . has_section ( ( 'profile %s' % want_profile ) ) ) : #NEWLINE# #INDENT# for opt in OPTIONS : #NEWLINE# #INDENT# OPTIONS [ opt ] = config . get ( ( 'profile %s' % want_profile ) , opt ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# for opt in OPTIONS : #NEWLINE# #INDENT# OPTIONS [ opt ] = config . get ( 'DEFAULT' , opt ) #NEWLINE# #UNINDENT# #UNINDENT# OPTIONS [ 'endpoint' ] = ( os . environ . get ( 'ALERTA_ENDPOINT' ) or OPTIONS [ 'endpoint' ] ) #NEWLINE# OPTIONS [ 'key' ] = ( os . environ . get ( 'ALERTA_API_KEY' ) or OPTIONS [ 'key' ] ) #NEWLINE# OPTIONS [ 'color' ] = ( os . environ . get ( 'CLICOLOR' ) or OPTIONS [ 'color' ] ) #NEWLINE# OPTIONS [ 'debug' ] = ( os . environ . get ( 'DEBUG' ) or OPTIONS [ 'debug' ] ) #NEWLINE# parser = argparse . ArgumentParser ( prog = 'alerta' , usage = 'alerta [OPTIONS] COMMAND [FILTERS]' , description = 'Alerta client unified command-line tool' , epilog = 'Filters:\n    Query parameters can be used to filter alerts by any valid alert attribute\n\n    resource=web01     Show alerts with resource equal to "web01"\n    resource!=web01    Show all alerts except those with resource of "web01"\n    event=~down        Show alerts that include "down" in event name\n    event!=~down       Show all alerts that don\'t have "down" in event name\n\n    Special query parameters include "limit", "sort-by", "from-date" and "q" (a\n    json-compliant mongo query).\n' , formatter_class = argparse . RawTextHelpFormatter , parents = [ profile_parser ] ) #NEWLINE# parser . add_argument ( '--endpoint-url' , dest = 'endpoint' , metavar = 'URL' , help = 'API endpoint URL' ) #NEWLINE# parser . add_argument ( '--output' , help = 'Output format of "text" or "json"' ) #NEWLINE# parser . add_argument ( '--json' , '-j' , action = 'store_true' , help = 'Output in JSON format. Shortcut for "--output json"' ) #NEWLINE# parser . add_argument ( '--color' , '--colour' , action = 'store_true' , help = 'Color-coded output based on severity' ) #NEWLINE# parser . add_argument ( '--no-color' , '--no-colour' , action = 'store_false' , dest = 'color' , help = argparse . SUPPRESS ) #NEWLINE# parser . add_argument ( '--debug' , action = 'store_true' , help = 'Print debug output' ) #NEWLINE# parser . set_defaults ( ** OPTIONS ) #NEWLINE# cli = AlertCommand ( ) #NEWLINE# subparsers = parser . add_subparsers ( title = 'Commands' , metavar = 'COMMAND' ) #NEWLINE# parser_send = subparsers . add_parser ( 'send' , help = 'Send alert to server' , usage = 'alerta [OPTIONS] send [-r RESOURCE] [-e EVENT] [-E ENVIRONMENT]\n                        [-s SEVERITY] [-C CORRELATE] [--status STATUS]\n                        [-S SERVICE] [-g GROUP] [-v VALUE] [-t TEXT]\n                        [-T TAG] [-A ATTRIBUTES] [-O ORIGIN]\n                        [--type EVENT_TYPE] [--timeout TIMEOUT]\n                        [--raw-data RAW_DATA]\n' ) #NEWLINE# parser_send . add_argument ( '-r' , '--resource' , help = 'resource under alarm' ) #NEWLINE# parser_send . add_argument ( '-e' , '--event' , help = 'event' ) #NEWLINE# parser_send . add_argument ( '-E' , '--environment' , help = 'environment eg. "production", "development", "testing"' ) #NEWLINE# parser_send . add_argument ( '-s' , '--severity' , default = DEFAULT_SEVERITY , help = 'severity' ) #NEWLINE# parser_send . add_argument ( '-C' , '--correlate' , action = 'append' , help = 'correlate' ) #NEWLINE# parser_send . add_argument ( '--status' , help = 'status should not normally be defined as it is server-assigned eg. "open", "closed"' ) #NEWLINE# parser_send . add_argument ( '-S' , '--service' , action = 'append' , help = 'service affected eg. the application name, "Web", "Network", "Storage", "Database", "Security"' ) #NEWLINE# parser_send . add_argument ( '-g' , '--group' , help = 'group' ) #NEWLINE# parser_send . add_argument ( '-v' , '--value' , help = 'value' ) #NEWLINE# parser_send . add_argument ( '-t' , '--text' , help = 'Freeform alert text eg. "Host not responding to ping."' ) #NEWLINE# parser_send . add_argument ( '-T' , '--tag' , metavar = 'TAG' , action = 'append' , dest = 'tags' , default = list ( ) , help = 'List of tags eg. "London", "os:linux", "AWS/EC2".' ) #NEWLINE# parser_send . add_argument ( '-A' , '--attribute' , action = 'append' , dest = 'attributes' , default = list ( ) , help = 'List of Key=Value attribute pairs eg. "priority=high", "moreInfo=..."' ) #NEWLINE# parser_send . add_argument ( '-O' , '--origin' , default = None , help = 'Origin of alert. Usually in form of "app/host"' ) #NEWLINE# parser_send . add_argument ( '--type' , dest = 'event_type' , default = 'exceptionAlert' , help = 'event type eg. "exceptionAlert", "serviceAlert"' ) #NEWLINE# parser_send . add_argument ( '--timeout' , default = DEFAULT_TIMEOUT , help = 'Timeout in seconds before an "open" alert will be automatically "expired" or "deleted"' , type = int ) #NEWLINE# parser_send . add_argument ( '--raw-data' , default = None , help = 'raw data' ) #NEWLINE# parser_send . set_defaults ( func = cli . send ) #NEWLINE# parser_query = subparsers . add_parser ( 'query' , help = 'List alerts based on query filter' , usage = 'alerta [OPTIONS] query [--details] [--ids IDs] [--filters FILTERS]' ) #NEWLINE# parser_query . add_argument ( '--details' , action = 'store_true' , help = 'Show alert details' ) #NEWLINE# parser_query . add_argument ( '-i' , '--ids' , metavar = 'IDs' , nargs = '+' , help = 'List of alert IDs (can use short 8-char id).' ) #NEWLINE# parser_query . add_argument ( '--filters' , nargs = '+' , default = list ( ) , help = 'KEY=VALUE eg. serverity=warning resource=web' ) #NEWLINE# parser_query . set_defaults ( func = cli . query ) #NEWLINE# parser_watch = subparsers . add_parser ( 'watch' , help = 'Watch alerts based on query filter' , usage = 'alerta [OPTIONS] watch [--ids IDs] [--filters FILTERS]' ) #NEWLINE# parser_watch . add_argument ( '--details' , action = 'store_true' , help = 'Show alert details' ) #NEWLINE# parser_watch . add_argument ( '-i' , '--ids' , metavar = 'IDs' , nargs = '+' , help = 'List of alert IDs (can use short 8-char id).' ) #NEWLINE# parser_watch . add_argument ( '--filters' , nargs = '+' , default = list ( ) , help = 'KEY=VALUE eg. serverity=warning resource=web' ) #NEWLINE# parser_watch . set_defaults ( func = cli . watch ) #NEWLINE# parser_top = subparsers . add_parser ( 'top' , help = 'Show top offenders and stats' , usage = 'alerta [OPTIONS] top [-h]' ) #NEWLINE# parser_top . set_defaults ( func = cli . top ) #NEWLINE# parser_raw = subparsers . add_parser ( 'raw' , help = 'Show alert raw data' , usage = 'alerta [OPTIONS] raw [--ids IDs] [--filters FILTERS]' ) #NEWLINE# parser_raw . add_argument ( '-i' , '--ids' , metavar = 'IDs' , nargs = '+' , help = 'List of alert IDs (can use short 8-char id).' ) #NEWLINE# parser_raw . add_argument ( '--filters' , nargs = '+' , default = list ( ) , help = 'KEY=VALUE eg. serverity=warning resource=web' ) #NEWLINE# parser_raw . set_defaults ( func = cli . raw ) #NEWLINE# parser_history = subparsers . add_parser ( 'history' , help = 'Show alert history' , usage = 'alerta [OPTIONS] history [--ids IDs] [--filters FILTERS]' ) #NEWLINE# parser_history . add_argument ( '-i' , '--ids' , metavar = 'IDs' , nargs = '+' , help = 'List of alert IDs (can use short 8-char id).' ) #NEWLINE# parser_history . add_argument ( '--filters' , nargs = '+' , default = list ( ) , help = 'KEY=VALUE eg. serverity=warning resource=web' ) #NEWLINE# parser_history . set_defaults ( func = cli . history ) #NEWLINE# parser_tag = subparsers . add_parser ( 'tag' , help = 'Tag alerts' , usage = 'alerta [OPTIONS] tag -T TAG [--ids IDs] [--filters FILTERS]' ) #NEWLINE# parser_tag . add_argument ( '-T' , '--tag' , metavar = 'TAG' , action = 'append' , dest = 'tags' , required = True , default = list ( ) , help = 'List of tags eg. "London", "os:linux", "AWS/EC2".' ) #NEWLINE# parser_tag . add_argument ( '-i' , '--ids' , metavar = 'IDs' , nargs = '+' , help = 'List of alert IDs (can use short 8-char id).' ) #NEWLINE# parser_tag . add_argument ( '--filters' , nargs = '+' , default = list ( ) , help = 'KEY=VALUE eg. serverity=warning resource=web' ) #NEWLINE# parser_tag . set_defaults ( func = cli . tag ) #NEWLINE# parser_untag = subparsers . add_parser ( 'untag' , help = 'Remove tags from alerts' , usage = 'alerta [OPTIONS] untag -T TAG [--ids IDs] [--filters FILTERS]' ) #NEWLINE# parser_untag . add_argument ( '-T' , '--tag' , metavar = 'TAG' , action = 'append' , dest = 'tags' , required = True , default = list ( ) , help = 'List of tags eg. "London", "os:linux", "AWS/EC2".' ) #NEWLINE# parser_untag . add_argument ( '-i' , '--ids' , metavar = 'IDs' , nargs = '+' , default = list ( ) , help = 'List of alert IDs (can use short 8-char id).' ) #NEWLINE# parser_untag . add_argument ( '--filters' , nargs = '+' , default = list ( ) , help = 'KEY=VALUE eg. serverity=warning resource=web' ) #NEWLINE# parser_untag . set_defaults ( func = cli . untag ) #NEWLINE# parser_ack = subparsers . add_parser ( 'ack' , help = 'Acknowledge alerts' , usage = 'alerta [OPTIONS] ack [--ids IDs] [--filters FILTERS]' ) #NEWLINE# parser_ack . add_argument ( '-i' , '--ids' , metavar = 'IDs' , nargs = '+' , help = 'List of alert IDs (can use short 8-char id).' ) #NEWLINE# parser_ack . add_argument ( '--filters' , nargs = '+' , default = list ( ) , help = 'KEY=VALUE eg. serverity=warning resource=web' ) #NEWLINE# parser_ack . set_defaults ( func = cli . ack ) #NEWLINE# parser_unack = subparsers . add_parser ( 'unack' , help = 'Unacknowledge alerts' , usage = 'alerta [OPTIONS] unack [--ids IDs] [--filters FILTERS]' ) #NEWLINE# parser_unack . add_argument ( '-i' , '--ids' , metavar = 'IDs' , nargs = '+' , help = 'List of alert IDs (can use short 8-char id).' ) #NEWLINE# parser_unack . add_argument ( '--filters' , nargs = '+' , default = list ( ) , help = 'KEY=VALUE eg. serverity=warning resource=web' ) #NEWLINE# parser_unack . set_defaults ( func = cli . unack ) #NEWLINE# parser_close = subparsers . add_parser ( 'close' , help = 'Close alerts' , usage = 'alerta [OPTIONS] close [--ids IDs] [--filters FILTERS]' ) #NEWLINE# parser_close . add_argument ( '-i' , '--ids' , metavar = 'IDs' , nargs = '+' , default = list ( ) , help = 'List of alert IDs (can use short 8-char id).' ) #NEWLINE# parser_close . add_argument ( '--filters' , nargs = '+' , default = list ( ) , help = 'KEY=VALUE eg. serverity=warning resource=web' ) #NEWLINE# parser_close . set_defaults ( func = cli . close ) #NEWLINE# parser_delete = subparsers . add_parser ( 'delete' , help = 'Delete alerts' , usage = 'alerta [OPTIONS] delete [--ids IDs] [--filters FILTERS]' ) #NEWLINE# parser_delete . add_argument ( '-i' , '--ids' , metavar = 'IDs' , nargs = '+' , default = list ( ) , help = 'List of alert IDs (can use short 8-char id).' ) #NEWLINE# parser_delete . add_argument ( '--filters' , nargs = '+' , default = list ( ) , help = 'KEY=VALUE eg. serverity=warning resource=web' ) #NEWLINE# parser_delete . set_defaults ( func = cli . delete ) #NEWLINE# parser_blackout = subparsers . add_parser ( 'blackout' , help = 'Blackout alerts based on attributes' , usage = 'alerta [OPTIONS] blackout [-r RESOURCE] [-e EVENT] [-E ENVIRONMENT]\n                            [-S SERVICE] [-g GROUP] [-T TAG]\n' ) #NEWLINE# parser_blackout . add_argument ( '-r' , '--resource' , help = 'resource under alarm' ) #NEWLINE# parser_blackout . add_argument ( '-e' , '--event' , help = 'event' ) #NEWLINE# parser_blackout . add_argument ( '-E' , '--environment' , help = 'environment eg. "production", "development", "testing"' ) #NEWLINE# parser_blackout . add_argument ( '-S' , '--service' , action = 'append' , help = 'service affected eg. the application name, "Web", "Network", "Storage", "Database", "Security"' ) #NEWLINE# parser_blackout . add_argument ( '-g' , '--group' , help = 'group' ) #NEWLINE# parser_blackout . add_argument ( '-T' , '--tag' , metavar = 'TAG' , action = 'append' , dest = 'tags' , default = list ( ) , help = 'List of tags eg. "London", "os:linux", "AWS/EC2".' ) #NEWLINE# parser_blackout . add_argument ( '--start' , default = datetime . utcnow ( ) . strftime ( '%Y-%m-%dT%H:%M:%S.%fZ' ) , help = 'Start of blackout period' ) #NEWLINE# parser_blackout . add_argument ( '--duration' , default = 3600 , help = 'Duration of blackout period (default: 1 hour)' , type = int ) #NEWLINE# parser_blackout . set_defaults ( func = cli . blackout ) #NEWLINE# parser_blackouts = subparsers . add_parser ( 'blackouts' , help = 'List all blackout periods' , usage = 'alerta [OPTIONS] blackouts [-h]' ) #NEWLINE# parser_blackouts . add_argument ( '--purge' , default = False , help = 'Delete all expired blackout periods' , action = 'store_true' ) #NEWLINE# parser_blackouts . set_defaults ( func = cli . blackouts ) #NEWLINE# parser_heartbeat = subparsers . add_parser ( 'heartbeat' , help = 'Send heartbeat to server' , usage = 'alerta [OPTIONS] heartbeat [-T TAG] [-O ORIGIN] [--timeout TIMEOUT]' ) #NEWLINE# parser_heartbeat . add_argument ( '-T' , '--tag' , metavar = 'TAG' , action = 'append' , dest = 'tags' , default = list ( ) , help = 'List of tags eg. "London", "os:linux", "AWS/EC2".' ) #NEWLINE# parser_heartbeat . add_argument ( '-O' , '--origin' , default = None , help = 'Origin of heartbeat. Usually in form of "app/host"' ) #NEWLINE# parser_heartbeat . add_argument ( '--timeout' , default = None , help = 'Timeout in seconds before a heartbeat will be considered stale' , type = int ) #NEWLINE# parser_heartbeat . set_defaults ( func = cli . heartbeat ) #NEWLINE# parser_heartbeats = subparsers . add_parser ( 'heartbeats' , help = 'List all heartbeats' , usage = 'alerta [OPTIONS] heartbeats [-h]' ) #NEWLINE# parser_heartbeats . add_argument ( '--alert' , default = False , help = 'Send alerts on stale or slow heartbeats' , action = 'store_true' ) #NEWLINE# parser_heartbeats . add_argument ( '--purge' , default = False , help = 'Delete all expired heartbeats' , action = 'store_true' ) #NEWLINE# parser_heartbeats . set_defaults ( func = cli . heartbeats ) #NEWLINE# parser_user = subparsers . add_parser ( 'user' , help = 'Manage user details (Basic Auth only).' , usage = 'alerta [OPTIONS] user --user-name USER [--password PASSWORD]' ) #NEWLINE# parser_user . add_argument ( '-u' , '--user-name' , dest = 'user' , required = True , help = 'User name' ) #NEWLINE# parser_user . add_argument ( '-p' , '--password' , help = 'New password' ) #NEWLINE# parser_user . set_defaults ( func = cli . user ) #NEWLINE# parser_users = subparsers . add_parser ( 'users' , help = 'List all users' , usage = 'alerta [OPTIONS] users [-h]' ) #NEWLINE# parser_users . set_defaults ( func = cli . users ) #NEWLINE# parser_key = subparsers . add_parser ( 'key' , help = 'Create API key' , usage = 'alerta [OPTIONS] key [-u USER] [--readonly] [--customer CUSTOMER|--no-customer] [-t TEXT]\n' ) #NEWLINE# parser_key . add_argument ( '-u' , '--user-name' , dest = 'user' , help = 'User name' ) #NEWLINE# parser_key . add_argument ( '-O' , '--readonly' , help = 'read only API key' , action = 'store_true' , default = False ) #NEWLINE# parser_key . add_argument ( '--customer' , help = 'customer view' ) #NEWLINE# parser_key . add_argument ( '--no-customer' , help = 'do not associate with customer' , action = 'store_true' , default = False ) #NEWLINE# parser_key . add_argument ( '-t' , '--text' , help = 'text' ) #NEWLINE# parser_key . set_defaults ( func = cli . key ) #NEWLINE# parser_keys = subparsers . add_parser ( 'keys' , help = 'List all API keys' , usage = 'alerta [OPTIONS] keys [-h]' ) #NEWLINE# parser_keys . set_defaults ( func = cli . keys ) #NEWLINE# parser_revoke = subparsers . add_parser ( 'revoke' , help = 'Revoke API key' , usage = 'alerta [OPTIONS] revoke [--api-key KEY]' ) #NEWLINE# parser_revoke . add_argument ( '-K' , '--api-key' , required = True , help = 'API key to be revoked.' ) #NEWLINE# parser_revoke . set_defaults ( func = cli . revoke ) #NEWLINE# parser_status = subparsers . add_parser ( 'status' , help = 'Show status and metrics' , usage = 'alerta [OPTIONS] status [-h]' ) #NEWLINE# parser_status . set_defaults ( func = cli . status ) #NEWLINE# parser_uptime = subparsers . add_parser ( 'uptime' , help = 'Show server uptime' , usage = 'alerta [OPTIONS] uptime [-h]' ) #NEWLINE# parser_uptime . set_defaults ( func = cli . uptime ) #NEWLINE# parser_version = subparsers . add_parser ( 'version' , help = 'Show alerta version info' , add_help = False ) #NEWLINE# parser_version . set_defaults ( func = cli . version ) #NEWLINE# parser_help = subparsers . add_parser ( 'help' , help = 'Show this help' , add_help = False ) #NEWLINE# parser_help . set_defaults ( func = cli . help ) #NEWLINE# args = parser . parse_args ( ) #NEWLINE# if ( args . func == cli . help ) : #NEWLINE# #INDENT# parser . print_help ( ) #NEWLINE# sys . exit ( 0 ) #NEWLINE# #UNINDENT# if args . debug : #NEWLINE# #INDENT# root . setLevel ( logging . DEBUG ) #NEWLINE# LOG . setLevel ( logging . DEBUG ) #NEWLINE# LOG . debug ( 'Alerta cli version: %s' , __version__ ) #NEWLINE# #UNINDENT# if ( hasattr ( args , 'ids' ) and parser_keys . ids ) : #NEWLINE# #INDENT# args . filters += [ ( 'id=' + i ) for i in args . ids ] #NEWLINE# #UNINDENT# args . output = ( 'json' if args . json else args . output ) #NEWLINE# cli . set ( endpoint = args . endpoint , key = args . key ) #NEWLINE# args . func ( args )