forked from schizofreny/lvm-exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlvCollector.go
52 lines (44 loc) · 1.28 KB
/
lvCollector.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
package main
import (
"log"
"os/exec"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
type lvmLvCollector struct {
lvTotalSizeMetric *prometheus.Desc
node string
}
func newLvmLvCollector(node string) *lvmLvCollector {
return &lvmLvCollector{
lvTotalSizeMetric: prometheus.NewDesc("lvm_lv_total_size_bytes",
"Shows LVM LV total size in Bytes",
[]string{"lv_name", "vg_name", "node"}, nil,
),
node: node,
}
}
func (collector *lvmLvCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.lvTotalSizeMetric
}
func (collector *lvmLvCollector) Collect(ch chan<- prometheus.Metric) {
out, err := exec.Command("/sbin/lvs", "--units", "B", "--separator", ",", "-o", "lv_size,lv_name,vg_name", "--noheadings").Output()
if err != nil {
log.Print(err)
}
lines := strings.Split(string(out), "\n")
for _, line := range lines {
values := strings.Split(strings.TrimSpace(line), ",")
if len(values) < 3 {
continue
}
logicalVolumeName := values[1]
volumeGroupName := values[2]
size, err := strconv.ParseFloat(strings.Trim(values[0], "B"), 64)
if err != nil {
continue
}
ch <- prometheus.MustNewConstMetric(collector.lvTotalSizeMetric, prometheus.GaugeValue, size, logicalVolumeName, volumeGroupName, collector.node)
}
}