python獲得linux服務器的內存使用率,虛擬內存使用率
python獲得linux服務器的內存使用率,虛擬內存使用率. linux系統的內存占用信息在/proc/meminfo文件中。 您可以打開這個文件會看到更多詳細的信息。
def get_mem_usage_percent(): try: f = open('/proc/meminfo', 'r') for line in f: if line.startswith('MemTotal:'): mem_total = int(line.split()[1]) elif line.startswith('MemFree:'): mem_free = int(line.split()[1]) elif line.startswith('Buffers:'): mem_buffer = int(line.split()[1]) elif line.startswith('Cached:'): mem_cache = int(line.split()[1]) elif line.startswith('SwapTotal:'): vmem_total = int(line.split()[1]) elif line.startswith('SwapFree:'): vmem_free = int(line.split()[1]) else: continue f.close() except: return None physical_percent = usage_percent(mem_total - (mem_free + mem_buffer + mem_cache), mem_total) virtual_percent = 0 if vmem_total > 0: virtual_percent = usage_percent((vmem_total - vmem_free), vmem_total) return physical_percent, virtual_percentdef usage_percent(use, total): try: ret = (float(use) / total) * 100 except ZeroDivisionError: raise Exception("ERROR - zero division error") return ret</pre>
本文由用戶 cgdf 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!