Skip to content
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ httpmock-record.yml

# Test binary, built with `go test -c`
*.test
*.profile
*.benchmark

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
Expand Down
33 changes: 30 additions & 3 deletions perfdata/type.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
package perfdata

import (
"fmt"
"github.com/NETWAYS/go-check"
"strings"
)

// Replace not allowed characters inside a label
var replacer = strings.NewReplacer("=", "_", "`", "_", "'", "_")

// formatNumeric returns a string representation of various possible numerics
//
// This supports most internal types of Go and all fmt.Stringer interfaces.
func formatNumeric(value interface{}) string {
switch v := value.(type) {
case float64:
return check.FormatFloat(v)
case float32:
return check.FormatFloat(float64(v))
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprintf("%d", v)
case fmt.Stringer, string:
return fmt.Sprint(v)
default:
panic(fmt.Sprintf("unsupported type for perfdata: %T", value))
}
}

// Perfdata represents all properties of performance data for Icinga
//
// Implements fmt.Stringer to return the plaintext format for a plugin output.
Expand All @@ -31,9 +53,14 @@ type Perfdata struct {
func (p Perfdata) String() string {
var sb strings.Builder

sb.WriteString(FormatLabel(p.Label) + "=")
// Add quotes if string contains any whitespace
if strings.ContainsAny(p.Label, "\t\n\f\r ") {
sb.WriteString(`'` + replacer.Replace(p.Label) + `'` + "=")
} else {
sb.WriteString(replacer.Replace(p.Label) + "=")
}

sb.WriteString(FormatNumeric(p.Value))
sb.WriteString(formatNumeric(p.Value))
sb.WriteString(p.Uom)

// Thresholds
Expand All @@ -50,7 +77,7 @@ func (p Perfdata) String() string {
sb.WriteString(";")

if value != nil {
sb.WriteString(FormatNumeric(value))
sb.WriteString(formatNumeric(value))
}
}

Expand Down
88 changes: 57 additions & 31 deletions perfdata/type_test.go
Original file line number Diff line number Diff line change
@@ -1,56 +1,82 @@
package perfdata

import (
"fmt"
"testing"

"github.com/NETWAYS/go-check"
"github.com/stretchr/testify/assert"
)

func ExamplePerfdata() {
func BenchmarkPerfdataString(b *testing.B) {
b.ReportAllocs()

perf := Perfdata{
Label: "test",
Label: "test test=test",
Value: 10.1,
Uom: "%",
Warn: &check.Threshold{Upper: 80},
Crit: &check.Threshold{Upper: 90},
Min: 0,
Max: 100}

fmt.Println(perf)

// Output:
// test=10.1%;80;90;0;100
}

func TestPerfdata(t *testing.T) {
perf := Perfdata{
Label: "test",
Value: 2,
for i := 0; i < b.N; i++ {
p := perf.String()
_ = p
}

assert.Equal(t, "test=2", perf.String())
}

func TestPerfdata2(t *testing.T) {
perf := Perfdata{
Label: "test",
Value: 2,
Uom: "%",
func TestRenderPerfdata(t *testing.T) {
testcases := map[string]struct {
perf Perfdata
expected string
}{
"simple": {
perf: Perfdata{
Label: "test",
Value: 2,
},
expected: "test=2",
},
"with-special-chars": {
perf: Perfdata{
Label: "test_🖥️_'test",
Value: 2,
},
expected: "test_🖥️__test=2",
},
"with-uom": {
perf: Perfdata{
Label: "test",
Value: 2,
Uom: "%",
},
expected: "test=2%",
},
"with-thresholds": {
perf: Perfdata{
Label: "foo bar",
Value: 2.76,
Uom: "m",
Warn: &check.Threshold{Lower: 10, Upper: 25, Inside: true},
Crit: &check.Threshold{Lower: 15, Upper: 20, Inside: false},
},
expected: "'foo bar'=2.76m;@10:25;15:20",
},
}

assert.Equal(t, "test=2%", perf.String())
}

func TestPerfdata3(t *testing.T) {
perf := Perfdata{
Label: "foo bar",
Value: 2.76,
Uom: "m",
Warn: &check.Threshold{Lower: 10, Upper: 25, Inside: true},
Crit: &check.Threshold{Lower: 15, Upper: 20, Inside: false},
for name, tc := range testcases {
t.Run(name, func(t *testing.T) {
assert.Equal(t, tc.expected, tc.perf.String())
})
}
}

assert.Equal(t, "'foo bar'=2.76m;@10:25;15:20", perf.String())
func TestFormatNumeric(t *testing.T) {
assert.Equal(t, "10", formatNumeric(10))
assert.Equal(t, "-10", formatNumeric(-10))
assert.Equal(t, "10", formatNumeric(uint8(10)))
assert.Equal(t, "1234.567", formatNumeric(1234.567))
assert.Equal(t, "1234.567", formatNumeric(float32(1234.567)))
assert.Equal(t, "1234.567", formatNumeric("1234.567"))
assert.Equal(t, "1234567890.988", formatNumeric(1234567890.9877))
}
43 changes: 0 additions & 43 deletions perfdata/util.go

This file was deleted.

24 changes: 0 additions & 24 deletions perfdata/util_test.go

This file was deleted.