<s> import gevent <EOL> from gevent . monkey import patch_all <EOL> patch_all ( ) <EOL> from gevent . pywsgi import WSGIServer <EOL> import locale <EOL> import argparse <EOL> import logging <EOL> import socket <EOL> import urllib <EOL> import urllib2 <EOL> from logging import getLogger <EOL> from flask import Flask <EOL> import zerorpc <EOL> from psdash import __version__ <EOL> from psdash . node import LocalNode , RemoteNode <EOL> from psdash . web import fromtimestamp <EOL> logger = getLogger ( '<STR_LIT>' ) <EOL> class PsDashRunner ( object ) : <EOL> DEFAULT_LOG_INTERVAL = <NUM_LIT> <EOL> DEFAULT_NET_IO_COUNTER_INTERVAL = <NUM_LIT:3> <EOL> DEFAULT_REGISTER_INTERVAL = <NUM_LIT> <EOL> DEFAULT_BIND_HOST = '<STR_LIT>' <EOL> DEFAULT_PORT = <NUM_LIT> <EOL> LOCAL_NODE = '<STR_LIT:localhost>' <EOL> @ classmethod <EOL> def create_from_cli_args ( cls ) : <EOL> return cls ( args = None ) <EOL> def __init__ ( self , config_overrides = None , args = tuple ( ) ) : <EOL> self . _nodes = { } <EOL> config = self . _load_args_config ( args ) <EOL> if config_overrides : <EOL> config . update ( config_overrides ) <EOL> self . app = self . _create_app ( config ) <EOL> self . _setup_nodes ( ) <EOL> self . _setup_logging ( ) <EOL> self . _setup_context ( ) <EOL> def _get_args ( cls , args ) : <EOL> parser = argparse . ArgumentParser ( <EOL> description = '<STR_LIT>' % __version__ <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT>' , <EOL> dest = '<STR_LIT>' , <EOL> default = None , <EOL> metavar = '<STR_LIT:path>' , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , <EOL> default = None , <EOL> metavar = '<STR_LIT:host>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , <EOL> type = int , <EOL> dest = '<STR_LIT:port>' , <EOL> default = None , <EOL> metavar = '<STR_LIT:port>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> action = '<STR_LIT:store_true>' , <EOL> dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , <EOL> default = None , <EOL> metavar = '<STR_LIT>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> parser . add_argument ( <EOL> '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , <EOL> default = None , <EOL> metavar = '<STR_LIT:name>' , <EOL> help = '<STR_LIT>' <EOL> ) <EOL> return parser . parse_args ( args ) <EOL> def _load_args_config ( self , args ) : <EOL> config = { } <EOL> for k , v in vars ( self . _get_args ( args ) ) . iteritems ( ) : <EOL> if v : <EOL> key = '<STR_LIT>' % k . upper ( ) if k != '<STR_LIT>' else '<STR_LIT>' <EOL> config [ key ] = v <EOL> return config <EOL> def _setup_nodes ( self ) : <EOL> self . add_node ( LocalNode ( ) ) <EOL> nodes = self . app . config . get ( '<STR_LIT>' , [ ] ) <EOL> logger . info ( "<STR_LIT>" , len ( nodes ) ) <EOL> for n in nodes : <EOL> self . register_node ( n [ '<STR_LIT:name>' ] , n [ '<STR_LIT:host>' ] , int ( n [ '<STR_LIT:port>' ] ) ) <EOL> def add_node ( self , node ) : <EOL> self . _nodes [ node . get_id ( ) ] = node <EOL> def get_local_node ( self ) : <EOL> return self . _nodes . get ( self . LOCAL_NODE ) <EOL> def get_node ( self , name ) : <EOL> return self . _nodes . get ( name ) <EOL> def get_nodes ( self ) : <EOL> return self . _nodes <EOL> def register_node ( self , name , host , port ) : <EOL> n = RemoteNode ( name , host , port ) <EOL> node = self . get_node ( n . get_id ( ) ) <EOL> if node : <EOL> n = node <EOL> logger . debug ( "<STR_LIT>" , n . get_id ( ) ) <EOL> else : <EOL> logger . info ( "<STR_LIT>" , n . get_id ( ) ) <EOL> n . update_last_registered ( ) <EOL> self . add_node ( n ) <EOL> return n <EOL> def _create_app ( self , config = None ) : <EOL> app = Flask ( __name__ ) <EOL> app . psdash = self <EOL> app . config . from_envvar ( '<STR_LIT>' , silent = True ) <EOL> if config and isinstance ( config , dict ) : <EOL> app . config . update ( config ) <EOL> self . _load_allowed_remote_addresses ( app ) <EOL> if not app . secret_key : <EOL> app . secret_key = '<STR_LIT>' <EOL> app . add_template_filter ( fromtimestamp ) <EOL> from psdash . web import webapp <EOL> prefix = app . config . get ( '<STR_LIT>' ) <EOL> if prefix : <EOL> prefix = '<STR_LIT:/>' + prefix . strip ( '<STR_LIT:/>' ) <EOL> webapp . url_prefix = prefix <EOL> app . register_blueprint ( webapp ) <EOL> return app <EOL> def _load_allowed_remote_addresses ( self , app ) : <EOL> key = '<STR_LIT>' <EOL> addrs = app . config . get ( key ) <EOL> if not addrs : <EOL> return <EOL> if isinstance ( addrs , ( str , unicode ) ) : <EOL> app . config [ key ] = [ a . strip ( ) for a in addrs . split ( '<STR_LIT:U+002C>' ) ] <EOL> def _setup_logging ( self ) : <EOL> level = self . app . config . get ( '<STR_LIT>' , logging . INFO ) if not self . app . debug else logging . DEBUG <EOL> format = self . app . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> logging . basicConfig ( <EOL> level = level , <EOL> format = format <EOL> ) <EOL> logging . getLogger ( '<STR_LIT>' ) . setLevel ( logging . WARNING if not self . app . debug else logging . DEBUG ) <EOL> def _setup_workers ( self ) : <EOL> net_io_interval = self . app . config . get ( '<STR_LIT>' , self . DEFAULT_NET_IO_COUNTER_INTERVAL ) <EOL> gevent . spawn_later ( net_io_interval , self . _net_io_counters_worker , net_io_interval ) <EOL> if '<STR_LIT>' in self . app . config : <EOL> logs_interval = self . app . config . get ( '<STR_LIT>' , self . DEFAULT_LOG_INTERVAL ) <EOL> gevent . spawn_later ( logs_interval , self . _logs_worker , logs_interval ) <EOL> if self . app . config . get ( '<STR_LIT>' ) : <EOL> register_interval = self . app . config . get ( '<STR_LIT>' , self . DEFAULT_REGISTER_INTERVAL ) <EOL> gevent . spawn_later ( register_interval , self . _register_agent_worker , register_interval ) <EOL> def _setup_locale ( self ) : <EOL> locale . setlocale ( locale . LC_ALL , '<STR_LIT>' ) <EOL> def _setup_context ( self ) : <EOL> self . get_local_node ( ) . net_io_counters . update ( ) <EOL> if '<STR_LIT>' in self . app . config : <EOL> self . get_local_node ( ) . logs . add_patterns ( self . app . config [ '<STR_LIT>' ] ) <EOL> def _logs_worker ( self , sleep_interval ) : <EOL> while True : <EOL> logger . debug ( "<STR_LIT>" ) <EOL> self . get_local_node ( ) . logs . add_patterns ( self . app . config [ '<STR_LIT>' ] ) <EOL> gevent . sleep ( sleep_interval ) <EOL> def _register_agent_worker ( self , sleep_interval ) : <EOL> while True : <EOL> logger . debug ( "<STR_LIT>" ) <EOL> self . _register_agent ( ) <EOL> gevent . sleep ( sleep_interval ) <EOL> def _net_io_counters_worker ( self , sleep_interval ) : <EOL> while True : <EOL> logger . debug ( "<STR_LIT>" ) <EOL> self . get_local_node ( ) . net_io_counters . update ( ) <EOL> gevent . sleep ( sleep_interval ) <EOL> def _register_agent ( self ) : <EOL> register_name = self . app . config . get ( '<STR_LIT>' ) <EOL> if not register_name : <EOL> register_name = socket . gethostname ( ) <EOL> url_args = { <EOL> '<STR_LIT:name>' : register_name , <EOL> '<STR_LIT:port>' : self . app . config . get ( '<STR_LIT>' , self . DEFAULT_PORT ) , <EOL> } <EOL> register_url = '<STR_LIT>' % ( self . app . config [ '<STR_LIT>' ] , urllib . urlencode ( url_args ) ) <EOL> if '<STR_LIT>' in self . app . config and '<STR_LIT>' in self . app . config : <EOL> auth_handler = urllib2 . HTTPBasicAuthHandler ( ) <EOL> auth_handler . add_password ( <EOL> realm = '<STR_LIT>' , <EOL> uri = register_url , <EOL> user = self . app . config [ '<STR_LIT>' ] , <EOL> passwd = self . app . config [ '<STR_LIT>' ] <EOL> ) <EOL> opener = urllib2 . build_opener ( auth_handler ) <EOL> urllib2 . install_opener ( opener ) <EOL> try : <EOL> urllib2 . urlopen ( register_url ) <EOL> except urllib2 . HTTPError as e : <EOL> logger . error ( '<STR_LIT>' , register_url , e ) <EOL> def _run_rpc ( self ) : <EOL> logger . info ( "<STR_LIT>" ) <EOL> if '<STR_LIT>' in self . app . config : <EOL> self . _register_agent ( ) <EOL> service = self . get_local_node ( ) . get_service ( ) <EOL> self . server = zerorpc . Server ( service ) <EOL> self . server . bind ( '<STR_LIT>' % ( self . app . config . get ( '<STR_LIT>' , self . DEFAULT_BIND_HOST ) , <EOL> self . app . config . get ( '<STR_LIT>' , self . DEFAULT_PORT ) ) ) <EOL> self . server . run ( ) <EOL> def _run_web ( self ) : <EOL> logger . info ( "<STR_LIT>" ) <EOL> log = '<STR_LIT:default>' if self . app . debug else None <EOL> ssl_args = { } <EOL> if self . app . config . get ( '<STR_LIT>' ) and self . app . config . get ( '<STR_LIT>' ) : <EOL> ssl_args = { <EOL> '<STR_LIT>' : self . app . config . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . app . config . get ( '<STR_LIT>' ) <EOL> } <EOL> listen_to = ( <EOL> self . app . config . get ( '<STR_LIT>' , self . DEFAULT_BIND_HOST ) , <EOL> self . app . config . get ( '<STR_LIT>' , self . DEFAULT_PORT ) <EOL> ) <EOL> self . server = WSGIServer ( <EOL> listen_to , <EOL> application = self . app , <EOL> log = log , <EOL> ** ssl_args <EOL> ) <EOL> self . server . serve_forever ( ) <EOL> def run ( self ) : <EOL> logger . info ( '<STR_LIT>' % __version__ ) <EOL> self . _setup_locale ( ) <EOL> self . _setup_workers ( ) <EOL> logger . info ( '<STR_LIT>' , <EOL> self . app . config . get ( '<STR_LIT>' , self . DEFAULT_BIND_HOST ) , <EOL> self . app . config . get ( '<STR_LIT>' , self . DEFAULT_PORT ) ) <EOL> if self . app . config . get ( '<STR_LIT>' ) : <EOL> return self . _run_rpc ( ) <EOL> else : <EOL> return self . _run_web ( ) <EOL> def main ( ) : <EOL> r = PsDashRunner . create_from_cli_args ( ) <EOL> r . run ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
