-
Notifications
You must be signed in to change notification settings - Fork 53
/
fs.go
142 lines (132 loc) · 3.67 KB
/
fs.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package metrics
import (
"bytes"
"fmt"
"text/template"
"github.com/prometheus/common/model"
alertingv1 "github.com/rancher/opni/pkg/apis/alerting/v1"
"google.golang.org/protobuf/types/known/durationpb"
)
const FilesystemMatcherName = "node_filesystem_free_bytes"
const NodeExporterMountpointLabel = "mountpoint"
const NodeExportFilesystemDeviceLabel = "device"
var FilesystemRuleAnnotations = map[string]string{}
type filesystemSaturation struct {
Filters string
Operation string
ExpectedValue string
}
type filesystemSaturationSpike struct {
Filters string
Operation string
ExpectedValue string
SpikeWindow string
NumSpike string
}
func NewFsRule(
nodeFilters map[string]*alertingv1.FilesystemInfo,
operation string,
expectedValue float64,
duration *durationpb.Duration,
annotations map[string]string,
) (*AlertingRule, error) {
dur := model.Duration(duration.AsDuration())
filters := NewPrometheusFilters()
filters.AddFilter(NodeFilter)
filters.AddFilter(NodeExporterMountpointLabel)
filters.AddFilter(NodeExportFilesystemDeviceLabel)
for node, info := range nodeFilters {
filters.Or(NodeFilter, node)
for _, device := range info.Devices {
filters.Or(NodeExportFilesystemDeviceLabel, device)
}
for _, mountpoint := range info.Mountpoints {
filters.Or(NodeFilter, mountpoint)
}
}
filters.Match(NodeFilter)
filters.Match(NodeExporterMountpointLabel)
filters.Match(NodeExportFilesystemDeviceLabel)
tmpl := template.Must(template.New("").Parse(`
sum(
1- (
node_filesystem_free_bytes{{ .Filters }}
)
/
node_filesystem_size_bytes
) {{ .Operation }} bool {{ .ExpectedValue }}
`))
var b bytes.Buffer
err := tmpl.Execute(&b, filesystemSaturation{
Filters: filters.Build(),
Operation: operation,
ExpectedValue: fmt.Sprintf("%.7f", expectedValue),
})
if err != nil {
return nil, err
}
return &AlertingRule{
Alert: "",
Expr: PostProcessRuleString(b.String()),
For: dur,
Labels: annotations,
Annotations: annotations,
}, nil
}
func NewFsSpikeRule(
nodeFilters map[string]*alertingv1.FilesystemInfo,
operation string,
expectedValue float64,
numSpikes int64,
duration *durationpb.Duration,
spikeWindow *durationpb.Duration,
annotations map[string]string,
) (*AlertingRule, error) {
dur := model.Duration(duration.AsDuration())
spikeDur := model.Duration(spikeWindow.AsDuration())
filters := NewPrometheusFilters()
filters.AddFilter(NodeFilter)
filters.AddFilter(NodeExporterMountpointLabel)
filters.AddFilter(NodeExportFilesystemDeviceLabel)
for node, info := range nodeFilters {
filters.Or(NodeFilter, node)
for _, device := range info.Devices {
filters.Or(NodeExportFilesystemDeviceLabel, device)
}
for _, mountpoint := range info.Mountpoints {
filters.Or(NodeFilter, mountpoint)
}
}
filters.Match(NodeFilter)
filters.Match(NodeExporterMountpointLabel)
filters.Match(NodeExportFilesystemDeviceLabel)
tmpl := template.Must(template.New("").Parse(`
count_over_time((
sum(
1- (
node_filesystem_free_bytes{{ .Filters }}
)
/
node_filesystem_size_bytes
) {{ .Operation }} bool {{ .ExpectedValue }}
)[{{ .SpikeWindow }}:5s]) > {{ .NumSpike }}
`))
var b bytes.Buffer
err := tmpl.Execute(&b, filesystemSaturationSpike{
Filters: filters.Build(),
Operation: operation,
ExpectedValue: fmt.Sprintf("%.7f", expectedValue),
SpikeWindow: spikeDur.String(),
NumSpike: fmt.Sprintf("%d", numSpikes),
})
if err != nil {
return nil, err
}
return &AlertingRule{
Alert: "",
Expr: PostProcessRuleString(b.String()),
For: dur,
Labels: annotations,
Annotations: annotations,
}, nil
}