Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Commit

Permalink
Catch DNS resolution errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Mar 26, 2017
1 parent 82a8954 commit 5bf1f40
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
25 changes: 11 additions & 14 deletions beater/pingbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"errors"
"fmt"
// "github.com/davecgh/go-spew/spew"
"net"
"os"
"time"

"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
Expand All @@ -13,9 +17,6 @@ import (
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
"gopkg.in/go-playground/pool.v3"
"net"
"os"
"time"
)

type Pingbeat struct {
Expand Down Expand Up @@ -52,7 +53,7 @@ func New(b *beat.Beat, cfg *common.Config) (beat.Beater, error) {
// Use privileged (i.e. raw socket) ping by default, else use a UDP ping
if bt.config.Privileged {
if os.Getuid() != 0 {
return nil, fmt.Errorf("Privileged specified but not running with privileges!")
return nil, fmt.Errorf("privileged specified but not running with privileges!")
}
bt.ipv4network = "ip4:icmp"
bt.ipv6network = "ip6:ipv6-icmp"
Expand Down Expand Up @@ -119,7 +120,6 @@ func (bt *Pingbeat) Run(b *beat.Beat) error {
go func(*icmp.PacketConn, *icmp.PacketConn) {
for ip, target := range bt.targets {
if net.ParseIP(ip).To4() != nil {
logp.Debug("pingbeat", "Sending a ping")
sendBatch.Queue(SendPing(ipv4conn, bt.config.Timeout, state.GetSeqNo(), target.Addr))
} else {
sendBatch.Queue(SendPing(ipv6conn, bt.config.Timeout, state.GetSeqNo(), target.Addr))
Expand Down Expand Up @@ -177,7 +177,6 @@ func RecvPings(bt *Pingbeat, state *PingState, conn *icmp.PacketConn) {
binary := make([]byte, 1500)
n, peer, err := conn.ReadFrom(binary)
if err != nil {
binary = nil
logp.Err("Couldn't read from connection: %v", err)
continue
}
Expand Down Expand Up @@ -297,13 +296,12 @@ func SendPing(conn *icmp.PacketConn, timeout time.Duration, seq int, addr net.Ad
// Send the request and if successful, set a read deadline for the connection
if _, err := conn.WriteTo(binary, addr); err != nil {
return ping, err
} else {
if err := conn.SetReadDeadline(time.Now().Add(timeout)); err != nil {
return ping, err
}
ping.Sent = time.Now().UTC()
return ping, nil
}
if err := conn.SetReadDeadline(time.Now().Add(timeout)); err != nil {
return ping, err
}
ping.Sent = time.Now().UTC()
return ping, nil
}
}

Expand Down Expand Up @@ -360,9 +358,8 @@ func createConn(n string, a string) (*icmp.PacketConn, error) {
c, err := icmp.ListenPacket(n, a)
if err != nil {
return nil, err
} else {
return c, nil
}
return c, nil
}

// milliSeconds converts seconds to milliseconds
Expand Down
2 changes: 1 addition & 1 deletion beater/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func AddTarget(target *targetConfig, privileged bool, ipv4 bool, ipv6 bool) pool
addrs, err := net.LookupIP(t.Name)
if err != nil {
err := errors.New(t.Name)
return nil, err
return t, err
}
for j := 0; j < len(addrs); j++ {
// If we have an IPv4 address and we aren't using IPv4, ignore
Expand Down

0 comments on commit 5bf1f40

Please sign in to comment.