2021-01-04 00:35:58 +01:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
import time
|
2021-02-03 13:36:13 +01:00
|
|
|
import psutil
|
|
|
|
import shutil
|
2021-11-29 22:52:58 +01:00
|
|
|
import os
|
2022-04-11 14:10:19 +02:00
|
|
|
import requests
|
2022-04-16 17:40:04 +02:00
|
|
|
from typing import Optional, Any
|
2022-11-19 14:16:33 +01:00
|
|
|
from multiprocessing.synchronize import Event as MpEvent
|
2021-01-04 00:35:58 +01:00
|
|
|
|
2022-11-24 03:03:20 +01:00
|
|
|
from frigate.comms.dispatcher import Dispatcher
|
2021-01-04 00:35:58 +01:00
|
|
|
from frigate.config import FrigateConfig
|
2021-02-03 13:36:13 +01:00
|
|
|
from frigate.const import RECORD_DIR, CLIPS_DIR, CACHE_DIR
|
2022-04-16 17:40:04 +02:00
|
|
|
from frigate.types import StatsTrackingTypes, CameraMetricsTypes
|
2021-01-04 00:35:58 +01:00
|
|
|
from frigate.version import VERSION
|
2022-11-13 19:48:14 +01:00
|
|
|
from frigate.util import get_cpu_stats
|
2022-11-04 03:23:09 +01:00
|
|
|
from frigate.object_detection import ObjectDetectProcess
|
2021-01-04 00:35:58 +01:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2022-04-11 14:10:19 +02:00
|
|
|
def get_latest_version() -> str:
|
2022-05-26 17:04:33 +02:00
|
|
|
try:
|
|
|
|
request = requests.get(
|
2022-10-01 15:58:23 +02:00
|
|
|
"https://api.github.com/repos/blakeblackshear/frigate/releases/latest",
|
|
|
|
timeout=10,
|
2022-05-26 17:04:33 +02:00
|
|
|
)
|
|
|
|
except:
|
|
|
|
return "unknown"
|
|
|
|
|
2022-04-11 14:10:19 +02:00
|
|
|
response = request.json()
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
if request.ok and response and "tag_name" in response:
|
|
|
|
return str(response.get("tag_name").replace("v", ""))
|
2022-04-11 14:10:19 +02:00
|
|
|
else:
|
|
|
|
return "unknown"
|
|
|
|
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def stats_init(
|
2022-11-04 03:23:09 +01:00
|
|
|
camera_metrics: dict[str, CameraMetricsTypes],
|
|
|
|
detectors: dict[str, ObjectDetectProcess],
|
2022-04-16 17:40:04 +02:00
|
|
|
) -> StatsTrackingTypes:
|
|
|
|
stats_tracking: StatsTrackingTypes = {
|
2021-02-17 14:23:32 +01:00
|
|
|
"camera_metrics": camera_metrics,
|
|
|
|
"detectors": detectors,
|
|
|
|
"started": int(time.time()),
|
2022-04-11 14:10:19 +02:00
|
|
|
"latest_frigate_version": get_latest_version(),
|
2021-01-04 00:35:58 +01:00
|
|
|
}
|
|
|
|
return stats_tracking
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def get_fs_type(path: str) -> str:
|
2021-02-03 13:36:13 +01:00
|
|
|
bestMatch = ""
|
|
|
|
fsType = ""
|
|
|
|
for part in psutil.disk_partitions(all=True):
|
|
|
|
if path.startswith(part.mountpoint) and len(bestMatch) < len(part.mountpoint):
|
|
|
|
fsType = part.fstype
|
|
|
|
bestMatch = part.mountpoint
|
|
|
|
return fsType
|
|
|
|
|
2021-12-12 17:27:01 +01:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def read_temperature(path: str) -> Optional[float]:
|
2021-11-29 22:52:58 +01:00
|
|
|
if os.path.isfile(path):
|
|
|
|
with open(path) as f:
|
2021-12-12 17:27:01 +01:00
|
|
|
line = f.readline().strip()
|
|
|
|
return int(line) / 1000
|
2021-11-29 22:52:58 +01:00
|
|
|
return None
|
|
|
|
|
2021-12-12 17:27:01 +01:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def get_temperatures() -> dict[str, float]:
|
2021-12-12 17:27:01 +01:00
|
|
|
temps = {}
|
2021-11-29 22:52:58 +01:00
|
|
|
|
|
|
|
# Get temperatures for all attached Corals
|
2021-12-12 17:27:01 +01:00
|
|
|
base = "/sys/class/apex/"
|
|
|
|
if os.path.isdir(base):
|
|
|
|
for apex in os.listdir(base):
|
|
|
|
temp = read_temperature(os.path.join(base, apex, "temp"))
|
|
|
|
if temp is not None:
|
|
|
|
temps[apex] = temp
|
2021-11-29 22:52:58 +01:00
|
|
|
|
|
|
|
return temps
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2021-12-12 17:27:01 +01:00
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def stats_snapshot(stats_tracking: StatsTrackingTypes) -> dict[str, Any]:
|
2021-02-17 14:23:32 +01:00
|
|
|
camera_metrics = stats_tracking["camera_metrics"]
|
2022-04-16 17:40:04 +02:00
|
|
|
stats: dict[str, Any] = {}
|
2021-01-04 00:35:58 +01:00
|
|
|
|
|
|
|
total_detection_fps = 0
|
|
|
|
|
|
|
|
for name, camera_stats in camera_metrics.items():
|
2021-02-17 14:23:32 +01:00
|
|
|
total_detection_fps += camera_stats["detection_fps"].value
|
2022-04-16 17:40:04 +02:00
|
|
|
pid = camera_stats["process"].pid if camera_stats["process"] else None
|
2022-11-13 19:48:14 +01:00
|
|
|
ffmpeg_pid = (
|
|
|
|
camera_stats["ffmpeg_pid"].value if camera_stats["ffmpeg_pid"] else None
|
|
|
|
)
|
2022-04-16 17:40:04 +02:00
|
|
|
cpid = (
|
|
|
|
camera_stats["capture_process"].pid
|
|
|
|
if camera_stats["capture_process"]
|
|
|
|
else None
|
|
|
|
)
|
2021-01-04 00:35:58 +01:00
|
|
|
stats[name] = {
|
2021-02-17 14:23:32 +01:00
|
|
|
"camera_fps": round(camera_stats["camera_fps"].value, 2),
|
|
|
|
"process_fps": round(camera_stats["process_fps"].value, 2),
|
|
|
|
"skipped_fps": round(camera_stats["skipped_fps"].value, 2),
|
|
|
|
"detection_fps": round(camera_stats["detection_fps"].value, 2),
|
2022-04-16 17:40:04 +02:00
|
|
|
"pid": pid,
|
|
|
|
"capture_pid": cpid,
|
2022-11-13 19:48:14 +01:00
|
|
|
"ffmpeg_pid": ffmpeg_pid,
|
2021-01-04 00:35:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
stats["detectors"] = {}
|
2021-01-04 00:35:58 +01:00
|
|
|
for name, detector in stats_tracking["detectors"].items():
|
2022-04-16 17:40:04 +02:00
|
|
|
pid = detector.detect_process.pid if detector.detect_process else None
|
2021-02-17 14:23:32 +01:00
|
|
|
stats["detectors"][name] = {
|
|
|
|
"inference_speed": round(detector.avg_inference_speed.value * 1000, 2),
|
|
|
|
"detection_start": detector.detection_start.value,
|
2022-04-16 17:40:04 +02:00
|
|
|
"pid": pid,
|
2021-01-04 00:35:58 +01:00
|
|
|
}
|
2021-02-17 14:23:32 +01:00
|
|
|
stats["detection_fps"] = round(total_detection_fps, 2)
|
2021-01-04 00:35:58 +01:00
|
|
|
|
2022-11-13 19:48:14 +01:00
|
|
|
stats["cpu_usages"] = get_cpu_stats()
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
stats["service"] = {
|
|
|
|
"uptime": (int(time.time()) - stats_tracking["started"]),
|
|
|
|
"version": VERSION,
|
2022-04-11 14:10:19 +02:00
|
|
|
"latest_version": stats_tracking["latest_frigate_version"],
|
2021-02-17 14:23:32 +01:00
|
|
|
"storage": {},
|
2021-12-12 17:27:01 +01:00
|
|
|
"temperatures": get_temperatures(),
|
2021-01-04 00:35:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-03 13:36:13 +01:00
|
|
|
for path in [RECORD_DIR, CLIPS_DIR, CACHE_DIR, "/dev/shm"]:
|
|
|
|
storage_stats = shutil.disk_usage(path)
|
2021-02-17 14:23:32 +01:00
|
|
|
stats["service"]["storage"][path] = {
|
|
|
|
"total": round(storage_stats.total / 1000000, 1),
|
|
|
|
"used": round(storage_stats.used / 1000000, 1),
|
|
|
|
"free": round(storage_stats.free / 1000000, 1),
|
|
|
|
"mount_type": get_fs_type(path),
|
2021-02-03 13:36:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 00:35:58 +01:00
|
|
|
return stats
|
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2021-01-04 00:35:58 +01:00
|
|
|
class StatsEmitter(threading.Thread):
|
2021-02-17 14:23:32 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
config: FrigateConfig,
|
2022-04-16 17:40:04 +02:00
|
|
|
stats_tracking: StatsTrackingTypes,
|
2022-11-24 03:03:20 +01:00
|
|
|
dispatcher: Dispatcher,
|
2022-11-19 14:16:33 +01:00
|
|
|
stop_event: MpEvent,
|
2021-02-17 14:23:32 +01:00
|
|
|
):
|
2021-01-04 00:35:58 +01:00
|
|
|
threading.Thread.__init__(self)
|
2021-02-17 14:23:32 +01:00
|
|
|
self.name = "frigate_stats_emitter"
|
2021-01-04 00:35:58 +01:00
|
|
|
self.config = config
|
|
|
|
self.stats_tracking = stats_tracking
|
2022-11-24 03:03:20 +01:00
|
|
|
self.dispatcher = dispatcher
|
2021-01-04 00:35:58 +01:00
|
|
|
self.stop_event = stop_event
|
|
|
|
|
2022-04-16 17:40:04 +02:00
|
|
|
def run(self) -> None:
|
2021-01-04 00:35:58 +01:00
|
|
|
time.sleep(10)
|
2021-05-21 17:39:14 +02:00
|
|
|
while not self.stop_event.wait(self.config.mqtt.stats_interval):
|
2021-01-04 00:35:58 +01:00
|
|
|
stats = stats_snapshot(self.stats_tracking)
|
2022-11-25 03:12:43 +01:00
|
|
|
self.dispatcher.publish("stats", json.dumps(stats), retain=False)
|
2021-05-21 17:39:14 +02:00
|
|
|
logger.info(f"Exiting watchdog...")
|