Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds simple multicast support to socket_listener plugin #5388

Merged
merged 3 commits into from
Feb 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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 := net.ListenPacket(protocol, addr)
pc, err := udpListen(protocol, addr)
if err != nil {
return err
}
Expand Down Expand Up @@ -317,6 +317,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)
sgtsquiggs marked this conversation as resolved.
Show resolved Hide resolved
}

func (sl *SocketListener) Stop() {
if sl.Closer != nil {
sl.Close()
Expand Down