diff --git a/docker/deploy.sh b/docker/deploy.sh index 2ba347f..83cb785 100755 --- a/docker/deploy.sh +++ b/docker/deploy.sh @@ -4,10 +4,12 @@ GIT_HASH=$(git rev-parse --short HEAD) cp ../pyutils/utils.py ../upsmon/ cp ../pyutils/utils.py ../routermon/ +cp ../pyutils/utils.py ../hwmon/ docker compose build --build-arg BUILD_VER=${GIT_HASH} rm ../upsmon/utils.py rm ../routermon/utils.py +rm ../hwmon/utils.py docker compose up -d diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 0143766..2fff481 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -98,6 +98,31 @@ services: - LOG_FILE_LVL=WARNING - 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: # image: lscr.io/linuxserver/unifi-controller:latest # container_name: edx-unifi diff --git a/hwmon/hwmon.Dockerfile b/hwmon/hwmon.Dockerfile new file mode 100644 index 0000000..651a79f --- /dev/null +++ b/hwmon/hwmon.Dockerfile @@ -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" ] diff --git a/hwmon/hwmon.py b/hwmon/hwmon.py new file mode 100644 index 0000000..0139e04 --- /dev/null +++ b/hwmon/hwmon.py @@ -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) \ No newline at end of file