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,16 +1,17 @@
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
@ -32,12 +33,12 @@ func NewCameraMonitor(pingInterval time.Duration, pingTimeout time.Duration, con
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) {
@ -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)
}
}