mirror of https://github.com/jetkvm/kvm.git
Merge fb690543fa
into 009b0abbe9
This commit is contained in:
commit
9889e10486
22
config.go
22
config.go
|
@ -7,6 +7,8 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/jetkvm/kvm/internal/usbgadget"
|
"github.com/jetkvm/kvm/internal/usbgadget"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||||
)
|
)
|
||||||
|
|
||||||
type WakeOnLanDevice struct {
|
type WakeOnLanDevice struct {
|
||||||
|
@ -129,6 +131,21 @@ var (
|
||||||
configLock = &sync.Mutex{}
|
configLock = &sync.Mutex{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
configSuccess = promauto.NewGauge(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "jetkvm_config_last_reload_successful",
|
||||||
|
Help: "The last configuration load succeeded",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
configSuccessTime = promauto.NewGauge(
|
||||||
|
prometheus.GaugeOpts{
|
||||||
|
Name: "jetkvm_config_last_reload_success_timestamp_seconds",
|
||||||
|
Help: "Timestamp of last successful config load",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
func LoadConfig() {
|
func LoadConfig() {
|
||||||
configLock.Lock()
|
configLock.Lock()
|
||||||
defer configLock.Unlock()
|
defer configLock.Unlock()
|
||||||
|
@ -144,6 +161,8 @@ func LoadConfig() {
|
||||||
file, err := os.Open(configPath)
|
file, err := os.Open(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Debug().Msg("default config file doesn't exist, using default")
|
logger.Debug().Msg("default config file doesn't exist, using default")
|
||||||
|
configSuccess.Set(1.0)
|
||||||
|
configSuccessTime.SetToCurrentTime()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
@ -152,6 +171,7 @@ func LoadConfig() {
|
||||||
loadedConfig := *defaultConfig
|
loadedConfig := *defaultConfig
|
||||||
if err := json.NewDecoder(file).Decode(&loadedConfig); err != nil {
|
if err := json.NewDecoder(file).Decode(&loadedConfig); err != nil {
|
||||||
logger.Warn().Err(err).Msg("config file JSON parsing failed")
|
logger.Warn().Err(err).Msg("config file JSON parsing failed")
|
||||||
|
configSuccess.Set(0.0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,6 +184,8 @@ func LoadConfig() {
|
||||||
loadedConfig.UsbDevices = defaultConfig.UsbDevices
|
loadedConfig.UsbDevices = defaultConfig.UsbDevices
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configSuccess.Set(1.0)
|
||||||
|
configSuccessTime.SetToCurrentTime()
|
||||||
config = &loadedConfig
|
config = &loadedConfig
|
||||||
|
|
||||||
rootLogger.UpdateLogLevel()
|
rootLogger.UpdateLogLevel()
|
||||||
|
|
19
wol.go
19
wol.go
|
@ -6,11 +6,27 @@ import (
|
||||||
"net"
|
"net"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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
|
// SendWOLMagicPacket sends a Wake-on-LAN magic packet to the specified MAC address
|
||||||
func rpcSendWOLMagicPacket(macAddress string) error {
|
func rpcSendWOLMagicPacket(macAddress string) error {
|
||||||
// Parse the MAC address
|
// Parse the MAC address
|
||||||
mac, err := net.ParseMAC(macAddress)
|
mac, err := net.ParseMAC(macAddress)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
wolErrors.Inc()
|
||||||
return ErrorfL(wolLogger, "invalid MAC address", err)
|
return ErrorfL(wolLogger, "invalid MAC address", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +36,7 @@ func rpcSendWOLMagicPacket(macAddress string) error {
|
||||||
// Set up UDP connection
|
// Set up UDP connection
|
||||||
conn, err := net.Dial("udp", "255.255.255.255:9")
|
conn, err := net.Dial("udp", "255.255.255.255:9")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
wolErrors.Inc()
|
||||||
return ErrorfL(wolLogger, "failed to establish UDP connection", err)
|
return ErrorfL(wolLogger, "failed to establish UDP connection", err)
|
||||||
}
|
}
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
@ -27,10 +44,12 @@ func rpcSendWOLMagicPacket(macAddress string) error {
|
||||||
// Send the packet
|
// Send the packet
|
||||||
_, err = conn.Write(packet)
|
_, err = conn.Write(packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
wolErrors.Inc()
|
||||||
return ErrorfL(wolLogger, "failed to send WOL packet", err)
|
return ErrorfL(wolLogger, "failed to send WOL packet", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
wolLogger.Info().Str("mac", macAddress).Msg("WOL packet sent")
|
wolLogger.Info().Str("mac", macAddress).Msg("WOL packet sent")
|
||||||
|
wolPackets.Inc()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue