Skip to content

Commit 0fe7831

Browse files
ClusterMmiaucl
andauthored
Fix parsing of memory values with suffixes from top (#43)
* Fix parsing of memory values with suffixes from `top` Previously, systemctl2mqtt failed to correctly parse memory usage values reported by `top` (e.g. "8.5g"). This patch adds proper handling of unit suffixes (k/m/g/t) and converts them into numeric values for consistent processing. * remove deprecated rule -https://docs.astral.sh/ruff/rules/suspicious-xmle-tree-usage/ * ruff linting * bump to 1.3.1 --------- Co-authored-by: Cyrill Raccaud <cyrill.raccaud@aposphere.com>
1 parent 386a8d6 commit 0fe7831

5 files changed

Lines changed: 24 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 1.3.1
4+
5+
* Fix parsing of memory values with suffixes from top
6+
37
## 1.3.0
48

59
* Add option to group all entities into a single device in home assistant

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ select = [
9292
"S317", # suspicious-xml-sax-usage
9393
"S318", # suspicious-xml-mini-dom-usage
9494
"S319", # suspicious-xml-pull-dom-usage
95-
"S320", # suspicious-xmle-tree-usage
9695
"S601", # paramiko-call
9796
"S602", # subprocess-popen-with-shell-equals-true
9897
"S604", # call-with-shell-equals-true

systemctl2mqtt/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""systemctl2mqtt package."""
22

3-
__version__ = "1.3.0"
3+
__version__ = "1.3.1"
44

55
from .const import (
66
ANSI_ESCAPE,

systemctl2mqtt/helpers.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,20 @@ def clean_for_discovery(
2525
for k, v in dict(val).items()
2626
if isinstance(v, str | int | float | object) and v not in (None, "")
2727
}
28+
29+
30+
def parse_top_size(s: str) -> float:
31+
"""Parse size string from top (like '8.5g', '512m', '1024k') into float (kilobytes)."""
32+
s = s.strip().lower()
33+
if s[-1] in "kmgt":
34+
num = float(s[:-1])
35+
unit = s[-1]
36+
if unit == "k":
37+
return num
38+
elif unit == "m":
39+
return num * 1024**1
40+
elif unit == "g":
41+
return num * 1024**2
42+
elif unit == "t":
43+
return num * 1024**3
44+
return float(s) # no suffix

systemctl2mqtt/systemctl2mqtt.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
Systemctl2MqttException,
4949
Systemctl2MqttStatsException,
5050
)
51-
from .helpers import clean_for_discovery
51+
from .helpers import clean_for_discovery, parse_top_size
5252
from .type_definitions import (
5353
PIDStats,
5454
ServiceDeviceEntry,
@@ -1049,7 +1049,7 @@ def _handle_stats_queue(self) -> None:
10491049
{
10501050
"pid": pid,
10511051
"cpu": float(stat[8]),
1052-
"memory": float(stat[5]) / 1024, # KB --> MB
1052+
"memory": parse_top_size(stat[5]) / 1024, # KB --> MB
10531053
}
10541054
)
10551055
stats_logger.debug("Printing pid stats: %s", pid_stats)

0 commit comments

Comments
 (0)