-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
remove cgo dependeny with forking sensors command #1414
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# sensors Input Plugin | ||
|
||
Collect [lm-sensors](https://en.wikipedia.org/wiki/Lm_sensors) metrics - requires the lm-sensors | ||
package installed. | ||
|
||
This plugin collects sensor metrics with the `sensors` executable from the lm-sensor package. | ||
|
||
### Configuration: | ||
``` | ||
# Monitor sensors, requires lm-sensors package | ||
[[inputs.sensors]] | ||
## Remove numbers from field names. | ||
## If true, a field name like 'temp1_input' will be changed to 'temp_input'. | ||
# remove_numbers = true | ||
# | ||
``` | ||
|
||
### Measurements & Fields: | ||
Fields are created dynamicaly depending on the sensors. All fields are float. | ||
|
||
### Tags: | ||
|
||
- All measurements have the following tags: | ||
- chip | ||
- feature | ||
|
||
### Example Output: | ||
|
||
#### Default | ||
``` | ||
$ telegraf -config telegraf.conf -input-filter sensors -test | ||
* Plugin: sensors, Collection 1 | ||
> sensors,chip=power_meter-acpi-0,feature=power1 power_average=0,power_average_interval=300 1466751326000000000 | ||
> sensors,chip=k10temp-pci-00c3,feature=temp1 temp_crit=70,temp_crit_hyst=65,temp_input=29,temp_max=70 1466751326000000000 | ||
> sensors,chip=k10temp-pci-00cb,feature=temp1 temp_input=29,temp_max=70 1466751326000000000 | ||
> sensors,chip=k10temp-pci-00d3,feature=temp1 temp_input=27.5,temp_max=70 1466751326000000000 | ||
> sensors,chip=k10temp-pci-00db,feature=temp1 temp_crit=70,temp_crit_hyst=65,temp_input=29.5,temp_max=70 1466751326000000000 | ||
``` | ||
|
||
#### With remove_numbers=false | ||
``` | ||
* Plugin: sensors, Collection 1 | ||
> sensors,chip=power_meter-acpi-0,feature=power1 power1_average=0,power1_average_interval=300 1466753424000000000 | ||
> sensors,chip=k10temp-pci-00c3,feature=temp1 temp1_crit=70,temp1_crit_hyst=65,temp1_input=29.125,temp1_max=70 1466753424000000000 | ||
> sensors,chip=k10temp-pci-00cb,feature=temp1 temp1_input=29,temp1_max=70 1466753424000000000 | ||
> sensors,chip=k10temp-pci-00d3,feature=temp1 temp1_input=29.5,temp1_max=70 1466753424000000000 | ||
> sensors,chip=k10temp-pci-00db,feature=temp1 temp1_crit=70,temp1_crit_hyst=65,temp1_input=30,temp1_max=70 1466753424000000000 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,119 @@ | ||
// +build linux,sensors | ||
// +build linux | ||
|
||
package sensors | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os/exec" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/md14454/gosensors" | ||
"time" | ||
|
||
"github.com/influxdata/telegraf" | ||
"github.com/influxdata/telegraf/internal" | ||
"github.com/influxdata/telegraf/plugins/inputs" | ||
) | ||
|
||
var ( | ||
execCommand = exec.Command // execCommand is used to mock commands in tests. | ||
numberRegp = regexp.MustCompile("[0-9]+") | ||
) | ||
|
||
type Sensors struct { | ||
Sensors []string | ||
RemoveNumbers bool `toml:"remove_numbers"` | ||
path string | ||
} | ||
|
||
func (_ *Sensors) Description() string { | ||
return "Monitor sensors using lm-sensors package" | ||
func (*Sensors) Description() string { | ||
return "Monitor sensors, requires lm-sensors package" | ||
} | ||
|
||
var sensorsSampleConfig = ` | ||
## By default, telegraf gathers stats from all sensors detected by the | ||
## lm-sensors module. | ||
## | ||
## Only collect stats from the selected sensors. Sensors are listed as | ||
## <chip name>:<feature name>. This information can be found by running the | ||
## sensors command, e.g. sensors -u | ||
## | ||
## A * as the feature name will return all features of the chip | ||
## | ||
# sensors = ["coretemp-isa-0000:Core 0", "coretemp-isa-0001:*"] | ||
` | ||
func (*Sensors) SampleConfig() string { | ||
return ` | ||
## Remove numbers from field names. | ||
## If true, a field name like 'temp1_input' will be changed to 'temp_input'. | ||
# remove_numbers = true | ||
# | ||
` | ||
|
||
func (_ *Sensors) SampleConfig() string { | ||
return sensorsSampleConfig | ||
} | ||
|
||
func (s *Sensors) Gather(acc telegraf.Accumulator) error { | ||
gosensors.Init() | ||
defer gosensors.Cleanup() | ||
|
||
for _, chip := range gosensors.GetDetectedChips() { | ||
for _, feature := range chip.GetFeatures() { | ||
chipName := chip.String() | ||
featureLabel := feature.GetLabel() | ||
|
||
if len(s.Sensors) != 0 { | ||
var found bool | ||
|
||
for _, sensor := range s.Sensors { | ||
parts := strings.SplitN(sensor, ":", 2) | ||
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") | ||
} | ||
|
||
if parts[0] == chipName { | ||
if parts[1] == "*" || parts[1] == featureLabel { | ||
found = true | ||
break | ||
} | ||
} | ||
} | ||
return s.parse(acc) | ||
} | ||
|
||
if !found { | ||
continue | ||
} | ||
// 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 := map[string]string{} | ||
fields := map[string]interface{}{} | ||
chip := "" | ||
cmd := execCommand(s.path, "-A", "-u") | ||
out, err := internal.CombinedOutputTimeout(cmd, time.Second*5) | ||
if err != nil { | ||
return fmt.Errorf("failed to run command %s: %s - %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 = map[string]string{} | ||
fields = 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) | ||
} | ||
|
||
tags := map[string]string{ | ||
"chip": chipName, | ||
"adapter": chip.AdapterName(), | ||
"feature-name": feature.Name, | ||
"feature-label": featureLabel, | ||
fields = map[string]interface{}{} | ||
tags = map[string]string{ | ||
"chip": chip, | ||
"feature": strings.TrimRight(snake(line), ":"), | ||
} | ||
|
||
fieldName := chipName + ":" + featureLabel | ||
|
||
fields := map[string]interface{}{ | ||
fieldName: feature.GetValue(), | ||
} else { | ||
splitted := strings.Split(line, ":") | ||
fieldName := strings.TrimSpace(splitted[0]) | ||
if s.RemoveNumbers { | ||
fieldName = numberRegp.ReplaceAllString(fieldName, "") | ||
} | ||
|
||
acc.AddFields("sensors", fields, tags) | ||
fieldValue, err := strconv.ParseFloat(strings.TrimSpace(splitted[1]), 64) | ||
if err != nil { | ||
return err | ||
} | ||
fields[fieldName] = fieldValue | ||
} | ||
} | ||
|
||
acc.AddFields("sensors", fields, tags) | ||
return nil | ||
} | ||
|
||
func init() { | ||
s := Sensors{ | ||
RemoveNumbers: true, | ||
} | ||
path, _ := exec.LookPath("sensors") | ||
if len(path) > 0 { | ||
s.path = path | ||
} | ||
inputs.Add("sensors", func() telegraf.Input { | ||
return &Sensors{} | ||
return &s | ||
}) | ||
} | ||
|
||
// snake converts string to snake case | ||
func snake(input string) string { | ||
return strings.ToLower(strings.Replace(input, " ", "_", -1)) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// +build !linux | ||
|
||
package sensors |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please remove the trailing
#