@@ -5,81 +5,66 @@ import (
55 "fmt"
66 "log"
77 "net/http"
8- "os"
9- "os/exec"
10- "strconv"
11- "strings"
128 "time"
139
1410 "github.com/prometheus/client_golang/prometheus"
1511 "github.com/prometheus/client_golang/prometheus/promhttp"
12+
13+ "github.com/Puppet-Finland/updates-exporter/distros"
14+ "github.com/Puppet-Finland/updates-exporter/distros/ubuntu"
1615)
1716
1817const (
19- DEFAULT_INTERVAL = 86400
18+ DEFAULT_INTERVAL = 3600
2019 DEFAULT_PORT = 9101
2120)
2221var (
23- securityUpdates = prometheus .NewGauge (
24- prometheus.GaugeOpts {
25- Name : "ubuntu_pending_security_updates" ,
26- Help : "Number of pending security updates on Ubuntu" ,
27- },
28- )
29- rebootRequired = prometheus .NewGauge (
30- prometheus.GaugeOpts {
31- Name : "ubuntu_reboot_required" ,
32- Help : "1 if a reboot is required, 0 otherwise" ,
33- },
34- )
22+ securityUpdates = prometheus .NewGauge (prometheus.GaugeOpts {
23+ Name : "pending_security_updates" ,
24+ Help : "Number of pending security updates" ,
25+ })
26+ rebootRequired = prometheus .NewGauge (prometheus.GaugeOpts {
27+ Name : "reboot_required" ,
28+ Help : "1 if a reboot is required, 0 otherwise" ,
29+ })
3530)
3631
37- func init () {
38- prometheus .MustRegister (securityUpdates )
39- prometheus .MustRegister (rebootRequired )
40- }
41-
42- func getPendingSecurityUpdates () int {
43- cmd := exec .Command ("sh" , "-c" , `apt-get -s dist-upgrade | grep "^Inst" | grep security | wc -l` )
44- output , err := cmd .Output ()
45- if err != nil {
46- log .Printf ("Error running apt-get: %v" , err )
47- return - 1
48- }
49- count , err := strconv .Atoi (strings .TrimSpace (string (output )))
50- if err != nil {
51- log .Printf ("Error parsing security update count: %v" , err )
52- return - 1
32+ func getDistro () distros.Distro {
33+ switch distros .GetLinuxDistro () {
34+ case "ubuntu" :
35+ return ubuntu.Ubuntu {}
36+ default :
37+ return nil
5338 }
54- return count
5539}
5640
57- func checkRebootRequired () bool {
58- if _ , err := os . Stat ( "/var/run/reboot-required" ); err == nil {
59- return true
41+ func updateMetrics ( d distros. Distro ) {
42+ if d == nil {
43+ return
6044 }
61- return false
62- }
45+ securityUpdates .Set (float64 (d .GetSecurityUpdates ()))
6346
64- func updateMetrics () {
65- securityUpdates .Set (float64 (getPendingSecurityUpdates ()))
66- if checkRebootRequired () {
47+ if d .GetRebootRequired () {
6748 rebootRequired .Set (1 )
6849 } else {
6950 rebootRequired .Set (0 )
7051 }
7152}
7253
7354func main () {
74- // Command-line flags
75- port := flag .Int ("port " , DEFAULT_PORT , "HTTP port to serve metrics " )
76- interval := flag . Int ( "interval" , DEFAULT_INTERVAL , "Metrics update interval in seconds" )
55+ port := flag . Int ( "port" , DEFAULT_PORT , "HTTP port" )
56+ interval := flag .Int ("interval " , DEFAULT_INTERVAL , "Metrics refresh interval (seconds) " )
57+
7758 flag .Parse ()
7859
79- // Update metrics periodically
60+ prometheus .MustRegister (securityUpdates )
61+ prometheus .MustRegister (rebootRequired )
62+
63+ distro := getDistro ()
64+
8065 go func () {
8166 for {
82- updateMetrics ()
67+ updateMetrics (distro )
8368 time .Sleep (time .Duration (* interval ) * time .Second )
8469 }
8570 }()
@@ -88,3 +73,4 @@ func main() {
8873 fmt .Printf ("Starting updates_exporter on :%d, updating every %d seconds\n " , * port , * interval )
8974 log .Fatal (http .ListenAndServe (fmt .Sprintf (":%d" , * port ), nil ))
9075}
76+
0 commit comments