Skip to content

Commit 7b032af

Browse files
author
RincewindsHat
committed
New perfdata handling
1 parent c9aa64b commit 7b032af

File tree

5 files changed

+103
-69
lines changed

5 files changed

+103
-69
lines changed

examples/check_example2/main.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ func main() {
2020
check.ExitError(err)
2121
}
2222

23-
check1.Perfdata.Add(&perfdata.Perfdata{
23+
pd := &perfdata.Perfdata{
2424
Label: "foo",
25-
Value: 23,
26-
})
25+
Value: perfdata.NewPdvUint64(23),
26+
}
27+
28+
check1.Perfdata.Add(pd)
2729

2830
check2 := result.PartialResult{}
2931

@@ -34,14 +36,17 @@ func main() {
3436
check.ExitError(err)
3537
}
3638

37-
check2.Perfdata.Add(&perfdata.Perfdata{
39+
pd2 := &perfdata.Perfdata{
3840
Label: "bar",
39-
Value: 42,
40-
})
41-
check2.Perfdata.Add(&perfdata.Perfdata{
41+
Value: perfdata.NewPdvUint64(42),
42+
}
43+
check2.Perfdata.Add(pd2)
44+
45+
pd3 := &perfdata.Perfdata{
4246
Label: "foo2 bar",
43-
Value: 46,
44-
})
47+
Value: perfdata.NewPdvUint64(46),
48+
}
49+
check2.Perfdata.Add(pd3)
4550

4651
overall.AddSubcheck(check1)
4752
overall.AddSubcheck(check2)

