<s> import eventlet <EOL> from eventlet . green import urllib , urllib2 <EOL> from stalkerutils . stalkerutils import get_basic_auth <EOL> smtplib = eventlet . import_patched ( '<STR_LIT>' ) <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> class PagerDuty ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , conf , logger , redis_client ) : <EOL> self . conf = conf <EOL> self . logger = logger <EOL> self . rc = redis_client <EOL> self . service_keys = { } <EOL> for e in conf . keys ( ) : <EOL> if ( e . startswith ( "<STR_LIT>" ) and e . endswith ( "<STR_LIT>" ) ) : <EOL> pagerduty_id = int ( conf [ e ] ) <EOL> pagerduty_key = conf [ e . replace ( "<STR_LIT>" , "<STR_LIT>" ) ] <EOL> self . service_keys [ pagerduty_id ] = pagerduty_key <EOL> if len ( self . service_keys ) < <NUM_LIT:1> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . url = conf . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . prefix = conf . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> def _resolve ( self , check , incident_key , priority ) : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> data = json . dumps ( { '<STR_LIT>' : self . service_keys [ priority ] , <EOL> '<STR_LIT>' : incident_key , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' % ( check [ '<STR_LIT>' ] , <EOL> check [ <EOL> '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : check } ) <EOL> try : <EOL> req = urllib2 . Request ( self . url , data , headers ) <EOL> response = urllib2 . urlopen ( req , timeout = <NUM_LIT:10> ) <EOL> result = json . loads ( response . read ( ) ) <EOL> response . close ( ) <EOL> if '<STR_LIT:status>' in result : <EOL> if result [ '<STR_LIT:status>' ] == '<STR_LIT:success>' : <EOL> self . logger . info ( '<STR_LIT>' % result ) <EOL> return True <EOL> else : <EOL> self . logger . info ( <EOL> '<STR_LIT>' % result ) <EOL> return False <EOL> else : <EOL> self . logger . info ( <EOL> '<STR_LIT>' % result ) <EOL> return False <EOL> except Exception : <EOL> self . logger . exception ( '<STR_LIT>' ) <EOL> return False <EOL> def _trigger ( self , check , incident_key , priority ) : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> data = json . dumps ( { '<STR_LIT>' : self . service_keys [ priority ] , <EOL> '<STR_LIT>' : incident_key , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' % <EOL> ( check [ '<STR_LIT>' ] , check [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : check } ) <EOL> try : <EOL> req = urllib2 . Request ( self . url , data , headers ) <EOL> response = urllib2 . urlopen ( req , timeout = <NUM_LIT:10> ) <EOL> result = json . loads ( response . read ( ) ) <EOL> response . close ( ) <EOL> if '<STR_LIT:status>' in result : <EOL> if result [ '<STR_LIT:status>' ] == '<STR_LIT:success>' : <EOL> self . logger . info ( '<STR_LIT>' % result ) <EOL> return True <EOL> else : <EOL> self . logger . info ( <EOL> '<STR_LIT>' % result ) <EOL> return False <EOL> else : <EOL> self . logger . info ( <EOL> '<STR_LIT>' % result ) <EOL> return False <EOL> except Exception : <EOL> self . logger . exception ( '<STR_LIT>' ) <EOL> return False <EOL> def clear ( self , check ) : <EOL> """<STR_LIT>""" <EOL> priority = check . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> if priority == <NUM_LIT:0> : <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> return <EOL> incident_key = "<STR_LIT>" % ( self . prefix , check [ '<STR_LIT>' ] , <EOL> check [ '<STR_LIT>' ] ) <EOL> check [ '<STR_LIT>' ] = str ( check [ '<STR_LIT>' ] ) <EOL> ok = self . _resolve ( check , incident_key , priority ) <EOL> if not ok : <EOL> pass <EOL> def fail ( self , check ) : <EOL> """<STR_LIT>""" <EOL> priority = check . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> if priority == <NUM_LIT:0> : <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> return <EOL> incident_key = "<STR_LIT>" % ( self . prefix , check [ '<STR_LIT>' ] , <EOL> check [ '<STR_LIT>' ] ) <EOL> check [ '<STR_LIT>' ] = str ( check [ '<STR_LIT>' ] ) <EOL> ok = self . _trigger ( check , incident_key , priority ) <EOL> if not ok : <EOL> pass <EOL> class GenericHTTP ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , conf , logger , redis_client ) : <EOL> self . conf = conf <EOL> self . logger = logger <EOL> self . rc = redis_client <EOL> standard_service_key = conf . get ( '<STR_LIT>' ) <EOL> crit_service_key = standard_service_key <EOL> self . service_keys = { <NUM_LIT:1> : standard_service_key , <NUM_LIT:2> : crit_service_key } <EOL> self . url = conf . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . prefix = conf . get ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> def _resolve ( self , check , incident_key , priority ) : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> data = json . dumps ( { '<STR_LIT>' : self . service_keys [ priority ] , <EOL> '<STR_LIT>' : incident_key , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' % ( check [ '<STR_LIT>' ] , <EOL> check [ <EOL> '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : check } ) <EOL> try : <EOL> req = urllib2 . Request ( self . url , data , headers ) <EOL> response = urllib2 . urlopen ( req , timeout = <NUM_LIT:10> ) <EOL> result = json . loads ( response . read ( ) ) <EOL> response . close ( ) <EOL> if '<STR_LIT:status>' in result : <EOL> if result [ '<STR_LIT:status>' ] == '<STR_LIT:success>' : <EOL> self . logger . info ( '<STR_LIT>' % result ) <EOL> return True <EOL> else : <EOL> self . logger . info ( <EOL> '<STR_LIT>' % result ) <EOL> return False <EOL> else : <EOL> self . logger . info ( <EOL> '<STR_LIT>' % result ) <EOL> return False <EOL> except Exception : <EOL> self . logger . exception ( '<STR_LIT>' ) <EOL> return False <EOL> def _trigger ( self , check , incident_key , priority ) : <EOL> headers = { '<STR_LIT:Content-Type>' : '<STR_LIT:application/json>' } <EOL> data = json . dumps ( { '<STR_LIT>' : self . service_keys [ priority ] , <EOL> '<STR_LIT>' : incident_key , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:description>' : '<STR_LIT>' % <EOL> ( check [ '<STR_LIT>' ] , check [ '<STR_LIT>' ] ) , <EOL> '<STR_LIT>' : check } ) <EOL> try : <EOL> req = urllib2 . Request ( self . url , data , headers ) <EOL> response = urllib2 . urlopen ( req , timeout = <NUM_LIT:10> ) <EOL> result = json . loads ( response . read ( ) ) <EOL> response . close ( ) <EOL> if '<STR_LIT:status>' in result : <EOL> if result [ '<STR_LIT:status>' ] == '<STR_LIT:success>' : <EOL> self . logger . info ( '<STR_LIT>' % result ) <EOL> return True <EOL> else : <EOL> self . logger . info ( <EOL> '<STR_LIT>' % result ) <EOL> return False <EOL> else : <EOL> self . logger . info ( <EOL> '<STR_LIT>' % result ) <EOL> return False <EOL> except Exception : <EOL> self . logger . exception ( '<STR_LIT>' ) <EOL> return False <EOL> def clear ( self , check ) : <EOL> """<STR_LIT>""" <EOL> priority = check . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> if priority == <NUM_LIT:0> : <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> return <EOL> incident_key = "<STR_LIT>" % ( self . prefix , check [ '<STR_LIT>' ] , <EOL> check [ '<STR_LIT>' ] ) <EOL> check [ '<STR_LIT>' ] = str ( check [ '<STR_LIT>' ] ) <EOL> ok = self . _resolve ( check , incident_key , priority ) <EOL> if not ok : <EOL> pass <EOL> def fail ( self , check ) : <EOL> """<STR_LIT>""" <EOL> priority = check . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> if priority == <NUM_LIT:0> : <EOL> self . logger . info ( '<STR_LIT>' ) <EOL> return <EOL> incident_key = "<STR_LIT>" % ( self . prefix , check [ '<STR_LIT>' ] , <EOL> check [ '<STR_LIT>' ] ) <EOL> check [ '<STR_LIT>' ] = str ( check [ '<STR_LIT>' ] ) <EOL> ok = self . _trigger ( check , incident_key , priority ) <EOL> if not ok : <EOL> pass <EOL> class Mailgun ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , conf , logger , redis_client ) : <EOL> self . conf = conf <EOL> self . logger = logger <EOL> self . rc = redis_client <EOL> self . domain = conf . get ( '<STR_LIT>' ) <EOL> if not self . domain : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . api_user = '<STR_LIT>' <EOL> self . api_key = conf . get ( '<STR_LIT>' ) <EOL> if not self . api_key : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . url = '<STR_LIT>' % self . domain <EOL> self . recipients = conf . get ( '<STR_LIT>' ) <EOL> self . from_addr = conf . get ( '<STR_LIT>' ) <EOL> if not self . recipients : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . basic_auth_creds = get_basic_auth ( self . api_user , self . api_key ) <EOL> def _send_email ( self , check ) : <EOL> check_name = check [ '<STR_LIT>' ] <EOL> hostname = check [ '<STR_LIT>' ] <EOL> if check [ '<STR_LIT:status>' ] is True : <EOL> status = '<STR_LIT>' <EOL> else : <EOL> status = '<STR_LIT>' <EOL> subject = "<STR_LIT>" % ( check_name , hostname , status ) <EOL> data = { "<STR_LIT>" : "<STR_LIT>" % self . from_addr , <EOL> "<STR_LIT:to>" : self . recipients , <EOL> "<STR_LIT>" : subject , <EOL> "<STR_LIT:text>" : "<STR_LIT:%s>" % check } <EOL> headers = { <EOL> '<STR_LIT>' : '<STR_LIT>' % self . basic_auth_creds , <EOL> '<STR_LIT:Content-Type>' : '<STR_LIT>' <EOL> } <EOL> try : <EOL> post_data = urllib . urlencode ( data ) <EOL> req = urllib2 . Request ( self . url , post_data , headers ) <EOL> response = urllib2 . urlopen ( req ) <EOL> result = response . read ( ) <EOL> response . close ( ) <EOL> self . logger . info ( '<STR_LIT>' % result ) <EOL> return True <EOL> except Exception : <EOL> self . logger . exception ( '<STR_LIT>' ) <EOL> return False <EOL> def clear ( self , check ) : <EOL> """<STR_LIT>""" <EOL> incident_key = '<STR_LIT>' % ( check [ '<STR_LIT>' ] , check [ '<STR_LIT>' ] ) <EOL> ok = self . _send_email ( check ) <EOL> self . logger . info ( '<STR_LIT>' % incident_key ) <EOL> if not ok : <EOL> pass <EOL> def fail ( self , check ) : <EOL> """<STR_LIT>""" <EOL> incident_key = '<STR_LIT>' % ( check [ '<STR_LIT>' ] , check [ '<STR_LIT>' ] ) <EOL> ok = self . _send_email ( check ) <EOL> if ok : <EOL> self . logger . info ( '<STR_LIT>' % incident_key ) <EOL> else : <EOL> pass <EOL> class EmailNotify ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , conf , logger , redis_client ) : <EOL> self . conf = conf <EOL> self . logger = logger <EOL> self . rc = redis_client <EOL> self . smtp_host = conf . get ( '<STR_LIT>' ) <EOL> if not self . smtp_host : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . smtp_port = int ( conf . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . from_addr = conf . get ( '<STR_LIT>' ) <EOL> if not self . from_addr : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> self . recipients = [ x . strip ( ) for x in conf . get ( <EOL> '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) ] <EOL> if not self . recipients : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def _send_email ( self , check ) : <EOL> check_name = check [ '<STR_LIT>' ] <EOL> hostname = check [ '<STR_LIT>' ] <EOL> if check [ '<STR_LIT:status>' ] is True : <EOL> status = '<STR_LIT>' <EOL> else : <EOL> status = '<STR_LIT>' <EOL> subject = "<STR_LIT>" % ( check_name , hostname , status ) <EOL> message = """<STR_LIT>""" % ( self . from_addr , self . recipients , subject , check ) <EOL> try : <EOL> conn = smtplib . SMTP ( self . smtp_host , self . smtp_port ) <EOL> conn . ehlo ( ) <EOL> conn . sendmail ( self . from_addr , self . recipients , message ) <EOL> conn . close ( ) <EOL> self . logger . info ( '<STR_LIT>' % check ) <EOL> return True <EOL> except Exception : <EOL> self . logger . exception ( '<STR_LIT>' ) <EOL> return False <EOL> def clear ( self , check ) : <EOL> """<STR_LIT>""" <EOL> incident_key = '<STR_LIT>' % ( check [ '<STR_LIT>' ] , check [ '<STR_LIT>' ] ) <EOL> ok = self . _send_email ( check ) <EOL> self . logger . info ( '<STR_LIT>' % incident_key ) <EOL> if not ok : <EOL> pass <EOL> def fail ( self , check ) : <EOL> """<STR_LIT>""" <EOL> incident_key = '<STR_LIT>' % ( check [ '<STR_LIT>' ] , check [ '<STR_LIT>' ] ) <EOL> ok = self . _send_email ( check ) <EOL> if ok : <EOL> self . logger . info ( '<STR_LIT>' % incident_key ) <EOL> else : <EOL> pass </s>
