Skip to content

Commit

Permalink
add simple multicast support to socket_listener plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtsquiggs committed Feb 15, 2019
1 parent 0df92df commit a2930db
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions plugins/inputs/socket_listener/socket_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a2930db

Please sign in to comment.