<s> import subprocess <EOL> from graphite_beacon . handlers import AbstractHandler , LOGGER <EOL> class CliHandler ( AbstractHandler ) : <EOL> name = '<STR_LIT>' <EOL> defaults = { <EOL> '<STR_LIT>' : None , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> def init_handler ( self ) : <EOL> self . commandTemplate = self . options . get ( '<STR_LIT>' ) <EOL> self . whitelist = self . options . get ( '<STR_LIT>' ) <EOL> assert self . commandTemplate , '<STR_LIT>' <EOL> def notify ( self , level , * args , ** kwargs ) : <EOL> LOGGER . debug ( "<STR_LIT>" , self . name , level ) <EOL> def getAlertName ( * args ) : <EOL> name = str ( args [ <NUM_LIT:0> ] ) <EOL> return name . rsplit ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] . strip ( ) <EOL> if not self . whitelist or getAlertName ( * args ) in self . whitelist : <EOL> command = substituteVariables ( self . commandTemplate , level , * args , ** kwargs ) <EOL> subprocess . Popen ( <EOL> command , <EOL> shell = True , <EOL> stdin = None , <EOL> stdout = None , <EOL> stderr = None , <EOL> close_fds = True ) <EOL> def substituteVariables ( command , level , name , value , target = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> rule = kwargs . get ( '<STR_LIT>' , { } ) <EOL> rule_value = rule . get ( '<STR_LIT:value>' , '<STR_LIT>' ) if rule else '<STR_LIT>' <EOL> substitutes = { <EOL> '<STR_LIT>' : str ( level ) , <EOL> '<STR_LIT>' : str ( target ) , <EOL> '<STR_LIT>' : '<STR_LIT:">' + str ( name ) + '<STR_LIT:">' , <EOL> '<STR_LIT>' : str ( value ) , <EOL> '<STR_LIT>' : str ( rule_value ) , <EOL> } <EOL> result = command <EOL> for pattern , value in substitutes . items ( ) : <EOL> result = result . replace ( pattern , value ) <EOL> return result </s>
