<s> """<STR_LIT>""" <EOL> import diamond . collector <EOL> import os <EOL> import re <EOL> import subprocess <EOL> from diamond . collector import str_to_bool <EOL> class PassengerCollector ( diamond . collector . Collector ) : <EOL> """<STR_LIT>""" <EOL> def get_default_config_help ( self ) : <EOL> """<STR_LIT>""" <EOL> config_help = super ( PassengerCollector , self ) . get_default_config_help ( ) <EOL> config_help . update ( { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } ) <EOL> return config_help <EOL> def get_default_config ( self ) : <EOL> """<STR_LIT>""" <EOL> config = super ( PassengerCollector , self ) . get_default_config ( ) <EOL> config . update ( { <EOL> "<STR_LIT:path>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } ) <EOL> return config <EOL> def get_passenger_memory_stats ( self ) : <EOL> """<STR_LIT>""" <EOL> command = [ self . config [ "<STR_LIT>" ] ] <EOL> if str_to_bool ( self . config [ "<STR_LIT>" ] ) : <EOL> command . insert ( <NUM_LIT:0> , self . config [ "<STR_LIT>" ] ) <EOL> try : <EOL> proc1 = subprocess . Popen ( command , stdout = subprocess . PIPE ) <EOL> ( std_out , std_err ) = proc1 . communicate ( ) <EOL> except OSError : <EOL> return { } <EOL> if std_out is None : <EOL> return { } <EOL> dict_stats = { <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : [ ] , <EOL> "<STR_LIT>" : <NUM_LIT:0.0> , <EOL> "<STR_LIT>" : <NUM_LIT:0.0> , <EOL> "<STR_LIT>" : <NUM_LIT:0.0> , <EOL> } <EOL> re_colour = re . compile ( "<STR_LIT>" ) <EOL> re_digit = re . compile ( "<STR_LIT>" ) <EOL> apache_flag = <NUM_LIT:0> <EOL> nginx_flag = <NUM_LIT:0> <EOL> passenger_flag = <NUM_LIT:0> <EOL> for raw_line in std_out . splitlines ( ) : <EOL> line = re_colour . sub ( "<STR_LIT>" , raw_line ) <EOL> if "<STR_LIT>" in line : <EOL> apache_flag = <NUM_LIT:1> <EOL> elif "<STR_LIT>" in line : <EOL> nginx_flag = <NUM_LIT:1> <EOL> elif "<STR_LIT>" in line : <EOL> passenger_flag = <NUM_LIT:1> <EOL> elif re_digit . match ( line ) : <EOL> line_splitted = line . split ( ) <EOL> if apache_flag == <NUM_LIT:1> : <EOL> dict_stats [ "<STR_LIT>" ] . append ( line_splitted [ <NUM_LIT:0> ] ) <EOL> dict_stats [ "<STR_LIT>" ] += float ( line_splitted [ <NUM_LIT:4> ] ) <EOL> elif nginx_flag == <NUM_LIT:1> : <EOL> dict_stats [ "<STR_LIT>" ] . append ( line_splitted [ <NUM_LIT:0> ] ) <EOL> dict_stats [ "<STR_LIT>" ] += float ( line_splitted [ <NUM_LIT:4> ] ) <EOL> elif passenger_flag == <NUM_LIT:1> : <EOL> dict_stats [ "<STR_LIT>" ] . append ( line_splitted [ <NUM_LIT:0> ] ) <EOL> dict_stats [ "<STR_LIT>" ] += float ( line_splitted [ <NUM_LIT:3> ] ) <EOL> elif "<STR_LIT>" in line : <EOL> passenger_flag = <NUM_LIT:0> <EOL> apache_flag = <NUM_LIT:0> <EOL> nginx_flag = <NUM_LIT:0> <EOL> return dict_stats <EOL> def get_passenger_cpu_usage ( self , dict_stats ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> proc1 = subprocess . Popen ( <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:2>" ] , <EOL> stdout = subprocess . PIPE ) <EOL> ( std_out , std_err ) = proc1 . communicate ( ) <EOL> except OSError : <EOL> return ( - <NUM_LIT:1> ) <EOL> re_lspaces = re . compile ( "<STR_LIT>" ) <EOL> re_digit = re . compile ( "<STR_LIT>" ) <EOL> overall_cpu = <NUM_LIT:0> <EOL> for raw_line in std_out . splitlines ( ) : <EOL> line = re_lspaces . sub ( "<STR_LIT>" , raw_line ) <EOL> if not re_digit . match ( line ) : <EOL> continue <EOL> line_splitted = line . split ( ) <EOL> if line_splitted [ <NUM_LIT:0> ] in dict_stats [ "<STR_LIT>" ] : <EOL> overall_cpu += float ( line_splitted [ <NUM_LIT:8> ] ) <EOL> elif line_splitted [ <NUM_LIT:0> ] in dict_stats [ "<STR_LIT>" ] : <EOL> overall_cpu += float ( line_splitted [ <NUM_LIT:8> ] ) <EOL> elif line_splitted [ <NUM_LIT:0> ] in dict_stats [ "<STR_LIT>" ] : <EOL> overall_cpu += float ( line_splitted [ <NUM_LIT:8> ] ) <EOL> return overall_cpu <EOL> def collect ( self ) : <EOL> """<STR_LIT>""" <EOL> if not os . access ( self . config [ "<STR_LIT>" ] , os . X_OK ) : <EOL> self . log . error ( "<STR_LIT>" , <EOL> self . config [ "<STR_LIT>" ] ) <EOL> return { } <EOL> dict_stats = self . get_passenger_memory_stats ( ) <EOL> if len ( dict_stats . keys ( ) ) == <NUM_LIT:0> : <EOL> return { } <EOL> overall_cpu = self . get_passenger_cpu_usage ( dict_stats ) <EOL> if overall_cpu >= <NUM_LIT:0> : <EOL> self . publish ( "<STR_LIT>" , overall_cpu ) <EOL> self . publish ( "<STR_LIT>" , dict_stats [ "<STR_LIT>" ] ) <EOL> self . publish ( "<STR_LIT>" , dict_stats [ "<STR_LIT>" ] ) <EOL> self . publish ( "<STR_LIT>" , <EOL> dict_stats [ "<STR_LIT>" ] ) </s>
