Skip to content

Commit

Permalink
Move IO outside of logic
Browse files Browse the repository at this point in the history
  • Loading branch information
calerogers committed Apr 14, 2017
1 parent dc4fe2a commit 103340e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 25 deletions.
22 changes: 12 additions & 10 deletions plugins/inputs/interrupts/interrupts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
"io"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -38,15 +39,10 @@ func (s *Interrupts) SampleConfig() string {
return sampleConfig
}

func parseInterrupts(irqfile string) ([]IRQ, error) {
func parseInterrupts(r io.Reader) ([]IRQ, error) {
var irqs []IRQ
var cpucount int
f, err := os.Open(irqfile)
if err != nil {
return nil, fmt.Errorf("Could not open file: %s", irqfile)
}
defer f.Close()
scanner := bufio.NewScanner(f)
scanner := bufio.NewScanner(r)
if scanner.Scan() {
cpus := strings.Fields(scanner.Text())
if cpus[0] != "CPU0" {
Expand Down Expand Up @@ -100,15 +96,21 @@ func gatherTagsFields(irq IRQ) (map[string]string, map[string]interface{}) {
}

func (s *Interrupts) Gather(acc telegraf.Accumulator) error {
for key, file := range map[string]string{"interrupts": "/proc/interrupts", "soft_interrupts": "/proc/softirqs"} {
irqs, err := parseInterrupts(file)
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
}
for _, irq := range irqs {
tags, fields := gatherTagsFields(irq)
acc.AddFields(key, fields, tags)
acc.AddFields(measurement, fields, tags)
}
}
return nil
Expand Down
18 changes: 3 additions & 15 deletions plugins/inputs/interrupts/interrupts_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package interrupts

import (
"bytes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"testing"
)

Expand All @@ -17,18 +16,7 @@ LOC: 2338608687 2334309625 Local timer interrupts
MIS: 0
NET_RX: 867028 225
TASKLET: 205 0`
tmpfile, err := ioutil.TempFile("", "interrupts")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())

if _, err := tmpfile.Write([]byte(interruptStr)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
f := bytes.NewBufferString(interruptStr)
parsed := []IRQ{
IRQ{
ID: "0", Type: "IO-APIC-edge", Device: "timer",
Expand Down Expand Up @@ -59,7 +47,7 @@ TASKLET: 205 0`
Total: int64(205),
},
}
got, err := parseInterrupts(tmpfile.Name())
got, err := parseInterrupts(f)
require.Equal(t, nil, err)
require.NotEqual(t, 0, len(got))
require.Equal(t, len(got), len(parsed))
Expand Down

0 comments on commit 103340e

Please sign in to comment.