Update system monitor.

Overall cleanup, and modificaton to the disk chart.  System tracks Byte,
KiB and MiB transfer rates.  Can't realld support GiB because I'm no
sure the PCIe bus can handle that.

Modifed the Y-Axis for disks so it displays the data on the closest
power of 2 scale. Seems to be a reasonable way to modify the scale so we
can actually see some of the smaller writes.
This commit is contained in:
Jeff Curless
2025-10-13 17:59:47 -04:00
parent 3f0727e590
commit cfadd92430
4 changed files with 529 additions and 54 deletions

View File

@@ -3,13 +3,38 @@ import os
import time
class CPULoad:
'''
A class to help with obtaining the CPU load of the system. If there is more information
needed, we can add to this.
Note:
This code automatically attempts to load the data from the system to initialize the
object with names, and an initial set of data.
This may result in th first actual call return some not very consistent values, for
the time period being observed, but that difference is minimal. In otherwords if we
the period of time being measured is 1 second, and it's been a minute since this class
was initialized, the first period reported will be CPU load over the minute, not 1 second,
and the second period reported will be for a second...
This is usually not an issue.
'''
def __init__( self ):
self._previousData = self._getRawData()
self._names = []
self._previousData : dict[str,tuple] = self._getRawData()
self._names : list[str] = []
for item in self._previousData:
self._names.append( item )
def _getRawData( self ):
def _getRawData( self ) -> dict[str : tuple]:
'''
Obtain the raw CPU data from the system (located in /prop/stat), and
return just the cpu0 -> cpux values. No assumption is made on the number of
cpus.
Returns:
A dictionary is returned, the format is name = (total, idle). The total
time and idle time are use to determine the percent utilization of the system.
'''
result = {}
with open( "/proc/stat", "r") as f:
allLines = f.readlines()
@@ -25,22 +50,50 @@ class CPULoad:
result[cpu[0]] = (total,idle)
return result
def getPercentages( self ):
def getPercentages( self ) -> dict[ str : float ]:
'''
Obtain the percent CPU utilization of the system for a period of time.
This routine gets the current raw data from the system, and then performs
a delta from the prior time this function was called. This data is then run
through the following equation:
utilization = ((total - idle)/total) * 100
If the snapshots are taken at relativy consistent intervals, the CPU
utilization in percent, is reasonably lose to the actual percentage.
Returns:
A dictionary consisting of the name of the CPU, and a floating point
number representing the current utilization of that CPU.
'''
results = {}
current = self._getRawData()
for item in current:
total = current[item][0] - self._previousData[item][0]
idle = current[item][1] - self._previousData[item][1]
percent = ((total - idle)/total) * 100
results[item] = percent
results[item] = round(percent,2)
self._previousData = current
return results
@property
def cpuNames( self ):
def cpuNames( self ) -> list[str]:
'''
Get a list of CPU names from the system.
Returns:
a list of strings
'''
return self._names
def __len__(self):
def __len__(self) -> int:
'''
handle getting the length (or count of CPU's).
Returns:
Number of CPU's
'''
return len(self._previousData)
if __name__ == "__main__":
@@ -48,6 +101,7 @@ if __name__ == "__main__":
print( f"Number of CPU's = {len(load)}" )
while True:
time.sleep( 1 )
percentage = load.getPercentages()
percentage : dict[str:float] = load.getPercentages()
print( f"percentage: {percentage}" )
for item in percentage:
print( f"{item} : {percentage[item]:.02f}" )