feat: add CPU usage metrics

This commit is contained in:
Oleksiy Fomenko
2024-06-16 10:25:12 -04:00
parent 28326160d4
commit 82ce0c56b6
4 changed files with 133 additions and 19 deletions

View File

@@ -52,9 +52,10 @@ These are the metrics this program exports, assuming the `METRICS_PREFIX` is `im
| `immich_server_stats_usage_growth` | `sum of disk space taken up by all users` |
| `metric name` | `description` |
|---------------------------------------|------------------------------------------------------------------|
|---------------------------------------|------------------------------------------------------------------------|
| `immich_system_info_loadAverage` | `array of load average (1m, 5m 15m)` |
| `immich_system_info_memory` | `array of memory states (Total, Available, Percent, Used, Free)` |
| `immich_system_cpu_usage` | `Representing the current system-wide CPU utilization as a percentage` |
## Screenshot

View File

@@ -528,7 +528,7 @@
},
"gridPos": {
"h": 8,
"w": 12,
"w": 8,
"x": 0,
"y": 3
},
@@ -567,6 +567,106 @@
"title": "Load Average",
"type": "timeseries"
},
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisBorderShow": false,
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"insertNulls": false,
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 8,
"w": 8,
"x": 8,
"y": 3
},
"id": 53,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": false
},
"tooltip": {
"maxHeight": 600,
"mode": "single",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"disableTextWrap": false,
"editorMode": "builder",
"expr": "immich_system_info_cpu_usage",
"fullMetaSearch": false,
"includeNullMetadata": true,
"instant": false,
"legendFormat": "CPU usage",
"range": true,
"refId": "A",
"useBackend": false
}
],
"title": "CPU Usage",
"type": "timeseries"
},
{
"datasource": {
"type": "prometheus",
@@ -630,8 +730,8 @@
},
"gridPos": {
"h": 8,
"w": 12,
"x": 12,
"w": 8,
"x": 16,
"y": 3
},
"id": 52,
@@ -838,7 +938,7 @@
"value"
],
"legend": {
"displayMode": "list",
"displayMode": "table",
"placement": "right",
"showLegend": true,
"values": [
@@ -855,6 +955,7 @@
"values": false
},
"tooltip": {
"maxHeight": 600,
"mode": "single",
"sort": "none"
}
@@ -1188,6 +1289,7 @@
"values": false
},
"tooltip": {
"maxHeight": 600,
"mode": "multi",
"sort": "none"
}
@@ -1419,6 +1521,7 @@
"showLegend": true
},
"tooltip": {
"maxHeight": 600,
"mode": "single",
"sort": "none"
}
@@ -1516,6 +1619,7 @@
"showLegend": true
},
"tooltip": {
"maxHeight": 600,
"mode": "single",
"sort": "none"
}
@@ -1702,7 +1806,7 @@
"percent"
],
"legend": {
"displayMode": "list",
"displayMode": "table",
"placement": "right",
"showLegend": true,
"values": [
@@ -1719,6 +1823,7 @@
"values": false
},
"tooltip": {
"maxHeight": 600,
"mode": "single",
"sort": "none"
}
@@ -1947,6 +2052,7 @@
"showLegend": true
},
"tooltip": {
"maxHeight": 600,
"mode": "single",
"sort": "none"
}
@@ -2044,6 +2150,7 @@
"showLegend": true
},
"tooltip": {
"maxHeight": 600,
"mode": "single",
"sort": "none"
}
@@ -2068,8 +2175,7 @@
],
"refresh": "30s",
"revision": 1,
"schemaVersion": 38,
"style": "dark",
"schemaVersion": 39,
"tags": [],
"templating": {
"list": []
@@ -2078,10 +2184,11 @@
"from": "now-15m",
"to": "now"
},
"timeRangeUpdatedDuringEditOrView": false,
"timepicker": {},
"timezone": "",
"title": "immich",
"uid": "ZWWp3aa4k",
"version": 5,
"version": 2,
"weekStart": ""
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 228 KiB

After

Width:  |  Height:  |  Size: 234 KiB

View File

@@ -198,6 +198,7 @@ class ImmichMetricsCollector:
def get_system_stats(self):
loadAvg = os.getloadavg()
virtualMem = psutil.virtual_memory()
cpu = psutil.cpu_percent(interval=1, percpu=False)
return [
{
"name": f"{self.config['metrics_prefix']}_system_info_loadAverage",
@@ -247,6 +248,11 @@ class ImmichMetricsCollector:
"help": "Virtual Memory - Free",
"labels": {"type": "Free"},
},
{
"name": f"{self.config['metrics_prefix']}_system_info_cpu_usage",
"value": cpu,
"help": "Representing the current system-wide CPU utilization as a percentage",
},
]
def combine_url(self, api_endpoint):