Skip to content

Instantly share code, notes, and snippets.

@ShahriyarR
Last active January 25, 2024 04:32
Show Gist options
  • Save ShahriyarR/7774055 to your computer and use it in GitHub Desktop.
Save ShahriyarR/7774055 to your computer and use it in GitHub Desktop.
Script for getting Linux Metadata
import platform
from collections import namedtuple
import glob
import re
import os
class OsInfo():
def get_os_info(self):
print "System Type: " + platform.uname()[0]
print "Linux Distro: " + platform.linux_distribution()[0] + " " + platform.linux_distribution()[1]
print "Platform info: " + platform.platform()
print "Hostname: " + platform.uname()[1]
print "Version: " + platform.uname()[2]
print "Release: " + platform.uname()[3]
print "Architecture: " + platform.architecture()[0] + " " + platform.architecture()[1]
print "Processor Type: " + platform.processor()
class CpuInfo():
final_list = []
def cpu_info(self):
model_name = []
with open('/proc/cpuinfo') as file:
for line in file:
if line.strip():
if line.rstrip('\n').startswith('model name'):
model_name.append(line.rstrip('\n').split(':')[1])
return model_name
def cpu_architecture(self):
cpu_arc = []
with open('/proc/cpuinfo') as f:
for line in f:
if line.strip():
if line.rstrip('\n').startswith('flags') \
or line.rstrip('\n').startswith('Features'):
if 'lm' in line.rstrip('\n').split():
cpu_arc.append('64-bit')
else:
cpu_arc.append('32-bit')
return cpu_arc
def cpu_info_arch(self):
for a in self.cpu_architecture():
step_index_1 = 0
for b in self.cpu_info():
step_index_2 = 0
if step_index_1 == step_index_2:
self.final_list.append(a + " " + b)
step_index_1 = step_index_1 + 1
step_index_2 = step_index_2 + 1
return self.final_list
class MemInfo():
def total_mem(self):
memory = []
with open('/proc/meminfo') as file:
for line in file:
if line.strip():
if line.rstrip('\n').startswith('MemTotal'):
memory.append(line.rstrip('\n').split(':')[1])
return memory
def free_mem(self):
memory = []
with open('/proc/meminfo') as file:
for line in file:
if line.strip():
if line.rstrip('\n').startswith('MemFree'):
memory.append(line.rstrip('\n').split(':')[1])
return memory
class NetworkInfo():
def netdevs(self):
"""
RX and TX bytes for each of the network devices
Reference: http://amitsaha.github.io/site/notes/articles/python_linux/article.html
"""
with open('/proc/net/dev') as f:
net_dump = f.readlines()
device_data = {}
data = namedtuple('data', ['rx', 'tx'])
for line in net_dump[2:]:
line = line.split(':')
if line[0].strip() != 'lo':
device_data[line[0].strip()] = data(float(line[1].split()[0])/(1024.0*1024.0),
float(line[1].split()[8])/(1024.0*1024.0))
return device_data
class ProcessInfo():
pids = []
def process_list(self):
for subdir in os.listdir('/proc'):
if subdir.isdigit():
self.pids.append(subdir)
return self.pids
class BlockInfo():
# Add any other device pattern to read from
dev_pattern = ['sd.*', 'mmcblk*']
def size(self, device):
nr_sectors = open(device+'/size').read().rstrip('\n')
sect_size = open(device+'/queue/hw_sector_size').read().rstrip('\n')
# The sect_size is in bytes, so we convert it to GiB and then send it back
return (float(nr_sectors)*float(sect_size))/(1024.0*1024.0*1024.0)
def detect_devs(self):
for device in glob.glob('/sys/block/*'):
for pattern in self.dev_pattern:
if re.compile(pattern).match(os.path.basename(device)):
print('Device:: {0}, Size:: {1} GiB'.format(device, self.size(device)))
print "#########################"
print "# ABOUT YOUR OS #"
print "#########################"
print " "
x = OsInfo()
x.get_os_info()
print " "
print "##########################"
print "# CPU INFO #"
print "##########################"
print " "
y = CpuInfo()
for i in y.cpu_info_arch():
print i
print " "
print "###########################"
print "# MEMORY INFO #"
print "###########################"
print " "
z = MemInfo()
for i in z.total_mem():
print "Total Memory: " + i.strip()
for a in z.free_mem():
print "Free Memory: " + a.strip()
print " "
print "###########################"
print "# NETWORK INFO #"
print "###########################"
print " "
v = NetworkInfo()
netdevs = v.netdevs()
for dev in netdevs.keys():
print '{0}: {1} MiB {2} MiB'.format(dev, netdevs[dev].rx, netdevs[dev].tx)
print " "
print "###########################"
print "# PROCESS INFO #"
print "###########################"
print " "
w = ProcessInfo()
w.process_list()
pids = w.process_list()
print 'Total number of running processes: {0}'.format(len(pids))
print " "
print "###########################"
print "# BLOCK INFO #"
print "###########################"
print " "
bc = BlockInfo()
bc.detect_devs()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment