<s> """<STR_LIT>""" <EOL> import sys <EOL> import socket <EOL> import logging <EOL> import re <EOL> import subprocess <EOL> from StringIO import StringIO <EOL> from optparse import OptionParser , OptionGroup <EOL> __version__ = ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) <EOL> log = logging . getLogger ( ) <EOL> logging . basicConfig ( level = logging . ERROR ) <EOL> class NagiosHandler ( object ) : <EOL> @ classmethod <EOL> def register_options ( cls , parser ) : <EOL> group = OptionGroup ( parser , '<STR_LIT>' ) <EOL> group . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' ) <EOL> group . add_option ( '<STR_LIT:-c>' , '<STR_LIT>' , dest = '<STR_LIT>' ) <EOL> parser . add_option_group ( group ) <EOL> def analyze ( self , opts , cluster_stats ) : <EOL> try : <EOL> warning = int ( opts . warning ) <EOL> critical = int ( opts . critical ) <EOL> except ( TypeError , ValueError ) : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> return <NUM_LIT:2> <EOL> if opts . key is None : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> return <NUM_LIT:2> <EOL> warning_state , critical_state , values = [ ] , [ ] , [ ] <EOL> for host , stats in cluster_stats . items ( ) : <EOL> if opts . key in stats : <EOL> value = stats [ opts . key ] <EOL> values . append ( '<STR_LIT>' % ( host , value , warning , critical ) ) <EOL> if warning >= value > critical or warning <= value < critical : <EOL> warning_state . append ( host ) <EOL> elif ( warning < critical and critical <= value ) or ( warning > critical and critical >= value ) : <EOL> critical_state . append ( host ) <EOL> if not values : <EOL> print '<STR_LIT>' % opts . key <EOL> return <NUM_LIT:2> <EOL> values = '<STR_LIT:U+0020>' . join ( values ) <EOL> if critical_state : <EOL> print '<STR_LIT>' % ( opts . key , '<STR_LIT:U+002CU+0020>' . join ( critical_state ) , values ) <EOL> return <NUM_LIT:2> <EOL> elif warning_state : <EOL> print '<STR_LIT>' % ( opts . key , '<STR_LIT:U+002CU+0020>' . join ( warning_state ) , values ) <EOL> return <NUM_LIT:1> <EOL> else : <EOL> print '<STR_LIT>' % ( opts . key , values ) <EOL> return <NUM_LIT:0> <EOL> class CactiHandler ( object ) : <EOL> @ classmethod <EOL> def register_options ( cls , parser ) : <EOL> group = OptionGroup ( parser , '<STR_LIT>' ) <EOL> group . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = "<STR_LIT:store_true>" , help = "<STR_LIT>" ) <EOL> parser . add_option_group ( group ) <EOL> def analyze ( self , opts , cluster_stats ) : <EOL> if opts . key is None : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> return <NUM_LIT:1> <EOL> if opts . leader is True : <EOL> try : <EOL> leader = [ x for x in cluster_stats . values ( ) if x . get ( '<STR_LIT>' , '<STR_LIT>' ) == '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> return <NUM_LIT:3> <EOL> if opts . key in leader : <EOL> print leader [ opts . key ] <EOL> return <NUM_LIT:0> <EOL> else : <EOL> print >> sys . stderr , '<STR_LIT>' % opts . key <EOL> return <NUM_LIT:2> <EOL> else : <EOL> for host , stats in cluster_stats . items ( ) : <EOL> if opts . key not in stats : <EOL> continue <EOL> host = host . replace ( '<STR_LIT::>' , '<STR_LIT:_>' ) <EOL> print '<STR_LIT>' % ( host , stats [ opts . key ] ) , <EOL> class GangliaHandler ( object ) : <EOL> @ classmethod <EOL> def register_options ( cls , parser ) : <EOL> group = OptionGroup ( parser , '<STR_LIT>' ) <EOL> group . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> default = '<STR_LIT>' , help = '<STR_LIT>' '<STR_LIT>' ) <EOL> parser . add_option_group ( group ) <EOL> def call ( self , * args , ** kwargs ) : <EOL> subprocess . call ( * args , ** kwargs ) <EOL> def analyze ( self , opts , cluster_stats ) : <EOL> if len ( cluster_stats ) != <NUM_LIT:1> : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> return <NUM_LIT:1> <EOL> for host , stats in cluster_stats . items ( ) : <EOL> for k , v in stats . items ( ) : <EOL> try : <EOL> self . call ( [ opts . gmetric , '<STR_LIT>' , k , '<STR_LIT>' , str ( int ( v ) ) , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> except ( TypeError , ValueError ) : <EOL> pass <EOL> class ZooKeeperServer ( object ) : <EOL> def __init__ ( self , host = '<STR_LIT:localhost>' , port = '<STR_LIT>' , timeout = <NUM_LIT:1> ) : <EOL> self . _address = ( host , int ( port ) ) <EOL> self . _timeout = timeout <EOL> def get_stats ( self ) : <EOL> """<STR_LIT>""" <EOL> data = self . _send_cmd ( '<STR_LIT>' ) <EOL> if data : <EOL> return self . _parse ( data ) <EOL> else : <EOL> data = self . _send_cmd ( '<STR_LIT>' ) <EOL> return self . _parse_stat ( data ) <EOL> def _create_socket ( self ) : <EOL> return socket . socket ( ) <EOL> def _send_cmd ( self , cmd ) : <EOL> """<STR_LIT>""" <EOL> s = self . _create_socket ( ) <EOL> s . settimeout ( self . _timeout ) <EOL> s . connect ( self . _address ) <EOL> s . send ( cmd ) <EOL> data = s . recv ( <NUM_LIT> ) <EOL> s . close ( ) <EOL> return data <EOL> def _parse ( self , data ) : <EOL> """<STR_LIT>""" <EOL> h = StringIO ( data ) <EOL> result = { } <EOL> for line in h . readlines ( ) : <EOL> try : <EOL> key , value = self . _parse_line ( line ) <EOL> result [ key ] = value <EOL> except ValueError : <EOL> pass <EOL> return result <EOL> def _parse_stat ( self , data ) : <EOL> """<STR_LIT>""" <EOL> h = StringIO ( data ) <EOL> result = { } <EOL> version = h . readline ( ) <EOL> if version : <EOL> result [ '<STR_LIT>' ] = version [ version . index ( '<STR_LIT::>' ) + <NUM_LIT:1> : ] . strip ( ) <EOL> while h . readline ( ) . strip ( ) : pass <EOL> for line in h . readlines ( ) : <EOL> m = re . match ( '<STR_LIT>' , line ) <EOL> if m is not None : <EOL> result [ '<STR_LIT>' ] = int ( m . group ( <NUM_LIT:1> ) ) <EOL> result [ '<STR_LIT>' ] = int ( m . group ( <NUM_LIT:2> ) ) <EOL> result [ '<STR_LIT>' ] = int ( m . group ( <NUM_LIT:3> ) ) <EOL> continue <EOL> m = re . match ( '<STR_LIT>' , line ) <EOL> if m is not None : <EOL> result [ '<STR_LIT>' ] = int ( m . group ( <NUM_LIT:1> ) ) <EOL> continue <EOL> m = re . match ( '<STR_LIT>' , line ) <EOL> if m is not None : <EOL> result [ '<STR_LIT>' ] = int ( m . group ( <NUM_LIT:1> ) ) <EOL> continue <EOL> m = re . match ( '<STR_LIT>' , line ) <EOL> if m is not None : <EOL> result [ '<STR_LIT>' ] = int ( m . group ( <NUM_LIT:1> ) ) <EOL> continue <EOL> m = re . match ( '<STR_LIT>' , line ) <EOL> if m is not None : <EOL> result [ '<STR_LIT>' ] = m . group ( <NUM_LIT:1> ) <EOL> continue <EOL> m = re . match ( '<STR_LIT>' , line ) <EOL> if m is not None : <EOL> result [ '<STR_LIT>' ] = int ( m . group ( <NUM_LIT:1> ) ) <EOL> continue <EOL> return result <EOL> def _parse_line ( self , line ) : <EOL> try : <EOL> key , value = map ( str . strip , line . split ( '<STR_LIT:\t>' ) ) <EOL> except ValueError : <EOL> raise ValueError ( '<STR_LIT>' % line ) <EOL> if not key : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> try : <EOL> value = int ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> pass <EOL> return key , value <EOL> def main ( ) : <EOL> opts , args = parse_cli ( ) <EOL> cluster_stats = get_cluster_stats ( opts . servers ) <EOL> if opts . output is None : <EOL> dump_stats ( cluster_stats ) <EOL> return <NUM_LIT:0> <EOL> handler = create_handler ( opts . output ) <EOL> if handler is None : <EOL> log . error ( '<STR_LIT>' % opts . output ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return handler . analyze ( opts , cluster_stats ) <EOL> def create_handler ( name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return globals ( ) [ '<STR_LIT>' % name . capitalize ( ) ] ( ) <EOL> except KeyError : <EOL> return None <EOL> def get_all_handlers ( ) : <EOL> """<STR_LIT>""" <EOL> return [ NagiosHandler , CactiHandler , GangliaHandler ] <EOL> def dump_stats ( cluster_stats ) : <EOL> """<STR_LIT>""" <EOL> for server , stats in cluster_stats . items ( ) : <EOL> print '<STR_LIT>' , server <EOL> for key , value in stats . items ( ) : <EOL> print "<STR_LIT>" % key , '<STR_LIT:U+0020>' , value <EOL> print <EOL> def get_cluster_stats ( servers ) : <EOL> """<STR_LIT>""" <EOL> stats = { } <EOL> for host , port in servers : <EOL> try : <EOL> zk = ZooKeeperServer ( host , port ) <EOL> stats [ "<STR_LIT>" % ( host , port ) ] = zk . get_stats ( ) <EOL> except socket . error , e : <EOL> logging . info ( '<STR_LIT>' '<STR_LIT>' % ( host , port ) ) <EOL> return stats <EOL> def get_version ( ) : <EOL> return '<STR_LIT:.>' . join ( map ( str , __version__ ) ) <EOL> def parse_cli ( ) : <EOL> parser = OptionParser ( usage = '<STR_LIT>' , version = get_version ( ) ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , metavar = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , metavar = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:key>' ) <EOL> for handler in get_all_handlers ( ) : <EOL> handler . register_options ( parser ) <EOL> opts , args = parser . parse_args ( ) <EOL> if opts . servers is None : <EOL> parser . error ( '<STR_LIT>' ) <EOL> opts . servers = [ s . split ( '<STR_LIT::>' ) for s in opts . servers . split ( '<STR_LIT:U+002C>' ) ] <EOL> return ( opts , args ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
