Fix go linter issues and simplify code
authorTobias Schmidt <tobidt@gmail.com>
Sat, 4 Mar 2017 03:35:45 +0000 (23:35 -0400)
committerTobias Schmidt <tobidt@gmail.com>
Sat, 4 Mar 2017 03:35:45 +0000 (23:35 -0400)
dns.go
dns_test.go
http.go
icmp.go
main.go
tcp.go
tcp_test.go

diff --git a/dns.go b/dns.go
index 8559064dd05b5d5842fa7e34504383c8365bcda9..ff4b4c4c5ea005acd0abc6ea36767c5e0d6dce41 100644 (file)
--- 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
                        }
index 39fea3983d1b69cb6a7233ba6490c84902082906..5468f2495ee83be4b4165805898f44ba5663ea0e 100644 (file)
@@ -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 fdf309888e860f755633dd7c9b9f398c3f7c4f0b..a101831c9e1d2c9c60df612d13bf87209db4b3bb 100644 (file)
--- 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 0d5124a7c1126502283e2995fcc4c6296681eb04..3f4db6290a91ae3011bd2bc34faa0f8df84a391c 100644 (file)
--- 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 5cd40e946be8ef8d9ee781e131a700e50f7b0277..009a98e25ae6c3296c8b603d30280248f9951ada 100644 (file)
--- 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 be1b7788c57eb737923cda841a36812f5968a271..ec9f099b9cdc4167c8403a4f876058626147805e 100644 (file)
--- 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
                        }
index e848832517e73efd315e21a26c29e4754428ddc3..73073936a910dcbda0a8c7b37ad1ca7a92e0e9b9 100644 (file)
@@ -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",
                },
        }