Skip to content

Commit

Permalink
Add temp input plugin (influxdata#4411)
Browse files Browse the repository at this point in the history
  • Loading branch information
pytimer authored and rgitzel committed Oct 17, 2018
1 parent 0240c4c commit b3f6e4b
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/inputs/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/inputs/tail"
_ "github.com/influxdata/telegraf/plugins/inputs/tcp_listener"
_ "github.com/influxdata/telegraf/plugins/inputs/teamspeak"
_ "github.com/influxdata/telegraf/plugins/inputs/temp"
_ "github.com/influxdata/telegraf/plugins/inputs/tengine"
_ "github.com/influxdata/telegraf/plugins/inputs/tomcat"
_ "github.com/influxdata/telegraf/plugins/inputs/trig"
Expand Down
10 changes: 10 additions & 0 deletions plugins/inputs/system/mock_PS.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"

"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
Expand Down Expand Up @@ -100,6 +101,15 @@ func (m *MockPS) SwapStat() (*mem.SwapMemoryStat, error) {
return r0, r1
}

func (m *MockPS) Temperature() ([]host.TemperatureStat, error) {
ret := m.Called()

r0 := ret.Get(0).([]host.TemperatureStat)
r1 := ret.Error(1)

return r0, r1
}

func (m *MockPS) NetConnections() ([]net.ConnectionStat, error) {
ret := m.Called()

Expand Down
6 changes: 6 additions & 0 deletions plugins/inputs/system/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)
Expand All @@ -23,6 +24,7 @@ type PS interface {
VMStat() (*mem.VirtualMemoryStat, error)
SwapStat() (*mem.SwapMemoryStat, error)
NetConnections() ([]net.ConnectionStat, error)
Temperature() ([]host.TemperatureStat, error)
}

type PSDiskDeps interface {
Expand Down Expand Up @@ -168,6 +170,10 @@ func (s *SystemPS) SwapStat() (*mem.SwapMemoryStat, error) {
return mem.SwapMemory()
}

func (s *SystemPS) Temperature() ([]host.TemperatureStat, error) {
return host.SensorsTemperatures()
}

func (s *SystemPSDisk) Partitions(all bool) ([]disk.PartitionStat, error) {
return disk.Partitions(all)
}
Expand Down
32 changes: 32 additions & 0 deletions plugins/inputs/temp/README.md
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
```
46 changes: 46 additions & 0 deletions plugins/inputs/temp/temp.go
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()}
})
}
38 changes: 38 additions & 0 deletions plugins/inputs/temp/temp_test.go
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)

}

0 comments on commit b3f6e4b

Please sign in to comment.