Add support for hex values to ipmi_sensor input (#5816)

This commit is contained in:
Alirie Gray 2019-05-07 15:42:44 -07:00 committed by Daniel Nelson
parent a1513e6235
commit b22bf01fdf
2 changed files with 120 additions and 2 deletions

View File

@ -150,9 +150,19 @@ func parseV1(acc telegraf.Accumulator, hostname string, cmdOut []byte, measured_
fields["status"] = 0
}
if strings.Index(ipmiFields["description"], " ") > 0 {
description := ipmiFields["description"]
// handle hex description field
if strings.HasPrefix(description, "0x") {
descriptionInt, err := strconv.ParseInt(description, 0, 64)
if err != nil {
continue
}
fields["value"] = float64(descriptionInt)
} else if strings.Index(description, " ") > 0 {
// split middle column into value and unit
valunit := strings.SplitN(ipmiFields["description"], " ", 2)
valunit := strings.SplitN(description, " ", 2)
var err error
fields["value"], err = aToFloat(valunit[0])
if err != nil {

View File

@ -610,3 +610,111 @@ Power Supply 1 | 03h | ok | 10.1 | 110 Watts, Presence detected
extractFieldsFromRegex(re_v2_parse_line, tests[i])
}
}
func Test_parseV1(t *testing.T) {
type args struct {
hostname string
cmdOut []byte
measuredAt time.Time
}
tests := []struct {
name string
args args
wantFields map[string]interface{}
wantErr bool
}{
{
name: "Test correct V1 parsing with hex code",
args: args{
hostname: "host",
measuredAt: time.Now(),
cmdOut: []byte("PS1 Status | 0x02 | ok"),
},
wantFields: map[string]interface{}{"value": float64(2), "status": 1},
wantErr: false,
},
{
name: "Test correct V1 parsing with value with unit",
args: args{
hostname: "host",
measuredAt: time.Now(),
cmdOut: []byte("Avg Power | 210 Watts | ok"),
},
wantFields: map[string]interface{}{"value": float64(210), "status": 1},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var acc testutil.Accumulator
if err := parseV1(&acc, tt.args.hostname, tt.args.cmdOut, tt.args.measuredAt); (err != nil) != tt.wantErr {
t.Errorf("parseV1() error = %v, wantErr %v", err, tt.wantErr)
}
acc.AssertContainsFields(t, "ipmi_sensor", tt.wantFields)
})
}
}
func Test_parseV2(t *testing.T) {
type args struct {
hostname string
cmdOut []byte
measuredAt time.Time
}
tests := []struct {
name string
args args
wantFields map[string]interface{}
wantTags map[string]string
wantErr bool
}{
{
name: "Test correct V2 parsing with analog value with unit",
args: args{
hostname: "host",
cmdOut: []byte("Power Supply 1 | 03h | ok | 10.1 | 110 Watts, Presence detected"),
measuredAt: time.Now(),
},
wantFields: map[string]interface{}{"value": float64(110)},
wantTags: map[string]string{
"name": "power_supply_1",
"status_code": "ok",
"server": "host",
"entity_id": "10.1",
"unit": "watts",
"status_desc": "presence_detected",
},
wantErr: false,
},
{
name: "Test correct V2 parsing without analog value",
args: args{
hostname: "host",
cmdOut: []byte("Intrusion | 73h | ok | 7.1 |"),
measuredAt: time.Now(),
},
wantFields: map[string]interface{}{"value": float64(0)},
wantTags: map[string]string{
"name": "intrusion",
"status_code": "ok",
"server": "host",
"entity_id": "7.1",
"status_desc": "ok",
},
wantErr: false,
},
}
for _, tt := range tests {
var acc testutil.Accumulator
t.Run(tt.name, func(t *testing.T) {
if err := parseV2(&acc, tt.args.hostname, tt.args.cmdOut, tt.args.measuredAt); (err != nil) != tt.wantErr {
t.Errorf("parseV2() error = %v, wantErr %v", err, tt.wantErr)
}
})
acc.AssertContainsTaggedFields(t, "ipmi_sensor", tt.wantFields, tt.wantTags)
}
}