42 lines
705 B
Go
42 lines
705 B
Go
package uptime
|
|
|
|
import (
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"bugs.watch/agent/internal/aggregate"
|
|
)
|
|
|
|
type Collector struct{}
|
|
|
|
func New() *Collector {
|
|
return &Collector{}
|
|
}
|
|
|
|
func (c *Collector) Name() string {
|
|
return "uptime"
|
|
}
|
|
|
|
// Collect reads /proc/uptime and stores the latest uptime in seconds.
|
|
// Failure is silently ignored by design.
|
|
func (c *Collector) Collect(window *aggregate.Window) {
|
|
data, err := os.ReadFile("/proc/uptime")
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// Format: "<uptime_seconds> <idle_seconds>"
|
|
fields := strings.Fields(string(data))
|
|
if len(fields) < 1 {
|
|
return
|
|
}
|
|
|
|
seconds, err := strconv.ParseFloat(fields[0], 64)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
window.SetUptime(uint64(seconds))
|
|
}
|