Skip to content

Commit

Permalink
ha - metrics add load and mem usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed Sep 17, 2024
1 parent 3df28bc commit 3a344cf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ph4_metrics/ha/sensor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@
state_topic: "metrics/liv"
value_template: "{{ value_json.temp_zone1 }}"
unit_of_measurement: "C"

- name: "Load Avg 1"
state_topic: "metrics/liv"
value_template: "{{ value_json.load_avg1 }}"
unit_of_measurement: "u"

- name: "Mem Percent"
state_topic: "metrics/liv"
value_template: "{{ value_json.mem_percent }}"
unit_of_measurement: "%"
11 changes: 11 additions & 0 deletions ph4_metrics/ph4_metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
from typing import Optional

import paho.mqtt.client as mqtt # paho-mqtt
import psutil
from attr import dataclass


@dataclass
class Metrics:
temp_zone0: Optional[float] = None
temp_zone1: Optional[float] = None
load_avg_1: Optional[float] = None
mem_percent: Optional[float] = None


class Bark:
Expand Down Expand Up @@ -44,6 +47,8 @@ def publish(self):
{
"temp_zone0": self.metrics.temp_zone0,
"temp_zone1": self.metrics.temp_zone1,
"load_avg1": self.metrics.load_avg_1,
"mem_percent": self.metrics.mem_percent,
},
)

Expand All @@ -59,6 +64,12 @@ def compute_metrics(self):
self.metrics.temp_zone0 = self.read_temp("/sys/class/thermal/thermal_zone0/temp")
self.metrics.temp_zone1 = self.read_temp("/sys/class/thermal/thermal_zone1/temp")

load_avg = psutil.getloadavg()
self.metrics.load_avg_1 = load_avg[0]

memory = psutil.virtual_memory()
self.metrics.mem_percent = memory.percent

except Exception as e:
print(f"Error in metrics collection {e}")

Expand Down
1 change: 1 addition & 0 deletions ph4_metrics/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"future",
"coloredlogs",
"paho-mqtt",
"psutil",
]

dev_extras = [
Expand Down

0 comments on commit 3a344cf

Please sign in to comment.