def get_mem_stats(): '''return mem stats''' if hasattr(psutil, 'cached_phymem') and hasattr(psutil, 'phymem_buffers'): cachemem = psutil.cached_phymem() + psutil.phymem_buffers() else: cachemem = -1 phymem = psutil.phymem_usage() mem = { 'cache': cachemem, 'total': phymem.total, 'used': phymem.used, 'free': phymem.free, 'percent': phymem.percent } virtmem = psutil.virtmem_usage() memswap = { 'total': virtmem.total, 'used': virtmem.used, 'free': virtmem.free, 'percent': virtmem.percent } return { "cache": cachemem, "mem": mem, "swap": memswap }
def get_mem_stats(): '''return mem stats''' if hasattr(psutil, 'cached_phymem') and hasattr(psutil, 'phymem_buffers'): cachemem = psutil.cached_phymem() + psutil.phymem_buffers() else: cachemem = -1 phymem = psutil.phymem_usage() mem = { 'cache': cachemem, 'total': phymem.total, 'used': phymem.used, 'free': phymem.free, 'percent': phymem.percent } virtmem = psutil.virtmem_usage() memswap = { 'total': virtmem.total, 'used': virtmem.used, 'free': virtmem.free, 'percent': virtmem.percent } return {"cache": cachemem, "mem": mem, "swap": memswap}
def calculate_resizing_tofitmemory(x_size,y_size,n_slices,byte): """ Predicts the percentage (between 0 and 1) to resize the image to fit the memory, giving the following information: x_size, y_size: image size n_slices: number of slices byte: bytes allocated for each pixel sample """ imagesize = x_size * y_size * n_slices * byte * 28 # USING LIBSIGAR #import sigar #sg = sigar.open() #ram_free = sg.mem().actual_free() #ram_total = sg.mem().total() #swap_free = sg.swap().free() #sg.close() # USING PSUTIL import psutil try: if (psutil.version_info>=(0,6,0)): ram_free = psutil.virtual_memory().available ram_total = psutil.virtual_memory().total swap_free = psutil.swap_memory().free else: ram_free = psutil.phymem_usage().free + psutil.cached_phymem() + psutil.phymem_buffers() ram_total = psutil.phymem_usage().total swap_free = psutil.virtmem_usage().free except: print "Exception! psutil version < 0.3 (not recommended)" ram_total = psutil.TOTAL_PHYMEM # this is for psutil < 0.3 ram_free = 0.8 * psutil.TOTAL_PHYMEM swap_free = psutil.avail_virtmem() print "RAM_FREE=", ram_free print "RAM_TOTAL=", ram_total if (sys.platform == 'win32'): if (platform.architecture()[0] == '32bit'): if ram_free>1400000000: ram_free=1400000000 if ram_total>1400000000: ram_total=1400000000 if (sys.platform == 'linux2'): if (platform.architecture()[0] == '32bit'): if ram_free>3500000000: ram_free=3500000000 if ram_total>3500000000: ram_total=3500000000 if (swap_free>ram_total): swap_free=ram_total resize = (float((ram_free+0.5*swap_free)/imagesize)) resize=math.sqrt(resize) # this gives the "resize" for each axis x and y if (resize>1): resize=1 return round(resize,2)
def cpu_stats(self): cfg_process = psutil.Process(os.getpid()) while True: # collect Vmsizes self._ip_change = 0 self._build_change = 0 rss = cfg_process.get_memory_info().rss if (self._rss != rss): self._rss = rss vms = cfg_process.get_memory_info().vms if (self._vms != vms): self._vms = vms pvms = vms if (pvms > self._pvms): self._pvms = pvms if self._sysinfo: # collect CPU Load avg load_avg = os.getloadavg() if (load_avg != self._load_avg): self._load_avg = load_avg # collect systemmeory info phymem_usage = psutil.phymem_usage() if (phymem_usage != self._phymem_usage): self._phymem_usage = phymem_usage phymem_buffers = psutil.phymem_buffers() if (phymem_buffers != self._phymem_buffers): self._phymem_buffers = phymem_buffers if (self._new_ip != self._curr_ip): self._new_ip = self.get_config_node_ip() self._ip_change = 1 # Retrieve build_info from package/rpm and cache it if self._curr_build_info is None: command = "contrail-version contrail-config | grep 'contrail-config'" version = os.popen(command).read() _, rpm_version, build_num = version.split() self._new_build_info = build_info + '"build-id" : "' + \ rpm_version + '", "build-number" : "' + \ build_num + '"}]}' if (self._new_build_info != self._curr_build_info): self._curr_build_info = self._new_build_info self._build_change = 1 num_cpus = psutil.NUM_CPUS if (num_cpus != self._num_cpus): self._num_cpus = num_cpus cpu_percent = cfg_process.get_cpu_percent(interval=0.1) cpu_share = cpu_percent / num_cpus self._cpu_share = cpu_share self._send_cpustats() gevent.sleep(self._time_interval)
def getStat(self): return {'memTotal': psutil.TOTAL_PHYMEM, 'memFree': psutil.avail_phymem(), 'swapTotal': psutil.total_virtmem(), 'swapFree': psutil.avail_virtmem(), 'buffers': psutil.phymem_buffers(), 'cached': psutil.cached_phymem()}
def update(self): phys_mem = psutil.phymem_usage() self.mem_usage = {'total': phys_mem.total, 'used': phys_mem.used, 'free': phys_mem.free, 'percent': phys_mem.percent, 'buffers': psutil.phymem_buffers(), 'cached': psutil.cached_phymem()} virt_mem = psutil.virtmem_usage() self.swap_usage = {'total': virt_mem.total, 'used': virt_mem.used, 'free': virt_mem.free, 'percent': virt_mem.percent}
def physical_memory_buffers(): ''' Return the amount of physical memory buffers. CLI Example:: salt '*' ps.physical_memory_buffers ''' return psutil.phymem_buffers()
def main(expr): vars = {'free': psutil.avail_virtmem(), 'available': psutil.avail_virtmem(), 'pfree': psutil.avail_phymem(), 'buffers': psutil.phymem_buffers(), 'total': psutil.total_virtmem(), 'cached': psutil.cached_phymem(), } return eval(expr, {}, vars)
def stat(self): ds = dict() ds['pt'] = int(psutil.phymem_usage().total / 1024) ds['pf'] = int(psutil.phymem_usage().free / 1024) ds['pu'] = int(psutil.phymem_usage().used / 1024) ds['pb'] = int(psutil.phymem_buffers() / 1024) ds['pc'] = int(psutil.cached_phymem() / 1024) ds['st'] = int(psutil.virtmem_usage().total / 1024) ds['su'] = int(psutil.virtmem_usage().used / 1024) return ds
def usedmemory(self): import psutil from distutils.version import StrictVersion # print sizes in human readable (MB) MB = 1024 * 1024 if StrictVersion(psutil.__version__) < '0.4': # deprecated total_mem = psutil.avail_phymem() + \ psutil.cached_phymem() + psutil.phymem_buffers() return "%d of %d" % (psutil.used_phymem() / MB, total_mem / MB) else: # I don't care about cached memory.. return "%d of %d" % \ ((psutil.phymem_usage().used - psutil.cached_phymem() - psutil.phymem_buffers()) / MB, psutil.phymem_usage().total / MB)
def test_phymem_buffers(self): # test psutil.phymem_buffers against "buffers" column of free # command line utility p = subprocess.Popen("free", shell=1, stdout=subprocess.PIPE) output = p.communicate()[0].strip() if PY3: output = str(output, sys.stdout.encoding) free_cmem = int(output.split('\n')[1].split()[5]) psutil_cmem = psutil.phymem_buffers() / 1024 self.assertEqual(free_cmem, psutil_cmem)
def test_phymem_buffers(self): # test psutil.phymem_buffers against "buffers" column of free # command line utility p = subprocess.Popen("free", shell=1, stdout=subprocess.PIPE) output = p.communicate()[0].strip() if sys.version_info >= (3,): output = str(output, sys.stdout.encoding) free_cmem = int(output.split("\n")[1].split()[5]) psutil_cmem = psutil.phymem_buffers() / 1024 self.assertEqual(free_cmem, psutil_cmem)
def memorypercent(self): import psutil from distutils.version import StrictVersion if StrictVersion(psutil.__version__) < '0.4': # deprecated total_mem = psutil.avail_phymem() + \ psutil.cached_phymem() + psutil.phymem_buffers() return psutil.used_phymem() * 100 / total_mem else: return int(psutil.phymem_usage().percent)
def physical_memory_buffers(): ''' Return the amount of physical memory buffers. CLI Example: .. code-block:: bash salt '*' ps.physical_memory_buffers ''' return psutil.phymem_buffers()
def _get_mem(self): retval = {} try: if hasattr(psutil, 'virtual_memory'): mem = psutil.virtual_memory() if hasattr(mem, 'active'): retval['active'] = self.to_gb(mem.active) if hasattr(mem, 'inactive'): retval['inactive'] = self.to_gb(mem.inactive) if hasattr(mem, 'buffers'): retval['buffers'] = self.to_gb(mem.buffers) if hasattr(mem, 'cached'): retval['cached'] = self.to_gb(mem.cached) if hasattr(mem, 'shared'): retval['shared'] = self.to_gb(mem.shared) else: mem = psutil.phymem_usage() retval['cached'] = self.to_gb(psutil.cached_phymem()) retval['buffers'] = self.to_gb(psutil.phymem_buffers()) if not self.is_win(): try: f = open('/proc/meminfo', 'r') for line in f: if line.startswith('Active:'): retval['active'] = self.to_gb(int(line.split()[1]) * 1024) if line.startswith('Inactive:'): retval['inactive'] = self.to_gb(int(line.split()[1]) * 1024) if line.startswith('Buffers:'): retval['buffers'] = self.to_gb(int(line.split()[1]) * 1024) if line.startswith('Cached:'): retval['cached'] = self.to_gb(int(line.split()[1]) * 1024) if line.startswith('Shared:'): retval['shared'] = self.to_gb(int(line.split()[1]) * 1024) f.close() except: pass retval['total'] = self.to_gb(mem.total) retval['used'] = self.to_gb(mem.used) retval['free'] = self.to_gb(mem.free) retval['percent'] = mem.percent except: pass return retval
def get_host_info(): """Return a ``dict`` with system's information `Example of its output <https://gist.github.com/gists/2891134>`_ """ memory_usage = psutil.phymem_usage() cached_memory = psutil.cached_phymem() buffered_memory = psutil.phymem_buffers() real_used = memory_usage.used - buffered_memory - cached_memory real_free = memory_usage.total - real_used percent = 100 * (float(memory_usage.used) / memory_usage.total) real_percent = 100 * (float(real_used) / memory_usage.total) virtual_used = psutil.used_virtmem() virtual_free = psutil.avail_virtmem() virtual_total = virtual_used + virtual_free info_per_nic = psutil.network_io_counters(pernic=True) network_info = {} for key, value in info_per_nic.iteritems(): network_info[key] = {'bytes sent': value.bytes_sent, 'bytes received': value.bytes_recv, 'packets sent': value.packets_sent, 'packets received': value.packets_recv,} partitions = psutil.disk_partitions() storage_info = {} for partition in partitions: disk_usage = psutil.disk_usage(partition.mountpoint) storage_info[partition.device] = {'mount point': partition.mountpoint, 'file system': partition.fstype, 'total bytes': disk_usage.total, 'total used bytes': disk_usage.used, 'total free bytes': disk_usage.free, 'percent used': disk_usage.percent,} return {'memory': {'free': memory_usage.free, 'total': memory_usage.total, 'used': memory_usage.used, 'cached': cached_memory, 'buffers': buffered_memory, 'real used': real_used, 'real free': real_free, 'percent': percent, 'real percent': real_percent, 'total virtual': virtual_total, 'used virtual': virtual_used, 'free virtual': virtual_free,}, 'cpu': {'number of cpus': psutil.NUM_CPUS, 'cpu percent': psutil.cpu_percent(),}, 'network': {'interfaces': network_info,}, 'storage': storage_info, 'uptime': time() - psutil.BOOT_TIME,}
def physical_memory_buffers(): ''' .. deprecated:: Helium Use :mod:`ps.virtual_memory <salt.modules.ps.virtual_memory>` instead. Return the amount of physical memory buffers. CLI Example: .. code-block:: bash salt '*' ps.physical_memory_buffers ''' salt.utils.warn_until( 'Helium', '\'ps.physical_memory_buffers\' is deprecated. Please use' '\'ps.virtual_memory\' instead. This functionality will' 'be removed in Salt {version}.') return psutil.phymem_buffers()
def collect(self): return dict( cpu_times=psutil.cpu_times()._asdict(), mem=dict( total_phymem=psutil.TOTAL_PHYMEM, avail_phymem=psutil.avail_phymem(), avail_virtmem=psutil.avail_virtmem(), cached_phymem=psutil.cached_phymem(), phymem_buffers=psutil.phymem_buffers(), total_virtmem=psutil.total_virtmem(), used_phymem=psutil.used_phymem(), used_virtmem=psutil.used_virtmem(), ), processes=list(self._processes()), net=dict(ifaces=self._net_dev(), proto=self._net_proto()), io=self._io_stats(), fs=dict(self._fs_usage()), fh=self._file_handles(), )
def update(self, cur_time): self.counter += 1L row = [] row.append(self.counter) row.append('%.2f' % (cur_time - self.get_creation_time())) pymem = psutil.phymem_usage() vmem = psutil.virtmem_usage() row.append('%.2f' % (atof(pymem[1]) / (1024.0 * 1024.0))) row.append('%.2f' % pymem[3]) row.append('%.2f' % (atof(vmem[1]) / (1024.0 * 1024.0))) row.append('%.2f' % vmem[3]) # cached memory and physical memory buffers on the system if self.linux == True: row.append('%d' % (psutil.cached_phymem() / (1024.0 * 1024.0))) row.append('%d' % (psutil.phymem_buffers() / (1024.0 * 1024.0))) self.writer.writerow(row)
def physical_memory_buffers(): ''' .. deprecated:: Helium Use :mod:`ps.virtual_memory <salt.modules.ps.virtual_memory>` instead. Return the amount of physical memory buffers. CLI Example: .. code-block:: bash salt '*' ps.physical_memory_buffers ''' salt.utils.warn_until( 'Helium', '\'ps.physical_memory_buffers\' is deprecated. Please use' '\'ps.virtual_memory\' instead. This functionality will' 'be removed in Salt {version}.' ) return psutil.phymem_buffers()
def physical_memory_buffers(): """ .. deprecated:: Helium Use :mod:`ps.virtual_memory <salt.modules.ps.virtual_memory>` instead. Return the amount of physical memory buffers. CLI Example: .. code-block:: bash salt '*' ps.physical_memory_buffers """ salt.utils.warn_until( "Helium", "'ps.physical_memory_buffers' is deprecated. Please use" "'ps.virtual_memory' instead. This functionality will" "be removed in Salt {version}.", ) return psutil.phymem_buffers()
def __call__(self): status = [] status.append(('num cpus', psutil.NUM_CPUS)) status.append(('cpu used', '%.2f%%' % psutil.cpu_percent())) total_phy = psutil.TOTAL_PHYMEM used_phy = psutil.used_phymem() status.append(('total phy mem', ConvertBytes(total_phy))) status.append(('used phy mem', ConvertBytes(used_phy))) status.append(('free phy mem', ConvertBytes(total_phy - used_phy))) try: status.append(('buffers', ConvertBytes(psutil.phymem_buffers()))) status.append(('cached', ConvertBytes(psutil.cached_phymem()))) except: pass total_virt = psutil.total_virtmem() used_virt = psutil.used_virtmem() status.append(('total virt mem', ConvertBytes(total_virt))) status.append(('used virt mem', ConvertBytes(used_virt))) status.append(('free virt mem', ConvertBytes(total_virt - used_virt))) return status
def collect(self): return dict( cpu_times=psutil.cpu_times()._asdict(), mem=dict( total_phymem=psutil.TOTAL_PHYMEM, avail_phymem=psutil.avail_phymem(), avail_virtmem=psutil.avail_virtmem(), cached_phymem=psutil.cached_phymem(), phymem_buffers=psutil.phymem_buffers(), total_virtmem=psutil.total_virtmem(), used_phymem=psutil.used_phymem(), used_virtmem=psutil.used_virtmem(), ), processes=list(self._processes()), net=dict( ifaces=self._net_dev(), proto=self._net_proto() ), io=self._io_stats(), fs=dict(self._fs_usage()), fh=self._file_handles(), )
# -*- coding: utf-8 -*- """ Created on Tue Sep 3 09:21:57 2013 @author: ozdemircili """ import psutil psutil.phymem_usage() psutil.virtmem_usage() psutil.cached_phymem() psutil.phymem_buffers() psutil.avail_phymem() psutil.used_phymem() psutil.total_virtmem() psutil.avail_virtmem() psutil.used_virtmem() psutil.cpu_percent() #CPU from __future__ import print_function with open('/proc/cpuinfo') as f: for line in f: #Formatting the output!! if line.strip(): if line.rstrip('\n').startswith('model name'): model_name = line.rstrip('\n').split(':')[1]
def cpu_stats(self): cfg_process = psutil.Process(os.getpid()) while True: #collect Vmsizes self._change = 0 self._ip_change = 0 self._build_change = 0 rss = cfg_process.get_memory_info().rss if (self._rss != rss): self._rss = rss self._change = 1 vms = cfg_process.get_memory_info().vms if (self._vms != vms): self._vms = vms self._change = 1 pvms = vms if (pvms > self._pvms): self._pvms = pvms self._change = 1 if self._sysinfo: #collect CPU Load avg load_avg = os.getloadavg() if (load_avg != self._load_avg): self._load_avg = load_avg self._change = 1 #collect systemmeory info phymem_usage = psutil.phymem_usage() if (phymem_usage != self._phymem_usage): self._phymem_usage = phymem_usage self._change = 1 phymem_buffers = psutil.phymem_buffers() if (phymem_buffers != self._phymem_buffers): self._phymem_buffers = phymem_buffers self._change = 1 if (self._new_ip != self._curr_ip): self._new_ip = self.get_config_node_ip() self._ip_change = 1 # placeholder for build_info. Need to retrieve build_info from #package/rpm and plug it in self._new_build_info from buildinfo import build_info rpm_version = os.popen("contrail-version | grep 'contrail-config '| awk '{print $2}'").read() build_num = os.popen("contrail-version | grep 'contrail-config '| awk '{print $3}'").read() self._new_build_info = build_info + '"build-id" : "' + rpm_version + '", "build-number" : "' + build_num + '"}]}' if (self._new_build_info != self._curr_build_info): self._curr_build_info = self._new_build_info self._build_change = 1 num_cpus = psutil.NUM_CPUS if (num_cpus != self._num_cpus): self._num_cpus = num_cpus self._change = 1 cpu_percent = cfg_process.get_cpu_percent(interval=0.1) cpu_share = cpu_percent/num_cpus if (cpu_share != self._cpu_share): self._cpu_share = cpu_share self._change = 1 if self._change: self._send_cpustats() gevent.sleep(self._time_interval)
def physical_memory_buffers(*args, **kwarg): """ def physical_memory_buffers(*args, **kwarg) -> Return the amount of physical memory buffers. @return int """ return psutil.phymem_buffers()
def phymem_buffers(): ''' Return the amount of physical memory buffers. ''' return psutil.phymem_buffers()
def test_phymem_buffers(self): x = psutil.phymem_buffers() self.assertTrue(isinstance(x, (int, long))) self.assertTrue(x >= 0)
result['cpu_used'] = psutil.cpu_percent(float(sys.argv[1])) all_process_info = [] own_pid = os.getpid() for p in psutil.process_iter(): if p.pid == own_pid: continue process_dict = {'cmd': ' '.join(get_cmdline(p)), 'name': get_name(p), 'pid': p.pid, 'user': get_username(p), 'cpu': int(p.get_cpu_percent(interval=0) * 10), 'mem': int(p.get_memory_percent() * 10)} all_process_info.append(process_dict) result['processes'] = all_process_info result['mem_used'] = psutil.used_phymem() - psutil.phymem_buffers() - psutil.cached_phymem() result['mem_total'] = psutil.TOTAL_PHYMEM result['disk_used'] = du.used result['disk_total'] = du.total interfaces = psutil.network_io_counters(pernic=True) if 'tunl0' in interfaces: # ignore tunl0 interface del interfaces['tunl0'] result['ifaces'] = interfaces sys.stdout.write(zlib.compress(json.dumps(result, separators=(',', ':'))))
def run(self): speed_avg = psutil.network_io_counters(pernic=True) seguir = True while seguir: #calculate time generate stats start = time.time() #calculate stats # speed network # network_actual = pick_speed_in_secs(2) network_avg = pick_speed_avg(speed_avg); speed_avg = network_avg[2]; # pack all # try: temperature = int(int(os.popen("cat /sys/class/thermal/thermal_zone0/temp").read())/1000) except: temperature = 40 data = { "network_down": network_actual[1],# "network_up": network_actual[0],# "network_avg_down": network_avg[1],# "network_avg_up": network_avg[0],# "cache": psutil.cached_phymem(),# "buffer": psutil.phymem_buffers(),# "used": psutil.used_phymem(),# "swap_total": psutil.total_virtmem(),# "swap_used": psutil.used_virtmem(),# "hdd_use_": psutil.disk_usage('/')[3], "hdd_use_home": psutil.disk_usage('/home')[3], "cpu_use": psutil.cpu_percent(interval=1),# "cpu_mhz": int(os.popen("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq").read()[:-3]), "temp": temperature } data_string = json.dumps(data) #print 'ENCODED:', data_string temp = time.localtime(start) datatime = str(temp[2])+"/"+str(temp[1])+"/"+str(temp[0])+" "+str(temp[3])+"/"+str(temp[4])+"/"+str(temp[5]) if (os.path.exists(url_file_location) != True): data = { "ava": psutil.avail_phymem(),# "swap_ava": psutil.avail_virtmem(),# } data_string_2 = json.dumps(data) tmp = True else: tmp = False f=open(str(url_file_location),"a") if (tmp == True): f.write('"data":'+data_string_2) f.write(', '+'"'+datatime+'":'+data_string) f.close() del data del data_string del temp del datatime del network_actual del temperature time.sleep(generate_time - int(time.time() - start))
def _store_resources(self, store=True): """Looks at the resources usage and store the data locally. store (bool): if False, run the method but do not store the resulting values - useful for initializing the previous values """ logger.debug("ResourceService._store_resources") # We use the precise time to compute the delta now = time.time() delta = now - self._last_saved_time self._last_saved_time = now now = int(now) data = {} # CPU cpu_times = self._get_cpu_times() data["cpu"] = dict((x, int(round((cpu_times[x] - self._prev_cpu_times[x]) / delta * 100.0))) for x in cpu_times) data["cpu"]["num_cpu"] = psutil.NUM_CPUS self._prev_cpu_times = cpu_times # Memory. We differentiate from old and deprecated (< 0.3.0) # methods to the new ones. Remove the differentiation when we # drop the support for Ubuntu 11.10 (which ships 0.2.1). ram_cached = psutil.cached_phymem() ram_buffers = psutil.phymem_buffers() if psutil_version < (0, 3, 0): data["memory"] = { "ram_total": psutil.TOTAL_PHYMEM / B_TO_MB, "ram_available": psutil.avail_phymem() / B_TO_MB, "ram_cached": ram_cached / B_TO_MB, "ram_buffers": ram_buffers / B_TO_MB, "ram_used": (psutil.used_phymem() - ram_cached - ram_buffers) / B_TO_MB, "swap_total": psutil.total_virtmem() / B_TO_MB, "swap_available": psutil.avail_virtmem() / B_TO_MB, "swap_used": psutil.used_virtmem() / B_TO_MB, } else: phymem = psutil.phymem_usage() virtmem = psutil.virtmem_usage() data["memory"] = { "ram_total": phymem.total / B_TO_MB, "ram_available": phymem.free / B_TO_MB, "ram_cached": ram_cached / B_TO_MB, "ram_buffers": ram_buffers / B_TO_MB, "ram_used": (phymem.used - ram_cached - ram_buffers) / B_TO_MB, "swap_total": virtmem.total / B_TO_MB, "swap_available": virtmem.free / B_TO_MB, "swap_used": virtmem.used / B_TO_MB, } data["services"] = {} # Details of our services for service in self._local_services: dic = {"autorestart": self._will_restart[service], "running": True} proc = self._procs[service] # If we don't have a previously found process for the # service, we find it if proc is None: proc = self._find_proc(service) # If we still do not find it, there is no process if proc is None: dic["running"] = False # We have a process, but maybe it has been shut down elif not proc.is_running(): # If so, let us find the new one proc = self._find_proc(service) # If there is no new one, continue if proc is None: dic["running"] = False # If the process is not running, we have nothing to do. if not dic["running"]: data["services"][str(service)] = dic continue try: dic["since"] = self._last_saved_time - proc.create_time dic["resident"], dic["virtual"] = \ (x / 1048576 for x in proc.get_memory_info()) cpu_times = proc.get_cpu_times() dic["user"] = int( round((cpu_times[0] - self._services_prev_cpu_times[service][0]) / delta * 100)) dic["sys"] = int( round((cpu_times[1] - self._services_prev_cpu_times[service][1]) / delta * 100)) self._services_prev_cpu_times[service] = cpu_times try: dic["threads"] = proc.get_num_threads() except AttributeError: dic["threads"] = 0 # 0 = Not implemented self._procs[service] = proc except psutil.error.NoSuchProcess: # Shut down while we operated? dic = {"autorestart": self._will_restart[service], "running": False} data["services"][str(service)] = dic if store: if len(self._local_store) >= 5000: # almost 7 hours self._local_store = self._local_store[1:] self._local_store.append((now, data)) return True
'cpu': int(p.get_cpu_percent(interval=0) * 10), 'mem': int(p.get_memory_percent() * 10) } except: continue all_process_info.append(process_dict) result['processes'] = all_process_info if psutil.version_info >= (5, 0, 1): mem_info = psutil.virtual_memory() result['mem_used'] = mem_info.used result['mem_total'] = mem_info.total else: result['mem_used'] = psutil.used_phymem() - psutil.phymem_buffers( ) - psutil.cached_phymem() result['mem_total'] = psutil.TOTAL_PHYMEM result['disk_used'] = du.used result['disk_total'] = du.total if psutil.version_info >= (5, 0, 1): interfaces = psutil.net_io_counters(pernic=True) else: interfaces = psutil.network_io_counters(pernic=True) if 'tunl0' in interfaces: # ignore tunl0 interface del interfaces['tunl0'] result['ifaces'] = interfaces
def cpu_stats(self): cfg_process = psutil.Process(os.getpid()) while True: #collect Vmsizes self._change = 0 self._ip_change = 0 self._build_change = 0 rss = cfg_process.get_memory_info().rss if (self._rss != rss): self._rss = rss self._change = 1 vms = cfg_process.get_memory_info().vms if (self._vms != vms): self._vms = vms self._change = 1 pvms = vms if (pvms > self._pvms): self._pvms = pvms self._change = 1 if self._sysinfo: #collect CPU Load avg load_avg = os.getloadavg() if (load_avg != self._load_avg): self._load_avg = load_avg self._change = 1 #collect systemmeory info phymem_usage = psutil.phymem_usage() if (phymem_usage != self._phymem_usage): self._phymem_usage = phymem_usage self._change = 1 phymem_buffers = psutil.phymem_buffers() if (phymem_buffers != self._phymem_buffers): self._phymem_buffers = phymem_buffers self._change = 1 if (self._new_ip != self._curr_ip): self._new_ip = self.get_config_node_ip() self._ip_change = 1 # placeholder for build_info. Need to retrieve build_info from #package/rpm and plug it in self._new_build_info from buildinfo import build_info rpm_version = os.popen( "contrail-version | grep 'contrail-config '| awk '{print $2}'" ).read() build_num = os.popen( "contrail-version | grep 'contrail-config '| awk '{print $3}'" ).read() self._new_build_info = build_info + '"build-id" : "' + rpm_version + '", "build-number" : "' + build_num + '"}]}' if (self._new_build_info != self._curr_build_info): self._curr_build_info = self._new_build_info self._build_change = 1 num_cpus = psutil.NUM_CPUS if (num_cpus != self._num_cpus): self._num_cpus = num_cpus self._change = 1 cpu_percent = cfg_process.get_cpu_percent(interval=0.1) cpu_share = cpu_percent / num_cpus if (cpu_share != self._cpu_share): self._cpu_share = cpu_share self._change = 1 if self._change: self._send_cpustats() gevent.sleep(self._time_interval)
help='warning level', ) parser.add_argument( "-c", type=int, default=256, help='critical level', ) args = parser.parse_args() #have to use avail_phymem to support psutils v 0.5 in debian wheezy. try: physmem = psutil.virtual_memory().available / MB #psutils >= 0.6 except AttributeError: # have to use old api physmem = (( psutil.avail_phymem() + psutil.phymem_buffers() + psutil.cached_phymem() ) / MB ) if args.c >= args.w: print ('UNKNOWN: critical level must be less than warning level') sys.exit(3) if physmem > args.w: print ("OK - %#s megabytes of physical memory available." % physmem) sys.exit(0) elif physmem > args.c: print ("WARNING - %#s megabytes of physical memory available." % physmem) sys.exit(1) elif physmem <= args.c: