This commit is contained in:
parent
5283f5c11e
commit
783eaea96a
1 changed files with 28 additions and 2 deletions
|
@ -2,7 +2,10 @@ package apt
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -12,9 +15,14 @@ import (
|
|||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
const rebootRequiredPath = "/var/run/reboot-required"
|
||||
|
||||
type AptCollector struct {
|
||||
installedPackages int
|
||||
pendingUpdates int
|
||||
rebootRequired int
|
||||
|
||||
rebootRequiredPath string
|
||||
|
||||
promExporter *prometheus.GaugeVec
|
||||
updateFreq time.Duration
|
||||
|
@ -56,6 +64,19 @@ func (c *AptCollector) updatePendingUpdates() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *AptCollector) updateRebootRequired() error {
|
||||
_, err := os.Stat(c.rebootRequiredPath)
|
||||
if err != nil {
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
c.rebootRequired = 0
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("failed to check if reboot-required exists: %w", err)
|
||||
}
|
||||
c.rebootRequired = 1
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *AptCollector) update() error {
|
||||
if err := c.updateInstalledPackages(); err != nil {
|
||||
return fmt.Errorf("failed to update count of installed packages: %w", err)
|
||||
|
@ -63,9 +84,13 @@ func (c *AptCollector) update() error {
|
|||
if err := c.updatePendingUpdates(); err != nil {
|
||||
return fmt.Errorf("failed to update count of pending updates: %w", err)
|
||||
}
|
||||
if err := c.updateRebootRequired(); err != nil {
|
||||
return fmt.Errorf("failed to update reboot status: %w", err)
|
||||
}
|
||||
|
||||
c.promExporter.WithLabelValues("total").Set(float64(c.installedPackages))
|
||||
c.promExporter.WithLabelValues("updates").Set(float64(c.pendingUpdates))
|
||||
c.promExporter.WithLabelValues("reboot_required").Set(float64(c.rebootRequired))
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -97,8 +122,9 @@ func (c *AptCollector) PromCollector() prometheus.Collector {
|
|||
|
||||
func New() *AptCollector {
|
||||
return &AptCollector{
|
||||
updateFreq: 30 * time.Minute,
|
||||
promExporter: prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "packages", Subsystem: "apt", Name: "packages_count", Help: "Count of apt packages"}, []string{"status"}),
|
||||
rebootRequiredPath: rebootRequiredPath,
|
||||
updateFreq: 30 * time.Minute,
|
||||
promExporter: prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "packages", Subsystem: "apt", Name: "packages_count", Help: "Count of apt packages"}, []string{"status"}),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue