catch key and value errors
parent
a89961079a
commit
1deecf4797
|
@ -7,36 +7,50 @@ import time
|
||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
def add_metric(metric, label, data, key, multiplier=1.0):
|
||||||
|
try:
|
||||||
|
string = str(data[key])
|
||||||
|
value = float(re.findall(r'\d+', string)[0])
|
||||||
|
metric.add_metric([label], value * multiplier)
|
||||||
|
except (KeyError, TypeError, IndexError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class CustomCollector(object):
|
class CustomCollector(object):
|
||||||
|
|
||||||
def __init__(self, url):
|
def __init__(self, _url):
|
||||||
self.url = url
|
self.url = _url
|
||||||
|
|
||||||
def collect(self):
|
def collect(self):
|
||||||
data = json.loads(urlopen(self.url).read())
|
data = json.loads(urlopen(self.url).read())
|
||||||
|
|
||||||
# camera stats
|
# camera stats
|
||||||
ffmpeg_pid = GaugeMetricFamily('frigate_ffmpeg_pid', 'PID for ffmpeg process', labels=['camera'])
|
ffmpeg_pid = GaugeMetricFamily('frigate_ffmpeg_pid', 'PID for ffmpeg process', labels=['camera'])
|
||||||
capture_pid = GaugeMetricFamily('frigate_capture_pid', 'PID for the ffmpeg process that consumes this camera', labels=['camera'])
|
capture_pid = GaugeMetricFamily('frigate_capture_pid', 'PID for the ffmpeg process that consumes this camera',
|
||||||
detect_pid = GaugeMetricFamily('frigate_detect_pid', 'PID for the process that runs detection for this camera', labels=['camera'])
|
labels=['camera'])
|
||||||
camera_fps = GaugeMetricFamily('frigate_camera_fps', 'Frames per second being consumed from your camera.', labels=['camera'])
|
detect_pid = GaugeMetricFamily('frigate_detect_pid', 'PID for the process that runs detection for this camera',
|
||||||
detection_fps = GaugeMetricFamily('frigate_detection_fps', 'Number of times detection is run per second.', labels=['camera'])
|
labels=['camera'])
|
||||||
process_fps = GaugeMetricFamily('frigate_process_fps', 'Frames per second being processed by frigate.', labels=['camera'])
|
camera_fps = GaugeMetricFamily('frigate_camera_fps', 'Frames per second being consumed from your camera.',
|
||||||
skipped_fps = GaugeMetricFamily('frigate_skipped_fps', 'Frames per second skip for processing by frigate.', labels=['camera'])
|
labels=['camera'])
|
||||||
detection_enabled = GaugeMetricFamily('frigate_detection_enabled', 'Detection enabled for camera', labels=['camera'])
|
detection_fps = GaugeMetricFamily('frigate_detection_fps', 'Number of times detection is run per second.',
|
||||||
|
labels=['camera'])
|
||||||
|
process_fps = GaugeMetricFamily('frigate_process_fps', 'Frames per second being processed by frigate.',
|
||||||
|
labels=['camera'])
|
||||||
|
skipped_fps = GaugeMetricFamily('frigate_skipped_fps', 'Frames per second skip for processing by frigate.',
|
||||||
|
labels=['camera'])
|
||||||
|
detection_enabled = GaugeMetricFamily('frigate_detection_enabled', 'Detection enabled for camera',
|
||||||
|
labels=['camera'])
|
||||||
|
|
||||||
for d in data:
|
for k, d in data.items():
|
||||||
try:
|
add_metric(ffmpeg_pid, k, d, 'ffmpeg_pid')
|
||||||
ffmpeg_pid.add_metric([d], float(data[d]['ffmpeg_pid']))
|
add_metric(detect_pid, k, d, 'pid')
|
||||||
detect_pid.add_metric([d], float(data[d]['pid']))
|
add_metric(capture_pid, k, d, 'capture_pid')
|
||||||
capture_pid.add_metric([d], float(data[d]['capture_pid']))
|
add_metric(camera_fps, k, d, 'camera_fps')
|
||||||
camera_fps.add_metric([d], (data[d]['camera_fps']))
|
add_metric(detection_fps, k, d, 'detection_fps')
|
||||||
detection_fps.add_metric([d], float(data[d]['detection_fps']))
|
add_metric(process_fps, k, d, 'process_fps')
|
||||||
process_fps.add_metric([d], float(data[d]['process_fps']))
|
add_metric(skipped_fps, k, d, 'skipped_fps')
|
||||||
skipped_fps.add_metric([d], float(data[d]['skipped_fps']))
|
add_metric(detection_enabled, k, d, 'detection_enabled')
|
||||||
detection_enabled.add_metric([d], float(data[d]['detection_enabled']))
|
|
||||||
except (AttributeError, TypeError, KeyError):
|
|
||||||
pass
|
|
||||||
|
|
||||||
yield ffmpeg_pid
|
yield ffmpeg_pid
|
||||||
yield capture_pid
|
yield capture_pid
|
||||||
|
@ -48,26 +62,43 @@ class CustomCollector(object):
|
||||||
yield detection_enabled
|
yield detection_enabled
|
||||||
|
|
||||||
# detector stats
|
# detector stats
|
||||||
yield GaugeMetricFamily('frigate_detection_total_fps', 'Sum of detection_fps across all cameras and detectors.', value=data['detection_fps'])
|
try:
|
||||||
|
yield GaugeMetricFamily('frigate_detection_total_fps',
|
||||||
|
'Sum of detection_fps across all cameras and detectors.',
|
||||||
|
value=data['detection_fps'])
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
detector_inference_speed = GaugeMetricFamily('frigate_detector_inference_speed_seconds', 'Time spent running object detection in seconds.', labels=['name'])
|
detector_inference_speed = GaugeMetricFamily('frigate_detector_inference_speed_seconds',
|
||||||
detector_pid = GaugeMetricFamily('frigate_detector_pid', 'PID for the shared process that runs object detection on the detector', labels=['name'])
|
'Time spent running object detection in seconds.', labels=['name'])
|
||||||
|
detector_pid = GaugeMetricFamily('frigate_detector_pid',
|
||||||
for d in data['detectors']:
|
'PID for the shared process that runs object detection on the detector',
|
||||||
detector_inference_speed.add_metric([d], data['detectors'][d]['inference_speed']/1000.0) # ms to seconds
|
labels=['name'])
|
||||||
detector_pid.add_metric([d], data['detectors'][d]['pid'])
|
detector_detection_start = GaugeMetricFamily('frigate_detection_start',
|
||||||
|
'Detector start time (unix timestamp)',
|
||||||
|
labels=['name'])
|
||||||
|
try:
|
||||||
|
for k, d in data['detectors'].items():
|
||||||
|
add_metric(detector_inference_speed, k, d, 'inference_speed', 0.001) # ms to seconds
|
||||||
|
add_metric(detector_pid, k, d, 'pid')
|
||||||
|
add_metric(detector_detection_start, k, d, 'detection_start')
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
yield detector_inference_speed
|
yield detector_inference_speed
|
||||||
yield detector_pid
|
yield detector_pid
|
||||||
|
yield detector_detection_start
|
||||||
|
|
||||||
# process stats
|
# process stats
|
||||||
cpu_usages = GaugeMetricFamily('frigate_cpu_usage_percent', 'Process CPU usage %', labels=['pid'])
|
cpu_usages = GaugeMetricFamily('frigate_cpu_usage_percent', 'Process CPU usage %', labels=['pid'])
|
||||||
mem_usages = GaugeMetricFamily('frigate_mem_usage_percent', 'Process memory usage %', labels=['pid'])
|
mem_usages = GaugeMetricFamily('frigate_mem_usage_percent', 'Process memory usage %', labels=['pid'])
|
||||||
|
|
||||||
data['cpu_usages'].pop('Tasks:')
|
try:
|
||||||
for d in data['cpu_usages']:
|
for k, d in data['cpu_usages'].items():
|
||||||
cpu_usages.add_metric([d], float(data['cpu_usages'][d]['cpu']))
|
add_metric(cpu_usages, k, d, 'cpu')
|
||||||
mem_usages.add_metric([d], float(data['cpu_usages'][d]['mem']))
|
add_metric(mem_usages, k, d, 'mem')
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
yield cpu_usages
|
yield cpu_usages
|
||||||
yield mem_usages
|
yield mem_usages
|
||||||
|
@ -76,33 +107,50 @@ class CustomCollector(object):
|
||||||
gpu_usages = GaugeMetricFamily('frigate_gpu_usage_percent', 'GPU utilisation %', labels=['gpu'])
|
gpu_usages = GaugeMetricFamily('frigate_gpu_usage_percent', 'GPU utilisation %', labels=['gpu'])
|
||||||
gpu_mem_usages = GaugeMetricFamily('frigate_gpu_mem_usage_percent', 'GPU memory usage %', labels=['gpu'])
|
gpu_mem_usages = GaugeMetricFamily('frigate_gpu_mem_usage_percent', 'GPU memory usage %', labels=['gpu'])
|
||||||
|
|
||||||
for d in data['gpu_usages']:
|
try:
|
||||||
try:
|
for k, d in data['gpu_usages'].items():
|
||||||
gpu_usages.add_metric([d], float(re.findall(r'\d+', data['gpu_usages'][d]['gpu'])[0]))
|
add_metric(gpu_usages, k, d, 'gpu')
|
||||||
mem_usages.add_metric([d], float(re.findall(r'\d+', data['gpu_usages'][d]['mem'])[0])) # no value for me
|
add_metric(gpu_usages, k, d, 'mem')
|
||||||
except IndexError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
yield gpu_usages
|
yield gpu_usages
|
||||||
yield gpu_mem_usages
|
yield gpu_mem_usages
|
||||||
|
|
||||||
# service stats
|
# service stats
|
||||||
yield GaugeMetricFamily('frigate_service_last_updated_timestamp', 'Stats recorded time (unix timestamp)', value=data['service']['last_updated'])
|
uptime_seconds = GaugeMetricFamily('frigate_service_uptime_seconds', 'Uptime seconds')
|
||||||
info = {'latest_version': data['service']['latest_version'], 'version': data['service']['version']}
|
last_updated_timestamp = GaugeMetricFamily('frigate_service_last_updated_timestamp',
|
||||||
yield InfoMetricFamily('frigate_service', 'Frigate version info', value=info)
|
'Stats recorded time (unix timestamp)')
|
||||||
yield GaugeMetricFamily('frigate_service_uptime_seconds', 'Uptime seconds', value=data['service']['uptime'])
|
|
||||||
# temperatures: no data for me
|
try:
|
||||||
|
s = data['service']
|
||||||
|
add_metric(uptime_seconds, '', s, 'uptime')
|
||||||
|
add_metric(last_updated_timestamp, '', s, 'last_updated')
|
||||||
|
|
||||||
|
info = {'latest_version': data['service']['latest_version'], 'version': data['service']['version']}
|
||||||
|
yield InfoMetricFamily('frigate_service', 'Frigate version info', value=info)
|
||||||
|
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
yield uptime_seconds
|
||||||
|
yield last_updated_timestamp
|
||||||
|
|
||||||
|
# service->temperatures: no data for me
|
||||||
|
|
||||||
storage_free = GaugeMetricFamily('frigate_storage_free_bytes', 'Storage free bytes', labels=['storage'])
|
storage_free = GaugeMetricFamily('frigate_storage_free_bytes', 'Storage free bytes', labels=['storage'])
|
||||||
storage_mount_type = InfoMetricFamily('frigate_storage_mount_type', 'Storage mount type', labels=['storage'])
|
storage_mount_type = InfoMetricFamily('frigate_storage_mount_type', 'Storage mount type', labels=['storage'])
|
||||||
storage_total = GaugeMetricFamily('frigate_storage_total_bytes', 'Storage total bytes', labels=['storage'])
|
storage_total = GaugeMetricFamily('frigate_storage_total_bytes', 'Storage total bytes', labels=['storage'])
|
||||||
storage_used = GaugeMetricFamily('frigate_storage_used_bytes', 'Storage used bytes', labels=['storage'])
|
storage_used = GaugeMetricFamily('frigate_storage_used_bytes', 'Storage used bytes', labels=['storage'])
|
||||||
|
|
||||||
for d in data['service']['storage']:
|
try:
|
||||||
storage_free.add_metric([d], data['service']['storage'][d]['free']*1e6) #MB to bytes
|
for k, d in data['service']['storage'].items():
|
||||||
storage_total.add_metric([d], (data['service']['storage'][d]['total']*1e6))
|
add_metric(storage_free, k, d, 'free', 1e6) # MB to bytes
|
||||||
storage_used.add_metric([d], (data['service']['storage'][d]['used']*1e6))
|
add_metric(storage_total, k, d, 'total', 1e6) # MB to bytes
|
||||||
storage_mount_type.add_metric([d], {'mount_type': data['service']['storage'][d]['mount_type']})
|
add_metric(storage_used, k, d, 'used', 1e6) # MB to bytes
|
||||||
|
storage_mount_type.add_metric(k, {'mount_type': d['mount_type']})
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
|
||||||
yield storage_free
|
yield storage_free
|
||||||
yield storage_mount_type
|
yield storage_mount_type
|
||||||
|
@ -114,15 +162,18 @@ if __name__ == '__main__':
|
||||||
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
|
logging.basicConfig(format='%(asctime)s - %(message)s', level=logging.INFO)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
REGISTRY.register(CustomCollector(sys.argv[1]))
|
url = sys.argv[1]
|
||||||
|
REGISTRY.register(CustomCollector(url))
|
||||||
except IndexError:
|
except IndexError:
|
||||||
logging.error("Provide Frigate stats url as environment variable, e.g. FRIGATE_STATS_URL=http://<your-frigate-ip>:5000/api/stats")
|
logging.error(
|
||||||
|
"Provide Frigate stats url as environment variable to container, "
|
||||||
|
"e.g. FRIGATE_STATS_URL=http://<your-frigate-ip>:5000/api/stats")
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
start_http_server(9100)
|
start_http_server(9100)
|
||||||
|
|
||||||
logging.info('Started: ' + sys.argv[1])
|
logging.info('Started, Frigate API URL: ' + sys.argv[1])
|
||||||
|
logging.info('Metrics at: http://localhost:9100/metrics')
|
||||||
while True: time.sleep(1)
|
|
||||||
|
|
||||||
logging.info("Finished")
|
while True:
|
||||||
|
time.sleep(1)
|
||||||
|
|
Loading…
Reference in New Issue