You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

277 lines
6.6 KiB

import os
import platform
import multiprocessing
from datetime import timedelta
import psutil
import cpuinfo
import uptime
import platform
import datetime
# https://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size
def sizeof_fmt(num, suffix=''):
for unit in ['','K','M','G','T','P','E','Z']:
if abs(num) < 1024.0:
return f"{num:3.1f}{unit}{suffix}"
num /= 1024.0
return "{:3.1f}{}{}".format(num, 'Yi', suffix)
def chunks(get, n):
return [get[i:i + n] for i in range(0, len(get), n)]
# win comp
def get_uptime():
"""
Get uptime
"""
import uptime
td = datetime.timedelta(seconds=round(uptime.uptime()))
return str(td)
def get_ipaddress():
"""
Get the IP Address
"""
data = []
try:
eth = os.popen("ip addr | grep LOWER_UP | awk '{print $2}'")
iface = eth.read().strip().replace(':', '').split('\n')
eth.close()
del iface[0]
for i in iface:
pipe = os.popen(
"ip addr show " + i + "| awk '{if ($2 == \"forever\"){!$2} else {print $2}}'")
data1 = pipe.read().strip().split('\n')
pipe.close()
if len(data1) == 2:
data1.append('unavailable')
if len(data1) == 3:
data1.append('unavailable')
data1[0] = i
data.append(data1)
ips = {'interface': iface, 'itfip': data}
data = ips
except Exception as err:
data = str(err)
return data
# win comp
_cpu_info = None
def get_cpus():
"""
Get the number of CPUs and model/type
"""
global _cpu_info
if not _cpu_info:
cpu_info = cpuinfo.get_cpu_info()
_cpu_info = {"cpus":cpu_info["count"],"type": cpu_info["brand"]}
return _cpu_info
# win comp
def get_users():
"""
Get the current logged in users
"""
return [[user.name,user.terminal,user.host] for user in psutil.users()]
def get_traffic(request):
"""
Get the traffic for the specified interface
"""
try:
pipe = os.popen(
"cat /proc/net/dev |" + "grep " + request + "| awk '{print $1, $9}'")
data = pipe.read().strip().split(':', 1)[-1]
pipe.close()
if not data[0].isdigit():
pipe = os.popen(
"cat /proc/net/dev |" + "grep " + request + "| awk '{print $2, $10}'")
data = pipe.read().strip().split(':', 1)[-1]
pipe.close()
data = data.split()
traffic_in = int(data[0])
traffic_out = int(data[1])
all_traffic = {'traffic_in': traffic_in, 'traffic_out': traffic_out}
data = all_traffic
except Exception as err:
data = str(err)
return data
_platform = None
def get_platform():
"""
Get the OS name, hostname and kernel
"""
global _platform
if not _platform:
_platform = {"kernel":platform.version(),"hostname":platform.node(),"osname":platform.platform(terse=True)}
return _platform
# win comp
def get_disk():
"""
Get disk usage
"""
data = []
for disk in psutil.disk_partitions():
try:
disk_usage = psutil.disk_usage(disk.mountpoint)
data.append(
[ disk.device,
sizeof_fmt(disk_usage.total),
sizeof_fmt(disk_usage.used),
sizeof_fmt(disk_usage.free),
"{}%".format(round(disk_usage.percent)),
disk.mountpoint,
])
except PermissionError:
pass
return data
def get_disk_rw():
"""
Get the disk reads and writes
"""
try:
pipe = os.popen(
"cat /proc/partitions | grep -v 'major' | awk '{print $4}'")
data = pipe.read().strip().split('\n')
pipe.close()
rws = []
for i in data:
if i.isalpha():
pipe = os.popen(
"cat /proc/diskstats | grep -w '" + i + "'|awk '{print $4, $8}'")
rw = pipe.read().strip().split()
pipe.close()
rws.append([i, rw[0], rw[1]])
if not rws:
pipe = os.popen(
"cat /proc/diskstats | grep -w '" + data[0] + "'|awk '{print $4, $8}'")
rw = pipe.read().strip().split()
pipe.close()
rws.append([data[0], rw[0], rw[1]])
data = rws
except Exception as err:
data = str(err)
return data
def get_mem():
"""
Get memory usage
"""
try:
pipe = os.popen(
"free -tm | " + "grep 'Mem' | " + "awk '{print $2,$4,$6,$7}'")
data = pipe.read().strip().split()
pipe.close()
allmem = int(data[0])
freemem = int(data[1])
buffers = int(data[2])
cachedmem = int(data[3])
# Memory in buffers + cached is actually available, so we count it
# as free. See http://www.linuxatemyram.com/ for details
freemem += buffers + cachedmem
percent = (100 - ((freemem * 100) / allmem))
usage = (allmem - freemem)
mem_usage = {'usage': usage, 'buffers': buffers, 'cached': cachedmem, 'free': freemem, 'percent': percent}
data = mem_usage
except Exception as err:
data = str(err)
return data
def get_cpu_usage():
"""
Get the CPU usage and running processes
"""
try:
pipe = os.popen("ps aux --sort -%cpu,-rss")
data = pipe.read().strip().split('\n')
pipe.close()
usage = [i.split(None, 10) for i in data]
del usage[0]
total_usage = []
for element in usage:
usage_cpu = element[2]
total_usage.append(usage_cpu)
total_usage = sum(float(i) for i in total_usage)
total_free = ((100 * int(get_cpus()['cpus'])) - float(total_usage))
cpu_used = {'free': total_free, 'used':
float(total_usage), 'all': usage}
data = cpu_used
except Exception as err:
data = str(err)
return data
def get_load():
"""
Get load average
"""
try:
data = os.getloadavg()[0]
except Exception as err:
data = "N/A"
return data
def get_netstat():
"""
Get ports and applications
"""
try:
pipe = os.popen(
"ss -tnp | grep ESTAB | awk '{print $4, $5}'| sed 's/::ffff://g' | awk -F: '{print $1, $2}' "
"| awk 'NF > 0' | sort -n | uniq -c")
data = pipe.read().strip().split('\n')
pipe.close()
data = [i.split(None, 4) for i in data]
except Exception as err:
data = str(err)
return data