-
Notifications
You must be signed in to change notification settings - Fork 0
/
providers.py
110 lines (88 loc) · 2.21 KB
/
providers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import paho.mqtt.client as mqtt
import psutil
class MetricProvider:
def __init__(self):
self._Name = "None"
def getValue():
"""
Returns the metric value.
Returns:
int: The metric value.
"""
return 0
def isValid(self):
"""
Checks if the metric provider is valid.
Returns:
bool: True if the metric provider is valid, False otherwise.
"""
return False
def getName(self):
"""
Returns the name of the metric provider.
Returns:
str: The name of the metric provider.
"""
return self._Name
class GPUTempMetricProvider(MetricProvider):
def __init__(self):
self._Name = "gpu_temperature_celsius"
try:
from pyadl import ADLManager
self._adlDevices = ADLManager.getInstance().getDevices()
except:
self._adlDevices = None
def isValid(self):
"""
Checks if the GPU temperature metric provider is valid.
Returns:
bool: True if the GPU temperature metric provider is valid, False otherwise.
"""
if (self._adlDevices is not None):
if (len(self._adlDevices) > 0):
return True
return False
def getValue(self):
"""
Returns the GPU temperature value.
Returns:
int: The GPU temperature value.
"""
gpuTemp = self._adlDevices[0].getCurrentTemperature()
if (gpuTemp == 0):
raise Exception("Failed to get GPU temperature")
return gpuTemp
class CPUUsageMetricProvider(MetricProvider):
def __init__(self):
self._Name = "cpu_utilization_percent"
def isValid(self):
"""
Checks if the CPU usage metric provider is valid.
Returns:
bool: True if the CPU usage metric provider is valid, False otherwise.
"""
return True
def getValue(self):
"""
Returns the CPU utilization value.
Returns:
float: The CPU utilization value.
"""
return round(psutil.cpu_percent(), 1)
class RAMUsageMetricProvider(MetricProvider):
def __init__(self):
self._Name = "ram_used_percent"
def isValid(self):
"""
Checks if the RAM usage metric provider is valid.
Returns:
bool: True if the RAM usage metric provider is valid, False otherwise.
"""
return True
def getValue(self):
"""
Returns the RAM usage value.
Returns:
float: The RAM usage value.
"""
return round(psutil.virtual_memory().percent, 1)