Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better regexp for validating thresholds #226

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion perfdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,33 @@ const (

// perfDataThresholdRangeSyntaxRegex represents the regex character class
// used to validate and parse the Warn and Crit fields.
perfDataThresholdRangeSyntaxRegex string = `[0-9~@:]+`
//
// This huge regexp is constructed from smaller regexpes joined
// with | (alternation - logical OR) operator. Each of this smaller regexp
// tries to capture one case from [Nagios Plugin Dev Guidelines].
// Note: This could be merged into shorter more complex regexp
// but it is kept it such long form for readability.
//
// [Nagios Plugin Dev Guidelines]: https://nagios-plugins.org/doc/guidelines.html#THRESHOLDFORMAT
//
// First one matches just single integer of float number with optional minus sign (ex. 1, 1.5, -1, -1.5)
perfDataThresholdRangeSyntaxRegex string = `(?:(^[-]?\d+(?:\.\d+)?$))` +
// range from x to +∞ (ex. 1:, 1.5:, -1:, -1.5:)
`|(?:(^[-]?\d+(?:\.\d+)?:$))` +
// range from -∞ to x (ex.~:-1, ~:0.1, ~:1, ~:-0.1)
`|(?:(^~:[-]?\d+(?:\.\d+)?$))` +
// range from x to y (ex. 0:1, 0.1:1.1, -5:0.4, -5.1:-1)
`|(?:(^[-]?\d+(?:\.\d+)?):([-]?\d+(?:\.\d+)?$))` +
// single integer or float preceded with @ (ex. @1, @1.1, @-1, @-1.1)
`|(?:(^@[-]?\d+(?:\.\d+)?$))` +
// range from x to +∞ with reversed inclusion (ex. @1:,@1.5:, @-1:, @-1.5:)
`|(?:(^@[-]?\d+(?:\.\d+)?:$))` +
// range from -∞ to x with reversed inclusion (ex.@~:-1, @~:0.1, @~:1, @~:-0.1)
`|(?:(^@~:[-]?\d+(?:\.\d+)?$))` +
// range from x to y with reversed inclusion (ex. @0:1, @0.1:1.1, @-5:0.4, @-5.1:-1)
`|(?:(^@[-]?\d+(?:\.\d+)?):([-]?\d+(?:\.\d+)?$))` +
// range from -∞ to +∞
`|(?:^~:$)`

// perfDataLabelFieldDisallowedCharacters are the characters disallowed in
// the Label field; the equals sign and single quote characters are not
Expand Down
55 changes: 52 additions & 3 deletions perfdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
// parsing the input performance data string and the implicit overall
// results count.
result []nagios.PerformanceData

// wantErr informs if we are expecting error for this testcase (true) or not
wantErr bool
}{
"Load averages double quoted": {
// https://github.com/nagios-plugins/nagios-plugins/blob/12446aea1d353d891cd6291ba8086a0f5247c93d/plugins/check_load.c#L206-L210
Expand Down Expand Up @@ -129,6 +132,23 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
Max: "",
},
},
wantErr: false,
},
"Negative floats double quoted": {
// https://github.com/nagios-plugins/nagios-plugins/blob/12446aea1d353d891cd6291ba8086a0f5247c93d/plugins/check_load.c#L206-L210
input: `"so_negative=-0.260;-5.000;-10.000;-50;"`,
result: []nagios.PerformanceData{
{
Label: "so_negative",
Value: "-0.260",
UnitOfMeasurement: "",
Warn: "-5.000",
Crit: "-10.000",
Min: "-50",
Max: "",
},
},
wantErr: false,
},

"Load averages unquoted": {
Expand Down Expand Up @@ -163,6 +183,7 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
Max: "",
},
},
wantErr: false,
},

"Single quoted time metric label with all semicolon separators": {
Expand All @@ -178,6 +199,7 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
Max: "",
},
},
wantErr: false,
},

"Single quoted time metric label with one trailing semicolon separator": {
Expand All @@ -193,6 +215,7 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
Max: "",
},
},
wantErr: false,
},

"Single quoted time metric label without semicolon separators": {
Expand All @@ -208,6 +231,7 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
Max: "",
},
},
wantErr: false,
},

"Disk usage labels single quoted": {
Expand Down Expand Up @@ -241,6 +265,7 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
Max: "476",
},
},
wantErr: false,
},

"Disk usage unquoted": {
Expand Down Expand Up @@ -274,6 +299,7 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
Max: "476",
},
},
wantErr: false,
},

"Processes unquoted": {
Expand All @@ -289,6 +315,7 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
Max: "",
},
},
wantErr: false,
},

"VMware Snapshots Age single quoted with all semicolon separators": {
Expand Down Expand Up @@ -385,6 +412,7 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
Max: "",
},
},
wantErr: false,
},

"All Statuspage components single quoted with all semicolon separators": {
Expand Down Expand Up @@ -520,6 +548,7 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
Max: "",
},
},
wantErr: false,
},

"check_cert plugin metrics single quoted with all semicolon separators": {
Expand Down Expand Up @@ -589,6 +618,27 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
Max: "",
},
},
wantErr: false,
},
"Single quoted time metric label with invalid thresholds set to ::": {
input: "'time'=49ms;::;;;",
result: nil,
wantErr: true,
},
"Single quoted time metric label with invalid thresholds set to @4:~": {
input: "'time'=49ms;@4:~;;;",
result: nil,
wantErr: true,
},
"Single quoted time metric label with invalid thresholds set to ~:~": {
input: "'time'=49ms;~:~;;;",
result: nil,
wantErr: true,
},
"Single quoted time metric label with invalid thresholds set to @@": {
input: "'time'=49ms;@@;;;",
result: nil,
wantErr: true,
},
}

Expand All @@ -605,10 +655,9 @@ func TestParsePerfDataSucceedsForValidInput(t *testing.T) {
t.Logf("Evaluating input %q", tt.input)

perfDataResults, err := nagios.ParsePerfData(tt.input)
if err != nil {
t.Fatalf("failed to parse perfdata input: %v", err)
if (err != nil) != tt.wantErr {
t.Fatalf("nagios.ParsePerfData() error = %v, wantErr %v", err, tt.wantErr)
}

testParsePerfDataCollection(t, perfDataResults, tt.result)

})
Expand Down
Loading