From 3d3f146d2bfa8830ce0b71c8e4469722d73e05d6 Mon Sep 17 00:00:00 2001 From: PovilasID <396243+PovilasID@users.noreply.github.com> Date: Sat, 25 Nov 2023 15:57:47 +0200 Subject: [PATCH 1/3] fix: api endpoint ver 0.13-beta5 --- prometheus_frigate_exporter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/prometheus_frigate_exporter.py b/prometheus_frigate_exporter.py index 87fdb5f..0cac1b9 100644 --- a/prometheus_frigate_exporter.py +++ b/prometheus_frigate_exporter.py @@ -35,6 +35,7 @@ class CustomCollector(object): def collect(self): stats = json.loads(urlopen(self.stats_url).read()) self.process_stats = stats['cpu_usages'] + self.cameras = stats['cameras'] # process stats for cameras, detectors and other cpu_usages = GaugeMetricFamily('frigate_cpu_usage_percent', 'Process CPU usage %', @@ -54,7 +55,7 @@ class CustomCollector(object): detection_enabled = GaugeMetricFamily('frigate_detection_enabled', 'Detection enabled for camera', labels=['camera_name']) - for camera_name, camera_stats in stats.items(): + for camera_name, camera_stats in self.cameras.items(): add_metric(camera_fps, camera_name, camera_stats, 'camera_fps') add_metric(detection_fps, camera_name, camera_stats, 'detection_fps') add_metric(process_fps, camera_name, camera_stats, 'process_fps') From e0eb861664cab35e9d7c4b1fc9dcfdf0b87f8a65 Mon Sep 17 00:00:00 2001 From: Rhys Bailey Date: Sun, 26 Nov 2023 12:06:35 +1100 Subject: [PATCH 2/3] read camera stats pre and post frigate:0.13.0-beta3 --- prometheus_frigate_exporter.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/prometheus_frigate_exporter.py b/prometheus_frigate_exporter.py index 0cac1b9..2ebcd8f 100644 --- a/prometheus_frigate_exporter.py +++ b/prometheus_frigate_exporter.py @@ -35,7 +35,6 @@ class CustomCollector(object): def collect(self): stats = json.loads(urlopen(self.stats_url).read()) self.process_stats = stats['cpu_usages'] - self.cameras = stats['cameras'] # process stats for cameras, detectors and other cpu_usages = GaugeMetricFamily('frigate_cpu_usage_percent', 'Process CPU usage %', @@ -55,7 +54,15 @@ class CustomCollector(object): detection_enabled = GaugeMetricFamily('frigate_detection_enabled', 'Detection enabled for camera', labels=['camera_name']) - for camera_name, camera_stats in self.cameras.items(): + # read camera stats assuming version < frigate:0.13.0-beta3 + cameras = stats + try: + # try to read camera stats in case >= frigate:0.13.0-beta3 + cameras = stats['cameras'] + except KeyError: + pass + + for camera_name, camera_stats in cameras.items(): add_metric(camera_fps, camera_name, camera_stats, 'camera_fps') add_metric(detection_fps, camera_name, camera_stats, 'detection_fps') add_metric(process_fps, camera_name, camera_stats, 'process_fps') From 0a4af50935c71a0c57c888968748061bb872a8ca Mon Sep 17 00:00:00 2001 From: Rhys Bailey Date: Sun, 26 Nov 2023 13:21:10 +1100 Subject: [PATCH 3/3] camera cpu and mem usage work for 0.12 and 0.13 --- prometheus_frigate_exporter.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/prometheus_frigate_exporter.py b/prometheus_frigate_exporter.py index 2ebcd8f..e6d3610 100644 --- a/prometheus_frigate_exporter.py +++ b/prometheus_frigate_exporter.py @@ -23,9 +23,9 @@ class CustomCollector(object): self.stats_url = _url self.process_stats = {} - def add_metric_process(self, metric, stats, camera_name, pid_name, process_name, cpu_or_memory, process_type): + def add_metric_process(self, metric, camera_stats, camera_name, pid_name, process_name, cpu_or_memory, process_type): try: - pid = str(stats[camera_name][pid_name]) + pid = str(camera_stats[pid_name]) label_values = [pid, camera_name, process_name, process_type] metric.add_metric(label_values, self.process_stats[pid][cpu_or_memory]) del self.process_stats[pid][cpu_or_memory] @@ -69,13 +69,13 @@ class CustomCollector(object): add_metric(skipped_fps, camera_name, camera_stats, 'skipped_fps') add_metric(detection_enabled, camera_name, camera_stats, 'detection_enabled') - self.add_metric_process(cpu_usages, stats, camera_name, 'ffmpeg_pid', 'ffmpeg', 'cpu', 'Camera') - self.add_metric_process(cpu_usages, stats, camera_name, 'capture_pid', 'capture', 'cpu', 'Camera') - self.add_metric_process(cpu_usages, stats, camera_name, 'pid', 'detect', 'cpu', 'Camera') + self.add_metric_process(cpu_usages, camera_stats, camera_name, 'ffmpeg_pid', 'ffmpeg', 'cpu', 'Camera') + self.add_metric_process(cpu_usages, camera_stats, camera_name, 'capture_pid', 'capture', 'cpu', 'Camera') + self.add_metric_process(cpu_usages, camera_stats, camera_name, 'pid', 'detect', 'cpu', 'Camera') - self.add_metric_process(mem_usages, stats, camera_name, 'ffmpeg_pid', 'ffmpeg', 'mem', 'Camera') - self.add_metric_process(mem_usages, stats, camera_name, 'capture_pid', 'capture', 'mem', 'Camera') - self.add_metric_process(mem_usages, stats, camera_name, 'pid', 'detect', 'mem', 'Camera') + self.add_metric_process(mem_usages, camera_stats, camera_name, 'ffmpeg_pid', 'ffmpeg', 'mem', 'Camera') + self.add_metric_process(mem_usages, camera_stats, camera_name, 'capture_pid', 'capture', 'mem', 'Camera') + self.add_metric_process(mem_usages, camera_stats, camera_name, 'pid', 'detect', 'mem', 'Camera') yield camera_fps yield detection_fps