#NEWLINE# def check( self , agentConfig ) : #NEWLINE# #INDENT# 'Return an aggregate of CPU stats across all CPUs\n        When figures are not available, False is sent back.\n        ' #NEWLINE# def format_results( us , sy , wa , idle , st ) : #NEWLINE# #INDENT# data = { #NEWLINE# #INDENT# 'cpuUser' : us , #NEWLINE# 'cpuSystem' : sy , #NEWLINE# 'cpuWait' : wa , #NEWLINE# 'cpuIdle' : idle , #NEWLINE# 'cpuStolen' : st , #NEWLINE# #UNINDENT# } #NEWLINE# for key in data . keys ( ) : #NEWLINE# #INDENT# if ( data [ key ] is None ) : #NEWLINE# #INDENT# del data [ key ] #NEWLINE# #UNINDENT# #UNINDENT# return data #NEWLINE# #UNINDENT# def get_value( legend , data , name , filter_value = None ) : #NEWLINE# #INDENT# 'Using the legend and a metric name, get the value or None from the data line' #NEWLINE# if ( name in legend ) : #NEWLINE# #INDENT# value = to_float ( data [ legend . index ( name ) ] ) #NEWLINE# if ( filter_value is not None ) : #NEWLINE# #INDENT# if ( value > filter_value ) : #NEWLINE# #INDENT# return None #NEWLINE# #UNINDENT# #UNINDENT# return value #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . logger . debug ( ( 'Cannot extract cpu value %s from %s (%s)' % ( name , data , legend ) ) ) #NEWLINE# return 0.0 #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# if Platform . is_linux ( ) : #NEWLINE# #INDENT# mpstat = sp . Popen ( [ 'mpstat' , '1' , '3' ] , stdout = sp . PIPE , close_fds = True ) . communicate ( ) [ 0 ] #NEWLINE# lines = mpstat . split ( '\n' ) #NEWLINE# legend = [ l for l in lines if ( ( '%usr' in l ) or ( '%user' in l ) ) ] #NEWLINE# avg = [ l for l in lines if ( 'Average' in l ) ] #NEWLINE# if ( ( len ( legend ) == 1 ) and ( len ( avg ) == 1 ) ) : #NEWLINE# #INDENT# headers = [ h for h in legend [ 0 ] . split ( ) if ( h not in ( 'AM' , 'PM' ) ) ] #NEWLINE# data = avg [ 0 ] . split ( ) #NEWLINE# cpu_metrics = { #NEWLINE# #INDENT# '%usr' : None , #NEWLINE# '%user' : None , #NEWLINE# '%nice' : None , #NEWLINE# '%iowait' : None , #NEWLINE# '%idle' : None , #NEWLINE# '%sys' : None , #NEWLINE# '%irq' : None , #NEWLINE# '%soft' : None , #NEWLINE# '%steal' : None , #NEWLINE# #UNINDENT# } #NEWLINE# for cpu_m in cpu_metrics : #NEWLINE# #INDENT# cpu_metrics [ cpu_m ] = get_value ( headers , data , cpu_m , filter_value = 110 ) #NEWLINE# #UNINDENT# if any ( [ ( v is None ) for v in cpu_metrics . values ( ) ] ) : #NEWLINE# #INDENT# self . logger . warning ( ( 'Invalid mpstat data: %s' % data ) ) #NEWLINE# #UNINDENT# cpu_user = ( ( cpu_metrics [ '%usr' ] + cpu_metrics [ '%user' ] ) + cpu_metrics [ '%nice' ] ) #NEWLINE# cpu_system = ( ( cpu_metrics [ '%sys' ] + cpu_metrics [ '%irq' ] ) + cpu_metrics [ '%soft' ] ) #NEWLINE# cpu_wait = cpu_metrics [ '%iowait' ] #NEWLINE# cpu_idle = cpu_metrics [ '%idle' ] #NEWLINE# cpu_stolen = cpu_metrics [ '%steal' ] #NEWLINE# return format_results ( cpu_user , cpu_system , cpu_wait , cpu_idle , cpu_stolen ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return False #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( sys . platform == 'darwin' ) : #NEWLINE# #INDENT# iostats = sp . Popen ( [ 'iostat' , '-C' , '-w' , '3' , '-c' , '2' ] , stdout = sp . PIPE , close_fds = True ) . communicate ( ) [ 0 ] #NEWLINE# lines = [ l for l in iostats . split ( '\n' ) if ( len ( l ) > 0 ) ] #NEWLINE# legend = [ l for l in lines if ( 'us' in l ) ] #NEWLINE# if ( len ( legend ) == 1 ) : #NEWLINE# #INDENT# headers = legend [ 0 ] . split ( ) #NEWLINE# data = lines [ ( - 1 ) ] . split ( ) #NEWLINE# cpu_user = get_value ( headers , data , 'us' ) #NEWLINE# cpu_sys = get_value ( headers , data , 'sy' ) #NEWLINE# cpu_wait = 0 #NEWLINE# cpu_idle = get_value ( headers , data , 'id' ) #NEWLINE# cpu_st = 0 #NEWLINE# return format_results ( cpu_user , cpu_sys , cpu_wait , cpu_idle , cpu_st ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . logger . warn ( ( 'Expected to get at least 4 lines of data from iostat instead of just ' + str ( iostats [ : max ( 80 , len ( iostats ) ) ] ) ) ) #NEWLINE# return False #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if sys . platform . startswith ( 'freebsd' ) : #NEWLINE# #INDENT# iostats = sp . Popen ( [ 'iostat' , '-w' , '3' , '-c' , '2' ] , stdout = sp . PIPE , close_fds = True ) . communicate ( ) [ 0 ] #NEWLINE# lines = [ l for l in iostats . split ( '\n' ) if ( len ( l ) > 0 ) ] #NEWLINE# legend = [ l for l in lines if ( 'us' in l ) ] #NEWLINE# if ( len ( legend ) == 1 ) : #NEWLINE# #INDENT# headers = legend [ 0 ] . split ( ) #NEWLINE# data = lines [ ( - 1 ) ] . split ( ) #NEWLINE# cpu_user = get_value ( headers , data , 'us' ) #NEWLINE# cpu_nice = get_value ( headers , data , 'ni' ) #NEWLINE# cpu_sys = get_value ( headers , data , 'sy' ) #NEWLINE# cpu_intr = get_value ( headers , data , 'in' ) #NEWLINE# cpu_wait = 0 #NEWLINE# cpu_idle = get_value ( headers , data , 'id' ) #NEWLINE# cpu_stol = 0 #NEWLINE# return format_results ( ( cpu_user + cpu_nice ) , ( cpu_sys + cpu_intr ) , cpu_wait , cpu_idle , cpu_stol ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# self . logger . warn ( ( 'Expected to get at least 4 lines of data from iostat instead of just ' + str ( iostats [ : max ( 80 , len ( iostats ) ) ] ) ) ) #NEWLINE# return False #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# if ( sys . platform == 'sunos5' ) : #NEWLINE# #INDENT# mpstat = sp . Popen ( [ 'mpstat' , '-aq' , '1' , '2' ] , stdout = sp . PIPE , close_fds = True ) . communicate ( ) [ 0 ] #NEWLINE# lines = [ l for l in mpstat . split ( '\n' ) if ( len ( l ) > 0 ) ] #NEWLINE# lines = lines [ ( len ( lines ) / 2 ) : ] #NEWLINE# legend = [ l for l in lines if ( 'SET' in l ) ] #NEWLINE# assert ( len ( legend ) == 1 ) #NEWLINE# if ( len ( legend ) == 1 ) : #NEWLINE# #INDENT# headers = legend [ 0 ] . split ( ) #NEWLINE# d_lines = [ l for l in lines if ( 'SET' not in l ) ] #NEWLINE# user = [ get_value ( headers , l . split ( ) , 'usr' ) for l in d_lines ] #NEWLINE# kern = [ get_value ( headers , l . split ( ) , 'sys' ) for l in d_lines ] #NEWLINE# wait = [ get_value ( headers , l . split ( ) , 'wt' ) for l in d_lines ] #NEWLINE# idle = [ get_value ( headers , l . split ( ) , 'idl' ) for l in d_lines ] #NEWLINE# size = [ get_value ( headers , l . split ( ) , 'sze' ) for l in d_lines ] #NEWLINE# count = sum ( size ) #NEWLINE# rel_size = [ ( s / count ) for s in size ] #NEWLINE# dot = ( lambda v1 , v2 : reduce ( operator . add , map ( operator . mul , v1 , v2 ) ) ) #NEWLINE# return format_results ( dot ( user , rel_size ) , dot ( kern , rel_size ) , dot ( wait , rel_size ) , dot ( iostats , rel_size ) , 0.0 ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# self . logger . warn ( 'CPUStats: unsupported platform' ) #NEWLINE# return False #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# except Exception : #NEWLINE# #INDENT# self . logger . exception ( 'Cannot compute CPU stats' ) #NEWLINE# return False