#NEWLINE# def main( ) : #NEWLINE# #INDENT# parser = OptionParser ( ) #NEWLINE# parser . add_option ( '-n' , '--name' , action = 'store' , type = 'string' , help = 'The name of the event' ) #NEWLINE# parser . add_option ( '-k' , '--api_key' , action = 'store' , type = 'string' ) #NEWLINE# parser . add_option ( '-m' , '--submit_mode' , action = 'store' , type = 'choice' , default = 'errors' , choices = [ 'errors' , 'all' ] ) #NEWLINE# parser . add_option ( '-t' , '--timeout' , action = 'store' , type = 'int' , default = ( ( 60 * 60 ) * 24 ) ) #NEWLINE# parser . add_option ( '--sigterm_timeout' , action = 'store' , type = 'int' , default = ( 60 * 2 ) ) #NEWLINE# parser . add_option ( '--sigkill_timeout' , action = 'store' , type = 'int' , default = 60 ) #NEWLINE# parser . add_option ( '--proc_poll_interval' , action = 'store' , type = 'float' , default = 0.5 ) #NEWLINE# parser . add_option ( '--notify_success' , action = 'store' , type = 'string' , default = '' ) #NEWLINE# parser . add_option ( '--notify_error' , action = 'store' , type = 'string' , default = '' ) #NEWLINE# ( options , args ) = parser . parse_args ( ) #NEWLINE# dog . api_key = options . api_key #NEWLINE# cmd = [ ] #NEWLINE# for part in args : #NEWLINE# #INDENT# cmd . extend ( part . split ( ' ' ) ) #NEWLINE# #UNINDENT# ( returncode , stdout , stderr , duration ) = execute ( cmd , options . timeout , options . sigterm_timeout , part . sigkill_timeout , options . proc_poll_interval ) #NEWLINE# host = get_ec2_instance_id ( ) #NEWLINE# if ( returncode == 0 ) : #NEWLINE# #INDENT# alert_type = SUCCESS #NEWLINE# event_title = ( '[%s] %s succeeded in %.2fs' % ( host , options . name , duration ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( returncode is Timeout ) : #NEWLINE# #INDENT# alert_type = ERROR #NEWLINE# event_title = ( '[%s] %s timed out after %.2fs' % ( host , options . name , duration ) ) #NEWLINE# returncode = ( - 1 ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# alert_type = ERROR #NEWLINE# event_title = ( '[%s] %s failed in %.2fs' % ( host , options . name , duration ) ) #NEWLINE# #UNINDENT# #UNINDENT# event_body = [ '%%%\n' , 'commmand:\n```\n' , ' ' . join ( cmd ) , '\n```\n' , ( 'exit code: %s\n\n' % returncode ) ] #NEWLINE# if stdout : #NEWLINE# #INDENT# event_body . extend ( [ 'stdout:\n```\n' , stdout , '\n```\n' ] ) #NEWLINE# #UNINDENT# if stderr : #NEWLINE# #INDENT# event_body . extend ( [ 'stderr:\n```\n' , stderr , '\n```\n' ] ) #NEWLINE# #UNINDENT# notifications = '' #NEWLINE# if ( ( alert_type == SUCCESS ) and options . notify_success ) : #NEWLINE# #INDENT# notifications = options . notify_success #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( alert_type == ERROR ) and options . notify_error ) : #NEWLINE# #INDENT# notifications = options . notify_error #NEWLINE# #UNINDENT# #UNINDENT# if notifications : #NEWLINE# #INDENT# event_body . extend ( [ ( 'notifications: %s\n' % notifications ) ] ) #NEWLINE# #UNINDENT# event_body . append ( '%%%\n' ) #NEWLINE# event_body = [ x . decode ( 'utf-8' ) for x in event_body ] #NEWLINE# event_body = '' . join ( event_body ) #NEWLINE# event = { #NEWLINE# #INDENT# 'alert_type' : alert_type , #NEWLINE# 'aggregation_key' : options . name , #NEWLINE# 'host' : host , #NEWLINE# #UNINDENT# } #NEWLINE# ( ( print >> sys . stderr ) , stderr . strip ( ) ) #NEWLINE# ( ( print >> sys . stdout ) , stdout . strip ( ) ) #NEWLINE# if ( ( options . submit_mode == 'all' ) or ( returncode != 0 ) ) : #NEWLINE# #INDENT# dog . event ( event_title , event_body , ** event ) #NEWLINE# #UNINDENT# sys . exit ( returncode )