github.com/google/cloudprober@v0.11.3/probes/dns/cmd/dns.go (about)

     1  // Copyright 2017 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Dns_bin implements a stand-alone dns prober binary using the
    16  // cloudprober/probes/dns package. It's intended to help prototype the dns package
    17  // quickly and doesn't provide the facilities that cloudprober provides.
    18  package main
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"io/ioutil"
    24  	"net"
    25  	"time"
    26  
    27  	"flag"
    28  	"github.com/golang/glog"
    29  	"github.com/golang/protobuf/proto"
    30  	"github.com/google/cloudprober/metrics"
    31  	"github.com/google/cloudprober/probes/dns"
    32  	configpb "github.com/google/cloudprober/probes/dns/proto"
    33  	"github.com/google/cloudprober/probes/options"
    34  	"github.com/google/cloudprober/targets"
    35  )
    36  
    37  var (
    38  	config    = flag.String("config_file", "", "Config file (ProbeConf)")
    39  	intervalF = flag.Duration("interval", 2*time.Second, "Interval between probes")
    40  	timeoutF  = flag.Duration("timeout", 1*time.Second, "Per-probe timeout")
    41  	targetsF  = flag.String("targets", "8.8.8.8", "Static host targets.")
    42  	sourceIP  = flag.String("source_ip", "", "Source IP for the DNS request")
    43  )
    44  
    45  func main() {
    46  	flag.Parse()
    47  
    48  	c := &configpb.ProbeConf{}
    49  
    50  	// If we are given a config file, read it. If not, use defaults.
    51  	if *config != "" {
    52  		b, err := ioutil.ReadFile(*config)
    53  		if err != nil {
    54  			glog.Exit(err)
    55  		}
    56  		if err := proto.UnmarshalText(string(b), c); err != nil {
    57  			glog.Exitf("Error while parsing config protobuf %s: Err: %v", string(b), err)
    58  		}
    59  	}
    60  
    61  	opts := &options.Options{
    62  		Interval:  *intervalF,
    63  		Timeout:   *timeoutF,
    64  		Targets:   targets.StaticTargets(*targetsF),
    65  		ProbeConf: c,
    66  	}
    67  
    68  	if *sourceIP != "" {
    69  		opts.SourceIP = net.ParseIP(*sourceIP)
    70  		if opts.SourceIP == nil {
    71  			glog.Exitf("Error parsing source IP: %s", *sourceIP)
    72  		}
    73  	}
    74  	dp := &dns.Probe{}
    75  	if err := dp.Init("dns_test", opts); err != nil {
    76  		glog.Exitf("Error in initializing probe %s from the config. Err: %v", "dns_test", err)
    77  	}
    78  	dataChan := make(chan *metrics.EventMetrics, 1000)
    79  	go dp.Start(context.Background(), dataChan)
    80  
    81  	for {
    82  		em := <-dataChan
    83  		fmt.Println(em.String())
    84  	}
    85  }