<s> """<STR_LIT>""" <EOL> import diamond . collector <EOL> from diamond . collector import str_to_bool <EOL> try : <EOL> from xml . etree import ElementTree <EOL> except ImportError : <EOL> import cElementTree as ElementTree <EOL> try : <EOL> import libvirt <EOL> except ImportError : <EOL> libvirt = None <EOL> class LibvirtKVMCollector ( diamond . collector . Collector ) : <EOL> blockStats = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> <EOL> } <EOL> vifStats = { <EOL> '<STR_LIT>' : <NUM_LIT:0> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:4> , <EOL> '<STR_LIT>' : <NUM_LIT:5> , <EOL> '<STR_LIT>' : <NUM_LIT:6> , <EOL> '<STR_LIT>' : <NUM_LIT:7> <EOL> } <EOL> def get_default_config_help ( self ) : <EOL> config_help = super ( LibvirtKVMCollector , <EOL> 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 ( LibvirtKVMCollector , self ) . get_default_config ( ) <EOL> config . update ( { <EOL> '<STR_LIT:path>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : False <EOL> } ) <EOL> return config <EOL> def get_devices ( self , dom , type ) : <EOL> devices = [ ] <EOL> tree = ElementTree . fromstring ( dom . XMLDesc ( <NUM_LIT:0> ) ) <EOL> for target in tree . findall ( "<STR_LIT>" % type ) : <EOL> dev = target . get ( "<STR_LIT>" ) <EOL> if dev not in devices : <EOL> devices . append ( dev ) <EOL> return devices <EOL> def get_disk_devices ( self , dom ) : <EOL> return self . get_devices ( dom , '<STR_LIT>' ) <EOL> def get_network_devices ( self , dom ) : <EOL> return self . get_devices ( dom , '<STR_LIT>' ) <EOL> def report_cpu_metric ( self , statname , value , instance ) : <EOL> if str_to_bool ( self . config [ '<STR_LIT>' ] ) : <EOL> metric = value <EOL> else : <EOL> metric = self . derivative ( statname , float ( value ) / <NUM_LIT> , <EOL> max_value = diamond . collector . MAX_COUNTER , <EOL> instance = instance ) <EOL> self . publish ( statname , metric , instance = instance ) <EOL> def collect ( self ) : <EOL> if libvirt is None : <EOL> self . log . error ( '<STR_LIT>' ) <EOL> return { } <EOL> conn = libvirt . openReadOnly ( self . config [ '<STR_LIT>' ] ) <EOL> for dom in [ conn . lookupByID ( n ) for n in conn . listDomainsID ( ) ] : <EOL> if str_to_bool ( self . config [ '<STR_LIT>' ] ) : <EOL> name = dom . UUIDString ( ) <EOL> else : <EOL> name = dom . name ( ) <EOL> vcpus = dom . getCPUStats ( True , <NUM_LIT:0> ) <EOL> totalcpu = <NUM_LIT:0> <EOL> idx = <NUM_LIT:0> <EOL> for vcpu in vcpus : <EOL> cputime = vcpu [ '<STR_LIT>' ] <EOL> self . report_cpu_metric ( '<STR_LIT>' % idx , cputime , name ) <EOL> idx += <NUM_LIT:1> <EOL> totalcpu += cputime <EOL> self . report_cpu_metric ( '<STR_LIT>' , totalcpu , name ) <EOL> disks = self . get_disk_devices ( dom ) <EOL> accum = { } <EOL> for stat in self . blockStats . keys ( ) : <EOL> accum [ stat ] = <NUM_LIT:0> <EOL> for disk in disks : <EOL> stats = dom . blockStats ( disk ) <EOL> for stat in self . blockStats . keys ( ) : <EOL> idx = self . blockStats [ stat ] <EOL> val = stats [ idx ] <EOL> accum [ stat ] += val <EOL> self . publish ( '<STR_LIT>' % ( disk , stat ) , val , <EOL> instance = name ) <EOL> for stat in self . blockStats . keys ( ) : <EOL> self . publish ( '<STR_LIT>' % stat , accum [ stat ] , <EOL> instance = name ) <EOL> vifs = self . get_network_devices ( dom ) <EOL> accum = { } <EOL> for stat in self . vifStats . keys ( ) : <EOL> accum [ stat ] = <NUM_LIT:0> <EOL> for vif in vifs : <EOL> stats = dom . interfaceStats ( vif ) <EOL> for stat in self . vifStats . keys ( ) : <EOL> idx = self . vifStats [ stat ] <EOL> val = stats [ idx ] <EOL> accum [ stat ] += val <EOL> self . publish ( '<STR_LIT>' % ( vif , stat ) , val , <EOL> instance = name ) <EOL> for stat in self . vifStats . keys ( ) : <EOL> self . publish ( '<STR_LIT>' % stat , accum [ stat ] , <EOL> instance = name ) <EOL> mem = dom . memoryStats ( ) <EOL> self . publish ( '<STR_LIT>' , mem [ '<STR_LIT>' ] * <NUM_LIT> , <EOL> instance = name ) <EOL> self . publish ( '<STR_LIT>' , mem [ '<STR_LIT>' ] * <NUM_LIT> , instance = name ) </s>
