Skip to content

Commit

Permalink
Use redfish host/ip as address tag
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson committed Jun 23, 2020
1 parent d439300 commit 241c432
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
14 changes: 10 additions & 4 deletions plugins/inputs/redfish/redfish.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -240,6 +241,11 @@ func (r *Redfish) getThermal(ref string) (*Thermal, error) {
}

func (r *Redfish) Gather(acc telegraf.Accumulator) error {
address, _, err := net.SplitHostPort(r.baseURL.Host)
if err != nil {
address = r.baseURL.Host
}

system, err := r.getComputerSystem(r.ComputerSystemId)
if err != nil {
return err
Expand All @@ -258,7 +264,7 @@ func (r *Redfish) Gather(acc telegraf.Accumulator) error {

for _, j := range thermal.Temperatures {
tags := map[string]string{}
tags["address"] = r.Address
tags["address"] = address
tags["name"] = j.Name
tags["source"] = system.Hostname
tags["state"] = j.Status.State
Expand All @@ -280,7 +286,7 @@ func (r *Redfish) Gather(acc telegraf.Accumulator) error {
for _, j := range thermal.Fans {
tags := map[string]string{}
fields := make(map[string]interface{})
tags["address"] = r.Address
tags["address"] = address
tags["name"] = j.Name
tags["source"] = system.Hostname
tags["state"] = j.Status.State
Expand Down Expand Up @@ -309,7 +315,7 @@ func (r *Redfish) Gather(acc telegraf.Accumulator) error {

for _, j := range power.PowerSupplies {
tags := map[string]string{}
tags["address"] = r.Address
tags["address"] = address
tags["name"] = j.Name
tags["source"] = system.Hostname
tags["state"] = j.Status.State
Expand All @@ -332,7 +338,7 @@ func (r *Redfish) Gather(acc telegraf.Accumulator) error {

for _, j := range power.Voltages {
tags := map[string]string{}
tags["address"] = r.Address
tags["address"] = address
tags["name"] = j.Name
tags["source"] = system.Hostname
tags["state"] = j.Status.State
Expand Down
72 changes: 42 additions & 30 deletions plugins/inputs/redfish/redfish_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package redfish

import (
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"time"

Expand Down Expand Up @@ -36,13 +38,18 @@ func TestDellApis(t *testing.T) {

defer ts.Close()

u, err := url.Parse(ts.URL)
require.NoError(t, err)
address, _, err := net.SplitHostPort(u.Host)
require.NoError(t, err)

expected_metrics := []telegraf.Metric{
testutil.MustMetric(
"redfish_thermal_temperatures",
map[string]string{
"name": "CPU1 Temp",
"source": "tpa-hostname",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -62,7 +69,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan1A",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -80,7 +87,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan1B",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -98,7 +105,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan2A",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -116,7 +123,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan2B",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -134,7 +141,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan3A",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -152,7 +159,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan3B",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -170,7 +177,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan4A",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -188,7 +195,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan4B",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -206,7 +213,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan5A",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -224,7 +231,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan5B",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -242,7 +249,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan6A",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -260,7 +267,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan6B",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -278,7 +285,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan7A",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -296,7 +303,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan7B",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -314,7 +321,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan8A",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -332,7 +339,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board Fan8B",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -350,7 +357,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "PS1 Status",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -371,7 +378,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board DIMM PG",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -389,7 +396,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board NDC PG",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -408,7 +415,7 @@ func TestDellApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "System Board PS1 PG FAIL",
"address": ts.URL,
"address": address,
"datacenter": "",
"health": "OK",
"rack": "",
Expand All @@ -431,7 +438,7 @@ func TestDellApis(t *testing.T) {
plugin.Init()
var acc testutil.Accumulator

err := plugin.Gather(&acc)
err = plugin.Gather(&acc)
require.NoError(t, err)
require.True(t, acc.HasMeasurement("redfish_thermal_temperatures"))
testutil.RequireMetricsEqual(t, expected_metrics, acc.GetTelegrafMetrics(),
Expand Down Expand Up @@ -463,13 +470,18 @@ func TestHPApis(t *testing.T) {

defer ts.Close()

u, err := url.Parse(ts.URL)
require.NoError(t, err)
address, _, err := net.SplitHostPort(u.Host)
require.NoError(t, err)

expected_metrics_hp := []telegraf.Metric{
testutil.MustMetric(
"redfish_thermal_temperatures",
map[string]string{
"name": "01-Inlet Ambient",
"source": "tpa-hostname",
"address": ts.URL,
"address": address,
"health": "OK",
"state": "Enabled",
},
Expand All @@ -485,7 +497,7 @@ func TestHPApis(t *testing.T) {
map[string]string{
"name": "44-P/S 2 Zone",
"source": "tpa-hostname",
"address": ts.URL,
"address": address,
"health": "OK",
"state": "Enabled",
},
Expand All @@ -501,7 +513,7 @@ func TestHPApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "Fan 1",
"address": ts.URL,
"address": address,
"health": "OK",
"state": "Enabled",
},
Expand All @@ -515,7 +527,7 @@ func TestHPApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "Fan 2",
"address": ts.URL,
"address": address,
"health": "OK",
"state": "Enabled",
},
Expand All @@ -529,7 +541,7 @@ func TestHPApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "Fan 3",
"address": ts.URL,
"address": address,
"health": "OK",
"state": "Enabled",
},
Expand All @@ -543,7 +555,7 @@ func TestHPApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "HpeServerPowerSupply",
"address": ts.URL,
"address": address,
"health": "OK",
"state": "Enabled",
},
Expand All @@ -559,7 +571,7 @@ func TestHPApis(t *testing.T) {
map[string]string{
"source": "tpa-hostname",
"name": "HpeServerPowerSupply",
"address": ts.URL,
"address": address,
"health": "OK",
"state": "Enabled",
},
Expand All @@ -581,7 +593,7 @@ func TestHPApis(t *testing.T) {
hp_plugin.Init()
var hp_acc testutil.Accumulator

err := hp_plugin.Gather(&hp_acc)
err = hp_plugin.Gather(&hp_acc)
require.NoError(t, err)
require.True(t, hp_acc.HasMeasurement("redfish_thermal_temperatures"))
testutil.RequireMetricsEqual(t, expected_metrics_hp, hp_acc.GetTelegrafMetrics(),
Expand Down

0 comments on commit 241c432

Please sign in to comment.