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 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 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 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 get_system_status(self): system_info = [] get_sys_cpu_info = str(psutil.cpu_percent(interval=int(psutil.NUM_CPUS))) + "%" get_sys_mem_info = "total phymem: " + str(int(psutil.TOTAL_PHYMEM ) /1000 /1000) + " MB" get_sys_virtmem_info = "total virtmem: " + str(int(psutil.total_virtmem()) /1000 /1000) + " MB" system_info.append([get_sys_cpu_info,get_sys_mem_info,get_sys_virtmem_info]) return system_info
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 getMemStatus(self): memstatus = {} memstatus['total_phymem'] = str(int(psutil.TOTAL_PHYMEM ) /1024 /1024) + " MB" memstatus['total_virtmem'] = str(int(psutil.total_virtmem()) /1024 /1024) + " MB" memstatus['phymem_usage_percent'] = psutil.phymem_usage().percent memstatus['phymem_usage_used'] = str(int(psutil.phymem_usage().used) /1024 /1024) + "MB" memstatus['phtmem_usage_free'] = str(int(psutil.phymem_usage().free) /1024 /1024) + "MB" memstatus['virtmem_usage_percent'] = psutil.virtmem_usage().percent memstatus['virtmem_usage_used'] = str(int(psutil.virtmem_usage().used) /1024 /1024) + "MB" memstatus['virtmem_usage_free'] = str(int(psutil.virtmem_usage().free) /1024 /1024) + "MB" return memstatus
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 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 test_total_virtmem(self): # This test is not particularly accurate and may fail if the OS is # consuming memory for other applications. # We just want to test that the difference between psutil result # and sysctl's is not too high. p = subprocess.Popen("sysctl vm.vmtotal", shell=1, stdout=subprocess.PIPE) result = p.communicate()[0].strip() if sys.version_info >= (3,): result = str(result, sys.stdout.encoding) sysctl_total_virtmem, _ = parse_sysctl_vmtotal(result) psutil_total_virtmem = psutil.total_virtmem() difference = abs(sysctl_total_virtmem - psutil_total_virtmem) # On my system I get a difference of 4657152 bytes, probably because # the system is consuming memory for this same test. # Assuming psutil is right, let's use a tollerance of 10 MB and consider # the test as failed if we go over it. if difference > (10 * 2**20): self.fail("sysctl=%s; psutil=%s; difference=%s;" %( sysctl_total_virtmem, psutil_total_virtmem, difference))
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
# -*- 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 test_total_virtmem(self): x = psutil.total_virtmem() self.assertTrue(isinstance(x, (int, long))) self.assertTrue(x >= 0)
# -*- 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'):
def getMemPerc(cls): return "%03d" % int(100.0 * (1.0 - float(psutil.avail_virtmem()) / float(psutil.total_virtmem()))) + \ "m% "
def check_mem(): server = 'localhost' port = 2003 ## hostname needed for the graphite tree hostname = socket.gethostname() ## build the tree up a bit tree = hostname+".memory." ## build a dictionary of the values we want to graph try: mem_dict = {"total_physical_memory": psutil.phymem_usage().total, 'free_physical_memory':psutil.phymem_usage().free, "used_physical_memory": psutil.phymem_usage().used, "total_virtual_memory":psutil.total_virtmem(), "free_virtual_memory": psutil.avail_virtmem(), "used_virtual_memory": psutil.used_virtmem()} ## Iterate the dictionary and write out the value to graphite for each key for key in mem_dict: w2g(server, port, tree+key, mem_dict[key]) except Exception as e: ## clean up errors a bit print e exit(1)
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
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 swapinfo(): return (psutil.total_virtmem(),psutil.used_virtmem(),) # Return total and used swap
#os._exit(0) sent = True class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer): pass global auth auth = hashlib.sha512(hashlib.sha512(bot.config["user"]).hexdigest()+hashlib.sha512(bot.config["pass"]).hexdigest()).hexdigest() randport = random.randint(10000,12000) upnpdata = {} upnpdata = auth+",,,"+str(randport)+",,,"+bot.config["host_config_name"] upnp_response = client(bot.config["web_server_ip"],55555, upnpdata) if upnp_response=="clean": server_thread = threading.Thread(target=ThreadedTCPServer((bot.config["ip"], randport),ThreadedTCPRequestHandler).serve_forever) server_thread.daemon = True server_thread.start() global sysinfo sysinfo = {} sysinfo["ram"] = str(psutil.TOTAL_PHYMEM/1024/1024) sysinfo["swap"] = str(psutil.total_virtmem()/1024/1024) sysinfo["pyver"] = ('%s %s' % (platform.python_implementation(),platform.python_version())) while True: time.sleep(2) if upnp_response=="error": print("bad auth to web server. shutting down.") #server_thread = threading.Thread(target=ThreadedTCPServer((bot.config["ip"], int(bot.config["port"])),ThreadedTCPRequestHandler).serve_forever)