Skip to content

Commit

Permalink
At #57 Fix Unable to Collect Data When customTags is Missing
Browse files Browse the repository at this point in the history
  • Loading branch information
kim-jinhyeong committed Oct 6, 2023
1 parent a3b9165 commit 23a3a02
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 4 deletions.
11 changes: 7 additions & 4 deletions plugins/inputs/ipmi_sensor/ipmi_sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ func (m *Ipmi) parse(acc telegraf.Accumulator, server string) error {
server := trimAll(server)
startIndex := strings.LastIndex(server, "),{")
connInfo := regexp.MustCompile(`(.*\)),(\{.*\})`).FindStringSubmatch(server)

serverConn := server
if startIndex >= 0 && len(connInfo) > 2 {

serverConn = connInfo[1]
jsonBytes := []byte(strings.ReplaceAll(connInfo[2], "'", "\""))

err := json.Unmarshal(jsonBytes, &customTags)
Expand All @@ -125,10 +127,11 @@ func (m *Ipmi) parse(acc telegraf.Accumulator, server string) error {
return fmt.Errorf("Error unmarshaling %s ", err)
}

conn := NewConnection(connInfo[1], m.Privilege, m.HexKey)
ipmiIP = conn.IpmiIP
opts = conn.options()
}

conn := NewConnection(serverConn, m.Privilege, m.HexKey)
ipmiIP = conn.IpmiIP
opts = conn.options()
}
opts = append(opts, "sdr")
if m.UseCache {
Expand Down
104 changes: 104 additions & 0 deletions plugins/inputs/ipmi_sensor/ipmi_sensor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,110 @@ func TestGather(t *testing.T) {
for _, test := range testsWithoutServer {
acc.AssertContainsTaggedFields(t, "ipmi_sensor", test.fields, test.tags)
}

i = &Ipmi{
Servers: []string{"USERID:PASSW0,RD@lan(192.168.1.1) "},
Path: "ipmitool",
Privilege: "USER",
Timeout: config.Duration(time.Second * 5),
HexKey: "1234567F",
Log: testutil.Logger{},
}

require.NoError(t, i.Init())
require.NoError(t, acc.GatherError(i.Gather))

conn = NewConnection(i.Servers[0], i.Privilege, i.HexKey)
require.EqualValues(t, "USERID", conn.Username)
require.EqualValues(t, "lan", conn.Interface)
require.EqualValues(t, "1234567F", conn.HexKey)

var testsWithoutCutsomTagServer = []struct {
fields map[string]interface{}
tags map[string]string
}{
{
map[string]interface{}{
"value": float64(20),
"status": 1,
},
map[string]string{
"name": "ambient_temp",
"server": "192.168.1.1",
"unit": "degrees_c",
},
},
{
map[string]interface{}{
"value": float64(80),
"status": 1,
},
map[string]string{
"name": "altitude",
"server": "192.168.1.1",
"unit": "feet",
},
},
{
map[string]interface{}{
"value": float64(210),
"status": 1,
},
map[string]string{
"name": "avg_power",
"server": "192.168.1.1",
"unit": "watts",
},
},
{
map[string]interface{}{
"value": float64(4.9),
"status": 1,
},
map[string]string{
"name": "planar_5v",
"server": "192.168.1.1",
"unit": "volts",
},
},
{
map[string]interface{}{
"value": float64(3.05),
"status": 1,
},
map[string]string{
"name": "planar_vbat",
"server": "192.168.1.1",
"unit": "volts",
},
},
{
map[string]interface{}{
"value": float64(2610),
"status": 1,
},
map[string]string{
"name": "fan_1a_tach",
"server": "192.168.1.1",
"unit": "rpm",
},
},
{
map[string]interface{}{
"value": float64(1775),
"status": 1,
},
map[string]string{
"name": "fan_1b_tach",
"server": "192.168.1.1",
"unit": "rpm",
},
},
}

for _, test := range testsWithoutCutsomTagServer {
acc.AssertContainsTaggedFields(t, "ipmi_sensor", test.fields, test.tags)
}
}

// fackeExecCommand is a helper function that mock
Expand Down

0 comments on commit 23a3a02

Please sign in to comment.