Skip to content

Commit

Permalink
remove interval configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
zbindenren authored and sparrc committed Apr 7, 2016
1 parent 264ac0b commit be3374a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
7 changes: 0 additions & 7 deletions plugins/inputs/sysstat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ the created binary data file with the `sadf` utility.
```toml
# Sysstat metrics collector
[[inputs.sysstat]]
## Collect interval in seconds. This value has to be equal
## to the telegraf collect interval.
collect_interval = 30 # required
#
#
## Path to the sadc command.
sadc_path = "/usr/lib/sa/sadc" # required
#
Expand Down Expand Up @@ -127,7 +122,6 @@ And more if you define some `device_tags`.
With the configuration below:
```toml
[[inputs.sysstat]]
collect_interval = 30
sadc_path = "/usr/lib/sa/sadc" # required
activities = ["DISK", "SNMP", "INT"]
group = true
Expand Down Expand Up @@ -196,7 +190,6 @@ $ telegraf -config telegraf.conf -input-filter sysstat -test
If you change the group value to false like below:
```toml
[[inputs.sysstat]]
collect_interval = 30
sadc_path = "/usr/lib/sa/sadc" # required
activities = ["DISK", "SNMP", "INT"]
group = false
Expand Down
34 changes: 19 additions & 15 deletions plugins/inputs/sysstat/sysstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@ import (
)

var (
firstTimestamp time.Time
execCommand = exec.Command // execCommand is used to mock commands in tests.
dfltActivities = []string{"DISK"}
)

const parseInterval = 1 // parseInterval is the interval (in seconds) where the parsing takes place.
const parseInterval = 1 // parseInterval is the interval (in seconds) where the parsing of the binary file takes place.

type Sysstat struct {
// Interval that defines how long data is collected by Sadc cmd.
//
// This value has to be the same as the thelegraf collection interval.
Interval int `toml:"collect_interval"`

// Sadc represents the path to the sadc collector utility.
Sadc string `toml:"sadc_path"`

Expand Down Expand Up @@ -66,18 +62,14 @@ type Sysstat struct {
// DeviceTags adds the possibility to add additional tags for devices.
DeviceTags map[string][]map[string]string `toml:"device_tags"`
tmpFile string
interval int
}

func (*Sysstat) Description() string {
return "Sysstat metrics collector"
}

var sampleConfig = `
## Collect interval in seconds. This value has to be equal
## to the telegraf collect interval.
collect_interval = 5 # required
#
#
## Path to the sadc command.
sadc_path = "/usr/lib/sa/sadc" # required
#
Expand All @@ -101,15 +93,14 @@ var sampleConfig = `
# group = false
#
#
## Options for the sasf command. The values on the left represent the sadf options and
## Options for the sadf command. The values on the left represent the sadf options and
## the values on the right their description (wich are used for grouping and prefixing metrics).
[inputs.sysstat.options]
-C = "cpu"
-B = "paging"
-b = "io"
-d = "disk" # requires DISK activity
-H = "hugepages"
"-I ALL" = "interrupts" # requires INT activity
"-n ALL" = "network"
"-P ALL" = "per_cpu"
-q = "queue"
Expand All @@ -120,6 +111,7 @@ var sampleConfig = `
-v = "inode"
-W = "swap"
-w = "task"
# "-I ALL" = "interrupts" # requires INT activity
#
#
## Device tags can be used to add additional tags for devices. For example the configuration below
Expand All @@ -133,7 +125,14 @@ func (*Sysstat) SampleConfig() string {
}

func (s *Sysstat) Gather(acc telegraf.Accumulator) error {
ts := time.Now().Add(time.Duration(s.Interval) * time.Second)
if s.interval == 0 {
if firstTimestamp.IsZero() {
firstTimestamp = time.Now()
} else {
s.interval = int(time.Since(firstTimestamp).Seconds())
}
}
ts := time.Now().Add(time.Duration(s.interval) * time.Second)
if err := s.collect(); err != nil {
return err
}
Expand Down Expand Up @@ -187,7 +186,12 @@ func (s *Sysstat) collect() error {
options = append(options, "-S", act)
}
s.tmpFile = path.Join("/tmp", fmt.Sprintf("sysstat-%d", time.Now().Unix()))
collectInterval := s.Interval - parseInterval // collectInterval has to be smaller than the telegraf data collection interval
collectInterval := s.interval - parseInterval // collectInterval has to be smaller than the telegraf data collection interval

if collectInterval < 0 { // If true, interval is not defined yet and Gather is run for the first time.
collectInterval = 1 // In that case we only collect for 1 second.
}

options = append(options, strconv.Itoa(collectInterval), "2", s.tmpFile)
cmd := execCommand(s.Sadc, options...)
out, err := cmd.CombinedOutput()
Expand Down
30 changes: 29 additions & 1 deletion plugins/inputs/sysstat/sysstat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"os/exec"
"path"
"testing"
"time"

"github.com/influxdata/telegraf/testutil"
)

var s = Sysstat{
Interval: 10,
interval: 10,
Sadc: "/usr/lib/sa/sadc",
Group: false,
Activities: []string{"DISK", "SNMP"},
Expand Down Expand Up @@ -303,3 +304,30 @@ dell-xps 5 2016-03-25 16:18:10 UTC sdb %util 0.30
// some code here to check arguments perhaps?
os.Exit(0)
}

// TestGatherInterval checks that interval is correctly set.
func TestGatherInterval(t *testing.T) {
// overwriting exec commands with mock commands
execCommand = fakeExecCommand
defer func() { execCommand = exec.Command }()
var acc testutil.Accumulator

s.interval = 0
wantedInterval := 3

err := s.Gather(&acc)
if err != nil {
t.Fatal(err)
}

time.Sleep(time.Duration(wantedInterval) * time.Second)

err = s.Gather(&acc)
if err != nil {
t.Fatal(err)
}

if s.interval != wantedInterval {
t.Errorf("wrong interval: got %d, want %d", s.interval, wantedInterval)
}
}

0 comments on commit be3374a

Please sign in to comment.