Refactor interrupts plugin code (#2670)

This commit is contained in:
calerogers 2017-04-14 13:40:36 -07:00 committed by Daniel Nelson
parent 3e5980d017
commit 4df8b034bf
3 changed files with 29 additions and 45 deletions

View File

@ -5,9 +5,9 @@ The interrupts plugin gathers metrics about IRQs from `/proc/interrupts` and `/p
### Configuration ### Configuration
``` ```
[[inputs.interrupts]] [[inputs.interrupts]]
## A list of IRQs to include for metric ingestion, if not specified ## To filter which IRQs to collect, make use of tagpass / tagdrop, i.e.
## will default to collecting all IRQs. # [inputs.interrupts.tagdrop]
include = ["0", "1", "30", "NET_RX"] # irq = [ "NET_RX", "TASKLET" ]
``` ```
### Measurements ### Measurements

View File

@ -5,7 +5,8 @@ import (
"fmt" "fmt"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
"io/ioutil" "io"
"os"
"strconv" "strconv"
"strings" "strings"
) )
@ -38,18 +39,16 @@ func (s *Interrupts) SampleConfig() string {
return sampleConfig return sampleConfig
} }
func parseInterrupts(irqdata string) ([]IRQ, error) { func parseInterrupts(r io.Reader) ([]IRQ, error) {
var irqs []IRQ var irqs []IRQ
var cpucount int var cpucount int
scanner := bufio.NewScanner(strings.NewReader(irqdata)) scanner := bufio.NewScanner(r)
ok := scanner.Scan() if scanner.Scan() {
if ok {
cpus := strings.Fields(scanner.Text()) cpus := strings.Fields(scanner.Text())
if cpus[0] == "CPU0" { if cpus[0] != "CPU0" {
cpucount = len(cpus) return nil, fmt.Errorf("Expected first line to start with CPU0, but was %s", scanner.Text())
} }
} else if scanner.Err() != nil { cpucount = len(cpus)
return irqs, fmt.Errorf("Reading %s: %s", scanner.Text(), scanner.Err())
} }
for scanner.Scan() { for scanner.Scan() {
fields := strings.Fields(scanner.Text()) fields := strings.Fields(scanner.Text())
@ -80,16 +79,10 @@ func parseInterrupts(irqdata string) ([]IRQ, error) {
} }
irqs = append(irqs, *irq) irqs = append(irqs, *irq)
} }
return irqs, nil if scanner.Err() != nil {
} return nil, fmt.Errorf("Error scanning file: %s", scanner.Err())
func fileToString(path string) (string, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return "", err
} }
content := string(data) return irqs, nil
return content, nil
} }
func gatherTagsFields(irq IRQ) (map[string]string, map[string]interface{}) { 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 { func (s *Interrupts) Gather(acc telegraf.Accumulator) error {
irqdata, err := fileToString("/proc/interrupts") for measurement, file := range map[string]string{"interrupts": "/proc/interrupts", "soft_interrupts": "/proc/softirqs"} {
if err != nil { f, err := os.Open(file)
acc.AddError(fmt.Errorf("Reading %s: %s", "/proc/interrupts", err)) if err != nil {
} acc.AddError(fmt.Errorf("Could not open file: %s", file))
irqs, err := parseInterrupts(irqdata) continue
if err != nil { }
acc.AddError(fmt.Errorf("Parsing %s: %s", "/proc/interrupts", err)) defer f.Close()
} else { irqs, err := parseInterrupts(f)
for _, irq := range irqs { if err != nil {
tags, fields := gatherTagsFields(irq) acc.AddError(fmt.Errorf("Parsing %s: %s", file, err))
acc.AddFields("interrupts", fields, tags) 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 { for _, irq := range irqs {
tags, fields := gatherTagsFields(irq) tags, fields := gatherTagsFields(irq)
acc.AddFields("softirqs", fields, tags) acc.AddFields(measurement, fields, tags)
} }
} }
return nil return nil

View File

@ -1,6 +1,7 @@
package interrupts package interrupts
import ( import (
"bytes"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"testing" "testing"
@ -15,7 +16,7 @@ LOC: 2338608687 2334309625 Local timer interrupts
MIS: 0 MIS: 0
NET_RX: 867028 225 NET_RX: 867028 225
TASKLET: 205 0` TASKLET: 205 0`
f := bytes.NewBufferString(interruptStr)
parsed := []IRQ{ parsed := []IRQ{
IRQ{ IRQ{
ID: "0", Type: "IO-APIC-edge", Device: "timer", ID: "0", Type: "IO-APIC-edge", Device: "timer",
@ -46,7 +47,7 @@ TASKLET: 205 0`
Total: int64(205), Total: int64(205),
}, },
} }
got, err := parseInterrupts(interruptStr) got, err := parseInterrupts(f)
require.Equal(t, nil, err) require.Equal(t, nil, err)
require.NotEqual(t, 0, len(got)) require.NotEqual(t, 0, len(got))
require.Equal(t, len(got), len(parsed)) require.Equal(t, len(got), len(parsed))