Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add temp inputs plugin for collect temperature #4411

Merged
merged 1 commit into from
Sep 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/inputs/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,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)

}