<s> from commands import getstatusoutput <EOL> from platform import node <EOL> from socket import socket , AF_INET , SOCK_STREAM <EOL> from sys import argv , exit <EOL> from time import sleep , time <EOL> DELAY = <NUM_LIT> <EOL> CARBON_SERVER = '<STR_LIT:localhost>' <EOL> CARBON_PORT = <NUM_LIT> <EOL> class Carbon : <EOL> def __init__ ( self , hostname , port ) : <EOL> self . s = socket ( AF_INET , SOCK_STREAM ) <EOL> self . hostname = hostname <EOL> self . port = int ( port ) <EOL> self . connect ( ) <EOL> def connect ( self ) : <EOL> try : <EOL> self . s . connect ( ( self . hostname , self . port ) ) <EOL> except IOError , e : <EOL> print "<STR_LIT>" , e <EOL> return <EOL> def disconnect ( self ) : self . s . close ( ) <EOL> def send ( self , data ) : <EOL> try : <EOL> self . s . sendall ( data + "<STR_LIT:\n>" ) <EOL> except : <EOL> self . connect ( ) <EOL> self . s . sendall ( data + "<STR_LIT:\n>" ) <EOL> class Host : <EOL> def __init__ ( self ) : <EOL> self . historical = { } <EOL> def get_all ( self ) : <EOL> data = [ ] <EOL> functions = dir ( self ) <EOL> for function in functions : <EOL> if not function . startswith ( "<STR_LIT>" ) : continue <EOL> for metric in eval ( "<STR_LIT>" % ( function ) ) : <EOL> data . append ( metric ) <EOL> return data <EOL> def read_file ( self , filename ) : <EOL> file_handle = open ( filename ) <EOL> contents = file_handle . readlines ( ) <EOL> file_handle . close ( ) <EOL> return contents <EOL> def delta_analyzer ( self , measurements , data , now ) : <EOL> result = [ ] <EOL> for line in data : <EOL> for measurement , loc in measurements . iteritems ( ) : <EOL> metric_name = "<STR_LIT>" % ( line [ <NUM_LIT:0> ] , measurement ) <EOL> try : value = line [ loc ] <EOL> except : continue <EOL> if self . historical . has_key ( metric_name ) : <EOL> current = value <EOL> delta = int ( value ) - int ( self . historical [ metric_name ] [ <NUM_LIT:1> ] ) <EOL> timedelta = time ( ) - self . historical [ metric_name ] [ <NUM_LIT:0> ] <EOL> self . historical [ metric_name ] = ( time ( ) , current ) <EOL> if timedelta < <NUM_LIT:1> : <EOL> continue <EOL> value = int ( delta / timedelta ) <EOL> if value > <NUM_LIT:0> : <EOL> result . append ( "<STR_LIT>" % ( metric_name , value , now ) ) <EOL> else : <EOL> self . historical [ metric_name ] = ( time ( ) , value ) <EOL> return result <EOL> def fetch_loadavg ( self ) : <EOL> data = [ ] <EOL> now = int ( time ( ) ) <EOL> ( loadavg_1 , loadavg_5 , loadavg_15 ) = self . read_file ( "<STR_LIT>" ) [ <NUM_LIT:0> ] . strip ( ) . split ( ) [ : <NUM_LIT:3> ] <EOL> data . append ( "<STR_LIT>" % ( loadavg_1 , now ) ) <EOL> data . append ( "<STR_LIT>" % ( loadavg_5 , now ) ) <EOL> data . append ( "<STR_LIT>" % ( loadavg_15 , now ) ) <EOL> return data <EOL> def fetch_network_io ( self ) : <EOL> measurements = { "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:2> , "<STR_LIT>" : <NUM_LIT:3> , "<STR_LIT>" : <NUM_LIT:4> , "<STR_LIT>" : <NUM_LIT:9> , "<STR_LIT>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:11> , "<STR_LIT>" : <NUM_LIT:12> } <EOL> now = int ( time ( ) ) <EOL> raw_data = self . read_file ( "<STR_LIT>" ) <EOL> prepared_data = [ ] <EOL> for line in raw_data [ <NUM_LIT:2> : ] : <EOL> ( interface , values ) = line . strip ( ) . split ( "<STR_LIT::>" ) <EOL> values = values . split ( ) <EOL> if interface == "<STR_LIT>" : continue <EOL> values . insert ( <NUM_LIT:0> , "<STR_LIT>" + interface ) <EOL> prepared_data . append ( values ) <EOL> return self . delta_analyzer ( measurements , prepared_data , now ) <EOL> def fetch_disk_usage ( self ) : <EOL> data = [ ] <EOL> now = int ( time ( ) ) <EOL> ( status , raw_data ) = getstatusoutput ( "<STR_LIT>" ) <EOL> if status != <NUM_LIT:0> : return data <EOL> for line in raw_data . split ( "<STR_LIT:\n>" ) : <EOL> if not ( line . startswith ( "<STR_LIT:/>" ) or line . find ( "<STR_LIT>" ) >= <NUM_LIT:0> ) : continue <EOL> device = line . split ( ) [ <NUM_LIT:2> ] <EOL> device_name = line . split ( ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> ( status , device_data ) = getstatusoutput ( "<STR_LIT>" + device ) <EOL> if status != <NUM_LIT:0> : continue <EOL> block_size , free_blocks_nonsu , total_blocks , total_file_nodes , free_file_nodes , free_blocks = [ a . isdigit ( ) and int ( a ) or <NUM_LIT:0> for a in device_data . split ( ) ] <EOL> data . append ( "<STR_LIT>" % ( device_name , free_blocks * block_size , now ) ) <EOL> data . append ( "<STR_LIT>" % ( device_name , free_file_nodes , now ) ) <EOL> data . append ( "<STR_LIT>" % ( device_name , float ( free_blocks ) / total_blocks * <NUM_LIT:100> , now ) ) <EOL> return data <EOL> def fetch_disk_io ( self ) : <EOL> measurements = { "<STR_LIT>" : <NUM_LIT:3> , "<STR_LIT>" : <NUM_LIT:6> , "<STR_LIT>" : <NUM_LIT:7> , "<STR_LIT>" : <NUM_LIT:10> , "<STR_LIT>" : <NUM_LIT:11> , "<STR_LIT>" : <NUM_LIT:12> , "<STR_LIT>" : <NUM_LIT> } <EOL> now = int ( time ( ) ) <EOL> raw_data = self . read_file ( "<STR_LIT>" ) <EOL> prepared_data = [ ] <EOL> for line in raw_data : <EOL> values = line . split ( ) <EOL> values [ <NUM_LIT:0> ] = "<STR_LIT>" + values [ <NUM_LIT:2> ] <EOL> prepared_data . append ( values ) <EOL> return self . delta_analyzer ( measurements , prepared_data , now ) <EOL> def fetch_memory_usage ( self ) : <EOL> metrics = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> data = [ ] <EOL> now = int ( time ( ) ) <EOL> raw_data = self . read_file ( "<STR_LIT>" ) <EOL> for line in raw_data : <EOL> metric , i = line . split ( "<STR_LIT::>" ) <EOL> value = int ( i . strip ( ) . strip ( "<STR_LIT>" ) ) * <NUM_LIT> <EOL> if metric in metrics . keys ( ) : <EOL> data . append ( "<STR_LIT>" % ( metrics [ metric ] , value , now ) ) <EOL> return data <EOL> def fetch_smb_statistics ( self ) : <EOL> measurements = { <NUM_LIT:0> : "<STR_LIT>" , <NUM_LIT:2> : "<STR_LIT>" } <EOL> data = [ ] <EOL> now = int ( time ( ) ) <EOL> this_node = None <EOL> ( status , raw_data ) = getstatusoutput ( "<STR_LIT>" ) <EOL> if status == <NUM_LIT:0> : <EOL> for line in raw_data . split ( "<STR_LIT:\n>" ) : <EOL> if line . find ( "<STR_LIT>" ) > <NUM_LIT:0> : <EOL> this_node = line . split ( ) [ <NUM_LIT:0> ] . split ( "<STR_LIT::>" ) [ <NUM_LIT:1> ] <EOL> if this_node is None : return <EOL> ( status , raw_data ) = getstatusoutput ( "<STR_LIT>" ) <EOL> if status != <NUM_LIT:0> : return data <EOL> for i , block in enumerate ( raw_data . split ( "<STR_LIT>" ) ) : <EOL> if i not in measurements . keys ( ) : continue <EOL> raw_data = block . split ( "<STR_LIT:\n>" ) <EOL> if this_node is not None : <EOL> this_node_count = [ line . startswith ( this_node + "<STR_LIT::>" ) for line in raw_data ] . count ( True ) <EOL> else : <EOL> this_node_count = len ( raw_data ) - <NUM_LIT:4> <EOL> if this_node_count < <NUM_LIT:0> : this_node_count = <NUM_LIT:0> <EOL> data . append ( "<STR_LIT>" % ( measurements [ i ] , this_node_count , now ) ) <EOL> return data <EOL> def main ( ) : <EOL> host = Host ( ) <EOL> hostname = node ( ) . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> graphite = Carbon ( CARBON_SERVER , CARBON_PORT ) ; <EOL> while True : <EOL> data = host . get_all ( ) <EOL> for datum in data : <EOL> metric = "<STR_LIT>" % ( hostname , datum ) <EOL> if "<STR_LIT>" in argv : print metric <EOL> graphite . send ( metric ) <EOL> sleep ( DELAY ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
