#NEWLINE# def heartbeats( self , args ) : #NEWLINE# #INDENT# response = self . _heartbeats ( ) #NEWLINE# heartbeats = response [ 'heartbeats' ] #NEWLINE# print ( '{:<28} {:<26} {:<19} {:>8} {:7} {:17} {:7}' . format ( 'ORIGIN' , 'TAGS' , 'CREATED' , 'LATENCY' , 'TIMEOUT' , 'SINCE' , 'STATUS' ) ) #NEWLINE# for heartbeat in heartbeats : #NEWLINE# #INDENT# hb = HeartbeatDocument . parse_heartbeat ( heartbeat ) #NEWLINE# latency = ( ( hb . receive_time - hb . create_time ) . microseconds / 1000 ) #NEWLINE# since = ( datetime . utcnow ( ) - hb . receive_time ) #NEWLINE# since = ( since - timedelta ( microseconds = since . microseconds ) ) #NEWLINE# latency_exceeded = ( latency > MAX_LATENCY ) #NEWLINE# timeout_exceeded = ( since . seconds > hb . timeout ) #NEWLINE# def status( ) : #NEWLINE# #INDENT# if ( args . purge and ( latency_exceeded or timeout_exceeded ) ) : #NEWLINE# #INDENT# r = self . api . delete_heartbeat ( heartbeat [ 'id' ] ) #NEWLINE# if ( r [ 'status' ] == 'ok' ) : #NEWLINE# #INDENT# return 'deleted' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return 'error' #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if latency_exceeded : #NEWLINE# #INDENT# return 'slow' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if timeout_exceeded : #NEWLINE# #INDENT# return 'stale' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return 'ok' #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# print ( '{:<28} {:<26} {:19} {:6}ms {:6}s {:17} {:7}' . format ( hb . origin , ' ' . join ( response . tags ) , hb . get_date ( 'create_time' , 'local' , args . timezone ) , latency , hb . timeout , since , status ( ) ) ) #NEWLINE# if args . alert : #NEWLINE# #INDENT# if timeout_exceeded : #NEWLINE# #INDENT# alert = Alert ( resource = hb . origin , event = 'HeartbeatFail' , correlate = [ 'HeartbeatFail' , 'HeartbeatSlow' , 'HeartbeatOK' ] , group = 'System' , environment = 'Production' , service = [ 'Alerta' ] , severity = 'major' , value = '{}' . format ( since ) , text = 'Heartbeat not received in {} seconds' . format ( hb . timeout ) , tags = hb . tags , type = 'heartbeatAlert' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if latency_exceeded : #NEWLINE# #INDENT# alert = Alert ( resource = hb . origin , event = 'HeartbeatSlow' , correlate = [ 'HeartbeatFail' , 'HeartbeatSlow' , 'HeartbeatOK' ] , group = 'System' , environment = 'Production' , service = [ 'Alerta' ] , severity = 'major' , value = '{}ms' . format ( latency ) , text = 'Heartbeat took more than {}ms to be processed' . format ( MAX_LATENCY ) , tags = hb . tags , type = 'heartbeatAlert' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# alert = Alert ( resource = hb . origin , event = 'HeartbeatOK' , correlate = [ 'HeartbeatFail' , 'HeartbeatSlow' , 'HeartbeatOK' ] , group = 'System' , environment = 'Production' , service = [ 'Alerta' ] , severity = 'normal' , value = '' , text = 'Heartbeat OK' , tags = hb . tags , type = 'heartbeatAlert' ) #NEWLINE# #UNINDENT# #UNINDENT# self . send ( alert )