github.com/google/cloudprober@v0.11.3/probes/udp/cmd/udp.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  // Udp_bin implements a stand-alone udp prober binary using the
    16  // cloudprober/probes/udp package. It's intended to help prototype the udp 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/udp"
    32  	configpb "github.com/google/cloudprober/probes/udp/proto"
    33  	"github.com/google/cloudprober/targets"
    34  )
    35  
    36  var (
    37  	config    = flag.String("config_file", "", "Config file (UDPProbe)")
    38  	intervalF = flag.Duration("interval", 2*time.Second, "Interval between probes")
    39  	timeoutF  = flag.Duration("timeout", 1*time.Second, "Per-probe timeout")
    40  	targetsF  = flag.String("targets", "", "Static host targets.")
    41  )
    42  
    43  func main() {
    44  	flag.Parse()
    45  
    46  	c := &configpb.ProbeConf{}
    47  
    48  	// If we are given a config file, read it. If not, use defaults.
    49  	if *config != "" {
    50  		b, err := ioutil.ReadFile(*config)
    51  		if err != nil {
    52  			glog.Exit(err)
    53  		}
    54  		if err := proto.UnmarshalText(string(b), c); err != nil {
    55  			glog.Exitf("Error while parsing config protobuf %s: Err: %v", string(b), err)
    56  		}
    57  	}
    58  
    59  	opts := &options.Options{
    60  		Targets:   targets.StaticTargets(*targetsF),
    61  		Interval:  *intervalF,
    62  		Timeout:   *timeoutF,
    63  		ProbeConf: c,
    64  	}
    65  
    66  	up := &udp.Probe{}
    67  	if err := up.Init("udp_test", opts); err != nil {
    68  		glog.Exitf("Error in initializing probe %s from the config. Err: %v", "udp_test", err)
    69  	}
    70  	dataChan := make(chan *metrics.EventMetrics, 1000)
    71  	go up.Start(context.Background(), dataChan)
    72  
    73  	for {
    74  		em := <-dataChan
    75  		fmt.Println(em.String())
    76  	}
    77  }