File tree 2 files changed +27
-9
lines changed 2 files changed +27
-9
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,6 @@ package collectd
3
3
import (
4
4
"bytes"
5
5
"fmt"
6
- "regexp"
7
6
"strings"
8
7
"sync"
9
8
"sync/atomic"
@@ -91,17 +90,19 @@ func (checker *Checker) checkRecord(record CollectdRecord) ([]shared.CheckResult
91
90
92
91
// Load host specific thresholds
93
92
for pattern , threshold := range rule .Check .HostThresholds {
94
- matched , err := regexp .MatchString (pattern , record .Host )
95
- if err != nil {
96
- checker .logger .Error (err )
93
+ if threshold .Regexp == nil {
97
94
continue
98
95
}
99
- if matched {
100
- matchName = fmt .Sprintf ("host:%s" , pattern )
101
- critical = threshold .Critical
102
- warning = threshold .Warning
103
- break
96
+
97
+ if ! threshold .Regexp .MatchString (record .Host ) {
98
+ continue
104
99
}
100
+
101
+ matchName = fmt .Sprintf ("host:%s" , pattern )
102
+
103
+ critical = threshold .Critical
104
+ warning = threshold .Warning
105
+ break
105
106
}
106
107
107
108
// CRITICAL CHECK
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package config
3
3
import (
4
4
"fmt"
5
5
"io/ioutil"
6
+ "regexp"
6
7
"text/template"
7
8
8
9
"github.com/hashicorp/hcl"
@@ -23,6 +24,7 @@ type CheckThreshold struct {
23
24
CriticalTpl string `hcl:"critical"`
24
25
Warning hil.EvaluationResult `hcl:"-"`
25
26
Critical hil.EvaluationResult `hcl:"-"`
27
+ Regexp * regexp.Regexp `hcl:"-"`
26
28
}
27
29
28
30
type CheckThresholdMap map [string ]CheckThreshold
@@ -44,6 +46,18 @@ func (m CheckThresholdMap) Parse(hilConfig *hil.EvalConfig) (err error) {
44
46
return
45
47
}
46
48
49
+ func (m CheckThresholdMap ) CompileRegexp () error {
50
+ for pattern , threshold := range m {
51
+ r , err := regexp .Compile (pattern )
52
+ if err != nil {
53
+ return err
54
+ }
55
+ threshold .Regexp = r
56
+ m [pattern ] = threshold
57
+ }
58
+ return nil
59
+ }
60
+
47
61
type Check struct {
48
62
Plugin string `hcl:"plugin"`
49
63
PluginInstance string `hcl:"plugin_instance"`
@@ -121,6 +135,9 @@ func loadFileHcl(root string) (*Config, error) {
121
135
}
122
136
123
137
check .HostThresholds .Parse (hilConfig )
138
+ if err := check .HostThresholds .CompileRegexp (); err != nil {
139
+ return nil , err
140
+ }
124
141
check .MetaThresholds .Parse (hilConfig )
125
142
126
143
out .Checks [name ] = check
You can’t perform that action at this time.
0 commit comments