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"
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
}
DNS: DNSProbe{
QueryName: "example.com",
Protocol: protocol,
- PreferredIpProtocol: "ip6",
+ PreferredIPProtocol: "ip6",
},
}
recorder = httptest.NewRecorder()
DNS: DNSProbe{
QueryName: "example.com",
Protocol: protocol,
- PreferredIpProtocol: "ip4",
+ PreferredIPProtocol: "ip4",
},
}
recorder = httptest.NewRecorder()
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"
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 {
}
// 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)
"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"`
}
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"`
}
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 {
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 {
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")
}
}
}
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 {
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)
}
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"
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
}
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"},
},
},
}
Timeout: time.Second,
TCP: TCPProbe{
QueryResponse: []QueryResponse{
- QueryResponse{
+ {
Expect: "SSH-2.0-(OpenSSH_6.9p1) Debian-2",
Send: "CONFIRM ${1}",
},
Timeout: time.Second,
TCP: TCPProbe{
Protocol: "tcp",
- PreferredIpProtocol: "ip4",
+ PreferredIPProtocol: "ip4",
},
}
Timeout: time.Second,
TCP: TCPProbe{
Protocol: "tcp",
- PreferredIpProtocol: "ip6",
+ PreferredIPProtocol: "ip6",
},
}