<s> import six <EOL> import sys <EOL> import json <EOL> import eventlet <EOL> import requests <EOL> from flask import request , Flask <EOL> from six . moves import urllib_parse <EOL> from st2reactor . sensor . base import Sensor <EOL> eventlet . monkey_patch ( <EOL> os = True , <EOL> select = True , <EOL> socket = True , <EOL> thread = False if '<STR_LIT>' in sys . argv else True , <EOL> time = True ) <EOL> PACK = '<STR_LIT>' <EOL> WEB_APP_ALERT_TRIGGER_REF = '<STR_LIT>' . format ( PACK , '<STR_LIT>' ) <EOL> WEB_APP_NORMAL_TRIGGER_REF = '<STR_LIT>' . format ( PACK , '<STR_LIT>' ) <EOL> SERVER_ALERT_TRIGGER_REF = '<STR_LIT>' . format ( PACK , '<STR_LIT>' ) <EOL> SERVER_NORMAL_TRIGGER_REF = '<STR_LIT>' . format ( PACK , '<STR_LIT>' ) <EOL> NR_API_URL_KEY = '<STR_LIT>' <EOL> NR_API_KEY_KEY = '<STR_LIT>' <EOL> APP_HOST_KEY = '<STR_LIT:host>' <EOL> APP_PORT_KEY = '<STR_LIT:port>' <EOL> APP_URL_KEY = '<STR_LIT:url>' <EOL> NORMAL_REPORT_DELAY_KEY = '<STR_LIT>' <EOL> class LegacyNewRelicHookSensor ( Sensor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sensor_service , config = None ) : <EOL> self . _config = config <EOL> self . _sensor_service = sensor_service <EOL> self . _api_url = config . get ( NR_API_URL_KEY , None ) <EOL> self . _api_key = config . get ( NR_API_KEY_KEY , None ) <EOL> self . _host = self . _get_sensor_config_param ( self . _config , APP_HOST_KEY ) <EOL> self . _port = self . _get_sensor_config_param ( self . _config , APP_PORT_KEY ) <EOL> self . _url = self . _get_sensor_config_param ( self . _config , APP_URL_KEY ) <EOL> self . _normal_report_delay = self . _get_sensor_config_param ( self . _config , <EOL> NORMAL_REPORT_DELAY_KEY , <NUM_LIT> ) <EOL> self . _app = Flask ( __name__ ) <EOL> self . _log = self . _sensor_service . get_logger ( __name__ ) <EOL> self . _headers = { '<STR_LIT>' : self . _api_key } <EOL> def setup ( self ) : <EOL> pass <EOL> def run ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _api_url : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if not self . _api_key : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> if not self . _host or not self . _port or not self . _url : <EOL> raise Exception ( '<STR_LIT>' % <EOL> ( self . _host , self . _port , self . _url ) ) <EOL> self . _log . info ( '<STR_LIT>' , self . _host , <EOL> self . _port , self . _url ) <EOL> @ self . _app . route ( self . _url , methods = [ '<STR_LIT:POST>' ] ) <EOL> def handle_nrhook ( ) : <EOL> data = request . form <EOL> alert_body = data . get ( '<STR_LIT>' , None ) <EOL> if not alert_body : <EOL> self . _log . info ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> try : <EOL> alert_body = json . loads ( alert_body ) <EOL> except Exception : <EOL> self . _log . exception ( '<STR_LIT>' % ( alert_body ) ) <EOL> return '<STR_LIT>' <EOL> if alert_body . get ( '<STR_LIT>' , None ) not in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . _log . debug ( '<STR_LIT>' , alert_body ) <EOL> return '<STR_LIT>' <EOL> hook_headers = self . _get_headers_as_dict ( request . headers ) <EOL> hook_handler = self . _get_hook_handler ( alert_body , hook_headers ) <EOL> try : <EOL> if hook_handler : <EOL> hook_handler ( alert_body , hook_headers ) <EOL> except Exception : <EOL> self . _log . exception ( '<STR_LIT>' , alert_body ) <EOL> return '<STR_LIT>' <EOL> self . _app . run ( host = self . _host , port = self . _port ) <EOL> def _get_hook_handler ( self , alert_body , hook_headers ) : <EOL> if not alert_body : <EOL> return None <EOL> if '<STR_LIT>' in alert_body : <EOL> return self . _server_hook_handler <EOL> return self . _app_hook_handler <EOL> def _app_hook_handler ( self , alert_body , hook_headers ) : <EOL> if not alert_body [ '<STR_LIT>' ] : <EOL> self . _log . info ( '<STR_LIT>' , alert_body ) <EOL> return <EOL> long_description = alert_body [ '<STR_LIT>' ] <EOL> if self . _is_alert_opened ( long_description ) or self . _is_escalated_downtime ( long_description ) : <EOL> payload = { <EOL> '<STR_LIT>' : alert_body , <EOL> '<STR_LIT>' : hook_headers <EOL> } <EOL> self . _dispatch_trigger ( WEB_APP_ALERT_TRIGGER_REF , payload ) <EOL> elif ( self . _is_alert_closed ( long_description ) or <EOL> self . _is_downtime_recovered ( long_description ) ) : <EOL> payload = { <EOL> '<STR_LIT>' : alert_body , <EOL> '<STR_LIT>' : hook_headers <EOL> } <EOL> self . _log . info ( '<STR_LIT>' ) <EOL> eventlet . spawn_after ( self . _normal_report_delay , self . _dispatch_application_normal , <EOL> payload ) <EOL> elif ( self . _is_alert_canceled ( long_description ) or <EOL> self . _is_alert_acknowledged ( long_description ) ) : <EOL> self . _log . info ( '<STR_LIT>' , alert_body ) <EOL> def _dispatch_application_normal ( self , payload , attempt_no = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> if attempt_no == <NUM_LIT:10> : <EOL> self . _log . warning ( '<STR_LIT>' , payload ) <EOL> return <EOL> try : <EOL> application = self . _get_application ( payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> if application [ '<STR_LIT>' ] in [ '<STR_LIT>' ] : <EOL> self . _dispatch_trigger ( WEB_APP_NORMAL_TRIGGER_REF , payload ) <EOL> else : <EOL> self . _log . info ( '<STR_LIT>' , <EOL> application [ '<STR_LIT:name>' ] , application [ '<STR_LIT>' ] ) <EOL> eventlet . spawn_after ( self . _normal_report_delay , self . _dispatch_application_normal , <EOL> payload , attempt_no + <NUM_LIT:1> ) <EOL> except Exception : <EOL> self . _log . exception ( '<STR_LIT>' , payload ) <EOL> def _server_hook_handler ( self , alert_body , hook_headers ) : <EOL> long_description = alert_body [ '<STR_LIT>' ] <EOL> if self . _is_alert_opened ( long_description ) or self . _is_escalated_downtime ( long_description ) : <EOL> payload = { <EOL> '<STR_LIT>' : alert_body , <EOL> '<STR_LIT>' : hook_headers <EOL> } <EOL> self . _dispatch_trigger ( SERVER_ALERT_TRIGGER_REF , payload ) <EOL> elif ( self . _is_alert_closed ( long_description ) or <EOL> self . _is_downtime_recovered ( long_description ) ) : <EOL> payload = { <EOL> '<STR_LIT>' : alert_body , <EOL> '<STR_LIT>' : hook_headers <EOL> } <EOL> self . _log . info ( '<STR_LIT>' ) <EOL> eventlet . spawn_after ( self . _normal_report_delay , self . _dispatch_server_normal , <EOL> payload ) <EOL> elif ( self . _is_alert_canceled ( long_description ) or <EOL> self . _is_alert_acknowledged ( long_description ) ) : <EOL> self . _log . info ( '<STR_LIT>' , alert_body ) <EOL> def _dispatch_server_normal ( self , payload , attempt_no = <NUM_LIT:0> ) : <EOL> '''<STR_LIT>''' <EOL> if attempt_no == <NUM_LIT:10> : <EOL> self . _log . warning ( '<STR_LIT>' , payload ) <EOL> return <EOL> try : <EOL> servers = self . _get_servers ( payload [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> all_servers_ok = True <EOL> for name , server in six . iteritems ( servers ) : <EOL> all_servers_ok &= server [ '<STR_LIT>' ] in [ '<STR_LIT>' ] <EOL> if not all_servers_ok : <EOL> break <EOL> if all_servers_ok : <EOL> self . _dispatch_trigger ( SERVER_NORMAL_TRIGGER_REF , payload ) <EOL> else : <EOL> for server in servers : <EOL> self . _log . info ( '<STR_LIT>' , <EOL> server [ '<STR_LIT:name>' ] , server [ '<STR_LIT>' ] ) <EOL> eventlet . spawn_after ( self . _normal_report_delay , self . _dispatch_server_normal , <EOL> payload , attempt_no + <NUM_LIT:1> ) <EOL> except : <EOL> self . _log . exception ( '<STR_LIT>' , payload ) <EOL> def _dispatch_trigger ( self , trigger , payload ) : <EOL> self . _sensor_service . dispatch ( trigger , payload ) <EOL> self . _log . info ( '<STR_LIT>' , trigger , payload ) <EOL> def _is_alert_opened ( self , long_description ) : <EOL> return long_description and long_description . startswith ( '<STR_LIT>' ) <EOL> def _is_alert_closed ( self , long_description ) : <EOL> return long_description and long_description . startswith ( '<STR_LIT>' ) <EOL> def _is_alert_canceled ( self , long_description ) : <EOL> return long_description and long_description . startswith ( '<STR_LIT>' ) <EOL> def _is_alert_acknowledged ( self , long_description ) : <EOL> return long_description and long_description . startswith ( '<STR_LIT>' ) <EOL> def _is_escalated_downtime ( self , long_description ) : <EOL> return long_description and long_description . startswith ( '<STR_LIT>' ) <EOL> def _is_downtime_recovered ( self , long_description ) : <EOL> return long_description and long_description . startswith ( '<STR_LIT>' ) <EOL> def _get_application ( self , app_name ) : <EOL> params = None <EOL> if app_name : <EOL> params = { '<STR_LIT>' : app_name } <EOL> url = urllib_parse . urljoin ( self . _api_url , '<STR_LIT>' ) <EOL> resp = requests . get ( url , headers = self . _headers , params = params ) . json ( ) <EOL> if '<STR_LIT>' in resp : <EOL> return resp [ '<STR_LIT>' ] [ <NUM_LIT:0> ] if resp [ '<STR_LIT>' ] else None <EOL> return None <EOL> def _get_servers ( self , server_names ) : <EOL> servers = { } <EOL> for server_name in server_names : <EOL> params = { '<STR_LIT>' : server_name } <EOL> url = urllib_parse . urljoin ( self . _api_url , '<STR_LIT>' ) <EOL> resp = requests . get ( url , headers = self . _headers , params = params ) . json ( ) <EOL> servers [ server_name ] = resp [ '<STR_LIT>' ] [ <NUM_LIT:0> ] if resp [ '<STR_LIT>' ] else None <EOL> return servers <EOL> @ staticmethod <EOL> def _get_sensor_config_param ( config , param_name , default = None ) : <EOL> sensor_config = LegacyNewRelicHookSensor . _get_sensor_config ( config ) <EOL> if sensor_config : <EOL> return sensor_config . get ( param_name , default ) <EOL> return default <EOL> @ staticmethod <EOL> def _get_sensor_config ( config ) : <EOL> return config . get ( '<STR_LIT>' , None ) <EOL> @ staticmethod <EOL> def _get_headers_as_dict ( headers ) : <EOL> headers_dict = { } <EOL> for k , v in headers : <EOL> headers_dict [ k ] = v <EOL> return headers_dict <EOL> def cleanup ( self ) : <EOL> pass <EOL> def add_trigger ( self , trigger ) : <EOL> pass <EOL> def update_trigger ( self , trigger ) : <EOL> pass <EOL> def remove_trigger ( self , trigger ) : <EOL> pass </s>
