2016-02-05 16:58:21 +00:00
|
|
|
package dovecot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2016-04-08 16:49:39 +00:00
|
|
|
// "log"
|
2016-02-05 16:58:21 +00:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf"
|
2016-07-19 13:03:28 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/errchan"
|
2016-02-05 16:58:21 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/inputs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Dovecot struct {
|
2016-04-08 16:49:39 +00:00
|
|
|
Type string
|
|
|
|
Filters []string
|
2016-02-05 16:58:21 +00:00
|
|
|
Servers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dovecot) Description() string {
|
|
|
|
return "Read statistics from one or many dovecot servers"
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## specify dovecot servers via an address:port list
|
|
|
|
## e.g.
|
|
|
|
## localhost:24242
|
|
|
|
##
|
|
|
|
## If no servers are specified, then localhost is used as the host.
|
2016-02-05 16:58:21 +00:00
|
|
|
servers = ["localhost:24242"]
|
2016-04-08 16:49:39 +00:00
|
|
|
## Type is one of "user", "domain", "ip", or "global"
|
|
|
|
type = "global"
|
|
|
|
## Wildcard matches like "*.com". An empty string "" is same as "*"
|
|
|
|
## If type = "ip" filters should be <IP/network>
|
|
|
|
filters = [""]
|
2016-02-05 16:58:21 +00:00
|
|
|
`
|
|
|
|
|
2016-02-29 16:52:58 +00:00
|
|
|
var defaultTimeout = time.Second * time.Duration(5)
|
|
|
|
|
2016-04-08 16:49:39 +00:00
|
|
|
var validQuery = map[string]bool{
|
|
|
|
"user": true, "domain": true, "global": true, "ip": true,
|
|
|
|
}
|
|
|
|
|
2016-02-05 16:58:21 +00:00
|
|
|
func (d *Dovecot) SampleConfig() string { return sampleConfig }
|
|
|
|
|
|
|
|
const defaultPort = "24242"
|
|
|
|
|
|
|
|
// Reads stats from all configured servers.
|
|
|
|
func (d *Dovecot) Gather(acc telegraf.Accumulator) error {
|
2016-04-08 16:49:39 +00:00
|
|
|
if !validQuery[d.Type] {
|
|
|
|
return fmt.Errorf("Error: %s is not a valid query type\n",
|
|
|
|
d.Type)
|
|
|
|
}
|
|
|
|
|
2016-02-05 16:58:21 +00:00
|
|
|
if len(d.Servers) == 0 {
|
|
|
|
d.Servers = append(d.Servers, "127.0.0.1:24242")
|
|
|
|
}
|
|
|
|
|
2016-04-08 16:49:39 +00:00
|
|
|
if len(d.Filters) <= 0 {
|
|
|
|
d.Filters = append(d.Filters, "")
|
2016-02-05 16:58:21 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 13:03:28 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
errChan := errchan.New(len(d.Servers) * len(d.Filters))
|
|
|
|
for _, server := range d.Servers {
|
2016-04-08 16:49:39 +00:00
|
|
|
for _, filter := range d.Filters {
|
|
|
|
wg.Add(1)
|
2016-07-19 13:03:28 +00:00
|
|
|
go func(s string, f string) {
|
2016-04-08 16:49:39 +00:00
|
|
|
defer wg.Done()
|
2016-07-19 13:03:28 +00:00
|
|
|
errChan.C <- d.gatherServer(s, acc, d.Type, f)
|
|
|
|
}(server, filter)
|
2016-04-08 16:49:39 +00:00
|
|
|
}
|
2016-02-05 16:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2016-07-19 13:03:28 +00:00
|
|
|
return errChan.Error()
|
2016-02-05 16:58:21 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 16:49:39 +00:00
|
|
|
func (d *Dovecot) gatherServer(addr string, acc telegraf.Accumulator, qtype string, filter string) error {
|
2016-02-05 16:58:21 +00:00
|
|
|
_, _, err := net.SplitHostPort(addr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error: %s on url %s\n", err, addr)
|
|
|
|
}
|
|
|
|
|
2016-02-29 16:52:58 +00:00
|
|
|
c, err := net.DialTimeout("tcp", addr, defaultTimeout)
|
2016-02-05 16:58:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Unable to connect to dovecot server '%s': %s", addr, err)
|
|
|
|
}
|
|
|
|
defer c.Close()
|
|
|
|
|
2016-02-29 16:52:58 +00:00
|
|
|
// Extend connection
|
|
|
|
c.SetDeadline(time.Now().Add(defaultTimeout))
|
|
|
|
|
2016-04-08 16:49:39 +00:00
|
|
|
msg := fmt.Sprintf("EXPORT\t%s", qtype)
|
|
|
|
if len(filter) > 0 {
|
|
|
|
msg += fmt.Sprintf("\t%s=%s", qtype, filter)
|
|
|
|
}
|
|
|
|
msg += "\n"
|
|
|
|
|
|
|
|
c.Write([]byte(msg))
|
2016-02-05 16:58:21 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, c)
|
|
|
|
|
|
|
|
host, _, _ := net.SplitHostPort(addr)
|
|
|
|
|
2016-04-08 16:49:39 +00:00
|
|
|
return gatherStats(&buf, acc, host, qtype)
|
2016-02-05 16:58:21 +00:00
|
|
|
}
|
|
|
|
|
2016-04-08 16:49:39 +00:00
|
|
|
func gatherStats(buf *bytes.Buffer, acc telegraf.Accumulator, host string, qtype string) error {
|
2016-02-05 16:58:21 +00:00
|
|
|
|
|
|
|
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")
|
2016-04-08 16:49:39 +00:00
|
|
|
|
2016-02-05 16:58:21 +00:00
|
|
|
fields := make(map[string]interface{})
|
2016-04-08 16:49:39 +00:00
|
|
|
tags := map[string]string{"server": host, "type": qtype}
|
|
|
|
|
|
|
|
if qtype != "global" {
|
|
|
|
tags[qtype] = val[0]
|
2016-02-05 16:58:21 +00:00
|
|
|
}
|
2016-04-08 16:49:39 +00:00
|
|
|
|
2016-02-05 16:58:21 +00:00
|
|
|
for n := range val {
|
|
|
|
switch head[n] {
|
2016-04-08 16:49:39 +00:00
|
|
|
case qtype:
|
2016-02-05 16:58:21 +00:00
|
|
|
continue
|
|
|
|
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)
|
|
|
|
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() telegraf.Input {
|
|
|
|
return &Dovecot{}
|
|
|
|
})
|
|
|
|
}
|