From bc088967b756af16ada19190b1da8accd6a04213 Mon Sep 17 00:00:00 2001 From: Tobias Schmidt Date: Sat, 4 Mar 2017 10:55:15 -0400 Subject: [PATCH] Use warn level for log messages about target communication errors --- dns.go | 4 ++-- http.go | 4 ++-- icmp.go | 26 +++++++++----------------- tcp.go | 4 ++-- 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/dns.go b/dns.go index ff4b4c4..90c8982 100644 --- a/dns.go +++ b/dns.go @@ -124,9 +124,9 @@ func probeDNS(target string, w http.ResponseWriter, module Module) bool { } if dialProtocol[len(dialProtocol)-1] == '6' { - fmt.Fprintf(w, "probe_ip_protocol 6\n") + fmt.Fprintln(w, "probe_ip_protocol 6") } else { - fmt.Fprintf(w, "probe_ip_protocol 4\n") + fmt.Fprintln(w, "probe_ip_protocol 4") } client := new(dns.Client) diff --git a/http.go b/http.go index a101831..d57b455 100644 --- a/http.go +++ b/http.go @@ -105,9 +105,9 @@ func probeHTTP(target string, w http.ResponseWriter, module Module) (success boo } if dialProtocol == "tcp6" { - fmt.Fprintf(w, "probe_ip_protocol 6\n") + fmt.Fprintln(w, "probe_ip_protocol 6") } else { - fmt.Fprintf(w, "probe_ip_protocol 4\n") + fmt.Fprintln(w, "probe_ip_protocol 4") } client := &http.Client{ diff --git a/icmp.go b/icmp.go index 3f4db62..60ae926 100644 --- a/icmp.go +++ b/icmp.go @@ -81,7 +81,7 @@ func probeICMP(target string, w http.ResponseWriter, module Module) (success boo fmt.Fprintf(w, "probe_dns_lookup_time_seconds %f\n", time.Since(resolveStart).Seconds()) if err != nil { - log.Errorf("Error resolving address %s: %s", target, err) + log.Warnf("Error resolving address %s: %s", target, err) return } @@ -89,33 +89,26 @@ func probeICMP(target string, w http.ResponseWriter, module Module) (success boo requestType = ipv6.ICMPTypeEchoRequest replyType = ipv6.ICMPTypeEchoReply socket, err = icmp.ListenPacket("ip6:ipv6-icmp", "::") - fmt.Fprintf(w, "probe_ip_protocol 6\n") + fmt.Fprintln(w, "probe_ip_protocol 6") } else { requestType = ipv4.ICMPTypeEcho replyType = ipv4.ICMPTypeEchoReply socket, err = icmp.ListenPacket("ip4:icmp", "0.0.0.0") - fmt.Fprintf(w, "probe_ip_protocol 4\n") + fmt.Fprintln(w, "probe_ip_protocol 4") } if err != nil { - log.Errorf("Error listening to socket: %s", err) + log.Errorf("Error listening to socket for %s: %s", target, err) return } defer socket.Close() - if err != nil { - log.Errorf("Error resolving address %s: %s", target, err) - return - } - - seq := getICMPSequence() - pid := os.Getpid() & 0xffff - wm := icmp.Message{ Type: requestType, Code: 0, Body: &icmp.Echo{ - ID: pid, Seq: int(seq), + ID: os.Getpid() & 0xffff, + Seq: int(getICMPSequence()), Data: []byte("Prometheus Blackbox Exporter"), }, } @@ -126,7 +119,7 @@ func probeICMP(target string, w http.ResponseWriter, module Module) (success boo return } if _, err := socket.WriteTo(wb, ip); err != nil { - log.Errorf("Error writing to socket for %s: %s", target, err) + log.Warnf("Error writing to socket for %s: %s", target, err) return } @@ -147,7 +140,7 @@ func probeICMP(target string, w http.ResponseWriter, module Module) (success boo n, peer, err := socket.ReadFrom(rb) if err != nil { if nerr, ok := err.(net.Error); ok && nerr.Timeout() { - log.Infof("Timeout reading from socket for %s: %s", target, err) + log.Warnf("Timeout reading from socket for %s: %s", target, err) return } log.Errorf("Error reading from socket for %s: %s", target, err) @@ -162,8 +155,7 @@ func probeICMP(target string, w http.ResponseWriter, module Module) (success boo rb[3] = 0 } if bytes.Compare(rb[:n], wb) == 0 { - success = true - return + return true } } } diff --git a/tcp.go b/tcp.go index ec9f099..ae27254 100644 --- a/tcp.go +++ b/tcp.go @@ -60,9 +60,9 @@ func dialTCP(target string, w http.ResponseWriter, module Module) (net.Conn, err } if dialProtocol == "tcp6" { - fmt.Fprintf(w, "probe_ip_protocol 6\n") + fmt.Fprintln(w, "probe_ip_protocol 6") } else { - fmt.Fprintf(w, "probe_ip_protocol 4\n") + fmt.Fprintln(w, "probe_ip_protocol 4") } if !module.TCP.TLS { -- 2.25.1