Skip to content

Commit 1337294

Browse files
committed
Add more metrics
* Configuration load success/timestamp. * Wake-on-Lan packets/errors. Signed-off-by: SuperQ <superq@gmail.com>
1 parent 82c018a commit 1337294

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

config.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"sync"
88

99
"github.com/jetkvm/kvm/internal/usbgadget"
10+
"github.com/prometheus/client_golang/prometheus"
11+
"github.com/prometheus/client_golang/prometheus/promauto"
1012
)
1113

1214
type WakeOnLanDevice struct {
@@ -127,6 +129,21 @@ var (
127129
configLock = &sync.Mutex{}
128130
)
129131

132+
var (
133+
configSuccess = promauto.NewGauge(
134+
prometheus.GaugeOpts{
135+
Name: "jetkvm_config_last_reload_successful",
136+
Help: "The last configuration load succeeded",
137+
},
138+
)
139+
configSuccessTime = promauto.NewGauge(
140+
prometheus.GaugeOpts{
141+
Name: "jetkvm_config_last_reload_success_timestamp_seconds",
142+
Help: "Timestamp of last successful config load",
143+
},
144+
)
145+
)
146+
130147
func LoadConfig() {
131148
configLock.Lock()
132149
defer configLock.Unlock()
@@ -142,6 +159,8 @@ func LoadConfig() {
142159
file, err := os.Open(configPath)
143160
if err != nil {
144161
logger.Debug().Msg("default config file doesn't exist, using default")
162+
configSuccess.Set(1.0)
163+
configSuccessTime.SetToCurrentTime()
145164
return
146165
}
147166
defer file.Close()
@@ -150,6 +169,7 @@ func LoadConfig() {
150169
loadedConfig := *defaultConfig
151170
if err := json.NewDecoder(file).Decode(&loadedConfig); err != nil {
152171
logger.Warn().Err(err).Msg("config file JSON parsing failed")
172+
configSuccess.Set(0.0)
153173
return
154174
}
155175

@@ -162,6 +182,8 @@ func LoadConfig() {
162182
loadedConfig.UsbDevices = defaultConfig.UsbDevices
163183
}
164184

185+
configSuccess.Set(1.0)
186+
configSuccessTime.SetToCurrentTime()
165187
config = &loadedConfig
166188
}
167189

wol.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,27 @@ import (
77
"net"
88
)
99

10+
var (
11+
wolPackets = promauto.NewCounter(
12+
prometheus.CounterOpts{
13+
Name: "jetkvm_wol_sent_packets_total",
14+
Help: "Total number of Wake-on-LAN magic packets sent.",
15+
},
16+
)
17+
wolErrors = promauto.NewCounter(
18+
prometheus.CounterOpts{
19+
Name: "jetkvm_wol_sent_packet_errors_total",
20+
Help: "Total number of Wake-on-LAN magic packets errors.",
21+
},
22+
)
23+
)
24+
1025
// SendWOLMagicPacket sends a Wake-on-LAN magic packet to the specified MAC address
1126
func rpcSendWOLMagicPacket(macAddress string) error {
1227
// Parse the MAC address
1328
mac, err := net.ParseMAC(macAddress)
1429
if err != nil {
30+
wolErrors.Inc()
1531
return fmt.Errorf("invalid MAC address: %v", err)
1632
}
1733

@@ -21,16 +37,20 @@ func rpcSendWOLMagicPacket(macAddress string) error {
2137
// Set up UDP connection
2238
conn, err := net.Dial("udp", "255.255.255.255:9")
2339
if err != nil {
40+
wolErrors.Inc()
2441
return fmt.Errorf("failed to establish UDP connection: %v", err)
2542
}
2643
defer conn.Close()
2744

2845
// Send the packet
2946
_, err = conn.Write(packet)
3047
if err != nil {
48+
wolErrors.Inc()
3149
return fmt.Errorf("failed to send WOL packet: %v", err)
3250
}
3351

52+
wolPackets.Inc()
53+
3454
return nil
3555
}
3656

0 commit comments

Comments
 (0)