<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import pwd <EOL> import errno <EOL> from os . path import join , dirname , normpath , exists , isdir <EOL> from optparse import OptionParser <EOL> from ConfigParser import ConfigParser <EOL> from carbon import log , state <EOL> from carbon . database import TimeSeriesDatabase <EOL> from carbon . exceptions import CarbonConfigException <EOL> from twisted . python import usage <EOL> defaults = dict ( <EOL> USER = "<STR_LIT>" , <EOL> MAX_CACHE_SIZE = float ( '<STR_LIT>' ) , <EOL> MAX_UPDATES_PER_SECOND = <NUM_LIT> , <EOL> MAX_CREATES_PER_MINUTE = float ( '<STR_LIT>' ) , <EOL> LINE_RECEIVER_INTERFACE = '<STR_LIT>' , <EOL> LINE_RECEIVER_PORT = <NUM_LIT> , <EOL> ENABLE_UDP_LISTENER = False , <EOL> UDP_RECEIVER_INTERFACE = '<STR_LIT>' , <EOL> UDP_RECEIVER_PORT = <NUM_LIT> , <EOL> PICKLE_RECEIVER_INTERFACE = '<STR_LIT>' , <EOL> PICKLE_RECEIVER_PORT = <NUM_LIT> , <EOL> MAX_RECEIVER_CONNECTIONS = float ( '<STR_LIT>' ) , <EOL> CACHE_QUERY_INTERFACE = '<STR_LIT>' , <EOL> CACHE_QUERY_PORT = <NUM_LIT> , <EOL> LOG_UPDATES = True , <EOL> LOG_CACHE_HITS = True , <EOL> LOG_CACHE_QUEUE_SORTS = True , <EOL> DATABASE = '<STR_LIT>' , <EOL> WHISPER_AUTOFLUSH = False , <EOL> WHISPER_SPARSE_CREATE = False , <EOL> WHISPER_FALLOCATE_CREATE = False , <EOL> WHISPER_LOCK_WRITES = False , <EOL> WHISPER_FADVISE_RANDOM = False , <EOL> MAX_DATAPOINTS_PER_MESSAGE = <NUM_LIT> , <EOL> MAX_AGGREGATION_INTERVALS = <NUM_LIT:5> , <EOL> FORWARD_ALL = True , <EOL> MAX_QUEUE_SIZE = <NUM_LIT:1000> , <EOL> QUEUE_LOW_WATERMARK_PCT = <NUM_LIT> , <EOL> TIME_TO_DEFER_SENDING = <NUM_LIT> , <EOL> ENABLE_AMQP = False , <EOL> AMQP_VERBOSE = False , <EOL> AMQP_SPEC = None , <EOL> BIND_PATTERNS = [ '<STR_LIT:#>' ] , <EOL> ENABLE_MANHOLE = False , <EOL> MANHOLE_INTERFACE = '<STR_LIT:127.0.0.1>' , <EOL> MANHOLE_PORT = <NUM_LIT> , <EOL> MANHOLE_USER = "<STR_LIT>" , <EOL> MANHOLE_PUBLIC_KEY = "<STR_LIT>" , <EOL> RELAY_METHOD = '<STR_LIT>' , <EOL> REPLICATION_FACTOR = <NUM_LIT:1> , <EOL> DIVERSE_REPLICAS = True , <EOL> DESTINATIONS = [ ] , <EOL> USE_FLOW_CONTROL = True , <EOL> USE_INSECURE_UNPICKLER = False , <EOL> USE_WHITELIST = False , <EOL> CARBON_METRIC_PREFIX = '<STR_LIT>' , <EOL> CARBON_METRIC_INTERVAL = <NUM_LIT> , <EOL> CACHE_WRITE_STRATEGY = '<STR_LIT>' , <EOL> WRITE_BACK_FREQUENCY = None , <EOL> MIN_RESET_STAT_FLOW = <NUM_LIT:1000> , <EOL> MIN_RESET_RATIO = <NUM_LIT> , <EOL> MIN_RESET_INTERVAL = <NUM_LIT> , <EOL> USE_RATIO_RESET = False , <EOL> LOG_LISTENER_CONN_SUCCESS = True , <EOL> LOG_AGGREGATOR_MISSES = True , <EOL> AGGREGATION_RULES = '<STR_LIT>' , <EOL> REWRITE_RULES = '<STR_LIT>' , <EOL> RELAY_RULES = '<STR_LIT>' , <EOL> ENABLE_LOGROTATION = True , <EOL> METRIC_CLIENT_IDLE_TIMEOUT = None <EOL> ) <EOL> def _umask ( value ) : <EOL> return int ( value , <NUM_LIT:8> ) <EOL> def _process_alive ( pid ) : <EOL> if exists ( "<STR_LIT>" ) : <EOL> return exists ( "<STR_LIT>" % pid ) <EOL> else : <EOL> try : <EOL> os . kill ( int ( pid ) , <NUM_LIT:0> ) <EOL> return True <EOL> except OSError , err : <EOL> return err . errno == errno . EPERM <EOL> class OrderedConfigParser ( ConfigParser ) : <EOL> """<STR_LIT>""" <EOL> _ordered_sections = [ ] <EOL> def read ( self , path ) : <EOL> if not os . access ( path , os . R_OK ) : <EOL> raise CarbonConfigException ( "<STR_LIT>" % path ) <EOL> result = ConfigParser . read ( self , path ) <EOL> sections = [ ] <EOL> for line in open ( path ) : <EOL> line = line . strip ( ) <EOL> if line . startswith ( '<STR_LIT:[>' ) and line . endswith ( '<STR_LIT:]>' ) : <EOL> sections . append ( line [ <NUM_LIT:1> : - <NUM_LIT:1> ] ) <EOL> self . _ordered_sections = sections <EOL> return result <EOL> def sections ( self ) : <EOL> return list ( self . _ordered_sections ) <EOL> class Settings ( dict ) : <EOL> __getattr__ = dict . __getitem__ <EOL> def __init__ ( self ) : <EOL> dict . __init__ ( self ) <EOL> self . update ( defaults ) <EOL> def readFrom ( self , path , section ) : <EOL> parser = ConfigParser ( ) <EOL> if not parser . read ( path ) : <EOL> raise CarbonConfigException ( "<STR_LIT>" % path ) <EOL> if not parser . has_section ( section ) : <EOL> return <EOL> for key , value in parser . items ( section ) : <EOL> key = key . upper ( ) <EOL> if key in defaults : <EOL> valueType = type ( defaults [ key ] ) <EOL> else : <EOL> valueType = str <EOL> if valueType is list : <EOL> value = [ v . strip ( ) for v in value . split ( '<STR_LIT:U+002C>' ) ] <EOL> elif valueType is bool : <EOL> value = parser . getboolean ( section , key ) <EOL> else : <EOL> try : <EOL> value = int ( value ) <EOL> except ValueError : <EOL> try : <EOL> value = float ( value ) <EOL> except ValueError : <EOL> pass <EOL> self [ key ] = value <EOL> settings = Settings ( ) <EOL> settings . update ( defaults ) <EOL> class CarbonCacheOptions ( usage . Options ) : <EOL> optFlags = [ <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> ] <EOL> optParameters = [ <EOL> [ "<STR_LIT>" , "<STR_LIT:c>" , None , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:a>" , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , None , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , None , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , None , "<STR_LIT>" ] , <EOL> ] <EOL> def postOptions ( self ) : <EOL> global settings <EOL> program = self . parent . subCommand <EOL> pidfile = self . parent [ "<STR_LIT>" ] <EOL> if pidfile . endswith ( "<STR_LIT>" ) : <EOL> pidfile = None <EOL> self [ "<STR_LIT>" ] = pidfile <EOL> if not self . parent . has_key ( "<STR_LIT>" ) or self . parent [ "<STR_LIT>" ] is None : <EOL> self . parent [ "<STR_LIT>" ] = <NUM_LIT:0> <NUM_LIT> <EOL> program_settings = read_config ( program , self ) <EOL> settings . update ( program_settings ) <EOL> settings [ "<STR_LIT>" ] = program <EOL> settings [ "<STR_LIT>" ] = os . path . normpath ( os . path . expanduser ( settings [ "<STR_LIT>" ] ) ) <EOL> settings [ "<STR_LIT>" ] = os . path . normpath ( os . path . expanduser ( settings [ "<STR_LIT>" ] ) ) <EOL> settings [ "<STR_LIT>" ] = os . path . normpath ( os . path . expanduser ( settings [ "<STR_LIT>" ] ) ) <EOL> settings [ "<STR_LIT>" ] = os . path . normpath ( os . path . expanduser ( settings [ "<STR_LIT>" ] ) ) <EOL> settings [ "<STR_LIT>" ] = os . path . normpath ( os . path . expanduser ( settings [ "<STR_LIT>" ] ) ) <EOL> settings [ "<STR_LIT>" ] = os . path . normpath ( os . path . expanduser ( settings [ "<STR_LIT>" ] ) ) <EOL> if settings . USER : <EOL> self . parent [ "<STR_LIT>" ] , self . parent [ "<STR_LIT>" ] = ( <EOL> pwd . getpwnam ( settings . USER ) [ <NUM_LIT:2> : <NUM_LIT:4> ] ) <EOL> self . parent [ "<STR_LIT>" ] = settings [ "<STR_LIT>" ] <EOL> storage_schemas = join ( settings [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> if not exists ( storage_schemas ) : <EOL> print "<STR_LIT>" % storage_schemas <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> database = settings . DATABASE <EOL> if database not in TimeSeriesDatabase . plugins : <EOL> print "<STR_LIT>" % database <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> database_class = TimeSeriesDatabase . plugins [ database ] <EOL> state . database = database_class ( settings ) <EOL> settings . CACHE_SIZE_LOW_WATERMARK = settings . MAX_CACHE_SIZE * <NUM_LIT> <EOL> if not "<STR_LIT:action>" in self : <EOL> self [ "<STR_LIT:action>" ] = "<STR_LIT:start>" <EOL> self . handleAction ( ) <EOL> if not self [ "<STR_LIT>" ] : <EOL> if self . parent . get ( "<STR_LIT>" , None ) : <EOL> log . logToSyslog ( self . parent [ "<STR_LIT>" ] ) <EOL> elif not self . parent [ "<STR_LIT>" ] : <EOL> logdir = settings . LOG_DIR <EOL> if not isdir ( logdir ) : <EOL> os . makedirs ( logdir ) <EOL> if settings . USER : <EOL> os . chown ( logdir , self . parent [ "<STR_LIT>" ] , self . parent [ "<STR_LIT>" ] ) <EOL> log . logToDir ( logdir ) <EOL> if self [ "<STR_LIT>" ] is None : <EOL> self [ "<STR_LIT>" ] = join ( settings [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> settings [ "<STR_LIT>" ] = self [ "<STR_LIT>" ] <EOL> if self [ "<STR_LIT>" ] is None : <EOL> self [ "<STR_LIT>" ] = join ( settings [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> settings [ "<STR_LIT>" ] = self [ "<STR_LIT>" ] <EOL> def parseArgs ( self , * action ) : <EOL> """<STR_LIT>""" <EOL> if len ( action ) == <NUM_LIT:1> : <EOL> self [ "<STR_LIT:action>" ] = action [ <NUM_LIT:0> ] <EOL> def handleAction ( self ) : <EOL> """<STR_LIT>""" <EOL> action = self [ "<STR_LIT:action>" ] <EOL> pidfile = self . parent [ "<STR_LIT>" ] <EOL> program = settings [ "<STR_LIT>" ] <EOL> instance = self [ "<STR_LIT>" ] <EOL> if action == "<STR_LIT>" : <EOL> if not exists ( pidfile ) : <EOL> print "<STR_LIT>" % pidfile <EOL> raise SystemExit ( <NUM_LIT:0> ) <EOL> pf = open ( pidfile , '<STR_LIT:r>' ) <EOL> try : <EOL> pid = int ( pf . read ( ) . strip ( ) ) <EOL> pf . close ( ) <EOL> except IOError : <EOL> print "<STR_LIT>" % pidfile <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> print "<STR_LIT>" % pid <EOL> try : <EOL> os . kill ( pid , <NUM_LIT:15> ) <EOL> except OSError , e : <EOL> if e . errno == errno . ESRCH : <EOL> print "<STR_LIT>" % pid <EOL> else : <EOL> raise <EOL> raise SystemExit ( <NUM_LIT:0> ) <EOL> elif action == "<STR_LIT:status>" : <EOL> if not exists ( pidfile ) : <EOL> print "<STR_LIT>" % ( program , instance ) <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> pf = open ( pidfile , "<STR_LIT:r>" ) <EOL> try : <EOL> pid = int ( pf . read ( ) . strip ( ) ) <EOL> pf . close ( ) <EOL> except IOError : <EOL> print "<STR_LIT>" % pidfile <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> if _process_alive ( pid ) : <EOL> print ( "<STR_LIT>" % <EOL> ( program , instance , pid ) ) <EOL> raise SystemExit ( <NUM_LIT:0> ) <EOL> else : <EOL> print "<STR_LIT>" % ( program , instance ) <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> elif action == "<STR_LIT:start>" : <EOL> if exists ( pidfile ) : <EOL> pf = open ( pidfile , '<STR_LIT:r>' ) <EOL> try : <EOL> pid = int ( pf . read ( ) . strip ( ) ) <EOL> pf . close ( ) <EOL> except IOError : <EOL> print "<STR_LIT>" % pidfile <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> if _process_alive ( pid ) : <EOL> print ( "<STR_LIT>" % <EOL> ( program , instance , pid ) ) <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> else : <EOL> print "<STR_LIT>" % pidfile <EOL> try : <EOL> os . unlink ( pidfile ) <EOL> except IOError : <EOL> print "<STR_LIT>" % pidfile <EOL> else : <EOL> if not os . path . exists ( settings [ "<STR_LIT>" ] ) : <EOL> try : <EOL> os . makedirs ( settings [ "<STR_LIT>" ] ) <EOL> except OSError as exc : <EOL> if exc . errno == errno . EEXIST and os . path . isdir ( settings [ "<STR_LIT>" ] ) : <EOL> pass <EOL> else : <EOL> raise <EOL> print "<STR_LIT>" % ( program , instance ) <EOL> else : <EOL> print "<STR_LIT>" % action <EOL> print "<STR_LIT>" <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> class CarbonAggregatorOptions ( CarbonCacheOptions ) : <EOL> optParameters = [ <EOL> [ "<STR_LIT>" , "<STR_LIT>" , None , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , None , "<STR_LIT>" ] , <EOL> ] + CarbonCacheOptions . optParameters <EOL> def postOptions ( self ) : <EOL> CarbonCacheOptions . postOptions ( self ) <EOL> if self [ "<STR_LIT>" ] is None : <EOL> self [ "<STR_LIT>" ] = join ( settings [ "<STR_LIT>" ] , settings [ '<STR_LIT>' ] ) <EOL> settings [ "<STR_LIT>" ] = self [ "<STR_LIT>" ] <EOL> if self [ "<STR_LIT>" ] is None : <EOL> self [ "<STR_LIT>" ] = join ( settings [ "<STR_LIT>" ] , <EOL> settings [ '<STR_LIT>' ] ) <EOL> settings [ "<STR_LIT>" ] = self [ "<STR_LIT>" ] <EOL> class CarbonRelayOptions ( CarbonCacheOptions ) : <EOL> optParameters = [ <EOL> [ "<STR_LIT>" , "<STR_LIT>" , None , "<STR_LIT>" ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , None , "<STR_LIT>" ] , <EOL> ] + CarbonCacheOptions . optParameters <EOL> def postOptions ( self ) : <EOL> CarbonCacheOptions . postOptions ( self ) <EOL> if self [ "<STR_LIT>" ] is None : <EOL> self [ "<STR_LIT>" ] = join ( settings [ "<STR_LIT>" ] , settings [ '<STR_LIT>' ] ) <EOL> settings [ "<STR_LIT>" ] = self [ "<STR_LIT>" ] <EOL> if self [ "<STR_LIT>" ] is None : <EOL> self [ "<STR_LIT>" ] = join ( settings [ "<STR_LIT>" ] , settings [ '<STR_LIT>' ] ) <EOL> settings [ "<STR_LIT>" ] = self [ "<STR_LIT>" ] <EOL> if settings [ "<STR_LIT>" ] not in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> settings . RELAY_METHOD ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def get_default_parser ( usage = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> parser = OptionParser ( usage = usage ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , <EOL> default = '<STR_LIT:a>' , <EOL> help = "<STR_LIT>" ) <EOL> return parser <EOL> def get_parser ( name ) : <EOL> parser = get_default_parser ( ) <EOL> if name == "<STR_LIT>" : <EOL> parser . add_option ( <EOL> "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" ) <EOL> elif name == "<STR_LIT>" : <EOL> parser . add_option ( <EOL> "<STR_LIT>" , <EOL> default = None , <EOL> help = "<STR_LIT>" ) <EOL> return parser <EOL> def parse_options ( parser , args ) : <EOL> """<STR_LIT>""" <EOL> ( options , args ) = parser . parse_args ( args ) <EOL> if not args : <EOL> parser . print_usage ( ) <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> if args [ <NUM_LIT:0> ] not in ( "<STR_LIT:start>" , "<STR_LIT>" , "<STR_LIT:status>" ) : <EOL> parser . print_usage ( ) <EOL> raise SystemExit ( <NUM_LIT:1> ) <EOL> return options , args <EOL> def read_config ( program , options , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> settings = Settings ( ) <EOL> settings . update ( defaults ) <EOL> for name , value in kwargs . items ( ) : <EOL> settings . setdefault ( name , value ) <EOL> graphite_root = kwargs . get ( "<STR_LIT>" ) <EOL> if graphite_root is None : <EOL> graphite_root = os . environ . get ( '<STR_LIT>' ) <EOL> if graphite_root is None : <EOL> raise CarbonConfigException ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> settings . setdefault ( "<STR_LIT>" , <EOL> os . environ . get ( "<STR_LIT>" , <EOL> join ( graphite_root , "<STR_LIT>" ) ) ) <EOL> if options [ "<STR_LIT>" ] is None : <EOL> options [ "<STR_LIT>" ] = join ( settings [ "<STR_LIT>" ] , "<STR_LIT>" ) <EOL> else : <EOL> settings [ "<STR_LIT>" ] = dirname ( normpath ( options [ "<STR_LIT>" ] ) ) <EOL> settings . setdefault ( "<STR_LIT>" , <EOL> os . environ . get ( "<STR_LIT>" , <EOL> join ( graphite_root , "<STR_LIT>" ) ) ) <EOL> settings . setdefault ( <EOL> "<STR_LIT>" , settings [ "<STR_LIT>" ] ) <EOL> settings . setdefault ( <EOL> "<STR_LIT>" , join ( settings [ "<STR_LIT>" ] , "<STR_LIT>" , program ) ) <EOL> settings . setdefault ( <EOL> "<STR_LIT>" , join ( settings [ "<STR_LIT>" ] , "<STR_LIT>" ) ) <EOL> settings . setdefault ( <EOL> "<STR_LIT>" , join ( settings [ "<STR_LIT>" ] , "<STR_LIT>" ) ) <EOL> section = program [ len ( "<STR_LIT>" ) : ] <EOL> config = options [ "<STR_LIT>" ] <EOL> if not exists ( config ) : <EOL> raise CarbonConfigException ( "<STR_LIT>" % config ) <EOL> settings . readFrom ( config , section ) <EOL> settings . setdefault ( "<STR_LIT>" , options [ "<STR_LIT>" ] ) <EOL> if options [ "<STR_LIT>" ] : <EOL> settings . readFrom ( config , <EOL> "<STR_LIT>" % ( section , options [ "<STR_LIT>" ] ) ) <EOL> settings [ "<STR_LIT>" ] = ( <EOL> options [ "<STR_LIT>" ] or <EOL> join ( settings [ "<STR_LIT>" ] , "<STR_LIT>" % <EOL> ( program , options [ "<STR_LIT>" ] ) ) ) <EOL> settings [ "<STR_LIT>" ] = ( options [ "<STR_LIT>" ] or <EOL> join ( settings [ "<STR_LIT>" ] , <EOL> "<STR_LIT>" % ( program , options [ "<STR_LIT>" ] ) ) ) <EOL> else : <EOL> settings [ "<STR_LIT>" ] = ( <EOL> options [ "<STR_LIT>" ] or <EOL> join ( settings [ "<STR_LIT>" ] , '<STR_LIT>' % program ) ) <EOL> settings [ "<STR_LIT>" ] = ( options [ "<STR_LIT>" ] or settings [ "<STR_LIT>" ] ) <EOL> return settings </s>
