Mini Shell
#!/opt/imh-python/bin/python3
# Calculates comprehensive load averages for BL servers
# Vanessa V, Brandon W. 5/6/13
import os
import re
import multiprocessing
from datetime import timedelta
def get_loadavg():
"""Gets the load average from the server"""
loadavg = ' '.join(open('/proc/loadavg').readline().split(' ')[:3])
return loadavg
def get_uptime():
"""Gets the uptime for the server"""
with open('/proc/uptime') as f:
uptime_secs = float(f.readline().split()[0])
uptime = str(timedelta(seconds=uptime_secs))
return uptime
def get_blload():
"""Gets the load average from betterlinux"""
cpu_matcher = re.compile(r'cpu(\d+) (\d+\.\d+) (\d+\.\d+) (\d+\.\d+)')
with open("/proc/loadavg_cpumask") as f:
mask = f.read().strip('\n\x00').replace(',', '')
int_mask = int(mask, 16)
load_average_nonjail = [0.0, 0.0, 0.0]
load_average_jail = [0.0, 0.0, 0.0]
num_jail = 0
num_non_jail = 0
with open("/proc/loadavg_cpu") as f:
for line in f:
load_entry = cpu_matcher.match(line)
if not load_entry:
continue
cpu_id = int(load_entry.group(1))
if int_mask & (1 << cpu_id):
num_non_jail += 1
for i in range(3):
load_average_nonjail[i] += float(load_entry.group(i + 2))
else:
num_jail += 1
for i in range(3):
load_average_jail[i] += float(load_entry.group(i + 2))
return num_non_jail, load_average_nonjail, num_jail, load_average_jail
if __name__ == "__main__":
kernel = os.uname()[2]
num_cpu = multiprocessing.cpu_count()
if 'bl' not in kernel:
print(" !!! Not a BetterLinux kernel")
print(" Uptime: %s" % get_uptime())
print(" Number of Cores: %s" % num_cpu)
print(" Load Average: %s " % get_loadavg())
else:
nnjail, nonjail, njail, jail = get_blload()
print(" Uptime: %s" % get_uptime())
print(" Number of Non-Jailed Cores: %s" % nnjail)
print(" Number of Jailed Cores: %s" % njail)
print(" Non-Jail Load Average: %.2f %.2f %.2f" % tuple(nonjail))
print(" Jail Load Average: %.2f %.2f %.2f" % tuple(jail))
Zerion Mini Shell 1.0