-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathsensors.go
135 lines (119 loc) · 3.09 KB
/
sensors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//go:generate ../../../tools/readme_config_includer/generator
//go:build linux
package sensors
import (
_ "embed"
"errors"
"fmt"
"os/exec"
"regexp"
"strconv"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
var (
execCommand = exec.Command // execCommand is used to mock commands in tests.
numberRegp = regexp.MustCompile("[0-9]+")
defaultTimeout = config.Duration(5 * time.Second)
)
const cmd = "sensors"
type Sensors struct {
RemoveNumbers bool `toml:"remove_numbers"`
Timeout config.Duration `toml:"timeout"`
path string
}
func (*Sensors) SampleConfig() string {
return sampleConfig
}
func (s *Sensors) Init() error {
// Set defaults
if s.path == "" {
path, err := exec.LookPath(cmd)
if err != nil {
return fmt.Errorf("looking up %q failed: %w", cmd, err)
}
s.path = path
}
// Check parameters
if s.path == "" {
return fmt.Errorf("no path specified for %q", cmd)
}
return nil
}
func (s *Sensors) Gather(acc telegraf.Accumulator) error {
if len(s.path) == 0 {
return errors.New("sensors not found: verify that lm-sensors package is installed and that sensors is in your PATH")
}
return s.parse(acc)
}
// parse forks the command:
//
// sensors -u -A
//
// and parses the output to add it to the telegraf.Accumulator.
func (s *Sensors) parse(acc telegraf.Accumulator) error {
tags := make(map[string]string)
fields := make(map[string]interface{})
chip := ""
cmd := execCommand(s.path, "-A", "-u")
out, err := internal.StdOutputTimeout(cmd, time.Duration(s.Timeout))
if err != nil {
return fmt.Errorf("failed to run command %q: %w - %s", strings.Join(cmd.Args, " "), err, string(out))
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
for _, line := range lines {
if len(line) == 0 {
acc.AddFields("sensors", fields, tags)
chip = ""
tags = make(map[string]string)
fields = make(map[string]interface{})
continue
}
if len(chip) == 0 {
chip = line
tags["chip"] = chip
continue
}
if !strings.HasPrefix(line, " ") {
if len(tags) > 1 {
acc.AddFields("sensors", fields, tags)
}
fields = make(map[string]interface{})
tags = map[string]string{
"chip": chip,
"feature": strings.TrimRight(snake(line), ":"),
}
} else {
splitted := strings.Split(line, ":")
fieldName := strings.TrimSpace(splitted[0])
if s.RemoveNumbers {
fieldName = numberRegp.ReplaceAllString(fieldName, "")
}
fieldValue, err := strconv.ParseFloat(strings.TrimSpace(splitted[1]), 64)
if err != nil {
return err
}
fields[fieldName] = fieldValue
}
}
acc.AddFields("sensors", fields, tags)
return nil
}
// snake converts string to snake case
func snake(input string) string {
return strings.ToLower(strings.ReplaceAll(strings.TrimSpace(input), " ", "_"))
}
func init() {
inputs.Add("sensors", func() telegraf.Input {
return &Sensors{
RemoveNumbers: true,
Timeout: defaultTimeout,
}
})
}