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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -12,9 +15,14 @@ import (
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const rebootRequiredPath = "/var/run/reboot-required"
|
||||||
|
|
||||||
type AptCollector struct {
|
type AptCollector struct {
|
||||||
installedPackages int
|
installedPackages int
|
||||||
pendingUpdates int
|
pendingUpdates int
|
||||||
|
rebootRequired int
|
||||||
|
|
||||||
|
rebootRequiredPath string
|
||||||
|
|
||||||
promExporter *prometheus.GaugeVec
|
promExporter *prometheus.GaugeVec
|
||||||
updateFreq time.Duration
|
updateFreq time.Duration
|
||||||
|
@ -56,6 +64,19 @@ func (c *AptCollector) updatePendingUpdates() error {
|
||||||
return nil
|
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 {
|
func (c *AptCollector) update() error {
|
||||||
if err := c.updateInstalledPackages(); err != nil {
|
if err := c.updateInstalledPackages(); err != nil {
|
||||||
return fmt.Errorf("failed to update count of installed packages: %w", err)
|
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 {
|
if err := c.updatePendingUpdates(); err != nil {
|
||||||
return fmt.Errorf("failed to update count of pending updates: %w", err)
|
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("total").Set(float64(c.installedPackages))
|
||||||
c.promExporter.WithLabelValues("updates").Set(float64(c.pendingUpdates))
|
c.promExporter.WithLabelValues("updates").Set(float64(c.pendingUpdates))
|
||||||
|
c.promExporter.WithLabelValues("reboot_required").Set(float64(c.rebootRequired))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,6 +122,7 @@ func (c *AptCollector) PromCollector() prometheus.Collector {
|
||||||
|
|
||||||
func New() *AptCollector {
|
func New() *AptCollector {
|
||||||
return &AptCollector{
|
return &AptCollector{
|
||||||
|
rebootRequiredPath: rebootRequiredPath,
|
||||||
updateFreq: 30 * time.Minute,
|
updateFreq: 30 * time.Minute,
|
||||||
promExporter: prometheus.NewGaugeVec(prometheus.GaugeOpts{Namespace: "packages", Subsystem: "apt", Name: "packages_count", Help: "Count of apt packages"}, []string{"status"}),
|
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