Add homelab service and system health monitoring

This commit is contained in:
Maxim
2026-08-01 12:02:09 +03:00
parent ad8972320d
commit 980f69cf8f
11 changed files with 674 additions and 33 deletions

View File

@@ -10,11 +10,22 @@ import (
)
type NetworkMetrics struct {
Interface string `json:"interface"`
ReceiveBytes uint64 `json:"receiveBytes"`
TransmitBytes uint64 `json:"transmitBytes"`
ReceivePerSec float64 `json:"receivePerSec"`
TransmitPerSec float64 `json:"transmitPerSec"`
Interface string `json:"interface"`
ReceiveBytes uint64 `json:"receiveBytes"`
TransmitBytes uint64 `json:"transmitBytes"`
ReceivePerSec float64 `json:"receivePerSec"`
TransmitPerSec float64 `json:"transmitPerSec"`
Interfaces []NetworkInterface `json:"interfaces"`
}
type NetworkInterface struct {
Name string `json:"name"`
ReceiveBytes uint64 `json:"receiveBytes"`
TransmitBytes uint64 `json:"transmitBytes"`
ReceiveErrors uint64 `json:"receiveErrors"`
TransmitErrors uint64 `json:"transmitErrors"`
ReceiveDrops uint64 `json:"receiveDrops"`
TransmitDrops uint64 `json:"transmitDrops"`
}
type networkSnapshot struct {
@@ -30,7 +41,7 @@ type networkCollector struct {
}
func newNetworkCollector(path string) (*networkCollector, error) {
snapshot, _, err := readNetworkSnapshot(path)
snapshot, _, _, err := readNetworkSnapshot(path)
if err != nil {
return nil, err
}
@@ -41,7 +52,7 @@ func (c *networkCollector) collect() (NetworkMetrics, error) {
c.mu.Lock()
defer c.mu.Unlock()
current, interfaces, err := readNetworkSnapshot(c.path)
current, interfaceNames, details, err := readNetworkSnapshot(c.path)
if err != nil {
return NetworkMetrics{}, err
}
@@ -53,20 +64,21 @@ func (c *networkCollector) collect() (NetworkMetrics, error) {
}
c.previous = current
return NetworkMetrics{
Interface: strings.Join(interfaces, ", "), ReceiveBytes: current.received,
Interface: strings.Join(interfaceNames, ", "), Interfaces: details, ReceiveBytes: current.received,
TransmitBytes: current.sent, ReceivePerSec: receivedRate, TransmitPerSec: sentRate,
}, nil
}
func readNetworkSnapshot(path string) (networkSnapshot, []string, error) {
func readNetworkSnapshot(path string) (networkSnapshot, []string, []NetworkInterface, error) {
file, err := os.Open(path)
if err != nil {
return networkSnapshot{}, nil, err
return networkSnapshot{}, nil, nil, err
}
defer file.Close()
var received, sent uint64
var interfaces []string
var details []NetworkInterface
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
@@ -84,7 +96,12 @@ func readNetworkSnapshot(path string) (networkSnapshot, []string, error) {
received += rx
sent += tx
interfaces = append(interfaces, strings.TrimSpace(name))
rxErr, _ := strconv.ParseUint(fields[2], 10, 64)
rxDrop, _ := strconv.ParseUint(fields[3], 10, 64)
txErr, _ := strconv.ParseUint(fields[10], 10, 64)
txDrop, _ := strconv.ParseUint(fields[11], 10, 64)
details = append(details, NetworkInterface{Name: strings.TrimSpace(name), ReceiveBytes: rx, TransmitBytes: tx, ReceiveErrors: rxErr, ReceiveDrops: rxDrop, TransmitErrors: txErr, TransmitDrops: txDrop})
}
}
return networkSnapshot{at: time.Now(), received: received, sent: sent}, interfaces, scanner.Err()
return networkSnapshot{at: time.Now(), received: received, sent: sent}, interfaces, details, scanner.Err()
}