From a2930db9530ec4a71f6a7373598ea285a43ffd82 Mon Sep 17 00:00:00 2001 From: Matthew Crenshaw Date: Wed, 6 Feb 2019 15:47:09 -0500 Subject: [PATCH 1/2] add simple multicast support to socket_listener plugin --- .../inputs/socket_listener/socket_listener.go | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/plugins/inputs/socket_listener/socket_listener.go b/plugins/inputs/socket_listener/socket_listener.go index c83f3eb68d2ca..e4986bb9d3bb9 100644 --- a/plugins/inputs/socket_listener/socket_listener.go +++ b/plugins/inputs/socket_listener/socket_listener.go @@ -114,7 +114,7 @@ func (ssl *streamSocketListener) read(c net.Conn) { metrics, err := ssl.Parse(scnr.Bytes()) if err != nil { ssl.AddError(fmt.Errorf("unable to parse incoming line: %s", err)) - //TODO rate limit + // TODO rate limit continue } for _, m := range metrics { @@ -150,7 +150,7 @@ func (psl *packetSocketListener) listen() { metrics, err := psl.Parse(buf[:n]) if err != nil { psl.AddError(fmt.Errorf("unable to parse incoming packet: %s", err)) - //TODO rate limit + // TODO rate limit continue } for _, m := range metrics { @@ -279,7 +279,7 @@ func (sl *SocketListener) Start(acc telegraf.Accumulator) error { sl.Closer = ssl go ssl.listen() case "udp", "udp4", "udp6", "ip", "ip4", "ip6", "unixgram": - pc, err := net.ListenPacket(spl[0], spl[1]) + pc, err := udpListen(spl[0], spl[1]) if err != nil { return err } @@ -310,6 +310,31 @@ func (sl *SocketListener) Start(acc telegraf.Accumulator) error { return nil } +func udpListen(network string, address string) (net.PacketConn, error) { + switch network { + case "udp", "udp4", "udp6": + var addr *net.UDPAddr + var err error + var ifi *net.Interface + if spl := strings.SplitN(address, "%", 2); len(spl) == 2 { + address = spl[0] + ifi, err = net.InterfaceByName(spl[1]) + if err != nil { + return nil, err + } + } + addr, err = net.ResolveUDPAddr(network, address) + if err != nil { + return nil, err + } + if addr.IP.IsMulticast() { + return net.ListenMulticastUDP(network, ifi, addr) + } + return net.ListenUDP(network, addr) + } + return net.ListenPacket(network, address) +} + func (sl *SocketListener) Stop() { if sl.Closer != nil { sl.Close() From feb5c87ee13c9863b063fd72284fd0f26f6812fa Mon Sep 17 00:00:00 2001 From: Matthew Crenshaw <3420325+sgtsquiggs@users.noreply.github.com> Date: Wed, 27 Feb 2019 12:36:10 -0500 Subject: [PATCH 2/2] Formatting --- plugins/inputs/socket_listener/socket_listener.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/inputs/socket_listener/socket_listener.go b/plugins/inputs/socket_listener/socket_listener.go index 108fa0ed83ad8..391427da9070e 100644 --- a/plugins/inputs/socket_listener/socket_listener.go +++ b/plugins/inputs/socket_listener/socket_listener.go @@ -284,7 +284,7 @@ func (sl *SocketListener) Start(acc telegraf.Accumulator) error { sl.Closer = ssl go ssl.listen() case "udp", "udp4", "udp6", "ip", "ip4", "ip6", "unixgram": - pc, err := udpListen(protocol, addr) + pc, err := udpListen(protocol, addr) if err != nil { return err }