mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00

endpoint-health checks endpoint. It can generate 3 outputs: 1. cannot connect to the member through endpoint 2. connected to the member, but member failed to commit any proposals 3. connected to the member, and member committed a proposal
81 lines
2.3 KiB
Go
81 lines
2.3 KiB
Go
// 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/Godeps/_workspace/src/github.com/spf13/cobra"
|
|
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
|
|
"github.com/coreos/etcd/clientv3"
|
|
)
|
|
|
|
// 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) {
|
|
endpoints, err := cmd.Flags().GetStringSlice("endpoints")
|
|
if err != nil {
|
|
ExitWithError(ExitError, err)
|
|
}
|
|
|
|
cert, key, cacert := keyAndCertFromCmd(cmd)
|
|
dt := dialTimeoutFromCmd(cmd)
|
|
cfgs := []*clientv3.Config{}
|
|
for _, ep := range endpoints {
|
|
cfg, err := newClientCfg([]string{ep}, dt, cert, key, cacert)
|
|
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.
|
|
_, err = cli.Get(context.TODO(), "health")
|
|
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()
|
|
}
|