Refactor interrupts plugin code (#2670)
This commit is contained in:
committed by
Daniel Nelson
parent
cadd845b36
commit
a12e082dbe
@@ -5,7 +5,8 @@ import (
|
||||
"fmt"
|
||||
"github.com/influxdata/telegraf"
|
||||
"github.com/influxdata/telegraf/plugins/inputs"
|
||||
"io/ioutil"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -38,18 +39,16 @@ func (s *Interrupts) SampleConfig() string {
|
||||
return sampleConfig
|
||||
}
|
||||
|
||||
func parseInterrupts(irqdata string) ([]IRQ, error) {
|
||||
func parseInterrupts(r io.Reader) ([]IRQ, error) {
|
||||
var irqs []IRQ
|
||||
var cpucount int
|
||||
scanner := bufio.NewScanner(strings.NewReader(irqdata))
|
||||
ok := scanner.Scan()
|
||||
if ok {
|
||||
scanner := bufio.NewScanner(r)
|
||||
if scanner.Scan() {
|
||||
cpus := strings.Fields(scanner.Text())
|
||||
if cpus[0] == "CPU0" {
|
||||
cpucount = len(cpus)
|
||||
if cpus[0] != "CPU0" {
|
||||
return nil, fmt.Errorf("Expected first line to start with CPU0, but was %s", scanner.Text())
|
||||
}
|
||||
} else if scanner.Err() != nil {
|
||||
return irqs, fmt.Errorf("Reading %s: %s", scanner.Text(), scanner.Err())
|
||||
cpucount = len(cpus)
|
||||
}
|
||||
for scanner.Scan() {
|
||||
fields := strings.Fields(scanner.Text())
|
||||
@@ -80,16 +79,10 @@ func parseInterrupts(irqdata string) ([]IRQ, error) {
|
||||
}
|
||||
irqs = append(irqs, *irq)
|
||||
}
|
||||
return irqs, nil
|
||||
}
|
||||
|
||||
func fileToString(path string) (string, error) {
|
||||
data, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
if scanner.Err() != nil {
|
||||
return nil, fmt.Errorf("Error scanning file: %s", scanner.Err())
|
||||
}
|
||||
content := string(data)
|
||||
return content, nil
|
||||
return irqs, nil
|
||||
}
|
||||
|
||||
func gatherTagsFields(irq IRQ) (map[string]string, map[string]interface{}) {
|
||||
@@ -103,31 +96,21 @@ func gatherTagsFields(irq IRQ) (map[string]string, map[string]interface{}) {
|
||||
}
|
||||
|
||||
func (s *Interrupts) Gather(acc telegraf.Accumulator) error {
|
||||
irqdata, err := fileToString("/proc/interrupts")
|
||||
if err != nil {
|
||||
acc.AddError(fmt.Errorf("Reading %s: %s", "/proc/interrupts", err))
|
||||
}
|
||||
irqs, err := parseInterrupts(irqdata)
|
||||
if err != nil {
|
||||
acc.AddError(fmt.Errorf("Parsing %s: %s", "/proc/interrupts", err))
|
||||
} else {
|
||||
for _, irq := range irqs {
|
||||
tags, fields := gatherTagsFields(irq)
|
||||
acc.AddFields("interrupts", fields, tags)
|
||||
for measurement, file := range map[string]string{"interrupts": "/proc/interrupts", "soft_interrupts": "/proc/softirqs"} {
|
||||
f, err := os.Open(file)
|
||||
if err != nil {
|
||||
acc.AddError(fmt.Errorf("Could not open file: %s", file))
|
||||
continue
|
||||
}
|
||||
defer f.Close()
|
||||
irqs, err := parseInterrupts(f)
|
||||
if err != nil {
|
||||
acc.AddError(fmt.Errorf("Parsing %s: %s", file, err))
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
irqdata, err = fileToString("/proc/softirqs")
|
||||
if err != nil {
|
||||
acc.AddError(fmt.Errorf("Reading %s: %s", "/proc/softirqs", err))
|
||||
}
|
||||
irqs, err = parseInterrupts(irqdata)
|
||||
if err != nil {
|
||||
acc.AddError(fmt.Errorf("Parsing %s: %s", "/proc/softirqs", err))
|
||||
} else {
|
||||
for _, irq := range irqs {
|
||||
tags, fields := gatherTagsFields(irq)
|
||||
acc.AddFields("softirqs", fields, tags)
|
||||
acc.AddFields(measurement, fields, tags)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user