This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
solution_collector.go
61 lines (51 loc) · 1.85 KB
/
solution_collector.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
package main
import (
"os/exec"
"strings"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
const subsystem = "solution"
// SolutionCollector is the saptune solution collector
type SolutionCollector struct {
DefaultCollector
saptunePath string
}
// NewSolutionCollector creates a new solution saptune collector
func NewSolutionCollector(saptunePath string) (*SolutionCollector, error) {
c := &SolutionCollector{
NewDefaultCollector(subsystem),
saptunePath,
}
c.SetDescriptor("enabled", "show the enabled solution's name. 1 means is enabled. disabled metric is absent ", []string{"solution_name"})
c.SetDescriptor("compliant", "show if current solution applied is compliant 1 or not 0", []string{"solution_name"})
return c, nil
}
// Collect various metrics for saptune solution
func (c *SolutionCollector) Collect(ch chan<- prometheus.Metric) {
log.Debugln("Collecting saptune solution metrics...")
err := checkExecutables(c.saptunePath)
if err != nil {
log.Warnf("%v failed to retrieve saptune executable", err)
return
}
// solution enabled
out, err := exec.Command(c.saptunePath, "solution", "enabled").CombinedOutput()
if err != nil {
log.Warnf("%v - Failed to run saptune solution enabled command n %s ", err, string(out))
return
}
// sanitize solutioname
solutionNameRaw := string(out)
solutionName := strings.TrimSpace(solutionNameRaw)
ch <- c.MakeGaugeMetric("enabled", float64(1), solutionName)
// TODO: the return code is a "fragile" check to base the metrics up on this
// is something could be improve on the saptune CLI
_, err = exec.Command(c.saptunePath, "solution", "verify").CombinedOutput()
if err != nil {
ch <- c.MakeGaugeMetric("compliant", float64(0), solutionName)
return
}
// no error so the solution is compliant
ch <- c.MakeGaugeMetric("compliant", float64(1), solutionName)
}