Skip to content

Commit 4250f50

Browse files
pytimerotherpirate
authored andcommitted
Add temp input plugin (influxdata#4411)
1 parent b737b77 commit 4250f50

File tree

6 files changed

+133
-0
lines changed

6 files changed

+133
-0
lines changed

plugins/inputs/all/all.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ import (
120120
_ "github.com/influxdata/telegraf/plugins/inputs/tail"
121121
_ "github.com/influxdata/telegraf/plugins/inputs/tcp_listener"
122122
_ "github.com/influxdata/telegraf/plugins/inputs/teamspeak"
123+
_ "github.com/influxdata/telegraf/plugins/inputs/temp"
123124
_ "github.com/influxdata/telegraf/plugins/inputs/tengine"
124125
_ "github.com/influxdata/telegraf/plugins/inputs/tomcat"
125126
_ "github.com/influxdata/telegraf/plugins/inputs/trig"

plugins/inputs/system/mock_PS.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/shirou/gopsutil/cpu"
99
"github.com/shirou/gopsutil/disk"
10+
"github.com/shirou/gopsutil/host"
1011

1112
"github.com/shirou/gopsutil/load"
1213
"github.com/shirou/gopsutil/mem"
@@ -100,6 +101,15 @@ func (m *MockPS) SwapStat() (*mem.SwapMemoryStat, error) {
100101
return r0, r1
101102
}
102103

104+
func (m *MockPS) Temperature() ([]host.TemperatureStat, error) {
105+
ret := m.Called()
106+
107+
r0 := ret.Get(0).([]host.TemperatureStat)
108+
r1 := ret.Error(1)
109+
110+
return r0, r1
111+
}
112+
103113
func (m *MockPS) NetConnections() ([]net.ConnectionStat, error) {
104114
ret := m.Called()
105115

plugins/inputs/system/ps.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/shirou/gopsutil/cpu"
1212
"github.com/shirou/gopsutil/disk"
13+
"github.com/shirou/gopsutil/host"
1314
"github.com/shirou/gopsutil/mem"
1415
"github.com/shirou/gopsutil/net"
1516
)
@@ -23,6 +24,7 @@ type PS interface {
2324
VMStat() (*mem.VirtualMemoryStat, error)
2425
SwapStat() (*mem.SwapMemoryStat, error)
2526
NetConnections() ([]net.ConnectionStat, error)
27+
Temperature() ([]host.TemperatureStat, error)
2628
}
2729

2830
type PSDiskDeps interface {
@@ -168,6 +170,10 @@ func (s *SystemPS) SwapStat() (*mem.SwapMemoryStat, error) {
168170
return mem.SwapMemory()
169171
}
170172

173+
func (s *SystemPS) Temperature() ([]host.TemperatureStat, error) {
174+
return host.SensorsTemperatures()
175+
}
176+
171177
func (s *SystemPSDisk) Partitions(all bool) ([]disk.PartitionStat, error) {
172178
return disk.Partitions(all)
173179
}

plugins/inputs/temp/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Temp Input plugin
2+
3+
This input plugin collect temperature.
4+
5+
### Configuration:
6+
7+
```
8+
[[inputs.temp]]
9+
```
10+
11+
### Measurements & Fields:
12+
13+
All fields are float64.
14+
15+
- temp ( unit: °Celsius)
16+
17+
### Tags:
18+
19+
- All measurements have the following tags:
20+
- host
21+
- sensor
22+
23+
### Example Output:
24+
25+
```
26+
$ ./telegraf --config telegraf.conf --input-filter temp --test
27+
* Plugin: temp, Collection 1
28+
> temp,host=localhost,sensor=coretemp_physicalid0_crit temp=100 1531298763000000000
29+
> temp,host=localhost,sensor=coretemp_physicalid0_critalarm temp=0 1531298763000000000
30+
> temp,host=localhost,sensor=coretemp_physicalid0_input temp=100 1531298763000000000
31+
> temp,host=localhost,sensor=coretemp_physicalid0_max temp=100 1531298763000000000
32+
```

plugins/inputs/temp/temp.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package temp
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/influxdata/telegraf"
7+
"github.com/influxdata/telegraf/plugins/inputs"
8+
"github.com/influxdata/telegraf/plugins/inputs/system"
9+
)
10+
11+
type Temperature struct {
12+
ps system.PS
13+
}
14+
15+
func (t *Temperature) Description() string {
16+
return "Read metrics about temperature"
17+
}
18+
19+
const sampleConfig = ""
20+
21+
func (t *Temperature) SampleConfig() string {
22+
return sampleConfig
23+
}
24+
25+
func (t *Temperature) Gather(acc telegraf.Accumulator) error {
26+
temps, err := t.ps.Temperature()
27+
if err != nil {
28+
return fmt.Errorf("error getting temperatures info: %s", err)
29+
}
30+
for _, temp := range temps {
31+
tags := map[string]string{
32+
"sensor": temp.SensorKey,
33+
}
34+
fields := map[string]interface{}{
35+
"temp": temp.Temperature,
36+
}
37+
acc.AddFields("temp", fields, tags)
38+
}
39+
return nil
40+
}
41+
42+
func init() {
43+
inputs.Add("temp", func() telegraf.Input {
44+
return &Temperature{ps: system.NewSystemPS()}
45+
})
46+
}

plugins/inputs/temp/temp_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package temp
2+
3+
import (
4+
"testing"
5+
6+
"github.com/shirou/gopsutil/host"
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/influxdata/telegraf/plugins/inputs/system"
10+
"github.com/influxdata/telegraf/testutil"
11+
)
12+
13+
func TestTemperature(t *testing.T) {
14+
var mps system.MockPS
15+
var err error
16+
defer mps.AssertExpectations(t)
17+
var acc testutil.Accumulator
18+
19+
ts := host.TemperatureStat{
20+
SensorKey: "coretemp_sensor1_crit",
21+
Temperature: 60.5,
22+
}
23+
24+
mps.On("Temperature").Return([]host.TemperatureStat{ts}, nil)
25+
26+
err = (&Temperature{ps: &mps}).Gather(&acc)
27+
require.NoError(t, err)
28+
29+
expectedFields := map[string]interface{}{
30+
"temp": float64(60.5),
31+
}
32+
33+
expectedTags := map[string]string{
34+
"sensor": "coretemp_sensor1_crit",
35+
}
36+
acc.AssertContainsTaggedFields(t, "temp", expectedFields, expectedTags)
37+
38+
}

0 commit comments

Comments
 (0)