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