2017-11-21 06:39:13 +08:00
|
|
|
package opensmtpd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2021-04-10 01:15:04 +08:00
|
|
|
"github.com/influxdata/telegraf/config"
|
2017-11-21 06:39:13 +08:00
|
|
|
"github.com/influxdata/telegraf/filter"
|
|
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
|
)
|
|
|
|
|
|
2021-04-10 01:15:04 +08:00
|
|
|
type runner func(cmdName string, timeout config.Duration, useSudo bool) (*bytes.Buffer, error)
|
2017-11-21 06:39:13 +08:00
|
|
|
|
|
|
|
|
// Opensmtpd is used to store configuration values
|
|
|
|
|
type Opensmtpd struct {
|
|
|
|
|
Binary string
|
2021-04-10 01:15:04 +08:00
|
|
|
Timeout config.Duration
|
2017-11-21 06:39:13 +08:00
|
|
|
UseSudo bool
|
|
|
|
|
|
2021-03-23 01:21:36 +08:00
|
|
|
run runner
|
2017-11-21 06:39:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var defaultBinary = "/usr/sbin/smtpctl"
|
2021-04-10 01:15:04 +08:00
|
|
|
var defaultTimeout = config.Duration(time.Second)
|
2017-11-21 06:39:13 +08:00
|
|
|
|
|
|
|
|
// Shell out to opensmtpd_stat and return the output
|
2021-04-10 01:15:04 +08:00
|
|
|
func opensmtpdRunner(cmdName string, timeout config.Duration, useSudo bool) (*bytes.Buffer, error) {
|
2017-11-21 06:39:13 +08:00
|
|
|
cmdArgs := []string{"show", "stats"}
|
|
|
|
|
|
|
|
|
|
cmd := exec.Command(cmdName, cmdArgs...)
|
|
|
|
|
|
2021-04-10 01:15:04 +08:00
|
|
|
if useSudo {
|
2017-11-21 06:39:13 +08:00
|
|
|
cmdArgs = append([]string{cmdName}, cmdArgs...)
|
|
|
|
|
cmd = exec.Command("sudo", cmdArgs...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
cmd.Stdout = &out
|
2021-04-10 01:15:04 +08:00
|
|
|
err := internal.RunTimeout(cmd, time.Duration(timeout))
|
2017-11-21 06:39:13 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return &out, fmt.Errorf("error running smtpctl: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &out, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Gather collects the configured stats from smtpctl and adds them to the
|
|
|
|
|
// Accumulator
|
|
|
|
|
//
|
|
|
|
|
// All the dots in stat name will replaced by underscores. Histogram statistics will not be collected.
|
|
|
|
|
func (s *Opensmtpd) Gather(acc telegraf.Accumulator) error {
|
|
|
|
|
// Always exclude uptime.human statistics
|
2021-03-02 05:04:35 +08:00
|
|
|
statExcluded := []string{"uptime.human"}
|
|
|
|
|
filterExcluded, err := filter.Compile(statExcluded)
|
2017-11-21 06:39:13 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out, err := s.run(s.Binary, s.Timeout, s.UseSudo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("error gathering metrics: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process values
|
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
|
scanner := bufio.NewScanner(out)
|
|
|
|
|
for scanner.Scan() {
|
|
|
|
|
cols := strings.Split(scanner.Text(), "=")
|
|
|
|
|
|
|
|
|
|
// Check split correctness
|
|
|
|
|
if len(cols) != 2 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stat := cols[0]
|
|
|
|
|
value := cols[1]
|
|
|
|
|
|
|
|
|
|
// Filter value
|
2021-03-02 05:04:35 +08:00
|
|
|
if filterExcluded.Match(stat) {
|
2017-11-21 06:39:13 +08:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
field := strings.Replace(stat, ".", "_", -1)
|
|
|
|
|
|
|
|
|
|
fields[field], err = strconv.ParseFloat(value, 64)
|
|
|
|
|
if err != nil {
|
2021-02-09 00:18:40 +08:00
|
|
|
acc.AddError(fmt.Errorf("expected a numerical value for %s = %v", stat, value))
|
2017-11-21 06:39:13 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
acc.AddFields("opensmtpd", fields, nil)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
inputs.Add("opensmtpd", func() telegraf.Input {
|
|
|
|
|
return &Opensmtpd{
|
|
|
|
|
run: opensmtpdRunner,
|
|
|
|
|
Binary: defaultBinary,
|
|
|
|
|
Timeout: defaultTimeout,
|
|
|
|
|
UseSudo: false,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|