// Copyright 2015 CoreOS, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package command import ( "fmt" "sync" "time" "github.com/coreos/etcd/clientv3" "github.com/coreos/etcd/pkg/flags" "github.com/spf13/cobra" ) // NewEpHealthCommand returns the cobra command for "endpoint-health". func NewEpHealthCommand() *cobra.Command { cmd := &cobra.Command{ Use: "endpoint-health", Short: "endpoint-health checks the healthiness of endpoints specified in `--endpoints` flag", Run: epHealthCommandFunc, } return cmd } // epHealthCommandFunc executes the "endpoint-health" command. func epHealthCommandFunc(cmd *cobra.Command, args []string) { flags.SetPflagsFromEnv("ETCDCTL", cmd.InheritedFlags()) endpoints, err := cmd.Flags().GetStringSlice("endpoints") if err != nil { ExitWithError(ExitError, err) } sec := secureCfgFromCmd(cmd) dt := dialTimeoutFromCmd(cmd) cfgs := []*clientv3.Config{} for _, ep := range endpoints { cfg, err := newClientCfg([]string{ep}, dt, sec) if err != nil { ExitWithError(ExitBadArgs, err) } cfgs = append(cfgs, cfg) } var wg sync.WaitGroup for _, cfg := range cfgs { wg.Add(1) go func(cfg *clientv3.Config) { defer wg.Done() ep := cfg.Endpoints[0] cli, err := clientv3.New(*cfg) if err != nil { fmt.Printf("%s is unhealthy: failed to connect: %v\n", ep, err) return } st := time.Now() // get a random key. As long as we can get the response without an error, the // endpoint is health. ctx, cancel := commandCtx(cmd) _, err = cli.Get(ctx, "health") cancel() if err != nil { fmt.Printf("%s is unhealthy: failed to commit proposal: %v\n", ep, err) } else { fmt.Printf("%s is healthy: successfully committed proposal: took = %v\n", ep, time.Since(st)) } }(cfg) } wg.Wait() }