-
Notifications
You must be signed in to change notification settings - Fork 1
/
fstab.go
112 lines (94 loc) · 2.27 KB
/
fstab.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
// Copyright (c) 2017 mgIT GmbH. All rights reserved.
// Distributed under the Apache License. See LICENSE for details.
package main
import (
"bufio"
"log"
"os"
"strings"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
type FsTabOptions struct{}
func (opts *FsTabOptions) initDefault() {
}
type FsTabChecker struct {
opts FsTabOptions
promSuccess *prometheus.Desc
promNoFail *prometheus.Desc
}
func NewFsTabChecker(opts FsTabOptions) *FsTabChecker {
opts.initDefault()
return &FsTabChecker{
opts: opts,
promSuccess: prometheus.NewDesc(
"mgit_fstab_success",
"Indicates that the fstab has been collected successfully",
[]string{},
nil),
promNoFail: prometheus.NewDesc(
"mgit_fstab_nofail",
"Indicates that the nofail flag is set for the specific remote fs",
[]string{"device", "mountpoint", "fstype"},
nil),
}
}
func (c *FsTabChecker) Describe(ch chan<- *prometheus.Desc) {
ch <- c.promSuccess
ch <- c.promNoFail
}
func (c *FsTabChecker) Collect(ch chan<- prometheus.Metric) {
fstab, err := c.readFsTab()
success := 1.0
if err != nil {
log.Println("failed to collect fstab:", err)
success = 0.0
}
ch <- prometheus.MustNewConstMetric(
c.promSuccess,
prometheus.GaugeValue,
success,
)
for _, x := range fstab {
device, mountpoint, fstype, flags := x[0], x[1], x[2], strings.Split(x[3], ",")
if fstype != "nfs" {
continue
}
nofail := 0.0
for _, f := range flags {
if f == "nofail" {
nofail = 1.0
}
}
ch <- prometheus.MustNewConstMetric(
c.promNoFail,
prometheus.GaugeValue,
nofail,
device, mountpoint, fstype,
)
}
}
func (c *FsTabChecker) readFsTab() ([][]string, error) {
var result [][]string
file, err := os.Open("/etc/fstab")
if err != nil {
return nil, errors.Wrap(err, "failed to open /etc/fstab")
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
fields := strings.Fields(line)
if len(fields) != 6 {
return nil, errors.Wrap(err, "failed to parse /etc/fstab")
}
result = append(result, fields)
}
if err := scanner.Err(); err != nil {
return nil, errors.Wrap(err, "failed to read /etc/fstab")
}
return result, nil
}