forked from schizofreny/lvm-exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
vgCollector.go
65 lines (57 loc) · 1.74 KB
/
vgCollector.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
package main
import (
"log"
"os/exec"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
type lvmVgCollector struct {
vgFreeMetric *prometheus.Desc
vgSizeMetric *prometheus.Desc
node string
}
// LVM Collector contains VG size and VG free in MB
func newLvmVgCollector(node string) *lvmVgCollector {
return &lvmVgCollector{
vgFreeMetric: prometheus.NewDesc("lvm_vg_free_bytes",
"Shows LVM VG free size in Bytes",
[]string{"vg_name", "node"}, nil,
),
vgSizeMetric: prometheus.NewDesc("lvm_vg_total_size_bytes",
"Shows LVM VG total size in Bytes",
[]string{"vg_name", "node"}, nil,
),
node: node,
}
}
func (collector *lvmVgCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.vgFreeMetric
ch <- collector.vgSizeMetric
}
// LVM Collect, call OS command and set values
func (collector *lvmVgCollector) Collect(ch chan<- prometheus.Metric) {
out, err := exec.Command("/sbin/vgs", "--units", "B", "--separator", ",", "-o", "vg_name,vg_free,vg_size", "--noheadings").Output()
if err != nil {
log.Print(err)
}
lines := strings.Split(string(out), "\n")
for _, line := range lines {
values := strings.Split(line, ",")
if len(values) == 3 {
freeSize, err := strconv.ParseFloat(strings.Trim(values[1], "B"), 64)
if err != nil {
log.Print(err)
} else {
totalSize, err := strconv.ParseFloat(strings.Trim(values[2], "B"), 64)
if err != nil {
log.Print(err)
} else {
vgName := strings.Trim(values[0], " ")
ch <- prometheus.MustNewConstMetric(collector.vgFreeMetric, prometheus.GaugeValue, freeSize, vgName, collector.node)
ch <- prometheus.MustNewConstMetric(collector.vgSizeMetric, prometheus.GaugeValue, totalSize, vgName, collector.node)
}
}
}
}
}