From e7c6a0fe9af222ce9f45d467272829974327e78f Mon Sep 17 00:00:00 2001 From: badsmoke Date: Thu, 29 Aug 2024 09:13:08 +0200 Subject: [PATCH] add save metrics as file --- README.md | 32 ++++++++++++++++++++++++++++++++ smartprom.py | 4 ++++ 2 files changed, 36 insertions(+) diff --git a/README.md b/README.md index b1149c4..a5270e9 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,36 @@ services: restart: unless-stopped ``` +Example docker-compose.yml with node-exporter and file export: + + +```yml +version: "3" +services: + node-exporter: + image: quay.io/prometheus/node-exporter + restart: always + volumes: + - '/:/host:ro,rslave' + - './tmp/:/tmp/' + network_mode: "host" + pid: "host" + command: + - "--path.rootfs=/host" + - "--collector.textfile.directory=/tmp/" + smartctl-exporter: + image: matusnovak/prometheus-smartctl:latest + container_name: smartctl-exporter + privileged: true + environment: + - "SMARTCTL_METRICS_FILE_ENABLE=True" + volumes: + - ./tmp/:/metrics/ + restart: unless-stopped +``` + + + Your metrics will be available at The exported metrics looks like these: @@ -68,6 +98,8 @@ All configuration is done with environment variables. - `SMARTCTL_REFRESH_INTERVAL`: (Optional) The refresh interval of the metrics. A larger value reduces CPU usage. The default is `60` seconds. - `SMARTCTL_EXPORTER_PORT`: (Optional) The address the exporter should listen on. The default is `9902`. - `SMARTCTL_EXPORTER_ADDRESS`: (Optional) The address the exporter should listen on. The default is to listen on all addresses. +- `SMARTCTL_METRICS_FILE_ENABLE`: (Optional) To enable metrics file, if you have a node exporter running anyway, you can simply read out this file . The default is `False`. +- `SMARTCTL_METRICS_FILE_PATH`: (Optional) the path, this must then also be specified in the docker-compose as volume. The default is `/metrics/`. ## Grafana dashboard diff --git a/smartprom.py b/smartprom.py index a998018..ac11f2f 100755 --- a/smartprom.py +++ b/smartprom.py @@ -293,6 +293,8 @@ def main(): exporter_address = os.environ.get("SMARTCTL_EXPORTER_ADDRESS", "0.0.0.0") exporter_port = int(os.environ.get("SMARTCTL_EXPORTER_PORT", 9902)) refresh_interval = int(os.environ.get("SMARTCTL_REFRESH_INTERVAL", 60)) + metrics_file_enable = os.environ.get("SMARTCTL_METRICS_FILE_ENABLE", False) + metrics_file_path = os.environ.get("SMARTCTL_METRICS_FILE_PATH", "/metrics/") # Get drives (test smartctl) DRIVES = get_drives() @@ -303,6 +305,8 @@ def main(): while True: collect() + if metrics_file_enable: + prometheus_client.write_to_textfile(metrics_file_path+"smartctl.prom", prometheus_client.REGISTRY) time.sleep(refresh_interval)