github.com/google/cloudprober@v0.11.3/prober/cmd/client.go (about)

     1  // Copyright 2019 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 binary implements a Cloudprober gRPC client. This binary is to only
    16  // demonstrate how cloudprober can be programmed dynamically.
    17  //
    18  // go run ./cmd/client.go --server localhost:9314 --add_probe newprobe.cfg
    19  // go run ./cmd/client.go --server localhost:9314 --rm_probe newprobe
    20  package main
    21  
    22  import (
    23  	"context"
    24  	"io/ioutil"
    25  
    26  	"flag"
    27  	"github.com/golang/glog"
    28  	"github.com/golang/protobuf/proto"
    29  	pb "github.com/google/cloudprober/prober/proto"
    30  	spb "github.com/google/cloudprober/prober/proto"
    31  	configpb "github.com/google/cloudprober/probes/proto"
    32  	"google.golang.org/grpc"
    33  )
    34  
    35  var (
    36  	server   = flag.String("server", "", "gRPC server address")
    37  	addProbe = flag.String("add_probe", "", "Path to probe config to add")
    38  	rmProbe  = flag.String("rm_probe", "", "Probe name to remove")
    39  )
    40  
    41  func main() {
    42  	flag.Parse()
    43  
    44  	conn, err := grpc.Dial(*server, grpc.WithInsecure())
    45  	if err != nil {
    46  		glog.Fatal(err)
    47  	}
    48  
    49  	client := spb.NewCloudproberClient(conn)
    50  
    51  	if *addProbe != "" && *rmProbe != "" {
    52  		glog.Fatal("Options --add_probe and --rm_probe cannot be specified at the same time")
    53  	}
    54  
    55  	if *rmProbe != "" {
    56  		_, err := client.RemoveProbe(context.Background(), &pb.RemoveProbeRequest{ProbeName: rmProbe})
    57  		if err != nil {
    58  			glog.Exit(err)
    59  		}
    60  	}
    61  
    62  	if *addProbe != "" {
    63  		b, err := ioutil.ReadFile(*addProbe)
    64  		if err != nil {
    65  			glog.Exitf("Failed to read the config file: %v", err)
    66  		}
    67  
    68  		glog.Infof("Read probe config: %s", string(b))
    69  
    70  		cfg := &configpb.ProbeDef{}
    71  		if err := proto.UnmarshalText(string(b), cfg); err != nil {
    72  			glog.Exit(err)
    73  		}
    74  
    75  		_, err = client.AddProbe(context.Background(), &pb.AddProbeRequest{ProbeConfig: cfg})
    76  		if err != nil {
    77  			glog.Exit(err)
    78  		}
    79  	}
    80  }