Fix Golang slog in uptime monitor #13
This commit is contained in:
parent
adb6e54c9c
commit
fb7a84cf4c
|
@ -1,16 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"frigate/uptime/ping"
|
|
||||||
"frigate/uptime/notify"
|
"frigate/uptime/notify"
|
||||||
|
"frigate/uptime/ping"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type CameraMonitor struct {
|
type CameraMonitor struct {
|
||||||
pingInterval time.Duration
|
pingInterval time.Duration
|
||||||
pingTimeout time.Duration
|
pingTimeout time.Duration
|
||||||
|
@ -32,12 +33,12 @@ func NewCameraMonitor(pingInterval time.Duration, pingTimeout time.Duration, con
|
||||||
|
|
||||||
func (c CameraMonitor) onCameraUp(camera string) {
|
func (c CameraMonitor) onCameraUp(camera string) {
|
||||||
c.SendNotification(camera, true)
|
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) {
|
func (c CameraMonitor) onCameraDown(camera string) {
|
||||||
c.SendNotification(camera, false)
|
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) {
|
func (c CameraMonitor) onCameraPingResult(camera string, online bool) {
|
||||||
|
@ -83,11 +84,18 @@ func (c CameraMonitor) Run(cameras map[string]string) {
|
||||||
for range cameras {
|
for range cameras {
|
||||||
select {
|
select {
|
||||||
case cameraPingResult := <-pingResultChannel:
|
case cameraPingResult := <-pingResultChannel:
|
||||||
slog.Debug(cameraPingResult.camera, "camera:", cameraPingResult.online)
|
slog.Debug(cameraPingResult.camera, "online", cameraPingResult.online)
|
||||||
c.onCameraPingResult(cameraPingResult.camera, cameraPingResult.online)
|
c.onCameraPingResult(cameraPingResult.camera, cameraPingResult.online)
|
||||||
delete(unknownPingResultCameras, cameraPingResult.camera) // Maintain set of cameras with unknown ping status
|
delete(unknownPingResultCameras, cameraPingResult.camera) // Maintain set of cameras with unknown ping status
|
||||||
case <-timeoutChannel:
|
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
|
break timeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -98,7 +106,7 @@ func (c CameraMonitor) Run(cameras map[string]string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var sleepDuration = c.pingInterval - time.Since(startTime)
|
var sleepDuration = c.pingInterval - time.Since(startTime)
|
||||||
slog.Debug("Sleeping for", sleepDuration)
|
slog.Debug("Sleeping", "seconds", sleepDuration)
|
||||||
time.Sleep(sleepDuration)
|
time.Sleep(sleepDuration)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
package notify
|
package notify
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"crypto/tls"
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
@ -15,7 +16,11 @@ type NtfyNotifier struct {
|
||||||
|
|
||||||
func NewNtfyNotifier() NtfyNotifier {
|
func NewNtfyNotifier() NtfyNotifier {
|
||||||
return 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",
|
Icon: "https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/frigate.png",
|
||||||
})
|
})
|
||||||
if jsonErr != nil {
|
if jsonErr != nil {
|
||||||
slog.Error("Failed to construct JSON request body", jsonErr)
|
slog.Error("Failed to construct JSON request body", "error", jsonErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var req, reqErr = http.NewRequest("POST", "https://ntfy.homelab.net", bytes.NewBuffer([]byte(reqJson)))
|
var req, reqErr = http.NewRequest("POST", "https://ntfy.homelab.net", bytes.NewBuffer([]byte(reqJson)))
|
||||||
if reqErr != nil {
|
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")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
var resp, respErr = n.client.Do(req)
|
var resp, respErr = n.client.Do(req)
|
||||||
if respErr != nil {
|
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 {
|
if resp.StatusCode != 200 {
|
||||||
slog.Error("Ntfy notification returned HTTP", resp.Status)
|
slog.Error("Ntfy notification returned HTTP", "status", resp.Status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue