From 2a62f0b01ed5a0d7322746b992572a85bdb01255 Mon Sep 17 00:00:00 2001 From: Josef Johansson Date: Thu, 29 Apr 2021 07:49:24 +0200 Subject: [PATCH] plugins/inputs/dovecot: Add support for unix domain sockets It's safer for dovecot to export metrics via a UDS instead of tcp port, this will add support for that option. --- plugins/inputs/dovecot/README.md | 3 +++ plugins/inputs/dovecot/dovecot.go | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/plugins/inputs/dovecot/README.md b/plugins/inputs/dovecot/README.md index 3b6129488dae3..9e44d99edbc07 100644 --- a/plugins/inputs/dovecot/README.md +++ b/plugins/inputs/dovecot/README.md @@ -14,6 +14,9 @@ the [upgrading steps][upgrading]. ## specify dovecot servers via an address:port list ## e.g. ## localhost:24242 + ## or as an UDS socket + ## e.g. + ## /var/run/dovecot/old-stats ## ## If no servers are specified, then localhost is used as the host. servers = ["localhost:24242"] diff --git a/plugins/inputs/dovecot/dovecot.go b/plugins/inputs/dovecot/dovecot.go index 94c941655ccc8..ec93ec51701cc 100644 --- a/plugins/inputs/dovecot/dovecot.go +++ b/plugins/inputs/dovecot/dovecot.go @@ -78,12 +78,20 @@ func (d *Dovecot) Gather(acc telegraf.Accumulator) error { } func (d *Dovecot) gatherServer(addr string, acc telegraf.Accumulator, qtype string, filter string) error { - _, _, err := net.SplitHostPort(addr) - if err != nil { - return fmt.Errorf("%q on url %s", err.Error(), addr) - } + var proto string + + if strings.HasPrefix(addr, "/") { + proto = "unix" + } else { + proto = "tcp" - c, err := net.DialTimeout("tcp", addr, defaultTimeout) + _, _, err := net.SplitHostPort(addr) + if err != nil { + return fmt.Errorf("%q on url %s", err.Error(), addr) + } + } + + c, err := net.DialTimeout(proto, addr, defaultTimeout) if err != nil { return fmt.Errorf("enable to connect to dovecot server '%s': %s", addr, err) }