Added hwmon

This commit is contained in:
2026-08-13 16:05:40 +02:00
parent a54c8e566b
commit 4999fd2832
4 changed files with 125 additions and 0 deletions
+2
View File
@@ -4,10 +4,12 @@ GIT_HASH=$(git rev-parse --short HEAD)
cp ../pyutils/utils.py ../upsmon/ cp ../pyutils/utils.py ../upsmon/
cp ../pyutils/utils.py ../routermon/ cp ../pyutils/utils.py ../routermon/
cp ../pyutils/utils.py ../hwmon/
docker compose build --build-arg BUILD_VER=${GIT_HASH} docker compose build --build-arg BUILD_VER=${GIT_HASH}
rm ../upsmon/utils.py rm ../upsmon/utils.py
rm ../routermon/utils.py rm ../routermon/utils.py
rm ../hwmon/utils.py
docker compose up -d docker compose up -d
+25
View File
@@ -98,6 +98,31 @@ services:
- LOG_FILE_LVL=WARNING - LOG_FILE_LVL=WARNING
- LOG_CLI_LVL=INFO - LOG_CLI_LVL=INFO
hwmon:
build:
context: ../hwmon
dockerfile: ../hwmon/hwmon.Dockerfile
args:
BUILD_VER:
#image: upsmon:influx3
container_name: edx-hwmon
restart: unless-stopped
depends_on:
- influxdb3
logging:
driver: "json-file"
options:
max-size: "10m"
max-file: "5"
environment:
- INTERVAL=5
- INFLUXDB_TOKEN=apiv3_tbEpA8JmIRTfr8Wbw9npD79BcMlFsnV4_jhdt-CdUP53Mos61KBODGaggl2g5oKZZvZrZu3e6mpob6zorhEdbg
- INFLUXDB_URL=http://influxdb3:8181
- INFLUXDB_DATABASE=edelweiss30d
- LOG_FILE=/tmp/hwmon.log
- LOG_FILE_LVL=WARNING
- LOG_CLI_LVL=INFO
# unifi-controller: # unifi-controller:
# image: lscr.io/linuxserver/unifi-controller:latest # image: lscr.io/linuxserver/unifi-controller:latest
# container_name: edx-unifi # container_name: edx-unifi
+12
View File
@@ -0,0 +1,12 @@
FROM python:3.12-alpine
ARG BUILD_VER
RUN apk update && apk upgrade --no-cache
RUN pip install --no-cache-dir psutil influxdb3-python
COPY ./hwmon.py /home/hwmon.py
COPY ./utils.py /home/pyutils/utils.py
ENV VER=${BUILD_VER}
CMD [ "python", "/home/hwmon.py" ]
+86
View File
@@ -0,0 +1,86 @@
import os
import logging
import sys
import time
import psutil
from pyutils.utils import *
from influxdb_client_3 import InfluxDBClient3, Point
# Get environment variables
env = dict(os.environ)
LOGGER: logging.Logger
def main():
INTERVAL = int(env['INTERVAL'])
try:
# Init InfluxDB-v3 Client
write_client = InfluxDBClient3(host=env['INFLUXDB_URL'],
token=env['INFLUXDB_TOKEN'],
database=env['INFLUXDB_DATABASE'])
except Exception as e:
LOGGER.error(e)
return 1
finally:
LOGGER.info(f"InfluxDB Connected: [{env['INFLUXDB_URL']}/{env['INFLUXDB_DATABASE']}]")
##############################
########## MAIN LOOP #########
##############################
handler: SignalHandler = SignalHandler(LOGGER)
while handler.running:
try:
p: Point = Point("hwmon").tag("host", "edelweiss-srv")
for i, cpu_percent in enumerate(psutil.cpu_percent(interval=INTERVAL, percpu=True)):
p.field(f"cpu_{i}", cpu_percent)
p.field("temp", psutil.sensors_temperatures()['k10temp'][0].current)
p.field("ram", psutil.virtual_memory().percent)
write_client.write(p)
LOGGER.debug(f"HW Stats: {p.to_line_protocol()}")
except psutil.Error as e:
LOGGER.error(e)
write_client.close()
time.sleep(10)
return 1
except KeyboardInterrupt:
LOGGER.info("Exiting Loop...")
write_client.close()
return 0
except:
LOGGER.error("An unexpected error occurred")
write_client.close()
time.sleep(10.0)
return 1
#### MAIN LOOP ####
if __name__ == "__main__":
# Logger Constants
LOG_FORMAT = '%(asctime)s| %(levelname)-7s|%(funcName)-10s|%(lineno)-3d: %(message)-50s'
# Enabling Logger
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.DEBUG)
LOGGER.propagate = False
formatter = logging.Formatter(LOG_FORMAT, None)
levels = logging.getLevelNamesMapping()
# File logging
log_name = os.path.abspath(env['LOG_FILE'])
fh = logging.FileHandler(log_name)
fh.setLevel(levels[env['LOG_FILE_LVL']])
fh.setFormatter(formatter)
LOGGER.addHandler(fh)
# Console logging
cl = logging.StreamHandler(sys.stdout)
cl.setLevel(levels[env['LOG_CLI_LVL']])
cl.setFormatter(formatter)
LOGGER.addHandler(cl)
LOGGER.warning(f"Routermon started on: {time.asctime()}")
LOGGER.info(f"Routermon BUILD: {env.get("VER", "Test")}")
while main():
LOGGER.error("Main thread exited unexpectedly")
time.sleep(15)
sys.exit(0)