|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "flag" |
5 | | - "fmt" |
6 | | - "log" |
7 | | - "net/http" |
8 | | - "time" |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "time" |
9 | 9 |
|
10 | | - "github.com/prometheus/client_golang/prometheus" |
11 | | - "github.com/prometheus/client_golang/prometheus/promhttp" |
| 10 | + "github.com/prometheus/client_golang/prometheus" |
| 11 | + "github.com/prometheus/client_golang/prometheus/promhttp" |
12 | 12 |
|
13 | | - "github.com/Puppet-Finland/updates-exporter/distros" |
14 | | - "github.com/Puppet-Finland/updates-exporter/distros/ubuntu" |
| 13 | + "github.com/Puppet-Finland/updates-exporter/distros" |
| 14 | + "github.com/Puppet-Finland/updates-exporter/distros/rhel" |
| 15 | + "github.com/Puppet-Finland/updates-exporter/distros/ubuntu" |
15 | 16 | ) |
16 | 17 |
|
17 | 18 | const ( |
18 | | - DEFAULT_INTERVAL = 3600 |
19 | | - DEFAULT_PORT = 9101 |
| 19 | + DEFAULT_INTERVAL = 3600 |
| 20 | + DEFAULT_PORT = 9101 |
20 | 21 | ) |
| 22 | + |
21 | 23 | var ( |
22 | | - securityUpdates = prometheus.NewGauge(prometheus.GaugeOpts{ |
23 | | - Name: "pending_security_updates", |
24 | | - Help: "Number of pending security updates", |
25 | | - }) |
26 | | - totalUpdates = prometheus.NewGauge(prometheus.GaugeOpts{ |
27 | | - Name: "pending_updates", |
28 | | - Help: "Total number of pending updates", |
29 | | - }) |
30 | | - rebootRequired = prometheus.NewGauge(prometheus.GaugeOpts{ |
31 | | - Name: "reboot_required", |
32 | | - Help: "1 if a reboot is required, 0 otherwise", |
33 | | - }) |
| 24 | + securityUpdates = prometheus.NewGauge(prometheus.GaugeOpts{ |
| 25 | + Name: "pending_security_updates", |
| 26 | + Help: "Number of pending security updates", |
| 27 | + }) |
| 28 | + totalUpdates = prometheus.NewGauge(prometheus.GaugeOpts{ |
| 29 | + Name: "pending_updates", |
| 30 | + Help: "Total number of pending updates", |
| 31 | + }) |
| 32 | + rebootRequired = prometheus.NewGauge(prometheus.GaugeOpts{ |
| 33 | + Name: "reboot_required", |
| 34 | + Help: "1 if a reboot is required, 0 otherwise", |
| 35 | + }) |
34 | 36 | ) |
35 | 37 |
|
36 | 38 | func getDistro() distros.Distro { |
37 | | - switch distros.GetLinuxDistro() { |
38 | | - case "ubuntu": |
39 | | - return ubuntu.Ubuntu{} |
40 | | - default: |
41 | | - return nil |
42 | | - } |
| 39 | + switch distros.GetLinuxDistro() { |
| 40 | + case "ubuntu": |
| 41 | + return ubuntu.Ubuntu{} |
| 42 | + case "rocky": |
| 43 | + return rhel.Rocky{} |
| 44 | + default: |
| 45 | + return nil |
| 46 | + } |
43 | 47 | } |
44 | 48 |
|
45 | 49 | func updateMetrics(d distros.Distro) { |
46 | | - if d == nil { |
47 | | - return |
48 | | - } |
49 | | - securityUpdates.Set(float64(d.GetSecurityUpdates())) |
50 | | - totalUpdates.Set(float64(d.GetTotalUpdates())) |
| 50 | + if d == nil { |
| 51 | + return |
| 52 | + } |
| 53 | + securityUpdates.Set(float64(d.GetSecurityUpdates())) |
| 54 | + totalUpdates.Set(float64(d.GetTotalUpdates())) |
51 | 55 |
|
52 | | - if d.GetRebootRequired() { |
53 | | - rebootRequired.Set(1) |
54 | | - } else { |
55 | | - rebootRequired.Set(0) |
56 | | - } |
| 56 | + if d.GetRebootRequired() { |
| 57 | + rebootRequired.Set(1) |
| 58 | + } else { |
| 59 | + rebootRequired.Set(0) |
| 60 | + } |
57 | 61 |
|
58 | 62 | } |
59 | 63 |
|
60 | 64 | func main() { |
61 | | - port := flag.Int("port", DEFAULT_PORT, "HTTP port") |
62 | | - interval := flag.Int("interval", DEFAULT_INTERVAL, "Metrics refresh interval (seconds)") |
| 65 | + port := flag.Int("port", DEFAULT_PORT, "HTTP port") |
| 66 | + interval := flag.Int("interval", DEFAULT_INTERVAL, "Metrics refresh interval (seconds)") |
63 | 67 |
|
64 | | - flag.Parse() |
| 68 | + flag.Parse() |
65 | 69 |
|
66 | | - prometheus.MustRegister(securityUpdates) |
67 | | - prometheus.MustRegister(totalUpdates) |
68 | | - prometheus.MustRegister(rebootRequired) |
| 70 | + prometheus.MustRegister(securityUpdates) |
| 71 | + prometheus.MustRegister(totalUpdates) |
| 72 | + prometheus.MustRegister(rebootRequired) |
69 | 73 |
|
70 | | - distro := getDistro() |
71 | | - if distro == nil { |
72 | | - log.Fatal("Error: Distro not detected") |
73 | | - } |
| 74 | + distro := getDistro() |
| 75 | + if distro == nil { |
| 76 | + log.Fatal("Error: Distro not detected") |
| 77 | + } |
74 | 78 |
|
75 | | - go func() { |
76 | | - for { |
77 | | - updateMetrics(distro) |
78 | | - time.Sleep(time.Duration(*interval) * time.Second) |
79 | | - } |
80 | | - }() |
| 79 | + go func() { |
| 80 | + for { |
| 81 | + updateMetrics(distro) |
| 82 | + time.Sleep(time.Duration(*interval) * time.Second) |
| 83 | + } |
| 84 | + }() |
81 | 85 |
|
82 | | - http.Handle("/metrics", promhttp.Handler()) |
83 | | - fmt.Printf("Starting updates_exporter on :%d, updating every %d seconds\n", *port, *interval) |
84 | | - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) |
| 86 | + http.Handle("/metrics", promhttp.Handler()) |
| 87 | + fmt.Printf("Starting updates_exporter on :%d, updating every %d seconds\n", *port, *interval) |
| 88 | + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)) |
85 | 89 | } |
86 | | - |
|
0 commit comments