From b02aa8042657fd3282b1f32c4af31565814587bd Mon Sep 17 00:00:00 2001 From: Miki Date: Fri, 5 Feb 2016 17:58:21 +0100 Subject: [PATCH] added dovecot plugin --- plugins/inputs/all/all.go | 1 + plugins/inputs/dovecot/README.md | 54 ++++++++ plugins/inputs/dovecot/dovecot.go | 167 +++++++++++++++++++++++++ plugins/inputs/dovecot/dovecot_test.go | 58 +++++++++ 4 files changed, 280 insertions(+) create mode 100644 plugins/inputs/dovecot/README.md create mode 100644 plugins/inputs/dovecot/dovecot.go create mode 100644 plugins/inputs/dovecot/dovecot_test.go diff --git a/plugins/inputs/all/all.go b/plugins/inputs/all/all.go index 52ab428f8..c0d67025b 100644 --- a/plugins/inputs/all/all.go +++ b/plugins/inputs/all/all.go @@ -6,6 +6,7 @@ import ( _ "github.com/influxdata/telegraf/plugins/inputs/bcache" _ "github.com/influxdata/telegraf/plugins/inputs/disque" _ "github.com/influxdata/telegraf/plugins/inputs/docker" + _ "github.com/influxdata/telegraf/plugins/inputs/dovecot" _ "github.com/influxdata/telegraf/plugins/inputs/elasticsearch" _ "github.com/influxdata/telegraf/plugins/inputs/exec" _ "github.com/influxdata/telegraf/plugins/inputs/github_webhooks" diff --git a/plugins/inputs/dovecot/README.md b/plugins/inputs/dovecot/README.md new file mode 100644 index 000000000..e12c72fa5 --- /dev/null +++ b/plugins/inputs/dovecot/README.md @@ -0,0 +1,54 @@ +# Dovecot Input Plugin + +The dovecot plugin uses the dovecot Stats protocol to gather metrics on configured +domains. You can read Dovecot's documentation +[here](http://wiki2.dovecot.org/Statistics) + +### Configuration: + +``` +# Read metrics about docker containers +[[inputs.dovecot]] + # Dovecot Endpoint + # To use TCP, set endpoint = "tcp://[ip]:[port]" + # To use environment variables (ie, docker-machine), set endpoint = "ENV" + endpoint = "unix:///var/run/docker.sock" + # Only collect metrics for these domains, collect all if empty + domains_names = [] + +``` + +### Fields: + + domain string + reset_timestamp time.Time + last_update time.Time + num_logins int + num_cmds int + num_connected_sessions int + user_cpu float32 + sys_cpu float32 + clock_time float64 + min_faults int + maj_faults int + vol_cs int + invol_cs int + disk_input int + disk_output int + read_count int + read_bytes int + write_count int + write_bytes int + mail_lookup_path int + mail_lookup_attr int + mail_read_count int + mail_read_bytes int + mail_cache_hits int + +### Example Output: + +``` +% ./telegraf -config ~/ws/telegraf.conf -input-filter docker -test +* Plugin: docker, Collection 1 + +``` \ No newline at end of file diff --git a/plugins/inputs/dovecot/dovecot.go b/plugins/inputs/dovecot/dovecot.go new file mode 100644 index 000000000..0d4606b3a --- /dev/null +++ b/plugins/inputs/dovecot/dovecot.go @@ -0,0 +1,167 @@ +package dovecot + +import ( + "bytes" + "fmt" + "io" + "net" + "strconv" + "strings" + "sync" + "time" + + "github.com/influxdata/telegraf/plugins/inputs" +) + +type Dovecot struct { + Servers []string + Domains []string +} + +func (d *Dovecot) Description() string { + return "Read statistics from one or many dovecot servers" +} + +var sampleConfig = ` + # specify dovecot servers via a url matching: + # address:port + # e.g. + # localhost:24242 + # + # If no servers are specified, then localhost is used as the host. + servers = ["localhost:24242"] + # Only collect metrics for these domains, collect all if empty + domains = [] +` + +func (d *Dovecot) SampleConfig() string { return sampleConfig } + +const defaultPort = "24242" + +// Reads stats from all configured servers. +func (d *Dovecot) Gather(acc inputs.Accumulator) error { + + if len(d.Servers) == 0 { + d.Servers = append(d.Servers, "127.0.0.1:24242") + } + + var wg sync.WaitGroup + + var outerr error + + var domains = make(map[string]bool) + + for _, dom := range d.Domains { + domains[dom] = true + } + + for _, serv := range d.Servers { + wg.Add(1) + go func(serv string) { + defer wg.Done() + outerr = d.gatherServer(serv, acc, domains) + }(serv) + } + + wg.Wait() + + return outerr +} + +func (d *Dovecot) gatherServer(addr string, acc inputs.Accumulator, doms map[string]bool) error { + _, _, err := net.SplitHostPort(addr) + if err != nil { + return fmt.Errorf("Error: %s on url %s\n", err, addr) + } + + c, err := net.Dial("tcp", addr) + if err != nil { + return fmt.Errorf("Unable to connect to dovecot server '%s': %s", addr, err) + } + defer c.Close() + + c.Write([]byte("EXPORT\tdomain\n\n")) + var buf bytes.Buffer + io.Copy(&buf, c) + // buf := bufio.NewReader(c) + + host, _, _ := net.SplitHostPort(addr) + + return gatherStats(&buf, acc, doms, host) +} + +func gatherStats(buf *bytes.Buffer, acc inputs.Accumulator, doms map[string]bool, host string) error { + + lines := strings.Split(buf.String(), "\n") + head := strings.Split(lines[0], "\t") + vals := lines[1:] + + for i := range vals { + if vals[i] == "" { + continue + } + val := strings.Split(vals[i], "\t") + fields := make(map[string]interface{}) + if len(doms) > 0 && !doms[val[0]] { + continue + } + tags := map[string]string{"server": host, "domain": val[0]} + for n := range val { + fmt.Println(n, head[n], val[n]) + switch head[n] { + case "domain": + fields[head[n]] = val[n] + case "user_cpu", "sys_cpu", "clock_time": + fields[head[n]] = secParser(val[n]) + case "reset_timestamp", "last_update": + fields[head[n]] = timeParser(val[n]) + default: + ival, _ := splitSec(val[n]) + fields[head[n]] = ival + } + } + + acc.AddFields("dovecot", fields, tags) + } + + return nil +} + +func splitSec(tm string) (sec int64, msec int64) { + var err error + ss := strings.Split(tm, ".") + + sec, err = strconv.ParseInt(ss[0], 10, 64) + if err != nil { + sec = 0 + } + if len(ss) > 1 { + msec, err = strconv.ParseInt(ss[1], 10, 64) + if err != nil { + msec = 0 + } + } else { + msec = 0 + } + + return sec, msec +} + +func timeParser(tm string) time.Time { + + sec, msec := splitSec(tm) + fmt.Println("time: ", sec, msec) + return time.Unix(sec, msec) +} + +func secParser(tm string) float64 { + + sec, msec := splitSec(tm) + return float64(sec) + (float64(msec) / 1000000.0) +} + +func init() { + inputs.Add("dovecot", func() inputs.Input { + return &Dovecot{} + }) +} diff --git a/plugins/inputs/dovecot/dovecot_test.go b/plugins/inputs/dovecot/dovecot_test.go new file mode 100644 index 000000000..36855cea2 --- /dev/null +++ b/plugins/inputs/dovecot/dovecot_test.go @@ -0,0 +1,58 @@ +package dovecot + +import ( + "bytes" + "testing" + "time" + + "github.com/influxdata/telegraf/testutil" + "github.com/stretchr/testify/require" +) + +func TestDovecot(t *testing.T) { + + var acc testutil.Accumulator + tags := map[string]string{"server": "dovecot.test", "domain": "domain.test"} + buf := bytes.NewBufferString(sampleStats) + + var doms = map[string]bool{ + "domain.test": true, + } + + err := gatherStats(buf, &acc, doms, "dovecot.test") + require.NoError(t, err) + + fields := map[string]interface{}{ + "domain": "domain.test", + "reset_timestamp": time.Unix(1453969886, 0), + "last_update": time.Unix(1454603963, 39864), + "num_logins": int64(7503897), + "num_cmds": int64(52595715), + "num_connected_sessions": int64(1204), + "user_cpu": 1.00831175372e+08, + "sys_cpu": 8.3849071112e+07, + "clock_time": 4.3260019315281835e+15, + "min_faults": int64(763950011), + "maj_faults": int64(1112443), + "vol_cs": int64(4120386897), + "invol_cs": int64(3685239306), + "disk_input": int64(41679480946688), + "disk_output": int64(1819070669176832), + "read_count": int64(2368906465), + "read_bytes": int64(2957928122981169), + "write_count": int64(3545389615), + "write_bytes": int64(1666822498251286), + "mail_lookup_path": int64(24396105), + "mail_lookup_attr": int64(302845), + "mail_read_count": int64(20155768), + "mail_read_bytes": int64(669946617705), + "mail_cache_hits": int64(1557255080), + } + + acc.AssertContainsTaggedFields(t, "dovecot", fields, tags) + +} + +const sampleStats = `domain reset_timestamp last_update num_logins num_cmds num_connected_sessions user_cpu sys_cpu clock_time min_faults maj_faults vol_cs invol_cs disk_input disk_output read_count read_bytes write_count write_bytes mail_lookup_path mail_lookup_attr mail_read_count mail_read_bytes mail_cache_hits +domain.bad 1453970076 1454603947.383029 10749 33828 0 177988.524000 148071.772000 7531838964717.193706 212491179 2125 2190386067 112779200 74487934976 3221808119808 2469948401 5237602841760 1091171292 2951966459802 15363 0 2922 136403379 334372 +domain.test 1453969886 1454603963.039864 7503897 52595715 1204 100831175.372000 83849071.112000 4326001931528183.495762 763950011 1112443 4120386897 3685239306 41679480946688 1819070669176832 2368906465 2957928122981169 3545389615 1666822498251286 24396105 302845 20155768 669946617705 1557255080`