forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add temp input plugin (influxdata#4411)
- Loading branch information
Showing
6 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Temp Input plugin | ||
|
||
This input plugin collect temperature. | ||
|
||
### Configuration: | ||
|
||
``` | ||
[[inputs.temp]] | ||
``` | ||
|
||
### Measurements & Fields: | ||
|
||
All fields are float64. | ||
|
||
- temp ( unit: °Celsius) | ||
|
||
### Tags: | ||
|
||
- All measurements have the following tags: | ||
- host | ||
- sensor | ||
|
||
### Example Output: | ||
|
||
``` | ||
$ ./telegraf --config telegraf.conf --input-filter temp --test | ||
* Plugin: temp, Collection 1 | ||
> temp,host=localhost,sensor=coretemp_physicalid0_crit temp=100 1531298763000000000 | ||
> temp,host=localhost,sensor=coretemp_physicalid0_critalarm temp=0 1531298763000000000 | ||
> temp,host=localhost,sensor=coretemp_physicalid0_input temp=100 1531298763000000000 | ||
> temp,host=localhost,sensor=coretemp_physicalid0_max temp=100 1531298763000000000 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package temp | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
"github.com/influxdata/telegraf/plugins/inputs/system" | ||
) | ||
|
||
type Temperature struct { | ||
ps system.PS | ||
} | ||
|
||
func (t *Temperature) Description() string { | ||
return "Read metrics about temperature" | ||
} | ||
|
||
const sampleConfig = "" | ||
|
||
func (t *Temperature) SampleConfig() string { | ||
return sampleConfig | ||
} | ||
|
||
func (t *Temperature) Gather(acc telegraf.Accumulator) error { | ||
temps, err := t.ps.Temperature() | ||
if err != nil { | ||
return fmt.Errorf("error getting temperatures info: %s", err) | ||
} | ||
for _, temp := range temps { | ||
tags := map[string]string{ | ||
"sensor": temp.SensorKey, | ||
} | ||
fields := map[string]interface{}{ | ||
"temp": temp.Temperature, | ||
} | ||
acc.AddFields("temp", fields, tags) | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
inputs.Add("temp", func() telegraf.Input { | ||
return &Temperature{ps: system.NewSystemPS()} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package temp | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/shirou/gopsutil/host" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/influxdata/telegraf/plugins/inputs/system" | ||
"github.com/influxdata/telegraf/testutil" | ||
) | ||
|
||
func TestTemperature(t *testing.T) { | ||
var mps system.MockPS | ||
var err error | ||
defer mps.AssertExpectations(t) | ||
var acc testutil.Accumulator | ||
|
||
ts := host.TemperatureStat{ | ||
SensorKey: "coretemp_sensor1_crit", | ||
Temperature: 60.5, | ||
} | ||
|
||
mps.On("Temperature").Return([]host.TemperatureStat{ts}, nil) | ||
|
||
err = (&Temperature{ps: &mps}).Gather(&acc) | ||
require.NoError(t, err) | ||
|
||
expectedFields := map[string]interface{}{ | ||
"temp": float64(60.5), | ||
} | ||
|
||
expectedTags := map[string]string{ | ||
"sensor": "coretemp_sensor1_crit", | ||
} | ||
acc.AssertContainsTaggedFields(t, "temp", expectedFields, expectedTags) | ||
|
||
} |