Skip to content

Commit

Permalink
At #67 Fix IPMI Struct Tag
Browse files Browse the repository at this point in the history
  • Loading branch information
kim-jinhyeong committed Sep 25, 2024
1 parent 24d6f2c commit 551af5a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
8 changes: 5 additions & 3 deletions plugins/inputs/ipmi_sensor/ipmi_sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (m *Ipmi) Gather(acc telegraf.Accumulator) error {
return nil
}

func (m *Ipmi) parse(acc telegraf.Accumulator, server string, sensor string) error {
func (m *Ipmi) parse(acc telegraf.Accumulator, server, sensor string) error {
var command []string
switch sensor {
case "sdr":
Expand Down Expand Up @@ -197,9 +197,11 @@ func (m *Ipmi) parseChassisPowerStatus(acc telegraf.Accumulator, hostname string
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, "Chassis Power is on") {
acc.AddFields("ipmi_sensor", map[string]interface{}{"value": 1}, map[string]string{"name": "chassis_power_status", "server": hostname}, measuredAt)
acc.AddFields("ipmi_sensor", map[string]interface{}{"value": 1.0},
map[string]string{"name": "chassis_power_status", "server": hostname}, measuredAt)
} else if strings.Contains(line, "Chassis Power is off") {
acc.AddFields("ipmi_sensor", map[string]interface{}{"value": 0}, map[string]string{"name": "chassis_power_status", "server": hostname}, measuredAt)
acc.AddFields("ipmi_sensor", map[string]interface{}{"value": 0.0},
map[string]string{"name": "chassis_power_status", "server": hostname}, measuredAt)
}
}

Expand Down
31 changes: 15 additions & 16 deletions plugins/inputs/ipmi_sensor_ext/ipmi_sensor_ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,17 @@ var (

// Ipmi stores the configuration values for the ipmi_sensor input plugin
type Ipmi struct {
Path string
Privilege string
HexKey string `toml:"hex_key"`
Servers []string
Sensors []string `toml:"sensors"`
Timeout config.Duration
MetricVersion int
UseSudo bool
UseCache bool
CachePath string

Log telegraf.Logger `toml:"-"`
Path string `toml:"path"`
Privilege string `toml:"privilege"`
HexKey string `toml:"hex_key"`
Servers []string `toml:"servers"`
Sensors []string `toml:"sensors"`
Timeout config.Duration `toml:"timeout"`
MetricVersion int `toml:"metric_version"`
UseSudo bool `toml:"use_sudo"`
UseCache bool `toml:"use_cache"`
CachePath string `toml:"cache_path"`
Log telegraf.Logger `toml:"-"`
}

const cmd = "ipmitool"
Expand Down Expand Up @@ -134,18 +133,17 @@ func (m *Ipmi) parse(acc telegraf.Accumulator, server string, sensor string) err
if server != "" {
server := trimAll(server)
connInfo := regexp.MustCompile(`(.*\)),(\{.*\})`).FindStringSubmatch(server)
serverConn := server

if len(connInfo) > 2 {
serverConn = connInfo[1]
server = connInfo[1]
jsonBytes := []byte(strings.ReplaceAll(connInfo[2], "'", "\""))
err := json.Unmarshal(jsonBytes, &customTags)
if err != nil {
return fmt.Errorf("Error unmarshaling %s ", err.Error())
}
}

conn := NewConnection(serverConn, m.Privilege, m.HexKey)
conn := NewConnection(server, m.Privilege, m.HexKey)
hostname = conn.Hostname
opts = conn.options()
}
Expand Down Expand Up @@ -316,6 +314,7 @@ func (m *Ipmi) parseV1(acc telegraf.Accumulator, hostname string, customTags map
}

description := ipmiFields["description"]

// handle hex description field
if strings.HasPrefix(description, "0x") {
descriptionInt, err := strconv.ParseInt(description, 0, 64)
Expand Down Expand Up @@ -373,7 +372,7 @@ func (m *Ipmi) parseV2(acc telegraf.Accumulator, hostname string, customTags map
"name": tag,
}

// tag the server is we have oneserver
// tag the server is we have one
if hostname != "" {
tags["server"] = hostname
}
Expand Down
22 changes: 12 additions & 10 deletions plugins/inputs/ipmi_sensor_formula/ipmi_sensor_formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ type FormulaData struct {

// Ipmi stores the configuration values for the ipmi_sensor input plugin
type Ipmi struct {
Path string
Privilege string
HexKey string `toml:"hex_key"`
Servers []Server `toml:"servers"`
Sensors []string `toml:"sensors"`
Timeout config.Duration
MetricVersion int
UseSudo bool
UseCache bool
CachePath string
Path string `toml:"path"`
Privilege string `toml:"privilege"`
HexKey string `toml:"hex_key"`
Servers []Server `toml:"servers"`
Sensors []string `toml:"sensors"`
Timeout config.Duration `toml:"timeout"`
MetricVersion int `toml:"metric_version"`
UseSudo bool `toml:"use_sudo"`
UseCache bool `toml:"use_cache"`
CachePath string `toml:"cache_path"`
Formula map[string]string `toml:"formula"`
ExtraTags []string `toml:"extra_tags"`

Expand Down Expand Up @@ -91,6 +91,7 @@ func (m *Ipmi) Init() error {
if err := choice.CheckSlice(m.Sensors, []string{"sdr", "chassis_power_status", "dcmi_power_reading"}); err != nil {
return err
}

// Check parameters
if m.Path == "" {
return fmt.Errorf("no path for %q specified", cmd)
Expand Down Expand Up @@ -383,6 +384,7 @@ func (m *Ipmi) parseV2(acc telegraf.Accumulator, hostname string, extraTags Tags
// Temp | 0Eh | ok | 3.1 | 55 degrees C
// Drive 0 | A0h | ok | 7.1 | Drive Present
scanner := bufio.NewScanner(bytes.NewReader(cmdOut))

for cpuIndex := 0; scanner.Scan(); {
ipmiFields := m.extractFieldsFromRegex(reV2ParseLine, scanner.Text())
if len(ipmiFields) < 3 || len(ipmiFields) > 4 {
Expand Down

0 comments on commit 551af5a

Please sign in to comment.