Skip to content

Commit f1953fd

Browse files
authored
chore: add metrics for configuration and WOL (jetkvm#193)
* Configuration load success/timestamp. * Wake-on-Lan packets/errors. Signed-off-by: SuperQ <[email protected]>
1 parent 9ba97eb commit f1953fd

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

config.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/jetkvm/kvm/internal/logging"
1010
"github.com/jetkvm/kvm/internal/network"
1111
"github.com/jetkvm/kvm/internal/usbgadget"
12+
"github.com/prometheus/client_golang/prometheus"
13+
"github.com/prometheus/client_golang/prometheus/promauto"
1214
)
1315

1416
type WakeOnLanDevice struct {
@@ -138,6 +140,21 @@ var (
138140
configLock = &sync.Mutex{}
139141
)
140142

143+
var (
144+
configSuccess = promauto.NewGauge(
145+
prometheus.GaugeOpts{
146+
Name: "jetkvm_config_last_reload_successful",
147+
Help: "The last configuration load succeeded",
148+
},
149+
)
150+
configSuccessTime = promauto.NewGauge(
151+
prometheus.GaugeOpts{
152+
Name: "jetkvm_config_last_reload_success_timestamp_seconds",
153+
Help: "Timestamp of last successful config load",
154+
},
155+
)
156+
)
157+
141158
func LoadConfig() {
142159
configLock.Lock()
143160
defer configLock.Unlock()
@@ -153,6 +170,8 @@ func LoadConfig() {
153170
file, err := os.Open(configPath)
154171
if err != nil {
155172
logger.Debug().Msg("default config file doesn't exist, using default")
173+
configSuccess.Set(1.0)
174+
configSuccessTime.SetToCurrentTime()
156175
return
157176
}
158177
defer file.Close()
@@ -161,6 +180,7 @@ func LoadConfig() {
161180
loadedConfig := *defaultConfig
162181
if err := json.NewDecoder(file).Decode(&loadedConfig); err != nil {
163182
logger.Warn().Err(err).Msg("config file JSON parsing failed")
183+
configSuccess.Set(0.0)
164184
return
165185
}
166186

@@ -181,6 +201,9 @@ func LoadConfig() {
181201

182202
logging.GetRootLogger().UpdateLogLevel(config.DefaultLogLevel)
183203

204+
configSuccess.Set(1.0)
205+
configSuccessTime.SetToCurrentTime()
206+
184207
logger.Info().Str("path", configPath).Msg("config loaded")
185208
}
186209

wol.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,32 @@ import (
44
"bytes"
55
"encoding/binary"
66
"net"
7+
8+
"github.com/prometheus/client_golang/prometheus"
9+
"github.com/prometheus/client_golang/prometheus/promauto"
10+
)
11+
12+
var (
13+
wolPackets = promauto.NewCounter(
14+
prometheus.CounterOpts{
15+
Name: "jetkvm_wol_sent_packets_total",
16+
Help: "Total number of Wake-on-LAN magic packets sent.",
17+
},
18+
)
19+
wolErrors = promauto.NewCounter(
20+
prometheus.CounterOpts{
21+
Name: "jetkvm_wol_sent_packet_errors_total",
22+
Help: "Total number of Wake-on-LAN magic packets errors.",
23+
},
24+
)
725
)
826

927
// SendWOLMagicPacket sends a Wake-on-LAN magic packet to the specified MAC address
1028
func rpcSendWOLMagicPacket(macAddress string) error {
1129
// Parse the MAC address
1230
mac, err := net.ParseMAC(macAddress)
1331
if err != nil {
32+
wolErrors.Inc()
1433
return ErrorfL(wolLogger, "invalid MAC address", err)
1534
}
1635

@@ -20,17 +39,20 @@ func rpcSendWOLMagicPacket(macAddress string) error {
2039
// Set up UDP connection
2140
conn, err := net.Dial("udp", "255.255.255.255:9")
2241
if err != nil {
42+
wolErrors.Inc()
2343
return ErrorfL(wolLogger, "failed to establish UDP connection", err)
2444
}
2545
defer conn.Close()
2646

2747
// Send the packet
2848
_, err = conn.Write(packet)
2949
if err != nil {
50+
wolErrors.Inc()
3051
return ErrorfL(wolLogger, "failed to send WOL packet", err)
3152
}
3253

3354
wolLogger.Info().Str("mac", macAddress).Msg("WOL packet sent")
55+
wolPackets.Inc()
3456

3557
return nil
3658
}

0 commit comments

Comments
 (0)