Created
August 31, 2022 15:42
-
-
Save dasbluehole/e007affd9e63625c4a47f825a9e72943 to your computer and use it in GitHub Desktop.
CPU and Memory information using python on Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#cpu info mem info by ashok.s.das@gmail.com | |
#reads and parses /proc/cpuinfo and /proc/meminfo to obtain information | |
def parse_cpu_info(): | |
info=dict() | |
try: | |
f=open('/proc/cpuinfo','r') | |
except IOError: | |
return{} | |
while True: | |
line = f.readline() | |
#print(line,len(line)) | |
if not line or len(line)<=1: break | |
pair = line.split(":") | |
info[pair[0].strip()]=pair[1].strip() | |
print('CPU is',info['vendor_id'],'model',info['model name'], 'having',info['cpu cores'],'cores') | |
f.close() | |
def mem_info(): | |
minfo=dict() | |
try: | |
f=open('/proc/meminfo','r') | |
except IOError: | |
return{} | |
while True: | |
line = f.readline() | |
#print(line,len(line)) | |
if not line or len(line)<=1: break | |
pair = line.split(":") | |
minfo[pair[0].strip()]=pair[1].strip() | |
print('Total Memory =',minfo['MemTotal'],'free',minfo['MemFree'],'Available',minfo['MemAvailable']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment