diff --git a/pydash/services.py b/pydash/services.py index 3e470d4..9e1b276 100644 --- a/pydash/services.py +++ b/pydash/services.py @@ -2,26 +2,31 @@ 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 """ - try: - with open('/proc/uptime', 'r') as f: - uptime_seconds = float(f.readline().split()[0]) - uptime_time = str(timedelta(seconds=uptime_seconds)) - data = uptime_time.split('.', 1)[0] - - except Exception as err: - data = str(err) - - return data + import uptime + td = datetime.timedelta(seconds=round(uptime.uptime())) + return str(td) def get_ipaddress(): @@ -56,30 +61,17 @@ def get_ipaddress(): return data - +# win comp +_cpu_info = None def get_cpus(): """ Get the number of CPUs and model/type """ - try: - pipe = os.popen("cat /proc/cpuinfo |" + "grep 'model name'") - data = pipe.read().strip().split(':')[-1] - pipe.close() - - if not data: - pipe = os.popen("cat /proc/cpuinfo |" + "grep 'Processor'") - data = pipe.read().strip().split(':')[-1] - pipe.close() - - cpus = multiprocessing.cpu_count() - - data = {'cpus': cpus, 'type': data} - - except Exception as err: - data = str(err) - - return data - + 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 def get_users(): """ @@ -131,43 +123,38 @@ def get_traffic(request): return data - +_platform = None def get_platform(): """ Get the OS name, hostname and kernel """ - try: - osname = " ".join(platform.linux_distribution()) - uname = platform.uname() - - if osname == ' ': - osname = uname[0] - - data = {'osname': osname, 'hostname': uname[1], 'kernel': uname[2]} - - except Exception as err: - data = str(err) - - return data - - + 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 """ - try: - pipe = os.popen( - "df -Ph | " + "grep -v Filesystem | " + "awk '{print $1, $2, $3, $4, $5, $6}'") - data = pipe.read().strip().split('\n') - pipe.close() - - data = [i.split(None, 6) for i in data] - - except Exception as err: - data = str(err) - + 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(): """ @@ -277,11 +264,12 @@ def get_load(): try: data = os.getloadavg()[0] except Exception as err: - data = str(err) + data = "N/A" return data + def get_netstat(): """ Get ports and applications