Last active
August 29, 2015 14:23
-
-
Save P7h/6ceca2a5bd9f1be7f658 to your computer and use it in GitHub Desktop.
Python snippets for quickly finding the platform type, cpu and memory stats of a Linux machine.
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 of a Linux machine. | |
### Picked from: http://echorand.me/site/notes/articles/python_linux/article.html | |
from __future__ import print_function | |
from collections import OrderedDict | |
import pprint | |
def cpuinfo(): | |
cpuinfo=OrderedDict() | |
procinfo=OrderedDict() | |
nprocs = 0 | |
with open('/proc/cpuinfo') as f: | |
for line in f: | |
if not line.strip(): | |
cpuinfo['proc%s' % nprocs] = procinfo | |
nprocs=nprocs+1 | |
procinfo=OrderedDict() | |
else: | |
if len(line.split(':')) == 2: | |
procinfo[line.split(':')[0].strip()] = line.split(':')[1].strip() | |
else: | |
procinfo[line.split(':')[0].strip()] = '' | |
return cpuinfo | |
cpuinfo = cpuinfo() | |
for processor in cpuinfo.keys(): | |
print(cpuinfo[processor]['model name']) |
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
###### Memory Info of a Linux machine. | |
from __future__ import print_function | |
from collections import OrderedDict | |
def meminfo(): | |
meminfo=OrderedDict() | |
with open('/proc/meminfo') as f: | |
for line in f: | |
meminfo[line.split(':')[0]] = line.split(':')[1].strip() | |
return meminfo | |
meminfo = meminfo() | |
print('Total memory: {0}'.format(meminfo['MemTotal'])) | |
print('Free memory: {0}'.format(meminfo['MemFree'])) |
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
###### Platform info and distribution of a Linux machine. | |
import platform | |
print(platform.uname()) | |
print(platform.linux_distribution()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment