Add temp input plugin (#4411)

This commit is contained in:
pytimer
2018-09-11 02:52:15 +08:00
committed by Daniel Nelson
parent 69100f60b8
commit 25f9cc0b8d
6 changed files with 133 additions and 0 deletions

View File

@@ -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
```

View File

@@ -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()}
})
}

View File

@@ -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)
}