github.com/google/cloudprober@v0.11.3/probes/ping/cmd/ping.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  // This program implements a stand-alone ping prober binary using the
    16  // cloudprober/ping package. It's intended to help prototype the ping 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  	"time"
    25  
    26  	"flag"
    27  	"github.com/golang/glog"
    28  	"github.com/golang/protobuf/proto"
    29  	"github.com/google/cloudprober/metrics"
    30  	"github.com/google/cloudprober/probes/options"
    31  	"github.com/google/cloudprober/probes/ping"
    32  	configpb "github.com/google/cloudprober/probes/ping/proto"
    33  	"github.com/google/cloudprober/targets"
    34  )
    35  
    36  var (
    37  	targetsF = flag.String("targets", "www.google.com,www.yahoo.com", "Comma separated list of targets")
    38  	config   = flag.String("config_file", "", "Config file to change ping probe options (see probes/ping/config.proto for default options)")
    39  	ipVer    = flag.Int("ip", 0, "IP version")
    40  )
    41  
    42  func main() {
    43  	flag.Parse()
    44  
    45  	probeConfig := &configpb.ProbeConf{}
    46  	if *config != "" {
    47  		b, err := ioutil.ReadFile(*config)
    48  		if err != nil {
    49  			glog.Exit(err)
    50  		}
    51  		if err = proto.UnmarshalText(string(b), probeConfig); err != nil {
    52  			glog.Exitf("error while parsing config: %v", err)
    53  		}
    54  	}
    55  
    56  	opts := &options.Options{
    57  		Targets:     targets.StaticTargets(*targetsF),
    58  		Interval:    2 * time.Second,
    59  		Timeout:     time.Second,
    60  		LatencyUnit: 1 * time.Millisecond,
    61  		ProbeConf:   probeConfig,
    62  		LogMetrics:  func(*metrics.EventMetrics) {},
    63  		IPVersion:   *ipVer,
    64  	}
    65  	p := &ping.Probe{}
    66  	if err := p.Init("ping", opts); err != nil {
    67  		glog.Exitf("error initializing ping probe from config: %v", err)
    68  	}
    69  
    70  	dataChan := make(chan *metrics.EventMetrics, 1000)
    71  	go p.Start(context.Background(), dataChan)
    72  
    73  	for {
    74  		em := <-dataChan
    75  		fmt.Println(em.String())
    76  	}
    77  }