From a7f3d49fea6c867c8c73e078669ed51e27181d45 Mon Sep 17 00:00:00 2001 From: Brad Stein Date: Tue, 27 Jan 2026 16:34:31 -0300 Subject: [PATCH] monitoring: read tegrastats per scrape --- .../jetson-tegrastats-exporter.yaml | 2 +- .../scripts/jetson_tegrastats_exporter.py | 32 ++++++++----------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/services/monitoring/jetson-tegrastats-exporter.yaml b/services/monitoring/jetson-tegrastats-exporter.yaml index d80d83e..3679938 100644 --- a/services/monitoring/jetson-tegrastats-exporter.yaml +++ b/services/monitoring/jetson-tegrastats-exporter.yaml @@ -17,7 +17,7 @@ spec: annotations: prometheus.io/scrape: "true" prometheus.io/port: "9100" - monitoring.bstein.dev/restart-rev: "4" + monitoring.bstein.dev/restart-rev: "5" spec: serviceAccountName: default hostPID: true diff --git a/services/monitoring/scripts/jetson_tegrastats_exporter.py b/services/monitoring/scripts/jetson_tegrastats_exporter.py index 204e439..8314ad7 100644 --- a/services/monitoring/scripts/jetson_tegrastats_exporter.py +++ b/services/monitoring/scripts/jetson_tegrastats_exporter.py @@ -7,7 +7,6 @@ from time import time PORT = int(os.environ.get("JETSON_EXPORTER_PORT", "9100")) NODE_NAME = os.environ.get("NODE_NAME") or os.uname().nodename -LOGFILE = "/tmp/tegrastats.log" BASE_METRICS = { "gr3d_freq_percent": 0.0, "gpu_temp_c": 0.0, @@ -39,25 +38,21 @@ def parse_line(line: str) -> dict: updates["power_5v_in_mw"] = float(m.group(1)) return updates -def start_tegrastats(): - subprocess.Popen( - ["/host/usr/bin/tegrastats", "--interval", "1000", "--logfile", LOGFILE], - stdout=subprocess.DEVNULL, - stderr=subprocess.STDOUT, - text=True, - ) - - def read_latest_line() -> str: - if not os.path.exists(LOGFILE): - return "" try: - with open(LOGFILE, "rb") as handle: - handle.seek(0, os.SEEK_END) - size = handle.tell() - handle.seek(max(size - 4096, 0), os.SEEK_SET) - tail = handle.read().decode("utf-8", errors="ignore").splitlines() - return tail[-1] if tail else "" + proc = subprocess.Popen( + ["/host/usr/bin/tegrastats", "--interval", "1000"], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + text=True, + ) + line = proc.stdout.readline() + proc.terminate() + try: + proc.wait(timeout=1) + except subprocess.TimeoutExpired: + proc.kill() + return line except OSError: return "" @@ -88,6 +83,5 @@ class Handler(http.server.BaseHTTPRequestHandler): return if __name__ == "__main__": - start_tegrastats() with socketserver.TCPServer(("", PORT), Handler) as httpd: httpd.serve_forever()