perfdata/list_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
func ExamplePerfdataList() {
1111
list := PerfdataList{}
12-
list.Add(&Perfdata{Label: "test1", Value: 23})
13-
list.Add(&Perfdata{Label: "test2", Value: 42})
12+
list.Add(&Perfdata{Label: "test1", Value: NewPdvUint64(23)})
13+
list.Add(&Perfdata{Label: "test2", Value: NewPdvUint64(42)})
1414

1515
fmt.Println(list)
1616

@@ -20,8 +20,8 @@ func ExamplePerfdataList() {
2020

2121
func TestPerfdataListFormating(t *testing.T) {
2222
list := PerfdataList{}
23-
list.Add(&Perfdata{Label: "test1", Value: 23})
24-
list.Add(&Perfdata{Label: "test2", Value: 42})
23+
list.Add(&Perfdata{Label: "test1", Value: NewPdvUint64(23)})
24+
list.Add(&Perfdata{Label: "test2", Value: NewPdvUint64(42)})
2525

2626
assert.Equal(t, "test1=23 test2=42", list.String())
2727
}
@@ -30,8 +30,8 @@ func BenchmarkPerfdataListFormating(b *testing.B) {
3030
b.ReportAllocs()
3131

3232
list := PerfdataList{}
33-
list.Add(&Perfdata{Label: "test1", Value: 23})
34-
list.Add(&Perfdata{Label: "test2", Value: 42})
33+
list.Add(&Perfdata{Label: "test1", Value: NewPdvUint64(23)})
34+
list.Add(&Perfdata{Label: "test2", Value: NewPdvUint64(42)})
3535

3636
for i := 0; i < b.N; i++ {
3737
l := list.String()

perfdata/type.go

+53-24
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,24 @@ var replacer = strings.NewReplacer("=", "_", "`", "_", "'", "_", "\"", "_")
1919
// represent a valid measurement, e.g INF for floats
2020
// This error can probably ignored in most cases and the perfdata point omitted,
2121
// but silently dropping the value and returning the empty strings seems like bad style
22-
func formatNumeric(value interface{}) (string, error) {
23-
switch v := value.(type) {
24-
case float64:
25-
if math.IsInf(v, 0) {
22+
func formatNumeric(value PerfdataValue) (string, error) {
23+
switch value.kind {
24+
case floatType:
25+
if math.IsInf(value.floatVal, 0) {
2626
return "", errors.New("Perfdata value is inifinite")
2727
}
2828

29-
if math.IsNaN(v) {
29+
if math.IsNaN(value.floatVal) {
3030
return "", errors.New("Perfdata value is inifinite")
3131
}
3232

33-
return check.FormatFloat(v), nil
34-
case float32:
35-
if math.IsInf(float64(v), 0) {
36-
return "", errors.New("Perfdata value is inifinite")
37-
}
38-
39-
if math.IsNaN(float64(v)) {
40-
return "", errors.New("Perfdata value is inifinite")
41-
}
42-
43-
return check.FormatFloat(float64(v)), nil
44-
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
45-
return fmt.Sprintf("%d", v), nil
33+
return check.FormatFloat(value.floatVal), nil
34+
case intType:
35+
return fmt.Sprintf("%d", value.intVal), nil
36+
case uintType:
37+
return fmt.Sprintf("%d", value.uintVal), nil
4638
default:
47-
panic(fmt.Sprintf("unsupported type for perfdata: %T", value))
39+
return "", errors.New("This should not happen")
4840
}
4941
}
5042

@@ -61,13 +53,50 @@ func formatNumeric(value interface{}) (string, error) {
6153
// https://icinga.com/docs/icinga-2/latest/doc/05-service-monitoring/#unit-of-measurement-uom
6254
type Perfdata struct {
6355
Label string
64-
Value interface{}
56+
Value PerfdataValue
6557
// Uom is the unit-of-measurement, see links above for details.
6658
Uom string
6759
Warn *check.Threshold
6860
Crit *check.Threshold
69-
Min interface{}
70-
Max interface{}
61+
Min PerfdataValue
62+
Max PerfdataValue
63+
}
64+
65+
type perfdataValueTypeEnum int
66+
67+
const (
68+
noneType perfdataValueTypeEnum = iota
69+
intType
70+
uintType
71+
floatType
72+
)
73+
74+
type PerfdataValue struct {
75+
kind perfdataValueTypeEnum
76+
uintVal uint64
77+
intVal int64
78+
floatVal float64
79+
}
80+
81+
func NewPdvUint64(val uint64) PerfdataValue {
82+
return PerfdataValue{
83+
kind: uintType,
84+
uintVal: val,
85+
}
86+
}
87+
88+
func NewPdvInt64(val int64) PerfdataValue {
89+
return PerfdataValue{
90+
kind: intType,
91+
intVal: val,
92+
}
93+
}
94+
95+
func NewPdvFloat64(val float64) PerfdataValue {
96+
return PerfdataValue{
97+
kind: floatType,
98+
floatVal: val,
99+
}
71100
}
72101

73102
// String returns the proper format for the plugin output
@@ -109,10 +138,10 @@ func (p Perfdata) ValidatedString() (string, error) {
109138
}
110139

111140
// Limits
112-
for _, value := range []interface{}{p.Min, p.Max} {
141+
for _, value := range []PerfdataValue{p.Min, p.Max} {
113142
sb.WriteString(";")
114143

115-
if value != nil {
144+
if value.kind != noneType {
116145
pfVal, err := formatNumeric(value)
117146
// Attention: we ignore limits if they are faulty
118147
if err == nil {

perfdata/type_test.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ func BenchmarkPerfdataString(b *testing.B) {
1313

1414
perf := Perfdata{
1515
Label: "test test=test",
16-
Value: 10.1,
16+
Value: NewPdvFloat64(10.1),
1717
Uom: "%",
1818
Warn: &check.Threshold{Upper: 80},
1919
Crit: &check.Threshold{Upper: 90},
20-
Min: 0,
21-
Max: 100}
20+
Min: NewPdvUint64(0),
21+
Max: NewPdvInt64(100)}
2222

2323
for i := 0; i < b.N; i++ {
2424
p := perf.String()
@@ -34,36 +34,36 @@ func TestRenderPerfdata(t *testing.T) {
3434
"simple": {
3535
perf: Perfdata{
3636
Label: "test",
37-
Value: 2,
37+
Value: NewPdvUint64(2),
3838
},
3939
expected: "test=2",
4040
},
4141
"with-quotes": {
4242
perf: Perfdata{
4343
Label: "te's\"t",
44-
Value: 2,
44+
Value: NewPdvInt64(2),
4545
},
4646
expected: "te_s_t=2",
4747
},
4848
"with-special-chars": {
4949
perf: Perfdata{
5050
Label: "test_🖥️_'test",
51-
Value: 2,
51+
Value: NewPdvUint64(2),
5252
},
5353
expected: "test_🖥️__test=2",
5454
},
5555
"with-uom": {
5656
perf: Perfdata{
5757
Label: "test",
58-
Value: 2,
58+
Value: NewPdvInt64(2),
5959
Uom: "%",
6060
},
6161
expected: "test=2%",
6262
},
6363
"with-thresholds": {
6464
perf: Perfdata{
6565
Label: "foo bar",
66-
Value: 2.76,
66+
Value: NewPdvFloat64(2.76),
6767
Uom: "m",
6868
Warn: &check.Threshold{Lower: 10, Upper: 25, Inside: true},
6969
Crit: &check.Threshold{Lower: 15, Upper: 20, Inside: false},
@@ -79,7 +79,7 @@ func TestRenderPerfdata(t *testing.T) {
7979
"invalid-value": {
8080
perf: Perfdata{
8181
Label: "to infinity",
82-
Value: math.Inf(+1),
82+
Value: NewPdvFloat64(math.Inf(+1)),
8383
},
8484
expected: "",
8585
},
@@ -104,34 +104,34 @@ func TestRenderPerfdata(t *testing.T) {
104104

105105
type pfFormatTest struct {
106106
Result string
107-
InputValue interface{}
107+
InputValue PerfdataValue
108108
}
109109

110110
func TestFormatNumeric(t *testing.T) {
111111
testdata := []pfFormatTest{
112112
{
113113
Result: "10",
114-
InputValue: 10,
114+
InputValue: NewPdvUint64(10),
115115
},
116116
{
117117
Result: "-10",
118-
InputValue: -10,
118+
InputValue: NewPdvInt64(-10),
119119
},
120120
{
121121
Result: "10",
122-
InputValue: uint8(10),
122+
InputValue: NewPdvUint64(10),
123123
},
124124
{
125125
Result: "1234.567",
126-
InputValue: float64(1234.567),
126+
InputValue: NewPdvFloat64(1234.567),
127127
},
128128
{
129129
Result: "3456.789",
130-
InputValue: float32(3456.789),
130+
InputValue: NewPdvFloat64(3456.789),
131131
},
132132
{
133133
Result: "1234567890.988",
134-
InputValue: 1234567890.9877,
134+
InputValue: NewPdvFloat64(1234567890.9877),
135135
},
136136
}
137137

result/overall_test.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func ExampleOverall_GetStatus() {
139139
func ExampleOverall_withSubchecks() {
140140
var overall Overall
141141

142-
example_perfdata := perfdata.Perfdata{Label: "pd_test", Value: 5, Uom: "s"}
142+
example_perfdata := perfdata.Perfdata{Label: "pd_test", Value: perfdata.NewPdvUint64(5), Uom: "s"}
143143
pd_list := perfdata.PerfdataList{}
144144
pd_list.Add(&example_perfdata)
145145

@@ -164,17 +164,17 @@ func ExampleOverall_withSubchecks() {
164164
func TestOverall_withEnhancedSubchecks(t *testing.T) {
165165
var overall Overall
166166

167-
example_perfdata := perfdata.Perfdata{Label: "pd_test", Value: 5, Uom: "s"}
167+
example_perfdata := perfdata.Perfdata{Label: "pd_test", Value: perfdata.NewPdvUint64(5), Uom: "s"}
168168
example_perfdata2 := perfdata.Perfdata{
169169
Label: "pd_test2",
170-
Value: 1099511627776,
170+
Value: perfdata.NewPdvUint64(1099511627776),
171171
Uom: "kB",
172172
Warn: &check.Threshold{Inside: true, Lower: 3.14, Upper: 0x66666666666},
173173
Crit: &check.Threshold{Inside: false, Lower: 07777777777777, Upper: 0xFFFFFFFFFFFFFFFFFFFF},
174-
Max: uint64(18446744073709551615),
174+
Max: perfdata.NewPdvUint64((18446744073709551615)),
175175
}
176-
example_perfdata3 := perfdata.Perfdata{Label: "kl;jr2if;l2rkjasdf", Value: 5, Uom: "m"}
177-
example_perfdata4 := perfdata.Perfdata{Label: "asdf", Value: uint64(18446744073709551615), Uom: "B"}
176+
example_perfdata3 := perfdata.Perfdata{Label: "kl;jr2if;l2rkjasdf", Value: perfdata.NewPdvUint64(5), Uom: "m"}
177+
example_perfdata4 := perfdata.Perfdata{Label: "asdf", Value: perfdata.NewPdvUint64(18446744073709551615), Uom: "B"}
178178

179179
pd_list := perfdata.PerfdataList{}
180180
pd_list.Add(&example_perfdata)
@@ -259,11 +259,11 @@ func TestOverall_withSubchecks_Perfdata(t *testing.T) {
259259

260260
perf1 := perfdata.Perfdata{
261261
Label: "foo",
262-
Value: 3,
262+
Value: perfdata.NewPdvUint64(3),
263263
}
264264
perf2 := perfdata.Perfdata{
265265
Label: "bar",
266-
Value: 300,
266+
Value: perfdata.NewPdvUint64(300),
267267
Uom: "%",
268268
}
269269

@@ -302,16 +302,16 @@ func TestOverall_withSubchecks_PartialResult(t *testing.T) {
302302

303303
perf1 := perfdata.Perfdata{
304304
Label: "foo",
305-
Value: 3,
305+
Value: perfdata.NewPdvUint64(3),
306306
}
307307
perf2 := perfdata.Perfdata{
308308
Label: "bar",
309-
Value: 300,
309+
Value: perfdata.NewPdvUint64(300),
310310
Uom: "%",
311311
}
312312
perf3 := perfdata.Perfdata{
313313
Label: "baz",
314-
Value: 23,
314+
Value: perfdata.NewPdvUint64(23),
315315
Uom: "B",
316316
}
317317

@@ -376,11 +376,11 @@ func TestSubchecksPerfdata(t *testing.T) {
376376
Perfdata: perfdata.PerfdataList{
377377
&perfdata.Perfdata{
378378
Label: "foo",
379-
Value: 23,
379+
Value: perfdata.NewPdvUint64(23),
380380
},
381381
&perfdata.Perfdata{
382382
Label: "bar",
383-
Value: 42,
383+
Value: perfdata.NewPdvUint64(42),
384384
},
385385
},
386386
}
@@ -392,7 +392,7 @@ func TestSubchecksPerfdata(t *testing.T) {
392392
Perfdata: perfdata.PerfdataList{
393393
&perfdata.Perfdata{
394394
Label: "foo2 bar",
395-
Value: 46,
395+
Value: perfdata.NewPdvUint64(46),
396396
},
397397
},
398398
}

0 commit comments

Comments
 (0)