sys-exporter/collector/sysinfo/sysinfo.go

187 lines
4 KiB
Go
Raw Permalink Normal View History

2024-12-07 16:19:05 +00:00
package sysinfo
import (
"context"
"errors"
"fmt"
"os"
"regexp"
"strings"
"time"
"git.faercol.me/monitoring/sys-exporter/registry"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
)
var versionRegexp = regexp.MustCompile(`(?m)^(\w+) version ([^ ]+) .*$`)
const (
hosnameFile = "/etc/hostname"
osRealeaseFile = "/etc/os-release"
versionFile = "/proc/version"
2024-12-07 16:43:03 +00:00
machineIDFile = "/etc/machine-id"
2024-12-07 16:19:05 +00:00
)
var labels = []string{
"hostname",
"kernel",
"os",
"distro",
"distro_version",
2024-12-07 16:43:03 +00:00
"machine_id",
2024-12-07 16:19:05 +00:00
}
type SysinfoCollector struct {
hostname string
kernelVersion string
os string
distro string
distroVersion string
2024-12-07 16:43:03 +00:00
machineID string
2024-12-07 16:19:05 +00:00
hostnameFile string
osReleaseFile string
versionFile string
2024-12-07 16:43:03 +00:00
machineIDFile string
2024-12-07 16:19:05 +00:00
promExporter *prometheus.GaugeVec
updateFreq time.Duration
l *zap.SugaredLogger
}
func (c *SysinfoCollector) Collect() interface{} {
return nil
}
func (c *SysinfoCollector) labels() prometheus.Labels {
return prometheus.Labels{
"hostname": c.hostname,
"kernel": c.kernelVersion,
"os": c.os,
"distro": c.distro,
"distro_version": c.distroVersion,
2024-12-07 16:43:03 +00:00
"machine_id": c.machineID,
2024-12-07 16:19:05 +00:00
}
}
func (c *SysinfoCollector) updateHostname() error {
rawHostname, err := os.ReadFile(c.hostnameFile)
if err != nil {
return err
}
c.hostname = strings.TrimSpace(string(rawHostname))
return nil
}
func (c *SysinfoCollector) updateKernelVersion() error {
rawVersion, err := os.ReadFile(c.versionFile)
if err != nil {
return err
}
if matches := versionRegexp.FindAllStringSubmatch(string(rawVersion), -1); matches == nil {
return errors.New("failed to match /proc/version")
} else {
c.os = matches[0][1]
c.kernelVersion = matches[0][2]
}
return nil
}
func (c *SysinfoCollector) updateDistro() error {
rawContent, err := os.ReadFile(c.osReleaseFile)
if err != nil {
return err
}
releaseData, err := parseOSRelease(rawContent)
if err != nil {
return fmt.Errorf("failed to parse /etc/os-release file: %w", err)
}
switch releaseData["ID"] {
case "arch":
c.distro = "Arch Linux"
c.distroVersion = releaseData["BUILD_ID"]
case "debian":
c.distro = "Debian GNU/Linux"
2024-12-13 09:58:40 +00:00
c.distroVersion = strings.ReplaceAll(releaseData["VERSION"], `"`, "")
2024-12-07 16:19:05 +00:00
default:
c.distro = releaseData["NAME"]
}
return nil
}
2024-12-07 16:43:03 +00:00
func (c *SysinfoCollector) updateMachineID() error {
machineIDContent, err := os.ReadFile(c.machineIDFile)
if err != nil {
return err
}
c.machineID = strings.TrimSpace(string(machineIDContent))
return nil
}
2024-12-07 16:19:05 +00:00
func (c *SysinfoCollector) update() error {
if err := c.updateKernelVersion(); err != nil {
return fmt.Errorf("failed to get kernel version: %w", err)
}
if err := c.updateHostname(); err != nil {
return fmt.Errorf("failed to get hostname: %w", err)
}
if err := c.updateDistro(); err != nil {
return fmt.Errorf("failed to get distro info: %w", err)
}
2024-12-07 16:43:03 +00:00
if err := c.updateMachineID(); err != nil {
return fmt.Errorf("failed to get machine-id: %w", err)
}
2024-12-07 16:19:05 +00:00
c.promExporter.With(c.labels()).Set(1)
return nil
}
func (c *SysinfoCollector) Run(ctx context.Context, l *zap.SugaredLogger) {
c.l = l
c.l.Debug("Initializing collector")
if err := c.update(); err != nil {
c.l.Errorf("Failed to init collector: %s\n", err)
}
for {
select {
case <-ctx.Done():
c.l.Debug("Stopping collector")
return
case <-time.After(c.updateFreq):
c.l.Debug("Updating collector")
if err := c.update(); err != nil {
c.l.Errorf("Failed to update collector: %s\n", err)
}
}
}
}
func (c *SysinfoCollector) PromCollector() prometheus.Collector {
return c.promExporter
}
func New() *SysinfoCollector {
return &SysinfoCollector{
updateFreq: 30 * time.Minute,
hostnameFile: hosnameFile,
osReleaseFile: osRealeaseFile,
versionFile: versionFile,
2024-12-07 16:43:03 +00:00
machineIDFile: machineIDFile,
2024-12-07 16:19:05 +00:00
promExporter: prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "system", Name: "info", Help: "System global information"}, labels),
}
}
func init() {
registry.R.MustRegisterCollector("system.info", New())
}