From 41157a3b7b95f60aa7630136155b2881c3bbefe5 Mon Sep 17 00:00:00 2001 From: Tobias Schmidt Date: Fri, 3 Mar 2017 23:35:45 -0400 Subject: [PATCH] Fix go linter issues and simplify code --- dns.go | 12 ++++++------ dns_test.go | 4 ++-- http.go | 8 ++++---- icmp.go | 14 +++++++------- main.go | 38 ++++++++++++++++++-------------------- tcp.go | 12 ++++++------ tcp_test.go | 12 ++++++------ 7 files changed, 49 insertions(+), 51 deletions(-) diff --git a/dns.go b/dns.go index 8559064..ff4b4c4 100644 --- a/dns.go +++ b/dns.go @@ -96,10 +96,10 @@ func probeDNS(target string, w http.ResponseWriter, module Module) bool { module.DNS.Protocol = "udp" } - if (module.DNS.Protocol == "tcp" || module.DNS.Protocol == "udp") && module.DNS.PreferredIpProtocol == "" { - module.DNS.PreferredIpProtocol = "ip6" + if (module.DNS.Protocol == "tcp" || module.DNS.Protocol == "udp") && module.DNS.PreferredIPProtocol == "" { + module.DNS.PreferredIPProtocol = "ip6" } - if module.DNS.PreferredIpProtocol == "ip6" { + if module.DNS.PreferredIPProtocol == "ip6" { fallbackProtocol = "ip4" } else { fallbackProtocol = "ip6" @@ -107,10 +107,10 @@ func probeDNS(target string, w http.ResponseWriter, module Module) bool { dialProtocol = module.DNS.Protocol if module.DNS.Protocol == "udp" || module.DNS.Protocol == "tcp" { - target_address, _, _ := net.SplitHostPort(target) - ip, err := net.ResolveIPAddr(module.DNS.PreferredIpProtocol, target_address) + targetAddress, _, _ := net.SplitHostPort(target) + ip, err := net.ResolveIPAddr(module.DNS.PreferredIPProtocol, targetAddress) if err != nil { - ip, err = net.ResolveIPAddr(fallbackProtocol, target_address) + ip, err = net.ResolveIPAddr(fallbackProtocol, targetAddress) if err != nil { return false } diff --git a/dns_test.go b/dns_test.go index 39fea39..5468f24 100644 --- a/dns_test.go +++ b/dns_test.go @@ -380,7 +380,7 @@ func TestDNSProtocol(t *testing.T) { DNS: DNSProbe{ QueryName: "example.com", Protocol: protocol, - PreferredIpProtocol: "ip6", + PreferredIPProtocol: "ip6", }, } recorder = httptest.NewRecorder() @@ -399,7 +399,7 @@ func TestDNSProtocol(t *testing.T) { DNS: DNSProbe{ QueryName: "example.com", Protocol: protocol, - PreferredIpProtocol: "ip4", + PreferredIPProtocol: "ip4", }, } recorder = httptest.NewRecorder() diff --git a/http.go b/http.go index fdf3098..a101831 100644 --- a/http.go +++ b/http.go @@ -66,10 +66,10 @@ func probeHTTP(target string, w http.ResponseWriter, module Module) (success boo module.HTTP.Protocol = "tcp" } - if module.HTTP.Protocol == "tcp" && module.HTTP.PreferredIpProtocol == "" { - module.HTTP.PreferredIpProtocol = "ip6" + if module.HTTP.Protocol == "tcp" && module.HTTP.PreferredIPProtocol == "" { + module.HTTP.PreferredIPProtocol = "ip6" } - if module.HTTP.PreferredIpProtocol == "ip6" { + if module.HTTP.PreferredIPProtocol == "ip6" { fallbackProtocol = "ip4" } else { fallbackProtocol = "ip6" @@ -89,7 +89,7 @@ func probeHTTP(target string, w http.ResponseWriter, module Module) (success boo if err != nil { targetHost = targetURL.Host } - ip, err := net.ResolveIPAddr(module.HTTP.PreferredIpProtocol, targetHost) + ip, err := net.ResolveIPAddr(module.HTTP.PreferredIPProtocol, targetHost) if err != nil { ip, err = net.ResolveIPAddr(fallbackProtocol, targetHost) if err != nil { diff --git a/icmp.go b/icmp.go index 0d5124a..3f4db62 100644 --- a/icmp.go +++ b/icmp.go @@ -57,28 +57,28 @@ func probeICMP(target string, w http.ResponseWriter, module Module) (success boo } // In case of ICMP prefer IPv6 by default - if module.ICMP.Protocol == "icmp" && module.ICMP.PreferredIpProtocol == "" { - module.ICMP.PreferredIpProtocol = "ip6" + if module.ICMP.Protocol == "icmp" && module.ICMP.PreferredIPProtocol == "" { + module.ICMP.PreferredIPProtocol = "ip6" } if module.ICMP.Protocol == "icmp4" { - module.ICMP.PreferredIpProtocol = "ip4" + module.ICMP.PreferredIPProtocol = "ip4" fallbackProtocol = "" } else if module.ICMP.Protocol == "icmp6" { - module.ICMP.PreferredIpProtocol = "ip6" + module.ICMP.PreferredIPProtocol = "ip6" fallbackProtocol = "" - } else if module.ICMP.PreferredIpProtocol == "ip6" { + } else if module.ICMP.PreferredIPProtocol == "ip6" { fallbackProtocol = "ip4" } else { fallbackProtocol = "ip6" } resolveStart := time.Now() - ip, err := net.ResolveIPAddr(module.ICMP.PreferredIpProtocol, target) + ip, err := net.ResolveIPAddr(module.ICMP.PreferredIPProtocol, target) if err != nil && fallbackProtocol != "" { ip, err = net.ResolveIPAddr(fallbackProtocol, target) } - fmt.Fprintf(w, "probe_dns_lookup_time_seconds %f\n", float64(time.Since(resolveStart).Seconds())) + 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) diff --git a/main.go b/main.go index 5cd40e9..009a98e 100644 --- a/main.go +++ b/main.go @@ -29,12 +29,6 @@ import ( "github.com/prometheus/common/version" ) -var ( - configFile = flag.String("config.file", "blackbox.yml", "Blackbox exporter configuration file.") - listenAddress = flag.String("web.listen-address", ":9115", "The address to listen on for HTTP requests.") - showVersion = flag.Bool("version", false, "Print version information.") -) - type Config struct { Modules map[string]Module `yaml:"modules"` } @@ -60,7 +54,7 @@ type HTTPProbe struct { FailIfNotMatchesRegexp []string `yaml:"fail_if_not_matches_regexp"` TLSConfig config.TLSConfig `yaml:"tls_config"` Protocol string `yaml:"protocol"` // Defaults to "tcp". - PreferredIpProtocol string `yaml:"preferred_ip_protocol"` // Defaults to "ip6". + PreferredIPProtocol string `yaml:"preferred_ip_protocol"` // Defaults to "ip6". Body string `yaml:"body"` } @@ -74,12 +68,12 @@ type TCPProbe struct { TLS bool `yaml:"tls"` TLSConfig config.TLSConfig `yaml:"tls_config"` Protocol string `yaml:"protocol"` // Defaults to "tcp". - PreferredIpProtocol string `yaml:"preferred_ip_protocol"` // Defaults to "ip6". + PreferredIPProtocol string `yaml:"preferred_ip_protocol"` // Defaults to "ip6". } type ICMPProbe struct { Protocol string `yaml:"protocol"` // Defaults to "icmp4". - PreferredIpProtocol string `yaml:"preferred_ip_protocol"` // Defaults to "ip6". + PreferredIPProtocol string `yaml:"preferred_ip_protocol"` // Defaults to "ip6". } type DNSProbe struct { @@ -90,7 +84,7 @@ type DNSProbe struct { ValidateAnswer DNSRRValidator `yaml:"validate_answer_rrs"` ValidateAuthority DNSRRValidator `yaml:"validate_authority_rrs"` ValidateAdditional DNSRRValidator `yaml:"validate_additional_rrs"` - PreferredIpProtocol string `yaml:"preferred_ip_protocol"` // Defaults to "ip6". + PreferredIPProtocol string `yaml:"preferred_ip_protocol"` // Defaults to "ip6". } type DNSRRValidator struct { @@ -108,31 +102,33 @@ var Probers = map[string]func(string, http.ResponseWriter, Module) bool{ func probeHandler(w http.ResponseWriter, r *http.Request, config *Config) { params := r.URL.Query() target := params.Get("target") - moduleName := params.Get("module") if target == "" { http.Error(w, "Target parameter is missing", 400) return } + + moduleName := params.Get("module") if moduleName == "" { moduleName = "http_2xx" } module, ok := config.Modules[moduleName] if !ok { - http.Error(w, fmt.Sprintf("Unknown module %s", moduleName), 400) + http.Error(w, fmt.Sprintf("Unknown module %q", moduleName), 400) return } prober, ok := Probers[module.Prober] if !ok { - http.Error(w, fmt.Sprintf("Unknown prober %s", module.Prober), 400) + http.Error(w, fmt.Sprintf("Unknown prober %q", module.Prober), 400) return } + start := time.Now() success := prober(target, w, module) - fmt.Fprintf(w, "probe_duration_seconds %f\n", float64(time.Now().Sub(start))/1e9) + fmt.Fprintf(w, "probe_duration_seconds %f\n", time.Since(start).Seconds()) if success { - fmt.Fprintf(w, "probe_success %d\n", 1) + fmt.Fprintln(w, "probe_success 1") } else { - fmt.Fprintf(w, "probe_success %d\n", 0) + fmt.Fprintln(w, "probe_success 0") } } @@ -141,6 +137,11 @@ func init() { } func main() { + var ( + configFile = flag.String("config.file", "blackbox.yml", "Blackbox exporter configuration file.") + listenAddress = flag.String("web.listen-address", ":9115", "The address to listen on for HTTP requests.") + showVersion = flag.Bool("version", false, "Print version information.") + ) flag.Parse() if *showVersion { @@ -152,15 +153,12 @@ func main() { log.Infoln("Build context", version.BuildContext()) yamlFile, err := ioutil.ReadFile(*configFile) - if err != nil { log.Fatalf("Error reading config file: %s", err) } config := Config{} - - err = yaml.Unmarshal(yamlFile, &config) - if err != nil { + if err := yaml.Unmarshal(yamlFile, &config); err != nil { log.Fatalf("Error parsing config file: %s", err) } diff --git a/tcp.go b/tcp.go index be1b778..ec9f099 100644 --- a/tcp.go +++ b/tcp.go @@ -32,10 +32,10 @@ func dialTCP(target string, w http.ResponseWriter, module Module) (net.Conn, err if module.TCP.Protocol == "" { module.TCP.Protocol = "tcp" } - if module.TCP.Protocol == "tcp" && module.TCP.PreferredIpProtocol == "" { - module.TCP.PreferredIpProtocol = "ip6" + if module.TCP.Protocol == "tcp" && module.TCP.PreferredIPProtocol == "" { + module.TCP.PreferredIPProtocol = "ip6" } - if module.TCP.PreferredIpProtocol == "ip6" { + if module.TCP.PreferredIPProtocol == "ip6" { fallbackProtocol = "ip4" } else { fallbackProtocol = "ip6" @@ -43,10 +43,10 @@ func dialTCP(target string, w http.ResponseWriter, module Module) (net.Conn, err dialProtocol = module.TCP.Protocol if module.TCP.Protocol == "tcp" { - target_address, _, err := net.SplitHostPort(target) - ip, err := net.ResolveIPAddr(module.TCP.PreferredIpProtocol, target_address) + targetAddress, _, err := net.SplitHostPort(target) + ip, err := net.ResolveIPAddr(module.TCP.PreferredIPProtocol, targetAddress) if err != nil { - ip, err = net.ResolveIPAddr(fallbackProtocol, target_address) + ip, err = net.ResolveIPAddr(fallbackProtocol, targetAddress) if err != nil { return nil, err } diff --git a/tcp_test.go b/tcp_test.go index e848832..7307393 100644 --- a/tcp_test.go +++ b/tcp_test.go @@ -65,9 +65,9 @@ func TestTCPConnectionQueryResponseIRC(t *testing.T) { Timeout: time.Second, TCP: TCPProbe{ QueryResponse: []QueryResponse{ - QueryResponse{Send: "NICK prober"}, - QueryResponse{Send: "USER prober prober prober :prober"}, - QueryResponse{Expect: "^:[^ ]+ 001"}, + {Send: "NICK prober"}, + {Send: "USER prober prober prober :prober"}, + {Expect: "^:[^ ]+ 001"}, }, }, } @@ -122,7 +122,7 @@ func TestTCPConnectionQueryResponseMatching(t *testing.T) { Timeout: time.Second, TCP: TCPProbe{ QueryResponse: []QueryResponse{ - QueryResponse{ + { Expect: "SSH-2.0-(OpenSSH_6.9p1) Debian-2", Send: "CONFIRM ${1}", }, @@ -213,7 +213,7 @@ func TestTCPConnectionProtocol(t *testing.T) { Timeout: time.Second, TCP: TCPProbe{ Protocol: "tcp", - PreferredIpProtocol: "ip4", + PreferredIPProtocol: "ip4", }, } @@ -232,7 +232,7 @@ func TestTCPConnectionProtocol(t *testing.T) { Timeout: time.Second, TCP: TCPProbe{ Protocol: "tcp", - PreferredIpProtocol: "ip6", + PreferredIPProtocol: "ip6", }, } -- 2.25.1