Fix Golang slog in uptime monitor #13

This commit is contained in:
Ashish D'Souza 2024-10-09 18:47:17 -05:00
parent adb6e54c9c
commit fb7a84cf4c
2 changed files with 36 additions and 21 deletions

View File

@ -1,19 +1,20 @@
package main
import (
"fmt"
"log/slog"
"strings"
"time"
)
import (
"frigate/uptime/ping"
"frigate/uptime/notify"
"frigate/uptime/ping"
)
type CameraMonitor struct {
pingInterval time.Duration
pingTimeout time.Duration
pingInterval time.Duration
pingTimeout time.Duration
consecutiveDownThreshold int
downtime map[string]int
notify.Notifier
@ -21,23 +22,23 @@ type CameraMonitor struct {
func NewCameraMonitor(pingInterval time.Duration, pingTimeout time.Duration, consecutiveDownThreshold int, notifier notify.Notifier) CameraMonitor {
return CameraMonitor{
pingInterval: pingInterval,
pingTimeout: pingTimeout,
pingInterval: pingInterval,
pingTimeout: pingTimeout,
consecutiveDownThreshold: consecutiveDownThreshold,
downtime: make(map[string]int),
Notifier: notifier,
downtime: make(map[string]int),
Notifier: notifier,
}
}
func (c CameraMonitor) onCameraUp(camera string) {
c.SendNotification(camera, true)
slog.Info(camera, "camera is back online!")
slog.Info(fmt.Sprintf("%s camera is back online!", camera))
}
func (c CameraMonitor) onCameraDown(camera string) {
c.SendNotification(camera, false)
slog.Info(camera, "camera is offline!")
slog.Info(fmt.Sprintf("%s camera is offline!", camera))
}
func (c CameraMonitor) onCameraPingResult(camera string, online bool) {
@ -57,7 +58,7 @@ func (c CameraMonitor) onCameraPingResult(camera string, online bool) {
func (c CameraMonitor) Run(cameras map[string]string) {
type pingResult struct {
camera string
online bool
online bool
}
var pingResultChannel = make(chan pingResult, 4)
@ -72,7 +73,7 @@ func (c CameraMonitor) Run(cameras map[string]string) {
go func() {
pingResultChannel <- pingResult{
camera: camera,
online: ping.VideoPing(host),
online: ping.VideoPing(host),
}
}()
unknownPingResultCameras[camera] = true
@ -83,11 +84,18 @@ func (c CameraMonitor) Run(cameras map[string]string) {
for range cameras {
select {
case cameraPingResult := <-pingResultChannel:
slog.Debug(cameraPingResult.camera, "camera:", cameraPingResult.online)
slog.Debug(cameraPingResult.camera, "online", cameraPingResult.online)
c.onCameraPingResult(cameraPingResult.camera, cameraPingResult.online)
delete(unknownPingResultCameras, cameraPingResult.camera) // Maintain set of cameras with unknown ping status
case <-timeoutChannel:
slog.Info("Timed out waiting for cameras", unknownPingResultCameras)
var b strings.Builder
for camera := range unknownPingResultCameras {
if b.Len() > 0 {
b.WriteString(", ")
}
b.WriteString(camera)
}
slog.Warn("Timed out waiting for ping result", "cameras", b.String())
break timeout
}
}
@ -98,7 +106,7 @@ func (c CameraMonitor) Run(cameras map[string]string) {
}
var sleepDuration = c.pingInterval - time.Since(startTime)
slog.Debug("Sleeping for", sleepDuration)
slog.Debug("Sleeping", "seconds", sleepDuration)
time.Sleep(sleepDuration)
}
}

View File

@ -1,9 +1,10 @@
package notify
import (
"fmt"
"crypto/tls"
"bytes"
"encoding/json"
"fmt"
"log/slog"
"net/http"
)
@ -15,7 +16,11 @@ type NtfyNotifier struct {
func NewNtfyNotifier() NtfyNotifier {
return NtfyNotifier{
client: &http.Client{},
client: &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
},
}
}
@ -38,21 +43,23 @@ func (n NtfyNotifier) SendNotification(camera string, online bool) {
Icon: "https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/frigate.png",
})
if jsonErr != nil {
slog.Error("Failed to construct JSON request body", jsonErr)
slog.Error("Failed to construct JSON request body", "error", jsonErr)
return
}
var req, reqErr = http.NewRequest("POST", "https://ntfy.homelab.net", bytes.NewBuffer([]byte(reqJson)))
if reqErr != nil {
slog.Error("Failed to create HTTP request", reqErr)
slog.Error("Failed to create HTTP request", "error", reqErr)
return
}
req.Header.Set("Content-Type", "application/json")
var resp, respErr = n.client.Do(req)
if respErr != nil {
slog.Error("Failed to send HTTP request", respErr)
slog.Error("Failed to send HTTP request", "error", respErr)
return
}
if resp.StatusCode != 200 {
slog.Error("Ntfy notification returned HTTP", resp.Status)
slog.Error("Ntfy notification returned HTTP", "status", resp.Status)
}
}