def Sysinfo(): Hostname = platform.node() Sys_version = platform.platform() Boot_Start = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(psutil.boot_time())) time.sleep(0.5) Cpu_usage = psutil.cpu_percent() RAM = int(psutil.virtual_memory().total/(1027*1024)) RAM_percent = psutil.virtual_memory().percent Swap = int(psutil.swap_memory().total/(1027*1024)) Swap_percent = psutil.swap_memory().percent Net_sent = psutil.net_io_counters().bytes_sent Net_recv = psutil.net_io_counters().bytes_recv Net_spkg = psutil.net_io_counters().packets_sent Net_rpkg = psutil.net_io_counters().packets_recv BFH = r'%' print " \033[1;32m主机名: %s\033[1;m" % Hostname print " \033[1;32m系统版本: %s\033[1;m" % Sys_version print " \033[1;32m开机时间:%s\033[1;m" % Boot_Start print " \033[1;32m当前CPU使用率:%s%s\033[1;m" % (Cpu_usage,BFH) print " \033[1;32m物理内存:%dM\t使用率:%s%s\033[1;m" % (RAM,RAM_percent,BFH) print " \033[1;32mSwap内存:%dM\t使用率:%s%s\033[1;m" % (Swap,Swap_percent,BFH) print " \033[1;32m发送:%d Byte\t发送包数:%d个\033[1;m" % (Net_sent,Net_spkg) print " \033[1;32m接收:%d Byte\t接收包数:%d个\033[1;m" % (Net_recv,Net_rpkg) for i in psutil.disk_partitions(): print " \033[1;32m盘符: %s 挂载点: %s 使用率: %s%s\033[1;m" % (i[0],i[1],psutil.disk_usage(i[1])[3],BFH) for key in psutil.net_if_addrs().keys(): print " \033[1;32m网卡: %s IP: %s\033[1;m" % (key,psutil.net_if_addrs()[key][0][1])
def sysinfo(): boot_start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(psutil.boot_time())) time.sleep(0.5) cpu_usage = psutil.cpu_percent() ram = int(psutil.virtual_memory().total / (1027 * 1024)) ram_percent = psutil.virtual_memory().percent swap = int(psutil.swap_memory().total / (1027 * 1024)) swap_percent = psutil.swap_memory().percent net_sent = psutil.net_io_counters().bytes_sent net_recv = psutil.net_io_counters().bytes_recv net_spkg = psutil.net_io_counters().packets_sent net_rpkg = psutil.net_io_counters().packets_recv sysinfo_string = "" if __name__ == "__main__": bfh = r'%' print(" \033[1;32m开机时间:%s\033[1;m" % boot_start) print(" \033[1;32m当前CPU使用率:%s%s\033[1;m" % (cpu_usage, bfh)) print(" \033[1;32m物理内存:%dM\t使用率:%s%s\033[1;m" % (ram, ram_percent, bfh)) print(" \033[1;32mSwap内存:%dM\t使用率:%s%s\033[1;m" % (swap, swap_percent, bfh)) print(" \033[1;32m发送:%d Byte\t发送包数:%d个\033[1;m" % (net_sent, net_spkg)) print(" \033[1;32m接收:%d Byte\t接收包数:%d个\033[1;m" % (net_recv, net_rpkg)) for i in psutil.disk_partitions(): print(" \033[1;32m盘符: %s 挂载点: %s 使用率: %s%s\033[1;m" % (i[0], i[1], psutil.disk_usage(i[1])[3], bfh)) sysinfo_string = '开机时间: ' + boot_start + '\n' sysinfo_string += '当前CPU使用率: ' + str(cpu_usage) + '%\n' sysinfo_string += '物理内存: ' + str(ram) + 'M\t使用率: ' + str(ram_percent) + '%\t' sysinfo_string += 'Swap内存: ' + str(swap) + 'M\t使用率: ' + str(swap_percent) + '%\n' return sysinfo_string else: file = open("sysinfo.log", "a") file.write("CPU:%s \tRAM:%s\tnet_recv:%d\tNet_sent:%d\r\n" % (cpu_usage, ram_percent, net_recv, net_sent)) file.flush() file.close()
def CatchMEM(RunNumb, Interval, TimeStart): """ Funzione per catturare i dati della Memoria Cattura le percentuali di utilizzo della memoria e della swap medie nell'intervallo richiesto e le impacchetta per formare la risposta. :param RunNumb: Il numero della Run corrente (al momento in cui è stato richiesto il lavoro al server) :param Interval: Intervallo tra un campionamento e l'altro. :param TimeStart: Tempo di avvio di questo campionamento, lo restituisco in uscita per poter calcolare la latenza. :returns: Una tupla contenente i dati rilevati nel Interval. """ #selettori per andare a selezionare la percentuale di memoria utilizzata nelle rispettive liste ritornate da psutil SelPerc = 3 Average_MEM, Average_SWP = 0, 0 for i in range(Interval): #Average_MEM += psutil.phymem_usage()[SelPerc]/(Interval+1) #Average_SWP += psutil.virtmem_usage()[SelPerc]/(Interval+1) Average_MEM += psutil.virtual_memory().percent/(Interval+1) Average_SWP += psutil.swap_memory().percent/(Interval+1) time.sleep(1) Average_MEM += psutil.virtual_memory().percent/(Interval+1) Average_SWP += psutil.swap_memory().percent/(Interval+1) ProbeManager.PutResult([ProbeID, RunNumb, "MEM", Average_MEM, Average_SWP, TimeStart])
def __init__(self, interval, history_len): """ :param interval: Collect statistics according to this interval. :param history_len: Use this many to compute avg/max statistics. """ self.interval = interval self.history_len = history_len try: import psutil # @UnresolvedImport @Reimport except: self._available = False else: self._available = True self.cpu = Collect('cpu', lambda: psutil.cpu_percent(interval=0), interval, history_len) try: # new in 0.8 psutil.virtual_memory().percent get_mem = lambda: psutil.virtual_memory().percent except: get_mem = lambda: psutil.phymem_usage().percent self.mem = Collect('mem', get_mem, interval, history_len) try: # new in 0.8 psutil.swap_memory().percent get_mem = lambda: psutil.swap_memory().percent except: get_mem = lambda: psutil.virtmem_usage().percent self.swap_mem = Collect('swap', get_mem, interval, history_len)
def update(self): """Get the latest system information.""" import psutil if self.type == 'disk_use_percent': self._state = psutil.disk_usage(self.argument).percent elif self.type == 'disk_use': self._state = round(psutil.disk_usage(self.argument).used / 1024**3, 1) elif self.type == 'disk_free': self._state = round(psutil.disk_usage(self.argument).free / 1024**3, 1) elif self.type == 'memory_use_percent': self._state = psutil.virtual_memory().percent elif self.type == 'memory_use': self._state = round((psutil.virtual_memory().total - psutil.virtual_memory().available) / 1024**2, 1) elif self.type == 'memory_free': self._state = round(psutil.virtual_memory().available / 1024**2, 1) elif self.type == 'swap_use_percent': self._state = psutil.swap_memory().percent elif self.type == 'swap_use': self._state = round(psutil.swap_memory().used / 1024**3, 1) elif self.type == 'swap_free': self._state = round(psutil.swap_memory().free / 1024**3, 1) elif self.type == 'processor_use': self._state = round(psutil.cpu_percent(interval=None)) elif self.type == 'process': if any(self.argument in l.name() for l in psutil.process_iter()): self._state = STATE_ON else: self._state = STATE_OFF elif self.type == 'network_out' or self.type == 'network_in': counters = psutil.net_io_counters(pernic=True) if self.argument in counters: counter = counters[self.argument][IO_COUNTER[self.type]] self._state = round(counter / 1024**2, 1) else: self._state = STATE_UNKNOWN elif self.type == 'packets_out' or self.type == 'packets_in': counters = psutil.net_io_counters(pernic=True) if self.argument in counters: self._state = counters[self.argument][IO_COUNTER[self.type]] else: self._state = STATE_UNKNOWN elif self.type == 'ipv4_address' or self.type == 'ipv6_address': addresses = psutil.net_if_addrs() if self.argument in addresses: self._state = addresses[self.argument][IF_ADDRS[self.type]][1] else: self._state = STATE_UNKNOWN elif self.type == 'last_boot': self._state = dt_util.as_local( dt_util.utc_from_timestamp(psutil.boot_time()) ).date().isoformat() elif self.type == 'since_last_boot': self._state = dt_util.utcnow() - dt_util.utc_from_timestamp( psutil.boot_time())
def update(self): """ Get the latest system informations. """ import psutil if self.type == 'disk_use_percent': self._state = psutil.disk_usage(self.argument).percent elif self.type == 'disk_use': self._state = round(psutil.disk_usage(self.argument).used / 1024**3, 1) elif self.type == 'disk_free': self._state = round(psutil.disk_usage(self.argument).free / 1024**3, 1) elif self.type == 'memory_use_percent': self._state = psutil.virtual_memory().percent elif self.type == 'memory_use': self._state = round((psutil.virtual_memory().total - psutil.virtual_memory().available) / 1024**2, 1) elif self.type == 'memory_free': self._state = round(psutil.virtual_memory().available / 1024**2, 1) elif self.type == 'swap_use_percent': self._state = psutil.swap_memory().percent elif self.type == 'swap_use': self._state = round(psutil.swap_memory().used / 1024**3, 1) elif self.type == 'swap_free': self._state = round(psutil.swap_memory().free / 1024**3, 1) elif self.type == 'processor_use': self._state = round(psutil.cpu_percent(interval=None)) elif self.type == 'process': if any(self.argument in l.name() for l in psutil.process_iter()): self._state = STATE_ON else: self._state = STATE_OFF elif self.type == 'network_out': self._state = round(psutil.net_io_counters(pernic=True) [self.argument][0] / 1024**2, 1) elif self.type == 'network_in': self._state = round(psutil.net_io_counters(pernic=True) [self.argument][1] / 1024**2, 1) elif self.type == 'packets_out': self._state = psutil.net_io_counters(pernic=True)[self.argument][2] elif self.type == 'packets_in': self._state = psutil.net_io_counters(pernic=True)[self.argument][3] elif self.type == 'ipv4_address': self._state = psutil.net_if_addrs()[self.argument][0][1] elif self.type == 'ipv6_address': self._state = psutil.net_if_addrs()[self.argument][1][1] elif self.type == 'last_boot': self._state = dt_util.datetime_to_date_str( dt_util.as_local( dt_util.utc_from_timestamp(psutil.boot_time()))) elif self.type == 'since_last_boot': self._state = dt_util.utcnow() - dt_util.utc_from_timestamp( psutil.boot_time())
def get_stats(name, nic): global prev_recv global prev_sent global prev_read global prev_write with open('/proc/uptime', 'r') as f: uptime_seconds = float(f.readline().split()[0]) stats = {'date': datetime.datetime.now().isoformat(), 'name': name, 'cpu': psutil.cpu_percent(interval=None, percpu=True), 'cpu_count': psutil.cpu_count(), 'cpu_ctx_switches': psutil.cpu_stats().ctx_switches, 'cpu_interrupts': psutil.cpu_stats().interrupts, 'ram': psutil.virtual_memory().percent, 'ram-available': psutil.virtual_memory().available, 'ram-used': psutil.virtual_memory().used, 'swap': psutil.swap_memory().percent, 'swap-total': psutil.swap_memory().total, 'swap-used': psutil.swap_memory().used, 'disk_io_read': psutil.disk_io_counters().read_bytes, 'disk_io_write': psutil.disk_io_counters().write_bytes, 'disk_total': psutil.disk_usage('/').total, 'disk_used': psutil.disk_usage('/').used, 'uptime': uptime_seconds} nic_list = psutil.net_io_counters(pernic=True) nic = nic_list[nic] stats['packets_sent'] = [nic.packets_sent, nic.errout] stats['packets_recv'] = [nic.packets_recv, nic.errin] stats['bytes_recv'] = nic.bytes_recv stats['bytes_sent'] = nic.bytes_sent if prev_recv != -1: stats['dl_rate'] = stats['bytes_recv'] - prev_recv else: stats['dl_rate'] = 0 prev_recv = stats['bytes_recv'] if prev_sent != -1: stats['ul_rate'] = stats['bytes_sent'] - prev_sent else: stats['ul_rate'] = 0 prev_sent = stats['bytes_sent'] if prev_read != -1: stats['disk_read_rate'] = stats['disk_io_read'] - prev_read else: stats['disk_read_rate'] = 0 prev_read = stats['disk_io_read'] if prev_read != -1: stats['disk_write_rate'] = stats['disk_io_write'] - prev_write else: stats['disk_write_rate'] = 0 prev_write = stats['disk_io_write'] return stats
def getDetails(): # return these details deets = {"cpu_usage":None,"memory_usage":None,"disk_usage":None,"os_version":platform.platform()} deets['cpu_usage'] = psutil.cpu_percent(interval=None) deets['memory_usage'] = psutil.virtual_memory().percent deets['disk_usage'] = psutil.disk_usage('/').percent #### # Bonus data: #### # CPU deets['cpu_details'] = {'details':[]} cpu_details = psutil.cpu_percent(interval=None, percpu=True) deets['cpu_details']['count'] = len(cpu_details) for i in range(0,len(cpu_details)): deets['cpu_details']['details'].append({'name':i,'cpu_usage':cpu_details[i]}) # Memory deets['memory_details'] = {} deets['memory_details']['virtual'] = { 'percent':psutil.virtual_memory().percent, 'total':psutil.virtual_memory().total } deets['memory_details']['swap'] = { 'percent':psutil.swap_memory().percent, 'total':psutil.swap_memory().total } # Disk deets['disk_details'] = {} deets['disk_details']['partition_count'] = len(psutil.disk_partitions()) deets['disk_details']['partition_count'] = len(psutil.disk_partitions()) deets['disk_details']['partitions'] = [] for partition in psutil.disk_partitions(): deets['disk_details']['partitions'].append({ 'device':partition.device, 'mountpoint':partition.mountpoint, 'percent':psutil.disk_usage(partition.mountpoint).percent, 'total':psutil.disk_usage(partition.mountpoint).total, }) deets['disk_details']['iostats'] = {} sdiskio = psutil.disk_io_counters() for k in ['read_count','write_count','read_bytes','write_bytes','read_time','write_time']: deets['disk_details']['iostats'][k] = getattr(sdiskio,k) deets['hostname'] = platform.node() deets['boottime'] = datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S") return deets
def wrapper(*args, **kwds): print("### running test {f}".format(f=f)) print("available physical memory before {}".format( psutil.virtual_memory())) print("available swap memory before {}".format( psutil.swap_memory())) result = f(*args, **kwds) print("available physical memory after {}".format( psutil.virtual_memory())) print("available swap memory after {}".format( psutil.swap_memory())) return result
def get_memory_node(): mem_virt_total = RunnableNode('total', method=lambda: (ps.virtual_memory().total, 'b')) mem_virt_available = RunnableNode('available', method=lambda: (ps.virtual_memory().available, 'b')) mem_virt_percent = RunnableNode('percent', method=lambda: (ps.virtual_memory().percent, '%')) mem_virt_used = RunnableNode('used', method=lambda: (ps.virtual_memory().used, 'b')) mem_virt_free = RunnableNode('free', method=lambda: (ps.virtual_memory().free, 'b')) mem_virt = ParentNode('virtual', children=(mem_virt_total, mem_virt_available, mem_virt_free, mem_virt_percent, mem_virt_used)) mem_swap_total = RunnableNode('total', method=lambda: (ps.swap_memory().total, 'b')) mem_swap_percent = RunnableNode('percent', method=lambda: (ps.swap_memory().percent, '%')) mem_swap_used = RunnableNode('used', method=lambda: (ps.swap_memory().used, 'b')) mem_swap_free = RunnableNode('free', method=lambda: (ps.swap_memory().free, 'b')) mem_swap = ParentNode('swap', children=[mem_swap_total, mem_swap_free, mem_swap_percent, mem_swap_used]) return ParentNode('memory', children=[mem_virt, mem_swap])
def _prelaunch(self, operation, uid=None, available_disk_space=0, **kwargs): """ Method to wrap LAUNCH. Will prepare data, and store results on return. """ self.meta_data.update(json.loads(operation.meta_data)) self.storage_path = self.file_handler.get_project_folder(operation.project, str(operation.id)) self.operation_id = operation.id self.current_project_id = operation.project.id self.user_id = operation.fk_launched_by self.configure(**kwargs) # Compare the amount of memory the current algorithms states it needs, # with the average between the RAM available on the OS and the free memory at the current moment. # We do not consider only the free memory, because some OSs are freeing late and on-demand only. total_free_memory = psutil.virtual_memory().free + psutil.swap_memory().free total_existent_memory = psutil.virtual_memory().total + psutil.swap_memory().total memory_reference = (total_free_memory + total_existent_memory) / 2 adapter_required_memory = self.get_required_memory_size(**kwargs) if adapter_required_memory > memory_reference: msg = "Machine does not have enough RAM memory for the operation (expected %.2g GB, but found %.2g GB)." raise NoMemoryAvailableException(msg % (adapter_required_memory / 2 ** 30, memory_reference / 2 ** 30)) # Compare the expected size of the operation results with the HDD space currently available for the user # TVB defines a quota per user. required_disk_space = self.get_required_disk_size(**kwargs) if available_disk_space < 0: msg = "You have exceeded you HDD space quota by %.2f MB Stopping execution." raise NoMemoryAvailableException(msg % (-available_disk_space / 2 ** 10)) if available_disk_space < required_disk_space: msg = ( "You only have %.2f GB of disk space available but the operation you " "launched might require %.2f Stopping execution..." ) raise NoMemoryAvailableException(msg % (available_disk_space / 2 ** 20, required_disk_space / 2 ** 20)) operation.start_now() operation.estimated_disk_size = required_disk_space dao.store_entity(operation) result = self.launch(**kwargs) if not isinstance(result, (list, tuple)): result = [result] self.__check_integrity(result) return self._capture_operation_results(result, uid)
def courtesyCheck(THRESHOLD): ram = psutil.virtual_memory() swp = psutil.swap_memory() while ram.percent > THRESHOLD: if swp.total == 0: print '[!] Notdbd: Taking a break since memory usage is high ...' time.sleep(30) elif swp.percent > THRESHOLD: print '[!] Notdbd: Taking a break since memory usage is high ...' time.sleep(30) else: break ram = psutil.virtual_memory() swp = psutil.swap_memory() return
def status (interval=0.1): """Retrieve system resource usage.""" cpu = cpu_percent(interval) mem = virtual_memory().percent swap = swap_memory().percent disk = disk_usage("/").percent return (cpu, mem, swap, disk)
def publishData(self): timestamp = time.time() info = {''} if self._pir == None: cpu_use = ps.cpu_percent() users = [u.name for u in ps.users()] nProcesses = len(ps.pids()) memUse = ps.virtual_memory().percent swapUse = ps.swap_memory().percent info = {'count': self._count, 'cpu_usage':cpu_use, 'users':users, 'processes':nProcesses, 'memory_usage':memUse, 'swap_usage':swapUse} else: info = {'count': self._count, 'pir_bool': self._pir.read()} self._count += 1 dataOut = Data(Name(self._dataPrefix).appendVersion(int(timestamp))) dataOut.setContent(json.dumps(info)) dataOut.getMetaInfo().setFreshnessPeriod(10000) self.signData(dataOut) #self._dataCache.add(dataOut) # instead of adding data to content cache, we put data to nfd anyway self.send(dataOut.wireEncode().buf()) print('data name: ' + dataOut.getName().toUri() + '; content: ' + str(info)) # repeat every 1 seconds self.loop.call_later(1, self.publishData)
def tick(self): swap = psutil.swap_memory() memory = psutil.virtual_memory() message = json.dumps({ 'host1': { 'memory': { 'swap_memory' : { 'name': 'Swap', 'min': 0, 'max': swap.total / (1024*10124), 'value': swap.used / (1024*10124), }, 'virtual_memory': { 'name': 'Virual Memory', 'min': 0, 'max': memory.total / (1024*1024), 'value': memory.used / (1024*1024), } }, 'cpu': { 'min': 0, 'max': 100, 'value': psutil.cpu_percent() } } }) self.logger.debug("SystemWebSocket: tick=%s" % message) self.write_message(message)
def _refresh(self): if self._type == 'virt': memUsage = psutil.virtual_memory() else: memUsage = psutil.swap_memory() if self._total != humanize_bytes(memUsage.total, 2): self._total = humanize_bytes(memUsage.total, 2) self.totalChanged.emit(self._total) if self._type == 'virt': if self._available != humanize_bytes(memUsage.available, 2): self._available = humanize_bytes(memUsage.available, 2) self.availableChanged.emit(self._available) if self._percent != str(memUsage.percent) + '%': self._percent = str(memUsage.percent) + '%' self.percentChanged.emit(self._percent) if self._used != humanize_bytes(memUsage.used, 2): self._used = humanize_bytes(memUsage.used, 2) self.usedChanged.emit(self._used) if self._free != humanize_bytes(memUsage.free, 2): self._free = humanize_bytes(memUsage.free, 2) self.freeChanged.emit(self._free)
def update(self): """Update quicklook stats using the input method.""" # Reset stats self.reset() # Grab quicklook stats: CPU, MEM and SWAP if self.input_method == 'local': # Get the latest CPU percent value self.stats['cpu'] = cpu_percent.get() self.stats['percpu'] = cpu_percent.get(percpu=True) # Use the PsUtil lib for the memory (virtual and swap) self.stats['mem'] = psutil.virtual_memory().percent self.stats['swap'] = psutil.swap_memory().percent elif self.input_method == 'snmp': # Not available pass # Optionnaly, get the CPU name/frequency # thanks to the cpuinfo lib: https://github.com/workhorsy/py-cpuinfo if cpuinfo_tag: cpu_info = cpuinfo.get_cpu_info() self.stats['cpu_name'] = cpu_info['brand'] self.stats['cpu_hz_current'] = cpu_info['hz_actual_raw'][0] self.stats['cpu_hz'] = cpu_info['hz_advertised_raw'][0] # Update the view self.update_views() return self.stats
def get_dict_smem_usage(): smem = psutil.swap_memory() return { 'total': smem.total, 'free': smem.free, 'used': smem.percent }
def index(): load_avg = os.getloadavg() uptime = datetime.now() - datetime.fromtimestamp(psutil.get_boot_time()) disks = get_disks() users = get_users() netifs = get_network_interfaces() netifs.sort(key=lambda x: x.get("bytes_sent"), reverse=True) data = { "os": platform.platform().decode("utf-8"), "hostname": socket.gethostname().decode("utf-8"), "uptime": str(uptime).split(".")[0], "load_avg": load_avg, "cpus": psutil.NUM_CPUS, "vmem": psutil.virtual_memory(), "swap": psutil.swap_memory(), "disks": disks, "cpu_percent": psutil.cpu_times_percent(0), "users": users, "net_interfaces": netifs, "page": "overview", "is_xhr": request.is_xhr } return render_template("index.html", **data)
def getSI(val=None): """ Récupération de données système ajoutées dans uns liste Arguments: val -- métriques à acquérir """ if val == None: val = dict() val["time"] = [] val["mem"] = [] val["swap"] = [] val["io_read"] = [] val["io_write"] = [] val["net_sent"] = [] val["net_recv"] = [] val["cpu"] = [] val["time"] += [time.time()] val["mem"] += [psutil.virtual_memory()[2]] val["swap"] += [psutil.swap_memory()[3]] val["io_read"] += [psutil.disk_io_counters(perdisk=False)[2]] val["io_write"] += [psutil.disk_io_counters(perdisk=False)[3]] val["net_sent"] += [psutil.network_io_counters(pernic=False)[0]] val["net_recv"] += [psutil.network_io_counters(pernic=False)[1]] val["cpu"] += [psutil.cpu_percent(interval=0.8)] return val
def swap(self): """ swap """ swap_memory = psutil.swap_memory() self.statsd.gauge('system.swap.total', swap_memory.total) self.statsd.gauge('system.swap.used', swap_memory.used) self.statsd.gauge('system.swap.free', swap_memory.free) self.statsd.gauge('system.swap.pct_free', swap_memory.percent)
def collect_ram_stats(): system_memory = psutil.virtual_memory() swap_memory = psutil.swap_memory() data = { "system_memory": { "total": system_memory.total, "available": system_memory.available, "percent": system_memory.percent, "used": system_memory.used, "free": system_memory.free, "active": system_memory.active, "inactive": system_memory.inactive, "buffers": system_memory.buffers, "cached": system_memory.cached }, "swap_memory": { "total": swap_memory.total, "used": swap_memory.used, "free": swap_memory.free, "percent": swap_memory.percent, "sin": swap_memory.sin, "sout": swap_memory.sout } } return data
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 get_mem_swap(self,): """ Get memory and swap usage """ try: # psutil >= 0.6.0 phymem = psutil.virtual_memory() buffers = psutil.virtual_memory().buffers cached = psutil.virtual_memory().cached vmem = psutil.swap_memory() except AttributeError: # psutil > 0.4.0 and < 0.6.0 phymem = psutil.phymem_usage() buffers = getattr(psutil, 'phymem_buffers', lambda: 0)() cached = getattr(psutil, 'cached_phymem', lambda: 0)() vmem = psutil.virtmem_usage() mem_used = phymem.total - (phymem.free + buffers + cached) return ( phymem.percent, mem_used, phymem.total, vmem.percent, vmem.used, vmem.total)
def _run(self): for k,s in self.streamers.iteritems(): s.process.start() print '_run: started all workers' sys.stdout.flush() t0 = time() elapsed = 0.0 self.ram,self.swap,self.t = [],[],[] print 'duration = %f' % self.duration sys.stdout.flush() while elapsed < self.duration: print '_run: logging psutil elapsed = %f' % elapsed sys.stdout.flush() self.ram.append(psutil.virtual_memory().used / 1e9) self.swap.append(psutil.swap_memory().used / 1e9) self.t.append(time()) wait(1) elapsed = self.t[-1] - t0 #print time() #sys,stdout.flush() print '_run end' sys.stdout.flush()
def main(): hostname = getfqdn() data = '' for k, v in psutil.cpu_times_percent(interval=0.5)._asdict().iteritems(): data += '{0}.cpu.{1} value={2}\n'.format(hostname, k, v) for k, v in psutil.virtual_memory()._asdict().iteritems(): data += '{0}.mem.{1} value={2}\n'.format(hostname, k, v) for k, v in psutil.swap_memory()._asdict().iteritems(): data += '{0}.swap.{1} value={2}\n'.format(hostname, k, v) du = { mp.mountpoint: psutil.disk_usage(mp.mountpoint).percent for mp in psutil.disk_partitions() } for k, v in du.iteritems(): data += '{0}.du_percent.{1} value={2}\n'.format(hostname, k, v) for k, v in psutil.disk_io_counters(perdisk=True).iteritems(): for m, p in v._asdict().iteritems(): data += '{0}.io.{1}.{2} value={3}\n'.format(hostname, k, m, p) for k, v in psutil.net_io_counters(pernic=True).iteritems(): for m, p in v._asdict().iteritems(): data += '{0}.net.{1}.{2} value={3}\n'.format(hostname, k, m, p) connections = map(lambda x: x.status.lower(), psutil.net_connections()) connections = map( lambda x: 'unknown_state' if x == 'none' else x, connections ) for conn in set(connections): data += '{0}.netstat.{1} value={2}\n'.format(hostname, conn, connections.count(conn)) if DEBUG: print data sendData(data)
def run_info(): return dict( cpu_usage = psutil.cpu_percent(), boot_time = psutil.boot_time(), swap_usage = psutil.swap_memory().used, mem_usage = psutil.virtual_memory().used )
def status (self): # pylint: disable=unused-argument rc = { "hostname": socket.gethostname (), "cpu": { "count": psutil.cpu_count (), "times": psutil.cpu_times (), "percent": psutil.cpu_percent (percpu = True), }, "memory": { "virtual": psutil.virtual_memory (), "swap": psutil.swap_memory (), }, "net": psutil.net_io_counters (), "uptime": { "ut": psutil.boot_time (), "since": logtool.time_str (psutil.boot_time (), slug = True), }, "disk": {}, } for part in psutil.disk_partitions (): rc["disk"]["mounts"] = { part.mountpoint: psutil.disk_usage (part.mountpoint), } for disk, io in psutil.disk_io_counters (perdisk = True).items (): rc["disk"]["io"] = { disk: io } return rc
def _getSummary(): """ :returns: a _CsvRow object """ timestamp = time.time() loadAvg1, loadAvg5, loadAvg15 = os.getloadavg() cpuTimesPct = psutil.cpu_times_percent(interval=0) virtualMem = psutil.virtual_memory() swapMem = psutil.swap_memory() row = _CsvRow( timestamp=timestamp, loadAvg1=loadAvg1, loadAvg5=loadAvg5, loadAvg15=loadAvg15, cpuUserPct=cpuTimesPct.user, cpuSystemPct=cpuTimesPct.system, cpuNicePct=cpuTimesPct.nice, cpuIdlePct=cpuTimesPct.idle, memTotalB=virtualMem.total, memUsageB=virtualMem.total - virtualMem.available, memAvailB=virtualMem.available, memUsagePct=virtualMem.percent, memBuffersB=virtualMem.buffers if hasattr(virtualMem, "buffers") else None, memCachedB=virtualMem.cached if hasattr(virtualMem, "cached") else None, swapTotalB=swapMem.total, swapUsedB=swapMem.used, swapFreeB=swapMem.free, swapUsedPct=swapMem.percent, swapInsB=swapMem.sin, swapOutsB=swapMem.sout ) return row
def report_mem(prev_values=None, msg=None): """ Show status of phy/virt mem, and if given previous values, report differences requires the psutil module - avail in apt-get, but I used pip """ if msg is None: msg = "" else: msg += ": " if type(prev_values) == type(""): # catch error in prev_values msg += prev_values prev_values = None print("need msg= in call to report_mem") # available in linux is free + buffers + cached if hasattr(psutil, "swap_memory"): # new version (0.7+) pm = psutil.virtual_memory().available # was psutil.avail_phymem() vm = psutil.swap_memory().free # was psutil.avail_virtmem() else: pm = psutil.phymem_usage().free # was .avail_phymem() vm = psutil.virtmem_usage().free # avail_virtmem() tim = seconds() print("{msg}{pm:.3g} GB phys mem, {vm:.3g} GB virt mem avail".format(msg=msg, pm=pm / 1e9, vm=vm / 1e9)), if prev_values is None: print() else: print( "- dt={dt:.2g}s, used {pm:.2g} GB phys, {vm:.2g} GB virt".format( pm=(prev_values[0] - pm) / 1e9, vm=(prev_values[1] - vm) / 1e9, dt=tim - prev_values[2] ) ) return (pm, vm, tim)
async def info(self, ctx, *args: str): """Summary of cpu, memory, disk and network information Usage: info [option] Examples: sysinfo Shows all available info sysinfo cpu Shows CPU usage sysinfo memory Shows memory usage sysinfo file Shows full path of open files sysinfo disk Shows disk usage sysinfo network Shows network usage sysinfo boot Shows boot time """ options = ('cpu', 'memory', 'file', 'disk', 'network', 'boot') # CPU cpu_count_p = psutil.cpu_count(logical=False) cpu_count_l = psutil.cpu_count() if cpu_count_p is None: cpu_count_p = "N/A" cpu_cs = ("CPU Count" "\n\t{0:<9}: {1:>3}".format("Physical", cpu_count_p) + "\n\t{0:<9}: {1:>3}".format("Logical", cpu_count_l)) psutil.cpu_percent(interval=None, percpu=True) await asyncio.sleep(1) cpu_p = psutil.cpu_percent(interval=None, percpu=True) cpu_ps = ("CPU Usage" "\n\t{0:<8}: {1}".format("Per CPU", cpu_p) + "\n\t{0:<8}: {1:.1f}%".format("Overall", sum(cpu_p) / len(cpu_p))) cpu_t = psutil.cpu_times() width = max([ len("{:,}".format(int(n))) for n in [cpu_t.user, cpu_t.system, cpu_t.idle] ]) cpu_ts = ("CPU Times" "\n\t{0:<7}: {1:>{width},}".format( "User", int(cpu_t.user), width=width) + "\n\t{0:<7}: {1:>{width},}".format( "System", int(cpu_t.system), width=width) + "\n\t{0:<7}: {1:>{width},}".format( "Idle", int(cpu_t.idle), width=width)) # Memory mem_v = psutil.virtual_memory() width = max([ len(self._size(n)) for n in [mem_v.total, mem_v.available, (mem_v.total - mem_v.available)] ]) mem_vs = ("Virtual Memory" "\n\t{0:<10}: {1:>{width}}".format( "Total", self._size(mem_v.total), width=width) + "\n\t{0:<10}: {1:>{width}}".format( "Available", self._size(mem_v.available), width=width) + "\n\t{0:<10}: {1:>{width}} {2}%".format( "Used", self._size(mem_v.total - mem_v.available), mem_v.percent, width=width)) mem_s = psutil.swap_memory() width = max([ len(self._size(n)) for n in [mem_s.total, mem_s.free, (mem_s.total - mem_s.free)] ]) mem_ss = ("Swap Memory" "\n\t{0:<6}: {1:>{width}}".format( "Total", self._size(mem_s.total), width=width) + "\n\t{0:<6}: {1:>{width}}".format( "Free", self._size(mem_s.free), width=width) + "\n\t{0:<6}: {1:>{width}} {2}%".format( "Used", self._size(mem_s.total - mem_s.free), mem_s.percent, width=width)) # Open files open_f = psutil.Process().open_files() open_fs = "Open File Handles\n\t" if open_f: if hasattr(open_f[0], "mode"): open_fs += "\n\t".join( ["{0} [{1}]".format(f.path, f.mode) for f in open_f]) else: open_fs += "\n\t".join(["{0}".format(f.path) for f in open_f]) else: open_fs += "None" # Disk usage disk_u = psutil.disk_usage(os.path.sep) width = max([ len(self._size(n)) for n in [disk_u.total, disk_u.free, disk_u.used] ]) disk_us = ( "Disk Usage" "\n\t{0:<6}: {1:>{width}}".format( "Total", self._size(disk_u.total), width=width) + "\n\t{0:<6}: {1:>{width}}".format( "Free", self._size(disk_u.free), width=width) + "\n\t{0:<6}: {1:>{width}} {2}%".format( "Used", self._size(disk_u.used), disk_u.percent, width=width)) # Network net_io = psutil.net_io_counters() width = max([ len(self._size(n)) for n in [net_io.bytes_sent, net_io.bytes_recv] ]) net_ios = ( "Network" "\n\t{0:<11}: {1:>{width}}".format( "Bytes sent", self._size(net_io.bytes_sent), width=width) + "\n\t{0:<11}: {1:>{width}}".format( "Bytes recv", self._size(net_io.bytes_recv), width=width)) # Boot time boot_s = ("Boot Time" "\n\t{0}".format( datetime.datetime.fromtimestamp( psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S"))) # Output msg = "" if not args or args[0].lower() not in options: msg = "\n\n".join([ cpu_cs, cpu_ps, cpu_ts, mem_vs, mem_ss, open_fs, disk_us, net_ios, boot_s ]) elif args[0].lower() == 'cpu': msg = "\n" + "\n\n".join([cpu_cs, cpu_ps, cpu_ts]) elif args[0].lower() == 'memory': msg = "\n" + "\n\n".join([mem_vs, mem_ss]) elif args[0].lower() == 'file': msg = "\n" + open_fs elif args[0].lower() == 'disk': msg = "\n" + disk_us elif args[0].lower() == 'network': msg = "\n" + net_ios elif args[0].lower() == 'boot': msg = "\n" + boot_s await self._say(ctx, msg) return
async def top(self, ctx): """Snapshot of real-time system information and tasks""" # sleep some time psutil.cpu_percent(interval=None, percpu=True) await asyncio.sleep(1) procs = [] procs_status = {} for p in psutil.process_iter(): try: p.dict = p.as_dict([ 'username', 'nice', 'memory_info', 'memory_percent', 'cpu_percent', 'cpu_times', 'name', 'status' ]) try: procs_status[p.dict['status']] += 1 except KeyError: procs_status[p.dict['status']] = 1 except psutil.NoSuchProcess: pass else: procs.append(p) # return processes sorted by CPU percent usage processes = sorted(procs, key=lambda p: p.dict['cpu_percent'], reverse=True) # Print system-related info, above the process list msg = "" num_procs = len(procs) def get_dashes(perc): dashes = "|" * int((float(perc) / 10 * 4)) empty_dashes = " " * (40 - len(dashes)) return dashes, empty_dashes # cpu usage percs = psutil.cpu_percent(interval=0, percpu=True) for cpu_num, perc in enumerate(percs): dashes, empty_dashes = get_dashes(perc) msg += " CPU{0:<2} [{1}{2}] {3:>5}%\n".format( cpu_num, dashes, empty_dashes, perc) mem = psutil.virtual_memory() dashes, empty_dashes = get_dashes(mem.percent) msg += " Mem [{0}{1}] {2:>5}% {3:>6} / {4}\n".format( dashes, empty_dashes, mem.percent, str(int(mem.used / 1024 / 1024)) + "M", str(int(mem.total / 1024 / 1024)) + "M") # swap usage swap = psutil.swap_memory() dashes, empty_dashes = get_dashes(swap.percent) msg += " Swap [{0}{1}] {2:>5}% {3:>6} / {4}\n".format( dashes, empty_dashes, swap.percent, str(int(swap.used / 1024 / 1024)) + "M", str(int(swap.total / 1024 / 1024)) + "M") # processes number and status st = [] for x, y in procs_status.items(): if y: st.append("%s=%s" % (x, y)) st.sort(key=lambda x: x[:3] in ('run', 'sle'), reverse=True) msg += " Processes: {0} ({1})\n".format(num_procs, ', '.join(st)) # load average, uptime uptime = datetime.datetime.now() - datetime.datetime.fromtimestamp( psutil.boot_time()) if not hasattr(os, "getloadavg"): msg += " Load average: N/A Uptime: {0}".format( str(uptime).split('.')[0]) else: av1, av2, av3 = os.getloadavg() msg += " Load average: {0:.2f} {1:.2f} {2:.2f} Uptime: {3}".format( av1, av2, av3, str(uptime).split('.')[0]) await self._say(ctx, msg) # print processes template = "{0:<6} {1:<9} {2:>5} {3:>8} {4:>8} {5:>8} {6:>6} {7:>10} {8:>2}\n" msg = template.format("PID", "USER", "NI", "VIRT", "RES", "CPU%", "MEM%", "TIME+", "NAME") for p in processes: # TIME+ column shows process CPU cumulative time and it # is expressed as: "mm:ss.ms" if p.dict['cpu_times'] is not None: ctime = datetime.timedelta(seconds=sum(p.dict['cpu_times'])) ctime = "%s:%s.%s" % (ctime.seconds // 60 % 60, str((ctime.seconds % 60)).zfill(2), str(ctime.microseconds)[:2]) else: ctime = '' if p.dict['memory_percent'] is not None: p.dict['memory_percent'] = round(p.dict['memory_percent'], 1) else: p.dict['memory_percent'] = '' if p.dict['cpu_percent'] is None: p.dict['cpu_percent'] = '' if p.dict['username']: username = p.dict['username'][:8] else: username = '' msg += template.format( p.pid, username, p.dict['nice'] or '', self._size(getattr(p.dict['memory_info'], 'vms', 0)), self._size(getattr(p.dict['memory_info'], 'rss', 0)), p.dict['cpu_percent'], p.dict['memory_percent'], ctime, p.dict['name'] or '') await self._say(ctx, msg) return
def get_swap(): Mem = psutil.swap_memory() return int(Mem.total/1024.0), int(Mem.used/1024.0)
def check(self, agentConfig): if Platform.is_linux(): try: with open('/proc/meminfo', 'r') as mem_info: lines = mem_info.readlines() except Exception: self.logger.exception('Cannot get memory metrics from /proc/meminfo') return False # NOTE: not all of the stats below are present on all systems as # not all kernel versions report all of them. # # $ cat /proc/meminfo # MemTotal: 7995360 kB # MemFree: 1045120 kB # MemAvailable: 1253920 kB # Buffers: 226284 kB # Cached: 775516 kB # SwapCached: 248868 kB # Active: 1004816 kB # Inactive: 1011948 kB # Active(anon): 455152 kB # Inactive(anon): 584664 kB # Active(file): 549664 kB # Inactive(file): 427284 kB # Unevictable: 4392476 kB # Mlocked: 4392476 kB # SwapTotal: 11120632 kB # SwapFree: 10555044 kB # Dirty: 2948 kB # Writeback: 0 kB # AnonPages: 5203560 kB # Mapped: 50520 kB # Shmem: 10108 kB # Slab: 161300 kB # SReclaimable: 136108 kB # SUnreclaim: 25192 kB # KernelStack: 3160 kB # PageTables: 26776 kB # NFS_Unstable: 0 kB # Bounce: 0 kB # WritebackTmp: 0 kB # CommitLimit: 15118312 kB # Committed_AS: 6703508 kB # VmallocTotal: 34359738367 kB # VmallocUsed: 400668 kB # VmallocChunk: 34359329524 kB # HardwareCorrupted: 0 kB # HugePages_Total: 0 # HugePages_Free: 0 # HugePages_Rsvd: 0 # HugePages_Surp: 0 # Hugepagesize: 2048 kB # DirectMap4k: 10112 kB # DirectMap2M: 8243200 kB regexp = re.compile(r'^(\w+):\s+([0-9]+)') # We run this several times so one-time compile now meminfo = {} for line in lines: try: match = re.search(regexp, line) if match is not None: meminfo[match.group(1)] = match.group(2) except Exception: self.logger.exception("Cannot parse /proc/meminfo") memData = {} # Physical memory # FIXME units are in MB, we should use bytes instead try: memData['physTotal'] = int(meminfo.get('MemTotal', 0)) / 1024 memData['physFree'] = int(meminfo.get('MemFree', 0)) / 1024 memData['physBuffers'] = int(meminfo.get('Buffers', 0)) / 1024 memData['physCached'] = int(meminfo.get('Cached', 0)) / 1024 memData['physShared'] = int(meminfo.get('Shmem', 0)) / 1024 memData['physSlab'] = int(meminfo.get('Slab', 0)) / 1024 memData['physPageTables'] = int(meminfo.get('PageTables', 0)) / 1024 memData['physUsed'] = memData['physTotal'] - memData['physFree'] if 'MemAvailable' in meminfo: memData['physUsable'] = int(meminfo.get('MemAvailable', 0)) / 1024 else: # Usable is relative since cached and buffers are actually used to speed things up. memData['physUsable'] = memData['physFree'] + memData['physBuffers'] + memData['physCached'] if memData['physTotal'] > 0: memData['physPctUsable'] = float(memData['physUsable']) / float(memData['physTotal']) except Exception: self.logger.exception('Cannot compute stats from /proc/meminfo') # Swap # FIXME units are in MB, we should use bytes instead try: memData['swapTotal'] = int(meminfo.get('SwapTotal', 0)) / 1024 memData['swapFree'] = int(meminfo.get('SwapFree', 0)) / 1024 memData['swapCached'] = int(meminfo.get('SwapCached', 0)) / 1024 memData['swapUsed'] = memData['swapTotal'] - memData['swapFree'] if memData['swapTotal'] > 0: memData['swapPctFree'] = float(memData['swapFree']) / float(memData['swapTotal']) except Exception: self.logger.exception('Cannot compute swap stats') return memData elif sys.platform == 'darwin': if psutil is None: self.logger.error("psutil must be installed on MacOS to collect memory metrics") return False phys_memory = psutil.virtual_memory() swap = psutil.swap_memory() return {'physUsed': phys_memory.used / float(1024**2), 'physFree': phys_memory.free / float(1024**2), 'physUsable': phys_memory.available / float(1024**2), 'physPctUsable': (100 - phys_memory.percent) / 100.0, 'swapUsed': swap.used / float(1024**2), 'swapFree': swap.free / float(1024**2)} elif sys.platform.startswith("freebsd"): try: output, _, _ = get_subprocess_output(['sysctl', 'vm.stats.vm'], self.logger) sysctl = output.splitlines() except Exception: self.logger.exception('getMemoryUsage') return False # ... # vm.stats.vm.v_page_size: 4096 # vm.stats.vm.v_page_count: 759884 # vm.stats.vm.v_wire_count: 122726 # vm.stats.vm.v_active_count: 109350 # vm.stats.vm.v_cache_count: 17437 # vm.stats.vm.v_inactive_count: 479673 # vm.stats.vm.v_free_count: 30542 # ... # We run this several times so one-time compile now regexp = re.compile(r'^vm\.stats\.vm\.(\w+):\s+([0-9]+)') meminfo = {} for line in sysctl: try: match = re.search(regexp, line) if match is not None: meminfo[match.group(1)] = match.group(2) except Exception: self.logger.exception("Cannot parse sysctl vm.stats.vm output") memData = {} # Physical memory try: pageSize = int(meminfo.get('v_page_size')) memData['physTotal'] = (int(meminfo.get('v_page_count', 0)) * pageSize) / 1048576 memData['physFree'] = (int(meminfo.get('v_free_count', 0)) * pageSize) / 1048576 memData['physCached'] = (int(meminfo.get('v_cache_count', 0)) * pageSize) / 1048576 memData['physUsed'] = ((int(meminfo.get('v_active_count'), 0) + int(meminfo.get('v_wire_count', 0))) * pageSize) / 1048576 memData['physUsable'] = ((int(meminfo.get('v_free_count'), 0) + int(meminfo.get('v_cache_count', 0)) + int(meminfo.get('v_inactive_count', 0))) * pageSize) / 1048576 if memData['physTotal'] > 0: memData['physPctUsable'] = float(memData['physUsable']) / float(memData['physTotal']) except Exception: self.logger.exception('Cannot compute stats from /proc/meminfo') # Swap try: output, _, _ = get_subprocess_output(['swapinfo', '-m'], self.logger) sysctl = output.splitlines() except Exception: self.logger.exception('getMemoryUsage') return False # ... # Device 1M-blocks Used Avail Capacity # /dev/ad0s1b 570 0 570 0% # ... assert "Device" in sysctl[0] try: memData['swapTotal'] = 0 memData['swapFree'] = 0 memData['swapUsed'] = 0 for line in sysctl[1:]: if len(line) > 0: line = line.split() memData['swapTotal'] += int(line[1]) memData['swapFree'] += int(line[3]) memData['swapUsed'] += int(line[2]) except Exception: self.logger.exception('Cannot compute stats from swapinfo') return memData elif sys.platform == 'sunos5': try: memData = {} cmd = ["kstat", "-m", "memory_cap", "-c", "zone_memory_cap", "-p"] output, _, _ = get_subprocess_output(cmd, self.logger) kmem = output.splitlines() # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:anon_alloc_fail 0 # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:anonpgin 0 # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:class zone_memory_cap # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:crtime 16359935.0680834 # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:execpgin 185 # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:fspgin 2556 # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:n_pf_throttle 0 # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:n_pf_throttle_usec 0 # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:nover 0 # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:pagedout 0 # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:pgpgin 2741 # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:physcap 536870912 <-- # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:rss 115544064 <-- # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:snaptime 16787393.9439095 # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:swap 91828224 <-- # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:swapcap 1073741824 <-- # memory_cap:360:53aa9b7e-48ba-4152-a52b-a6368c:zonename 53aa9b7e-48ba-4152-a52b-a6368c3d9e7c # turn memory_cap:360:zone_name:key value # into { "key": value, ...} kv = [l.strip().split() for l in kmem if len(l) > 0] entries = dict([(k.split(":")[-1], v) for (k, v) in kv]) # extract rss, physcap, swap, swapcap, turn into MB convert = lambda v: int(long(v))/2**20 memData["physTotal"] = convert(entries["physcap"]) memData["physUsed"] = convert(entries["rss"]) memData["physFree"] = memData["physTotal"] - memData["physUsed"] memData["swapTotal"] = convert(entries["swapcap"]) memData["swapUsed"] = convert(entries["swap"]) memData["swapFree"] = memData["swapTotal"] - memData["swapUsed"] if memData['swapTotal'] > 0: memData['swapPctFree'] = float(memData['swapFree']) / float(memData['swapTotal']) return memData except Exception: self.logger.exception("Cannot compute mem stats from kstat -c zone_memory_cap") return False else: return False
def get_mem_info(): swap_memory = psutil.swap_memory() # 获取swap分区信息 total_memory = psutil.virtual_memory().total # 获取内存总数 return total_memory
def test_swapmem_free(self): total, used, free = free_swap() return self.assertAlmostEqual(free, psutil.swap_memory().free, delta=MEMORY_TOLERANCE)
def getMemoryInfo(self): self._logger.info("<Memory Info>") self._logger.debug(psutil.virtual_memory()) self._logger.debug(psutil.swap_memory()) print '\n'
def os_infos(self): u_sysname = os.uname().sysname u_username = psutil.Process().username() uname_list = os.uname() u_name = uname_list.sysname u_nodename = uname_list.nodename u_release = uname_list.release u_dmname = os.environ['XDG_CURRENT_DESKTOP'] u_totmem = psutil.virtual_memory().total u_swapmem = 0 try: u_swapmem = psutil.swap_memory().total if u_swapmem == None: u_swapmem = 0 except: u_swapmem = 0 # partitions partitions = psutil.disk_partitions(all=False) num_partitions = len(partitions) # check the moutpoint home_partition = "" for i in range(num_partitions): if partitions[i].mountpoint == "/": root_partition = partitions[i].device root_fstype = partitions[i].fstype root_disk_usage = psutil.disk_usage('/') root_disk_total = self.el_size(root_disk_usage.total) if partitions[i].mountpoint == "/home": home_partition = partitions[i].device home_fstype = partitions[i].fstype home_disk_usage = psutil.disk_usage('/home') home_disk_total = self.el_size(home_disk_usage.total) home_disk_used = self.el_size(home_disk_usage.used) # processor u_proc_num = psutil.cpu_count() u_proc_num_real = psutil.cpu_count(logical=False) u_proc_model = "" try: f = open('/proc/cpuinfo', 'r') for line in f: if line.rstrip('\n').startswith('model name'): u_proc_model_name = line.rstrip('\n').split(':')[1].strip() break f.close() except: u_proc_model_name("NN") if u_proc_num_real == u_proc_num: self.label10.set_text(u_proc_model_name + " x " + str(u_proc_num_real)) else: self.label10.set_text(u_proc_model_name + " x (" + str(u_proc_num_real) + "+" + str(u_proc_num) + ")") # set the logo if u_sysname == "Linux": pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale( 'Tux.svg', 100, 100, 0) self.logop.set_from_pixbuf(pixbuf) else: pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale( 'Tux.svg', 100, 100, 0) self.logop.set_from_pixbuf(pixbuf) # set the labels self.labelun.set_text(u_username or "") self.label0.set_text(u_nodename or "") self.label1.set_text(self.name_distro() or uname_list.version or "") self.label2.set_text(u_release or "") self.label3.set_text(u_dmname or "") self.label4.set_text(str(self.el_size(u_totmem)) or "") self.label5.set_text(str(self.el_size(u_swapmem)) or "") self.label6.set_text(root_partition + " - " + root_fstype) self.label8.set_text(str(root_disk_total)) if home_partition: # home partition and fstype label7t = Gtk.Label(label="Home device and size") label7t.override_color(Gtk.StateFlags.NORMAL, OVVERIDE_TEXT_COLOR) label7t.props.xalign = 1 self.grid.attach(label7t, 1, 9, 1, 1) label7 = Gtk.Label() label7.props.xalign = 0 self.grid.attach(label7, 2, 9, 1, 1) # home disk total - used label9t = Gtk.Label(label="Home Disk Size") label9t.override_color(Gtk.StateFlags.NORMAL, OVVERIDE_TEXT_COLOR) label9t.props.xalign = 1 self.grid.attach(label9t, 1, 10, 1, 1) label9 = Gtk.Label() label9.props.xalign = 0 self.grid.attach(label9, 2, 10, 1, 1) # label7.set_text(home_partition + " - " + home_fstype) label9.set_text(str(home_disk_total) + " - " + str(home_disk_used))
data = '离离原上草,一岁一枯荣'.encode('gbk') data2 = '离离原上草,一岁一枯荣'.encode('utf-8') data3 = '最新の主要ニュース'.encode('euc-jp') print(chardet.detect(data)) print(chardet.detect(data2)) print(chardet.detect(data3)) print(psutil.cpu_count()) # CPU逻辑数量 print(psutil.cpu_count(logical=False)) # CPU物理核心 print(psutil.cpu_times()) # CPU的用户、系统、空闲时间 # for x in range(10): # print(psutil.cpu_percent(interval=1, percpu=True)) print('内存:') print(psutil.virtual_memory()) # 获取内存信息 print(psutil.swap_memory()) print('磁盘:') print(psutil.disk_partitions()) # 磁盘分区信息 print(psutil.disk_usage('/')) # 磁盘使用情况 print(psutil.disk_io_counters()) # 磁盘ID print('网络:') print(psutil.net_io_counters()) # 获取网络读写字节/包的个数 print(psutil.net_if_addrs()) # 网络接口信息 print(psutil.net_if_stats()) # 网络接口状态 print(psutil.net_connections()) # 获取当前网络连接信息 print('进程:') print(psutil.pids()) # 获取所哟进程 p = psutil.Process(2128) print(p.name()) # 获取指定进程 # print(p.exe()) # 进程的exe路径,需要对应权限 # p.cwd() # 进程工作目录
insertString = 'INSERT INTO test.' + destTable + ' (meter_name,reading_timestamp,reading_event,meter_state,hly_deliv_kwh_rup,hly_deliv_kwh,avg_volt0,deliv_kwh0,fluct_idx,rec_kwh0) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)' myCursor.executemany(insertString, readings) myConnection.commit() stdlib.timing_end(2) myCursor.close() myConnection.close() ##### process = psutil.Process(os.getpid()) vMemoryProcess = process.memory_info().rss / 1000000 vMemoryVM = psutil.virtual_memory() vMemorySwap = psutil.swap_memory() print "Interactions : {:>10,} ".format(MAX_VALUE).rjust(10) print "Timing List : {:>10}sec ".format(stdlib.timing_read(1)).rjust(10) print "Timing DB : {:>10}sec ".format(stdlib.timing_read(2)).rjust(10) print "Process size : {:>10,}MB ".format(vMemoryProcess) print "Physical mem : {:>10,}MB ".format(vMemoryVM.total / 1000000) print "SWAP : {:>10}% ".format(vMemorySwap.percent) del readings vMemoryProcess2 = process.memory_info().rss / 1000000 print "Process size : {:>10,}MB ".format(vMemoryProcess2) raw_input("press a key to finish !")
def do_status(self, line): output = list() arguments = line.lower().split(' ') length = len(arguments) if length < 1: print('Need more argument') return if arguments[0] == 'cpu': cpuCount = psutil.cpu_count() cpuUsage = psutil.cpu_percent(percpu = True) if length == 1: output.append('Number of processor: ' + str(cpuCount)) for processor in range(0, cpuCount): output.append('Processor ' + str(processor + 1)) output.append(Utils.formatProgressBar(cpuUsage[processor])) elif length == 2: if arguments[1] == 'realtime': while True: cpuUsage = psutil.cpu_percent(interval = 1, percpu = True) average = 0 total = 0 for processor in range(0, cpuCount): total += cpuUsage[processor] average = round(total / cpuCount, 1) print(Utils.formatProgressBar(average) + (' ' * 10) + '\r', end = '') else: if not Utils.isStringInt(arguments[1]): print('CPU id must be an integer') return if int(arguments[1]) < 1: print('Out of range, you only got ' + str(cpuCount) + ' cpu(s)') return if int(arguments[1]) > cpuCount: print('Out of range, must be above 0') return processor = int(arguments[1]) - 1 output.append('Information for processor: ' + str(processor + 1)) output.append(Utils.formatProgressBar(cpuUsage[processor])) elif arguments[0] == 'memory': memoryRam = psutil.virtual_memory() memorySwap = psutil.swap_memory() output.append('Memory information: RAM') output.append(' ') # output.append('\tAvailable: ' + str(Utils.byteToHuman(memoryRam.available))) # output.append('\tUsed: ' + str(Utils.byteToHuman(memoryRam.used))) # output.append('\tFree: ' + str(Utils.byteToHuman(memoryRam.free))) # output.append('\tTotal: ' + str(Utils.byteToHuman(memoryRam.total))) output.append('\t{:^15} {:^15} {:^15} {:^15}'.format('Available', 'Used', 'Free', 'Total')) output.append('\t{:^15} {:^15} {:^15} {:^15}'.format(Utils.byteToHuman(memoryRam.available), Utils.byteToHuman(memoryRam.used), Utils.byteToHuman(memoryRam.free), Utils.byteToHuman(memoryRam.total))) output.append(Utils.formatProgressBar(memoryRam.percent)) output.append('*-*') output.append('Memory information: SWAP') output.append(' ') output.append('\t{:^15} {:^15} {:^15}'.format('Used', 'Free', 'Total')) output.append('\t{:^15} {:^15} {:^15}'.format(Utils.byteToHuman(memorySwap.used), Utils.byteToHuman(memorySwap.free), Utils.byteToHuman(memorySwap.total))) output.append(Utils.formatProgressBar(memorySwap.percent)) Utils.printFormat(output)