Skip to content

Commit 794d141

Browse files
author
Ricky Cousins
committed
POC: Refactor Distros
1 parent 98912ca commit 794d141

File tree

5 files changed

+99
-47
lines changed

5 files changed

+99
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
bin/*
2+
updates-exporter

distros/distros.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package distros
2+
3+
type Distro interface {
4+
GetSecurityUpdates() int
5+
GetRebootRequired() bool
6+
}

distros/ubuntu/ubuntu.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ubuntu
2+
3+
import (
4+
"log"
5+
"os"
6+
"os/exec"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
type Ubuntu struct{}
12+
13+
func (Ubuntu) GetSecurityUpdates() int {
14+
cmd := exec.Command("sh", "-c", `apt-get -s dist-upgrade | grep "^Inst" | grep security | wc -l`)
15+
output, err := cmd.Output()
16+
if err != nil {
17+
log.Printf("Error running apt-get: %v", err)
18+
return 0
19+
}
20+
count, _ := strconv.Atoi(strings.TrimSpace(string(output)))
21+
return count
22+
}
23+
24+
func (Ubuntu) GetRebootRequired() bool {
25+
if _, err := os.Stat("/var/run/reboot-required"); err == nil {
26+
return true
27+
}
28+
return false
29+
}
30+

distros/utils.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package distros
2+
3+
import (
4+
"runtime"
5+
"strings"
6+
"os/exec"
7+
)
8+
9+
// DetectLinuxDistro returns "ubuntu", "rhel", or "unknown"
10+
func GetLinuxDistro() string {
11+
if runtime.GOOS != "linux" {
12+
return "unknown"
13+
}
14+
15+
out, err := exec.Command("sh", "-c", "cat /etc/os-release").Output()
16+
if err != nil {
17+
return "unknown"
18+
}
19+
20+
s := strings.ToLower(string(out))
21+
switch {
22+
case strings.Contains(s, "ubuntu"):
23+
return "ubuntu"
24+
case strings.Contains(s, "rhel"), strings.Contains(s, "centos"), strings.Contains(s, "fedora"):
25+
return "rhel"
26+
default:
27+
return "unknown"
28+
}
29+
}

main.go

Lines changed: 33 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1817
const (
19-
DEFAULT_INTERVAL = 86400
18+
DEFAULT_INTERVAL = 3600
2019
DEFAULT_PORT = 9101
2120
)
2221
var (
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

7354
func 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

Comments
 (0)