github.com/google/cloudprober@v0.11.3/examples/extensions/myprober/myprober.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  	"io/ioutil"
     7  	"os"
     8  
     9  	"cloud.google.com/go/compute/metadata"
    10  	"github.com/golang/glog"
    11  	"github.com/google/cloudprober"
    12  	"github.com/google/cloudprober/config"
    13  	"github.com/google/cloudprober/examples/extensions/myprober/myprobe"
    14  	"github.com/google/cloudprober/probes"
    15  	"github.com/google/cloudprober/web"
    16  )
    17  
    18  var (
    19  	configFile = flag.String("config_file", "", "Config file")
    20  )
    21  
    22  const (
    23  	configMetadataKeyName = "cloudprober_config"
    24  	defaultConfigFile     = "/etc/cloudprober.cfg"
    25  )
    26  
    27  func configFileToString(fileName string) string {
    28  	b, err := ioutil.ReadFile(fileName)
    29  	if err != nil {
    30  		glog.Exitf("Failed to read the config file: %v", err)
    31  	}
    32  	return string(b)
    33  }
    34  
    35  func getConfig() string {
    36  	if *configFile != "" {
    37  		return configFileToString(*configFile)
    38  	}
    39  	// On GCE first check if there is a config in custom metadata
    40  	// attributes.
    41  	if metadata.OnGCE() {
    42  		if config, err := config.ReadFromGCEMetadata(configMetadataKeyName); err != nil {
    43  			glog.Infof("Error reading config from metadata. Err: %v", err)
    44  		} else {
    45  			return config
    46  		}
    47  	}
    48  	// If config not found in metadata, check default config on disk
    49  	if _, err := os.Stat(defaultConfigFile); !os.IsNotExist(err) {
    50  		return configFileToString(defaultConfigFile)
    51  	}
    52  	glog.Warningf("Config file %s not found. Using default config.", defaultConfigFile)
    53  	return config.DefaultConfig()
    54  }
    55  
    56  func main() {
    57  	flag.Parse()
    58  
    59  	// Register stubby probe type
    60  	probes.RegisterProbeType(int(myprobe.E_RedisProbe.Field),
    61  		func() probes.Probe { return &myprobe.Probe{} })
    62  
    63  	err := cloudprober.InitFromConfig(getConfig())
    64  	if err != nil {
    65  		glog.Exitf("Error initializing cloudprober. Err: %v", err)
    66  	}
    67  
    68  	// web.Init sets up web UI for cloudprober.
    69  	web.Init()
    70  
    71  	cloudprober.Start(context.Background())
    72  
    73  	// Wait forever
    74  	select {}
    75  }