Created
March 6, 2023 11:22
-
-
Save cnicodeme/b3f4589bb385456a0a8b49e9c8fc7c3a to your computer and use it in GitHub Desktop.
Revisions
-
cnicodeme created this gist
Mar 6, 2023 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,129 @@ #!/usr/bin/python # # Inspired by https://gist.github.com/MrHamel/1b640a81ded45bfbac564d2fd4f9532c, but updated for Python3 # Thanks! # import json, psutil def update_top_info(): top_data = {} for k in psutil.cpu_times()._asdict(): top_data[k] = getattr(psutil.cpu_times(), k) return top_data def update_cpu_usage(): cpu_data = {} data = psutil.cpu_percent(interval=1, percpu=True) for cpu in data: cpu_data["CPU"+str(data.index(cpu))] = cpu return cpu_data def update_ram_usage(): ram_data = {} for k in psutil.virtual_memory()._asdict(): ram_data[k] = getattr(psutil.virtual_memory(), k) return ram_data def update_swap_usage(): swap_data = {} for k in psutil.swap_memory()._asdict(): swap_data[k] = getattr(psutil.swap_memory(), k) return swap_data def update_disk_usage(): disk_data = {} for mp in psutil.disk_partitions(): disk_data[mp.mountpoint] = {"mountpoint":mp.mountpoint} data = psutil.disk_usage(mp.mountpoint) for k in data._asdict(): disk_data[mp.mountpoint][k] = getattr(data, k) return disk_data def update_disk_io(): disk_data = {} for d, data in psutil.disk_io_counters(perdisk=True).items(): disk_data[d] = {"partition":d} for k in data._asdict(): disk_data[d][k] = getattr(data, k) return disk_data def update_network_io(): net_data = {} for n, data in psutil.net_io_counters(pernic=True).items(): net_data[n] = {"nic":n} for k in data._asdict(): net_data[n][k] = getattr(data, k) return net_data def update_temp_data(): temp_data = {} for t, data in psutil.sensors_temperatures().items(): temp_data[t] = [] for d in data: temp = {"probe":t} for k in d._asdict(): temp[k] = getattr(d, k) temp_data[t].append(temp) return temp_data def update_fan_speed(): temp_data = {} for t, data in psutil.sensors_fans().items(): temp_data[t] = [] for d in data: temp = {"probe":t} for k in d._asdict(): temp[k] = getattr(d, k) temp_data[t].append(temp) return temp_data report = { 'top': update_top_info(), 'cpu': update_cpu_usage(), 'ram': update_ram_usage(), 'swap': update_swap_usage(), 'disk': update_disk_usage(), 'io': update_disk_io(), 'net': update_network_io(), 'temps': update_temp_data(), 'fans': update_fan_speed() } print(json.dumps(report, indent=4))