-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
74 lines (66 loc) · 1.83 KB
/
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
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"regexp"
"strconv"
"strings"
sysctl "github.com/lorenzosaino/go-sysctl"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
type Exporter struct {
includeRegex string
excludeRegex string
prefix string
}
func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
}
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
rawSysctls, err := sysctl.GetAll()
if err != nil {
log.Error(err)
return
}
for sysctlName, value := range rawSysctls {
if sysctlNameIsFiltered(sysctlName, e.includeRegex, e.excludeRegex) {
continue
}
values := strings.Split(value, "\t")
if len(values) == 1 {
parsed, err := strconv.ParseFloat(values[0], 64)
if err != nil {
log.Debugf("%s value is not integer, skipped", sysctlName)
continue
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(prometheus.BuildFQName(e.prefix, "", "parameter"), "Values of sysctl parameters", []string{"param"}, nil),
prometheus.GaugeValue, parsed, sysctlName,
)
continue
}
for i, value := range values {
parsed, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Debugf("%s value %d is not integer, skipped", sysctlName, i)
continue
}
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(prometheus.BuildFQName("sysctl", "", "parameter"), "Values of sysctl parameters", []string{"param", "column"}, nil),
prometheus.GaugeValue, parsed, sysctlName, strconv.Itoa(i),
)
}
}
}
func sysctlNameIsFiltered(sysctlName string, include string, exclude string) bool {
matched, err := regexp.MatchString(include, sysctlName)
if err != nil || matched == false {
return true
}
if exclude == "" {
return false
}
matched, err = regexp.MatchString(exclude, sysctlName)
if err != nil || matched == true {
return true
}
return false
}