def crawl(self, vm_desc, **kwargs): if psvmi is None: raise NotImplementedError() else: (domain_name, kernel_version, distro, arch) = vm_desc # XXX: not good, context_init was being done once per VM # in previous monolithic model, now it's once per plugin/feature vm_context = psvmi.context_init(domain_name, domain_name, kernel_version, distro, arch) sys = psvmi.system_info(vm_context) feature_attributes = OSFeature(sys.boottime, 'unknown', sys.ipaddr, sys.ostype, sys.osversion, sys.osrelease, sys.osplatform) feature_key = sys.ostype return [(feature_key, feature_attributes, 'os')]
def crawl(self, vm_desc, **kwargs): if psvmi is None: raise NotImplementedError() else: (domain_name, kernel_version, distro, arch) = vm_desc # XXX: not good, context_init was being done once per VM # in previous monolithic model, now it's once per plugin/feature vm_context = psvmi.context_init( domain_name, domain_name, kernel_version, distro, arch) sys = psvmi.system_info(vm_context) feature_attributes = OSFeature( sys.boottime, 'unknown', sys.ipaddr, sys.ostype, sys.osversion, sys.osrelease, sys.osplatform ) feature_key = sys.ostype return [(feature_key, feature_attributes, 'os')]
def crawl_memory(self): # memory attributes: ["used", "buffered", "cached", "free"] logger.debug('Crawling memory') feature_key = 'memory' if self.crawl_mode == Modes.INVM: try: used = psutil.virtual_memory().used except Exception as e: used = 'unknown' try: buffered = psutil.virtual_memory().buffers except Exception as e: buffered = 'unknown' try: cached = psutil.virtual_memory().cached except Exception as e: cached = 'unknown' try: free = psutil.virtual_memory().free except Exception as e: free = 'unknown' feature_attributes = MemoryFeature(used, buffered, cached, free) elif self.crawl_mode == Modes.OUTVM: (domain_name, kernel_version, distro, arch) = self.vm from psvmi import system_info sys = system_info(domain_name, kernel_version, distro, arch) feature_attributes = MemoryFeature( sys.memory_used, sys.memory_buffered, sys.memory_cached, sys.memory_free) elif self.crawl_mode == Modes.OUTCONTAINER: used = buffered = cached = free = 'unknown' try: with open(self.container.get_memory_cgroup_path('memory.stat' ), 'r') as f: for line in f: (key, value) = line.strip().split(' ') if key == 'total_cache': cached = int(value) if key == 'total_active_file': buffered = int(value) with open(self.container.get_memory_cgroup_path( 'memory.limit_in_bytes'), 'r') as f: limit = int(f.readline().strip()) with open(self.container.get_memory_cgroup_path( 'memory.usage_in_bytes'), 'r') as f: used = int(f.readline().strip()) host_free = psutil.virtual_memory().free container_total = used + min(host_free, limit - used) free = container_total - used feature_attributes = MemoryFeature(used, buffered, cached, free) except Exception as e: logger.error('Error crawling memory', exc_info=True) raise CrawlError(e) else: logger.error('Unsupported crawl mode: ' + self.crawl_mode + '. Returning unknown memory key and attributes.' ) feature_attributes = MemoryFeature('unknown', 'unknown', 'unknown', 'unknown') try: yield (feature_key, feature_attributes) except Exception as e: logger.error('Error crawling memory', exc_info=True) raise CrawlError(e)
def _crawl_os(self, mountpoint=None): assert(self.crawl_mode is not Modes.OUTCONTAINER) logger.debug('Crawling OS') if self.crawl_mode == Modes.INVM: logger.debug('Using in-VM state information (crawl mode: ' + self.crawl_mode + ')') feature_key = platform.system().lower() try: ips = misc.get_host_ip4_addresses() except Exception as e: ips = 'unknown' try: distro = platform.linux_distribution()[0] except Exception as e: distro = 'unknown' try: osname = platform.platform() except Exception as e: osname = 'unknown' boot_time = ( psutil.boot_time() if hasattr( psutil, 'boot_time') else psutil.BOOT_TIME) uptime = int(time.time()) - boot_time feature_attributes = OSFeature( boot_time, uptime, ips, distro, osname, platform.machine(), platform.release(), platform.system().lower(), platform.version(), ) elif self.crawl_mode == Modes.MOUNTPOINT: logger.debug('Using disk image information (crawl mode: ' + self.crawl_mode + ')') feature_key = \ platform_outofband.system(prefix=mountpoint).lower() feature_attributes = OSFeature( # boot time unknown for img # live IP unknown for img 'unsupported', '0.0.0.0', platform_outofband.linux_distribution( prefix=mountpoint)[0], platform_outofband.platform(prefix=mountpoint), platform_outofband.machine(prefix=mountpoint), platform_outofband.release(prefix=mountpoint), platform_outofband.system(prefix=mountpoint).lower(), platform_outofband.version(prefix=mountpoint), ) elif self.crawl_mode == Modes.OUTVM: (domain_name, kernel_version, distro, arch) = self.vm from psvmi import system_info sys = system_info(domain_name, kernel_version, distro, arch) uptime = int(time.time()) - sys.boottime feature_attributes = OSFeature( sys.boottime, sys.ipaddr, sys.osdistro, sys.osname, sys.osplatform, sys.osrelease, sys.ostype, sys.osversion, ) feature_key = sys.ostype else: raise NotImplementedError() try: yield (feature_key, feature_attributes) except Exception as e: logger.error('Error crawling OS', exc_info=True) raise CrawlError(e)
import psvmi # Test script that prints the list of processes and system info for a kvm guest KERNEL = "3.13.0-83-generic" KERNEL_LONG = "3.13.0-83.127" ARCH = "x86_64" INSTANCE = "25738" # This is ubuntu kernel version format KERNEL_VERSION = '{0}_{1}.{2}'.format(KERNEL, KERNEL_LONG, ARCH) for p in psvmi.process_iter(qemu_pid=INSTANCE, kernel_version=KERNEL_VERSION, distro='ubuntu', arch=ARCH): print p.name(), p.pid, p.get_connections(), p.get_open_files() print print psvmi.system_info(qemu_pid=INSTANCE, kernel_version=KERNEL_VERSION, distro='ubuntu', arch=ARCH)
# Test script that prints the list of processes and system info for a kvm guest _vm_name = sys.argv[1] _qemu_pid = sys.argv[2] _kernel_version = sys.argv[3] _distro = sys.argv[4] _arch = sys.argv[5] RUNNING_VM = sys.argv[6] vm_context = psvmi.context_init(qemu_pid=_qemu_pid, kernel_version=_kernel_version, distro=_distro, arch=_arch) output = psvmi.system_info(vm_context) assert 'Linux' in output assert len(list(output)) > 0 output = psvmi.cpuHw_info(vm_context) assert 'QEMU' in str(output) assert len(list(output)) > 0 output = psvmi.interface_iter(vm_context) assert any('lo' in i for i in output) output = psvmi.module_iter(vm_context) assert len(list(output)) > 0 if RUNNING_VM is -1: output = psvmi.process_iter(vm_context)
def crawl_memory(self): # memory attributes: ["used", "buffered", "cached", "free"] logger.debug('Crawling memory') feature_key = 'memory' if self.crawl_mode == Modes.INVM: vm = psutil.virtual_memory() if (vm.free + vm.used) > 0: util_percentage = float(vm.used) / (vm.free + vm.used) * 100.0 else: util_percentage = 'unknown' feature_attributes = MemoryFeature(vm.used, vm.buffers, vm.cached, vm.free, util_percentage) elif self.crawl_mode == Modes.OUTVM: (domain_name, kernel_version, distro, arch) = self.vm sys = system_info(domain_name, kernel_version, distro, arch) feature_attributes = MemoryFeature( sys.memory_used, sys.memory_buffered, sys.memory_cached, sys.memory_free, sys.memory_free / (sys.memory_used + sys.memory_buffered)) elif self.crawl_mode == Modes.OUTCONTAINER: used = buffered = cached = free = 'unknown' try: with open(self.container.get_memory_cgroup_path('memory.stat' ), 'r') as f: for line in f: (key, value) = line.strip().split(' ') if key == 'total_cache': cached = int(value) if key == 'total_active_file': buffered = int(value) with open(self.container.get_memory_cgroup_path( 'memory.limit_in_bytes'), 'r') as f: limit = int(f.readline().strip()) with open(self.container.get_memory_cgroup_path( 'memory.usage_in_bytes'), 'r') as f: used = int(f.readline().strip()) host_free = psutil.virtual_memory().free container_total = used + min(host_free, limit - used) free = container_total - used if 'unknown' not in [used, free] and (free + used) > 0: util_percentage = float(used) / (free + used) * 100.0 else: util_percentage = 'unknown' feature_attributes = MemoryFeature( used, buffered, cached, free, util_percentage) except Exception as e: logger.error('Error crawling memory', exc_info=True) raise CrawlError(e) else: raise NotImplementedError('Unsupported crawl mode') yield (feature_key, feature_attributes)
def _crawl_os(self, mountpoint='/'): assert (self.crawl_mode is not Modes.OUTCONTAINER) feature_key = platform.system().lower() try: os_kernel = platform.platform() except: os_kernel = 'unknown' result = osinfo.get_osinfo(mount_point=mountpoint) if result: os_distro = result['os'] os_version = result['version'] else: os_distro = 'unknown' os_version = 'unknown' logger.debug('Crawling OS') if self.crawl_mode == Modes.INVM: logger.debug('Using in-VM state information (crawl mode: ' + self.crawl_mode + ')') ips = misc.get_host_ip4_addresses() boot_time = psutil.boot_time() uptime = int(time.time()) - boot_time feature_attributes = OSFeature(boot_time, uptime, ips, os_distro, os_version, os_kernel, platform.machine()) elif self.crawl_mode == Modes.MOUNTPOINT: logger.debug('Using disk image information (crawl mode: ' + self.crawl_mode + ')') feature_key = 'linux' feature_attributes = OSFeature( # boot time unknown for img # live IP unknown for img 'unsupported', 'unsupported', '0.0.0.0', os_distro, os_version, 'unknown', 'unknown') elif self.crawl_mode == Modes.OUTVM: if psvmi is None: raise NotImplementedError() else: sys = psvmi.system_info(self.get_vm_context()) feature_attributes = OSFeature( sys.boottime, 'unknown', sys.ipaddr, sys.osdistro, sys.osname, sys.osplatform, sys.osversion, ) feature_key = sys.ostype else: raise NotImplementedError('Unsupported crawl mode') yield (feature_key, feature_attributes)
import psvmi # Test script that prints the list of processes and system info for a kvm guest ARCH = "x86_64" INSTANCE = '<INSTANCE>' KERNEL_VERSION = '4.0.3.x86_64' print( psvmi.system_info( qemu_pid=INSTANCE, kernel_version=KERNEL_VERSION, distro='vanilla', arch=ARCH)) for p in psvmi.process_iter( qemu_pid=INSTANCE, kernel_version=KERNEL_VERSION, distro='vanilla', arch=ARCH): print(p.name(), p.pid) # , p.get_connections(), p.get_open_files()