From cc47382da0c4b3c889904a5b98410fddd1c668ad Mon Sep 17 00:00:00 2001 From: Craig Wickesser Date: Mon, 16 Oct 2017 17:18:36 -0400 Subject: [PATCH] Add UDP IPv6 support to statsd input (#3344) --- plugins/inputs/statsd/README.md | 2 +- plugins/inputs/statsd/statsd.go | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/plugins/inputs/statsd/README.md b/plugins/inputs/statsd/README.md index 69d77580dd4b0..9562b93626e31 100644 --- a/plugins/inputs/statsd/README.md +++ b/plugins/inputs/statsd/README.md @@ -5,7 +5,7 @@ ```toml # Statsd Server [[inputs.statsd]] - ## Protocol, must be "tcp" or "udp" (default=udp) + ## Protocol, must be "tcp", "udp4", "udp6" or "udp" (default=udp) protocol = "udp" ## MaxTCPConnection - applicable when protocol is set to tcp (default=250) diff --git a/plugins/inputs/statsd/statsd.go b/plugins/inputs/statsd/statsd.go index db412b549bacb..93819cb09d0fd 100644 --- a/plugins/inputs/statsd/statsd.go +++ b/plugins/inputs/statsd/statsd.go @@ -171,7 +171,7 @@ func (_ *Statsd) Description() string { } const sampleConfig = ` - ## Protocol, must be "tcp" or "udp" (default=udp) + ## Protocol, must be "tcp", "udp", "udp4" or "udp6" (default=udp) protocol = "udp" ## MaxTCPConnection - applicable when protocol is set to tcp (default=250) @@ -327,10 +327,9 @@ func (s *Statsd) Start(_ telegraf.Accumulator) error { s.wg.Add(2) // Start the UDP listener - switch s.Protocol { - case "udp": + if s.isUDP() { go s.udpListen() - case "tcp": + } else { go s.tcpListen() } // Start the line parser @@ -382,8 +381,8 @@ func (s *Statsd) tcpListen() error { func (s *Statsd) udpListen() error { defer s.wg.Done() var err error - address, _ := net.ResolveUDPAddr("udp", s.ServiceAddress) - s.UDPlistener, err = net.ListenUDP("udp", address) + address, _ := net.ResolveUDPAddr(s.Protocol, s.ServiceAddress) + s.UDPlistener, err = net.ListenUDP(s.Protocol, address) if err != nil { log.Fatalf("ERROR: ListenUDP - %s", err) } @@ -825,10 +824,9 @@ func (s *Statsd) Stop() { s.Lock() log.Println("I! Stopping the statsd service") close(s.done) - switch s.Protocol { - case "udp": + if s.isUDP() { s.UDPlistener.Close() - case "tcp": + } else { s.TCPlistener.Close() // Close all open TCP connections // - get all conns from the s.conns map and put into slice @@ -843,8 +841,6 @@ func (s *Statsd) Stop() { for _, conn := range conns { conn.Close() } - default: - s.UDPlistener.Close() } s.Unlock() @@ -856,6 +852,11 @@ func (s *Statsd) Stop() { s.Unlock() } +// IsUDP returns true if the protocol is UDP, false otherwise. +func (s *Statsd) isUDP() bool { + return strings.HasPrefix(s.Protocol, "udp") +} + func init() { inputs.Add("statsd", func() telegraf.Input { return &Statsd{