-
Notifications
You must be signed in to change notification settings - Fork 0
/
exporter.go
321 lines (292 loc) · 8.73 KB
/
exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
namespace = "package_exporter"
)
type aptOriginArchKey struct {
origin string
arch string
}
func (k aptOriginArchKey) getOriginArch() (string, string) {
return k.origin, k.arch
}
var (
listenAddress = flag.String("web.listen-address", ":9888", "Address to listen on for web interface.")
metricPath = flag.String("web.metrics-path", "/metrics", "Path under which to expose metrics.")
)
type yumCollector struct {
packagesPending *prometheus.Desc
packagesObsolete *prometheus.Desc
rebootRequired *prometheus.Desc
}
type aptCollector struct {
packagesPending *prometheus.Desc
packagesAutoremove *prometheus.Desc
rebootRequired *prometheus.Desc
}
func newYumCollector() *yumCollector {
return &yumCollector{
packagesPending: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "yum_packages_pending"),
"Number of packages pending for update by YUM",
[]string{"origin"}, nil,
),
packagesObsolete: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "yum_packages_obsolete"),
"Number of obsolete packages",
[]string{"origin"}, nil,
),
rebootRequired: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "yum_reboot_required"),
"Reboot required",
nil, nil,
),
}
}
func newAptCollector() *aptCollector {
return &aptCollector{
packagesPending: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "apt_packages_pending"),
"Number of packages pending for update by APT",
[]string{"origin", "arch"}, nil,
),
packagesAutoremove: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "apt_autoremove_pending"),
"Number of pending autoremove packages by APT",
nil, nil,
),
rebootRequired: prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "apt_reboot_required"),
"Reboot required",
nil, nil,
),
}
}
func (collector *yumCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.packagesPending
ch <- collector.packagesObsolete
ch <- collector.rebootRequired
}
func (collector *aptCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.packagesPending
ch <- collector.packagesAutoremove
ch <- collector.rebootRequired
}
func (collector *yumCollector) Collect(ch chan<- prometheus.Metric) {
packagesPerOrigin, err := getYumPendingPackages()
if err != nil {
log.Println("Error collecting YUM metrics:", err)
return
}
obsoletesPerOrigin, err := getYumObsoletePackages()
if err != nil {
log.Println("Error collecting YUM metrics:", err)
return
}
rebootRequired, err := getYumRebootRequired()
if err != nil {
log.Println("Error collecting YUM metrics:", err)
return
}
for origin, packages := range packagesPerOrigin {
var pending_packages int
pending_packages = len(packages)
ch <- prometheus.MustNewConstMetric(collector.packagesPending, prometheus.GaugeValue, float64(pending_packages), origin)
}
for origin, packages := range obsoletesPerOrigin {
var obsolete_packages int
obsolete_packages = len(packages)
ch <- prometheus.MustNewConstMetric(collector.packagesObsolete, prometheus.GaugeValue, float64(obsolete_packages), origin)
}
ch <- prometheus.MustNewConstMetric(collector.rebootRequired, prometheus.GaugeValue, float64(rebootRequired))
}
func (collector *aptCollector) Collect(ch chan<- prometheus.Metric) {
packagesPerOrigin, err := getAptPendingPackages()
if err != nil {
log.Println("Error collecting APT metrics:", err)
return
}
packagesAutoremove, err := getAptAutoremovePackages()
if err != nil {
log.Println("Error collecting APT metrics:", err)
return
}
rebootRequired, err := getAptRebootRequired()
if err != nil {
log.Println("Error collecting APT metrics:", err)
return
}
for originarch, packages := range packagesPerOrigin {
var pending_packages int
pending_packages = len(packages)
origin, arch := originarch.getOriginArch()
ch <- prometheus.MustNewConstMetric(collector.packagesPending, prometheus.GaugeValue, float64(pending_packages), origin, arch)
}
ch <- prometheus.MustNewConstMetric(collector.packagesAutoremove, prometheus.GaugeValue, float64(packagesAutoremove))
ch <- prometheus.MustNewConstMetric(collector.rebootRequired, prometheus.GaugeValue, float64(rebootRequired))
}
func getYumPendingPackages() (map[string][]string, error) {
cmd := exec.Command("/usr/bin/yum", "check-update", "--quiet")
output, err := cmd.Output()
if err != nil {
if err.Error() != "exit status 100" {
return nil, err
}
}
if len(output) > 0 {
lines := strings.Split(string(output), "\n")
packagesPerOrigin := make(map[string][]string)
for _, line := range lines {
if strings.Contains(line, ".") {
parts := strings.Fields(line)
packageName := parts[0]
origin := parts[2]
packagesPerOrigin[origin] = append(packagesPerOrigin[origin], packageName)
}
}
return packagesPerOrigin, nil
} else {
return nil, nil
}
}
func getAptPendingPackages() (map[aptOriginArchKey][]string, error) {
cmd := exec.Command("/usr/bin/apt-get", "--just-print", "dist-upgrade")
output, err := cmd.Output()
if err != nil {
return nil, err
}
if len(output) > 0 {
lines := strings.Split(string(output), "\n")
packagesPerOrigin := make(map[aptOriginArchKey][]string)
instRegex := regexp.MustCompile(`^Inst`)
re := regexp.MustCompile(`\(([^)]+)\)`)
packageRegex := regexp.MustCompile(`\s([\S\s]+?)\s`)
for _, line := range lines {
if instRegex.MatchString(line) {
packageName := packageRegex.FindStringSubmatch(line)[1]
match := re.FindStringSubmatch(line)
origins := strings.Split(match[1], " ")[1:]
originString := strings.Join(origins[:len(origins)-1], " ")
origin := strings.ReplaceAll(originString, ", ", ",")
arch := strings.Trim(origins[len(origins)-1], "[]")
key := aptOriginArchKey{origin, arch}
packagesPerOrigin[key] = append(packagesPerOrigin[key], packageName)
}
}
return packagesPerOrigin, nil
} else {
return nil, nil
}
}
func getYumObsoletePackages() (map[string][]string, error) {
cmd := exec.Command("/usr/bin/yum", "list", "obsoletes", "--quiet")
output, err := cmd.Output()
if err != nil {
return nil, err
}
if len(output) > 0 {
lines := strings.Split(string(output), "\n")
packagesPerOrigin := make(map[string][]string)
for _, line := range lines {
if strings.Contains(line, ".") {
if !strings.HasPrefix(line, " ") {
parts := strings.Fields(line)
packageName := parts[0]
origin := parts[2]
packagesPerOrigin[origin] = append(packagesPerOrigin[origin], packageName)
}
}
}
return packagesPerOrigin, nil
} else {
return nil, nil
}
}
func getAptAutoremovePackages() (int, error) {
cmd := exec.Command("/usr/bin/apt-get", "--just-print", "autoremove")
output, err := cmd.Output()
if err != nil {
return 0, err
}
if len(output) < 0 {
count := 0
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "Remv") {
count++
}
}
return count, nil
} else {
return 0, nil
}
}
func getYumRebootRequired() (int, error) {
cmd := exec.Command("/bin/needs-restarting", "-r")
cmd.Run()
var exitCode int
exitCode = cmd.ProcessState.ExitCode()
if exitCode == 0 {
return 0, nil
} else {
return 1, nil
}
}
func getAptRebootRequired() (int, error) {
if _, err := os.Stat("/run/reboot-required"); err == nil {
return 1, nil
} else {
return 0, nil
}
}
func getPackageManager() (string, error) {
// Try apt-get first
cmd := exec.Command("apt-get", "--version")
output, err := cmd.CombinedOutput()
if err == nil {
return "apt", nil
}
// Try yum next
cmd = exec.Command("yum", "--version")
output, err = cmd.CombinedOutput()
if err == nil {
return "yum", nil
}
// If neither apt-get nor yum were found, return an error
return "", fmt.Errorf("neither apt nor yum package manager found: %s", string(output))
}
func main() {
packageManager, err := getPackageManager()
if err != nil {
log.Println("Error:", err)
return
}
if packageManager == "yum" {
collector := newYumCollector()
prometheus.MustRegister(collector)
} else {
collector := newAptCollector()
prometheus.MustRegister(collector)
}
log.Fatal(serverMetrics(*listenAddress, *metricPath))
}
func serverMetrics(listenAddress, metricsPath string) error {
http.Handle(metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Package Metrics</title></head>
<body>
<h1>Package Exporter</h1>
<p><a href='` + metricsPath + `'>Metrics</a></p>
</body>
</html>
`))
})
return http.ListenAndServe(listenAddress, nil)
}