Skip to content

Commit

Permalink
Fix postfix plugin age to use ctime, not mtime (influxdata#3525)
Browse files Browse the repository at this point in the history
  • Loading branch information
phemmer authored and danielnelson committed Nov 29, 2017
1 parent 2c70958 commit bf65e19
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
31 changes: 23 additions & 8 deletions plugins/inputs/postfix/postfix.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,23 @@ func qScan(path string) (int64, int64, int64, error) {
for _, finfo := range finfos {
length++
size += finfo.Size()
if oldest.IsZero() || finfo.ModTime().Before(oldest) {
oldest = finfo.ModTime()

ctime := statCTime(finfo.Sys())
if ctime.IsZero() {
continue
}
if oldest.IsZero() || ctime.Before(oldest) {
oldest = ctime
}
}
var age time.Duration
var age int64
if !oldest.IsZero() {
age = time.Now().Sub(oldest) / time.Second
age = int64(time.Now().Sub(oldest) / time.Second)
} else if len(finfos) != 0 {
// system doesn't support ctime
age = -1
}
return length, size, int64(age), nil
return length, size, age, nil
}

type Postfix struct {
Expand All @@ -75,11 +83,15 @@ func (p *Postfix) Gather(acc telegraf.Accumulator) error {
acc.AddError(fmt.Errorf("error scanning queue %s: %s", q, err))
continue
}
fields := map[string]interface{}{"length": length, "size": size, "age": age}
fields := map[string]interface{}{"length": length, "size": size}
if age != -1 {
fields["age"] = age
}
acc.AddFields("postfix_queue", fields, map[string]string{"queue": q})
}

var dLength, dSize, dAge int64
var dLength, dSize int64
dAge := int64(-1)
for _, q := range []string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"} {
length, size, age, err := qScan(path.Join(p.QueueDirectory, "deferred", q))
if err != nil {
Expand All @@ -96,7 +108,10 @@ func (p *Postfix) Gather(acc telegraf.Accumulator) error {
dAge = age
}
}
fields := map[string]interface{}{"length": dLength, "size": dSize, "age": dAge}
fields := map[string]interface{}{"length": dLength, "size": dSize}
if dAge != -1 {
fields["age"] = dAge
}
acc.AddFields("postfix_queue", fields, map[string]string{"queue": "deferred"})

return nil
Expand Down
4 changes: 1 addition & 3 deletions plugins/inputs/postfix/postfix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"path"
"testing"
"time"

"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
Expand All @@ -26,7 +25,6 @@ func TestGather(t *testing.T) {

require.NoError(t, ioutil.WriteFile(path.Join(td, "active", "01"), []byte("abc"), 0644))
require.NoError(t, ioutil.WriteFile(path.Join(td, "active", "02"), []byte("defg"), 0644))
require.NoError(t, os.Chtimes(path.Join(td, "active", "02"), time.Now(), time.Now().Add(-time.Hour)))
require.NoError(t, ioutil.WriteFile(path.Join(td, "hold", "01"), []byte("abc"), 0644))
require.NoError(t, ioutil.WriteFile(path.Join(td, "incoming", "01"), []byte("abcd"), 0644))
require.NoError(t, ioutil.WriteFile(path.Join(td, "deferred", "0", "01"), []byte("abc"), 0644))
Expand All @@ -46,7 +44,7 @@ func TestGather(t *testing.T) {

assert.Equal(t, int64(2), metrics["active"].Fields["length"])
assert.Equal(t, int64(7), metrics["active"].Fields["size"])
assert.InDelta(t, int64(time.Hour/time.Second), metrics["active"].Fields["age"], 10)
assert.InDelta(t, 0, metrics["active"].Fields["age"], 10)

assert.Equal(t, int64(1), metrics["hold"].Fields["length"])
assert.Equal(t, int64(3), metrics["hold"].Fields["size"])
Expand Down
16 changes: 16 additions & 0 deletions plugins/inputs/postfix/stat_ctim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// +build dragonfly linux netbsd openbsd solaris

package postfix

import (
"syscall"
"time"
)

func statCTime(sys interface{}) time.Time {
stat, ok := sys.(*syscall.Stat_t)
if !ok {
return time.Time{}
}
return time.Unix(stat.Ctim.Unix())
}
16 changes: 16 additions & 0 deletions plugins/inputs/postfix/stat_ctimespec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// +build darwin freebsd

package postfix

import (
"syscall"
"time"
)

func statCTime(sys interface{}) time.Time {
stat, ok := sys.(*syscall.Stat_t)
if !ok {
return time.Time{}
}
return time.Unix(stat.Ctimespec.Unix())
}
7 changes: 7 additions & 0 deletions plugins/inputs/postfix/stat_none.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// +build !dragonfly,!linux,!netbsd,!openbsd,!solaris,!darwin,!freebsd

package postfix

func statCTime(_ interface{}) time.Time {
return time.Time{}
}

0 comments on commit bf65e19

Please sign in to comment.