def getPhyMem(): """取本机的物理内存,可用物理内存,剩余可用物理内存,单位K """ phymem = int(psutil.TOTAL_PHYMEM) / 1024 availphymem = int(psutil.avail_phymem()) / 1024 usedphymem = int(psutil.used_phymem()) / 1024 return phymem, availphymem, usedphymem
def percentage_ram_usage(self): total = psutil.TOTAL_PHYMEM used = psutil.used_phymem() # TODO correggere calcolo della memoria utilizzata considerando anche i buffer e la cache val = int(float(used) / float(total) * 100.0) val = 50 return self.xmlFormat('RAMUsage', val)
def mem(self, widget, data=None) : print ' \n DETAILS OF PHYSICAL AND VIRTUAL MEMORY\n ' print 'Total physical memory (in bytes) - ' print psutil.TOTAL_PHYMEM print 'Available physical memory (in bytes) - ' print psutil.avail_phymem() print 'Used physical memory (in bytes) - ' print psutil.used_phymem() print 'Total virtual memory (in bytes) - ' print psutil.total_virtmem() print 'Available virtual memory (in bytes) - ' print psutil.avail_virtmem() print 'Used virtual memory (in bytes) - ' print psutil.used_virtmem() print 'Total cached memory (in bytes) - ' print psutil.cached_phymem()
def getMemoryInfo(): """ return a tuple(total_phymen,avi_phymen.used_phymen) KB .get System Memory Inforation""" if psutilUsed==False: return None totalPhymen=psutil.TOTAL_PHYMEM aviPhymen=psutil.avail_phymem() usedPhymen=psutil.used_phymem() return (totalPhymen/1024,aviPhymen/1024,usedPhymen/1024)
def used_memory(pl, steps=5, circle_glyph='●', memory_glyph='🔲'): memory = float(psutil.used_phymem()) * 100 / psutil.TOTAL_PHYMEM ret = [] format = '{0:.1f}%' denom = int(steps) numer = int(denom * memory / 100) ret.append({ 'contents': memory_glyph + ' ', 'draw_soft_divider': False, 'divider_highlight_group': 'background:divider', 'highlight_group': ['used_memory'], 'gradient_level': 30, }) ret.append({ 'contents': circle_glyph * numer, 'draw_soft_divider': False, 'highlight_group': ['used_memory'], 'gradient_level': 99, }) ret.append({ 'contents': circle_glyph * (denom - numer), 'draw_soft_divider': False, 'highlight_group': ['used_memory'], 'gradient_level': 1, }) ret.append({ 'contents': ' ' + format.format(memory), 'draw_soft_divider': True, 'divider_highlight_group': 'background:divider', 'highlight_group': ['used_memory'], 'gradient_level': 1, }) return ret
def getPhyMem(): """取本机的物理内存,可用物理内存,剩余可用物理内存,单位K """ phymem = int(psutil.TOTAL_PHYMEM)/1024 availphymem = int(psutil.avail_phymem())/1024 usedphymem = int(psutil.used_phymem())/1024 return phymem,availphymem,usedphymem
def updateScreen(self): dt = datetime.datetime.now() with self.context(): self._text[1] = dt.strftime('%A, %b %d, %Y').center(27) self._text[2] = dt.strftime('%I:%M:%S %p').center(27) self._text[3] = ('CPU:%4.1f%% RAM:%6.2f%%' % (psutil.cpu_percent(), (psutil.used_phymem()-psutil.cached_phymem())/psutil.TOTAL_PHYMEM*100)).center(27) self._text[4] = ('%i processes' % len(psutil.get_pid_list())).center(27)
def update_memory(self, name, config): self.g[name] = ["memory_used","memory_available"] if("virtual_memory" in dir(psutil)): # new style function call data = psutil.virtual_memory() self.d["memory_used"] = data.used self.d["memory_available"] = data.available else: self.d["memory_used"] = psutil.used_phymem() self.d["memory_available"] = psutil.avail_phymem() self.c[name] =[ "graph_title Memory", "graph_order memory_used memory_available", "graph_category system", "graph_period second", "memory_used.label in use", "memory_used.min 0", "memory_used.type GAUGE", "memory_used.draw AREA", "memory_available.label available", "memory_available.type GAUGE", "memory_available.draw STACK", "."]
def used_memory(pl, steps=5, circle_glyph='●', memory_glyph='🔲'): memory = float(psutil.used_phymem()) * 100 / psutil.TOTAL_PHYMEM ret = [] denom = int(steps) numer = int(denom * memory / 100) ret.append({ 'contents': memory_glyph + ' ', 'draw_soft_divider': False, 'divider_highlight_group': 'background:divider', 'highlight_group': ['used_memory'], 'gradient_level': 99, }) ret.append({ 'contents': circle_glyph * numer, 'draw_soft_divider': False, 'highlight_group': ['used_memory'], 'gradient_level': 99, }) ret.append({ 'contents': circle_glyph * (denom - numer), 'draw_soft_divider': False, 'highlight_group': ['used_memory'], 'gradient_level': 1, }) return ret
def mem(): """ returns a tuple: index 0 : the used bytes of memory index 1 : the total bytes of memory """ used = psutil.used_phymem() total = psutil.TOTAL_PHYMEM return (used, total)
def used_memory_percent_gradient(pl, format='{0:.0f}%'): memory_percent = float(psutil.used_phymem()) * 100 / psutil.TOTAL_PHYMEM return [{ 'contents': format.format(memory_percent), 'highlight_group': ['used_memory_percent_gradient', 'used_memory_percent'], 'draw_divider': True, 'divider_highlight_group': 'background:divider', 'gradient_level': memory_percent, }]
def generate(self, **kwargs): result = {'mem_phys_total': psutil.TOTAL_PHYMEM, 'mem_phys_avail': psutil.avail_phymem(), 'mem_phys_used': psutil.used_phymem(), 'mem_virt_total': psutil.total_virtmem(), 'mem_virt_avail': psutil.used_virtmem(), 'cpu_cores': self._get_total_core(), } return result
def updateDB(): ''' updates the db with CPU details ''' timestamp = time.time() ioreadusage = psutil.disk_io_counters().read_count iowriteusage = psutil.disk_io_counters().write_count memoryusage = psutil.used_phymem() cpupercent = psutil.cpu_percent() cpuDetails = CPUStatistics(timestamp=timestamp,ioreadusage=ioreadusage,iowriteusage=iowriteusage,memoryusage=memoryusage,cpupercent=cpupercent) cpuDetails.save()
class MoniterMiddleWare(crawler.BaseCrawlerMiddleWare): mw_name = 'mem_moniter' def __init__(self, controller, settings): crawler.BaseCrawlerMiddleWare.__init__(self,controller, settings) self.remote_connect(self.on_overflow,'mem_overflow') #------------------------------------ def _do_process(self): pass cur = psutil.used_phymem() if cur<self.LIMIT: return log.msg('begin free memory<%d>'%cur,log.DEBUG) for e in psutil.process_iter(): self._check_process(e) cur = psutil.used_phymem() log.msg('end free memory<%d>'%cur,log.DEBUG)
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 generate(self, **kwargs): result = { 'mem_phys_total': psutil.TOTAL_PHYMEM, 'mem_phys_avail': psutil.avail_phymem(), 'mem_phys_used': psutil.used_phymem(), 'mem_virt_total': psutil.total_virtmem(), 'mem_virt_avail': psutil.avail_virtmem(), 'mem_virt_used': psutil.used_virtmem(), 'cpu_cores': self._get_total_cores(), } return result
def main(): time.sleep(1) cpu_percent = psutil.cpu_percent() mem_used = psutil.used_phymem() rx_bits, tx_bits = net_stats(NET_INTERFACE) localhost_name = socket.gethostname() rrd_ops('net_bps_in', rx_bits, INTERVAL, 'DERIVE', '6666FF', localhost_name) rrd_ops('net_bps_out', tx_bits, INTERVAL, 'DERIVE', '000099', localhost_name) rrd_ops('cpu_percent', cpu_percent, INTERVAL, 'GAUGE', 'FF0000', localhost_name) rrd_ops('mem_used', mem_used, INTERVAL, 'GAUGE', '00FF00', localhost_name)
def handleStatusQuery(self, packet): cpuUsage = psutil.cpu_percent() usedMem = psutil.used_phymem() simsRunning = 0 fields = [str(f) for f in [self.name,cpuUsage,usedMem,simsRunning]] response = PacketFactory.packetFromFields( packets.STATUS_RESPONSE, packet.header.sourceIP, fields ) self.transmissionThread.queueData(response)
def info(): ps = get_ps() print "PID,\tPPID,\tCPU,\tRSS - \tVMS" total = 0 for p in ps: rss, vms = p.get_memory_info() total += rss print "%s\t%s\t%0.2f%%\t%s - \t%s" % (p.pid, p.ppid, p.get_cpu_percent(), mb(rss), mb(vms)) print 'total watched RSS: %d' % mb(total) used, all, avail = mb(psutil.used_phymem()), mb(psutil.TOTAL_PHYMEM), mb(psutil.avail_phymem()) print 'Used memory %dMB out of %dMB' % (used, all) print 'Available memory %dMB' % avail
def __init__(self): self.memfree = int(psutil.avail_phymem()) / (1024 * 1024) self.memused = int(psutil.used_phymem()) / (1024 * 1024) self.memcached = int(psutil.cached_phymem()) / (1024 * 1024) self.memtotal = self.memfree + self.memused self.memload = psutil.phymem_usage()[3] self.load = os.getloadavg() self.uptime = ProcParser().getuptime() self.coresload = psutil.cpu_percent(0.7, True) self.time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) statvfs = os.statvfs('/') self.hddtotal = statvfs.f_frsize * statvfs.f_blocks / 1024 / 1024 # Size of filesystem in bytes self.hddfree = statvfs.f_frsize * statvfs.f_bfree / 1024 / 1024 # Actual number of free bytes self.hddused = self.hddtotal - self.hddfree
def on_global(self, msg=None): global_cpu = psutil.get_system_cpu_times() global_cpu['percent'] = psutil.cpu_percent() global_mem = { 'used_physical': psutil.used_phymem(), 'used_virtual': psutil.used_virtmem(), 'available_physical': psutil.avail_phymem(), 'available_virtual': psutil.avail_virtmem(), } result = { 'hostnome': socket.gethostname(), 'cpu': global_cpu, 'mem': global_mem, } return {'global': result}
def get_ram_used(self): percent_used = 100 if self.is_windows: #print(psutil.phymem_usage())#svmem(total=17057431552L, available=1096183808L, percent=93.6, used=15961247744L, free=1096183808L) #print(psutil.virtual_memory()) #ram=psutil.virtual_memory() #percent_used=float(ram[2]) try: percent_used = float(psutil.used_phymem()) / self.max_mem * 100 except: #print(psutil.phymem_usage())#svmem(total=17057431552L, available=1096183808L, percent=93.6, used=15961247744L, free=1096183808L) percent_used = -1 # else: mem = psutil.virtual_memory() #svmem(total=10367352832, available=6472179712, percent=37.6, used=8186245120, free=2181107712, active=4748992512, inactive=2758115328, buffers=790724608, cached=3500347392, shared=787554304) percent_used = mem.percent return percent_used
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 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 refresh(self): logging.debug("Host.refresh(): Refreshing all host entries") self.refreshVirtServiceSpecs() self.__machine_type=platform.machine() self.__network_name=platform.node() self.__processor=platform.processor() self.__python_version=platform.python_version() self.__linuxKernel_version=platform.release() #self.__name=platform.system() dist_info=platform.linux_distribution(distname='', version='', id='', supported_dists=('SuSE', 'debian', 'redhat'), full_distribution_name=1) self.__distroName=dist_info[0] self.__distroVersion=dist_info[1] self.__distroCode=dist_info[2] self.__vm_manager_pid=os.getpid() self.__freePhyMem=psutil.avail_phymem()/1048576 self.__freeVirtMem=psutil.avail_virtmem()/1048576 self.__totalVirtMem=psutil.total_virtmem()/1048576 self.__usedPhyMem=psutil.used_phymem()/1048576 self.__manager_process=psutil.Process(self.__vm_manager_pid) self.__virt_process=psutil.Process(int(self.__virtProcessPid)) self.__processUptime=datetime.datetime.fromtimestamp(self.__manager_process.create_time).strftime("%Y-%m-%d %H:%M") #self.__processOwner=self.__manager_process.username #self.__numOfProcessThreads=self.__manager_process.get_num_threads() #self.__numOfVirtProcessThreads=self.__virt_process.get_num_threads() #for thread in self.__virt_process.get_threads(): # self.__virtProcessThreadIDs.append(thread[0]) #for thread in self.__manager_process.get_threads(): # self.__processThreadIDs.append(thread[0]) self.__virtProcessCpuUsage=self.__virt_process.get_cpu_percent() self.__proccessCpuUsage=self.__manager_process.get_cpu_percent() mem=self.__virt_process.get_memory_info() self.__virtProcessMemUsage=mem[1]/1048576 mem=self.__manager_process.get_memory_info() self.__processMemUsage=mem[1]/1048576 #self.__processOpenedFilesSpecs=self.__manager_process.get_open_files() #self.__virtProcessOpenedFilesSpecs=self.__virt_process.get_open_files() #self.__processConnectionsSpecs=self.__manager_process.get_connections() #self.__virtProcessConnectionsSpecs=self.__virt_process.get_connections() self.__cpuUsage=psutil.cpu_percent()
def _collect_and_write(self, process, writer, ts=None, iostat=None, netstat=None): import psutil if ts is None: ts = time.time() cpu_percent = process.get_cpu_percent() cpu_times = process.get_cpu_times() mem_info = process.get_memory_info() data = [ int(ts), process.pid, process.name, cpu_percent, cpu_times.user, cpu_times.system, process.get_memory_percent(), mem_info.rss, mem_info.vms ] box_data = None if iostat and netstat: box_cpu = psutil.cpu_times_percent() io = psutil.disk_io_counters() netio = psutil.net_io_counters() box_mem = psutil.phymem_usage() box_data = [ box_cpu.user, box_cpu.system, box_cpu.idle, io.read_bytes - iostat.read_bytes, io.write_bytes - iostat.write_bytes, netio.bytes_recv - netstat.bytes_recv, netio.bytes_sent - netstat.bytes_sent, box_mem.used, box_mem.free, (psutil.used_phymem() - psutil.cached_phymem()), box_mem.total ] else: box_data = [0, 0, 0, 0, 0, 0, 0, 0, 0] data.extend(box_data) # write data print >> writer, "\t".join([str(s) for s in data]) # process children for child in process.get_children(): self._collect_and_write(child, writer, ts=ts) pass
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(), )
def done(self): server = {} server['load_average'] = os.getloadavg() server['total_phymem'] = psutil.TOTAL_PHYMEM server['total_virtmem'] = psutil.total_virtmem() server['used_phymem'] = psutil.used_phymem() server['used_virtmem'] = psutil.used_virtmem() server['cpu_percent'] = psutil.cpu_percent() server['cpu_count'] = psutil.NUM_CPUS f_processes = [] processes = psutil.get_process_list() processes.sort(key=lambda x: x.get_cpu_times()) processes.reverse() for p in processes[:10]: f_processes.append((p.name, p.get_cpu_times(), p.get_memory_percent(),time.ctime(p.create_time),)) server['processes'] = f_processes return server
def hello_world_app(environ,start_response): message="" status = '200 ok' headers = [('Content-type', 'html; charset=utf-8')] start_response(status,headers) #1 psutil parameter boot_time = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S") print("\nBOOT TIME:", boot_time) #2 psutil parameter cpu_util = psutil.cpu_percent(interval=1, percpu=True) i=1 print("\nCPU UTILIZATION:") for cpu in cpu_util: print("CPU {} : {}% ---".format(i, cpu)) i+=1 #3 psutil parameter mem = psutil.virtual_memory() availablemem=psutil.avail_virtmem() #4 psutil parameter usedmem=psutil.used_phymem() #5 psutil parameter usedper=mem.percent THRESHOLD = 100 * 1024 * 1024 # 100MB message +="<TABLE border=10> <TR> <TD> BOOT TIME</TD><TD>"+boot_time+"</TD></TR>" #message +="<TR><TD>CPU UTILIZATION</TD><TD><TABLE BORDER =2><TR><TD>CPU1</TD><TD>VAL</TD></TR><TR><TD>CPU2</TD><TD>VAL</TD></TR><TR><TD>CPU3</TD><TD>VAL</TD></TR></TABLE></TD></TR>" message +="<TR> <TD> AVAILABLE MEMORY </TD><TD>"+str(availablemem)+"</TD></TR>" message +="<TR> <TD> USED MEMORY </TD><TD>"+str(usedmem)+"</TD></TR>" message +="<TR> <TD> USED PERCENT </TD><TD>"+str(usedper)+"</TD></TR></TABLE>" return[bytes(message,'utf-8')]
def get_used_mem(): return int(psutil.used_phymem()) / 1024
def percentage_ram_usage(self): total = psutil.TOTAL_PHYMEM used = psutil.used_phymem() val = int(float(used) / float(total) * 100.0) return self.xmlFormat('RAMUsage', val)
sensorvals[sensor] = value break except: sensorvals[sensor] = 0 else: sensorvals[sensor] = 0 sql("INSERT INTO temperature VALUES (%i, %f, %f, %f)" % (timestamp, sensorvals['cpu'], sensorvals['indoor'], sensorvals['outdoor']) ) # Load cpu = psutil.cpu_percent() sql("INSERT INTO load VALUES (%i, %d)" % (timestamp, cpu)) # Memory ram = int(psutil.used_phymem() / 1024 / 1024) swap = int(psutil.used_virtmem() / 1024 / 1024) sql("INSERT INTO memory VALUES (%i, %d, %d)" % (timestamp, ram, swap)) # Power sensorvals = {} got_data = False ec3k_cache_dir = os.path.join(config['cache_dir'], 'ec3k') if not os.path.exists(ec3k_cache_dir): os.makedirs(ec3k_cache_dir) for sensor in ['powersupply_12v']: if config['ec3k']['sensors'].get(sensor, False): sensorid = config['ec3k']['sensors'][sensor].upper() filename = "%s.json" % sensorid sensor_file = os.path.join(config['ec3k']['fs_dir'], filename) sensor_cache = os.path.join(ec3k_cache_dir, filename)
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
# -*- 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 update(self): """ Record information about the cpu and memory usage for this host into a buffer """ self.cpu_load_log.append(psutil.cpu_percent(interval=0, percpu=True)) self.phymem_used_log.append(psutil.used_phymem()) self.phymem_avail_log.append(psutil.avail_phymem())
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))
'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 get_system_info(self): cpu_percent = psutil.cpu_percent(interval=0.0, percpu=False) mem_percent = float(psutil.used_phymem()) / self.max_mem * 100 return cpu_percent, mem_percent
def test_used_phymem(self): x = psutil.used_phymem() self.assertTrue(isinstance(x, (int, long))) self.assertTrue(x > 0)
def meminfo(): return (physmem(), psutil.avail_phymem(), psutil.used_phymem())