mirror of
https://github.com/luckfox-eng29/kvm.git
synced 2026-01-20 10:14:18 +01:00
chore: add metrics for configuration and WOL (#193)
* Configuration load success/timestamp. * Wake-on-Lan packets/errors. Signed-off-by: SuperQ <superq@gmail.com>
This commit is contained in:
22
wol.go
22
wol.go
@@ -4,6 +4,24 @@ import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"net"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
)
|
||||
|
||||
var (
|
||||
wolPackets = promauto.NewCounter(
|
||||
prometheus.CounterOpts{
|
||||
Name: "jetkvm_wol_sent_packets_total",
|
||||
Help: "Total number of Wake-on-LAN magic packets sent.",
|
||||
},
|
||||
)
|
||||
wolErrors = promauto.NewCounter(
|
||||
prometheus.CounterOpts{
|
||||
Name: "jetkvm_wol_sent_packet_errors_total",
|
||||
Help: "Total number of Wake-on-LAN magic packets errors.",
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
// SendWOLMagicPacket sends a Wake-on-LAN magic packet to the specified MAC address
|
||||
@@ -11,6 +29,7 @@ func rpcSendWOLMagicPacket(macAddress string) error {
|
||||
// Parse the MAC address
|
||||
mac, err := net.ParseMAC(macAddress)
|
||||
if err != nil {
|
||||
wolErrors.Inc()
|
||||
return ErrorfL(wolLogger, "invalid MAC address", err)
|
||||
}
|
||||
|
||||
@@ -20,6 +39,7 @@ func rpcSendWOLMagicPacket(macAddress string) error {
|
||||
// Set up UDP connection
|
||||
conn, err := net.Dial("udp", "255.255.255.255:9")
|
||||
if err != nil {
|
||||
wolErrors.Inc()
|
||||
return ErrorfL(wolLogger, "failed to establish UDP connection", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
@@ -27,10 +47,12 @@ func rpcSendWOLMagicPacket(macAddress string) error {
|
||||
// Send the packet
|
||||
_, err = conn.Write(packet)
|
||||
if err != nil {
|
||||
wolErrors.Inc()
|
||||
return ErrorfL(wolLogger, "failed to send WOL packet", err)
|
||||
}
|
||||
|
||||
wolLogger.Info().Str("mac", macAddress).Msg("WOL packet sent")
|
||||
wolPackets.Inc()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user