// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. // code for the "vespa-get-cluster-state" command // Author: arnej // utilities to get and manipulate node states in a storage cluster package clusterstate import ( "fmt" "os" "github.com/fatih/color" "github.com/spf13/cobra" "github.com/vespa-engine/vespa/client/go/internal/admin/envvars" "github.com/vespa-engine/vespa/client/go/internal/admin/trace" "github.com/vespa-engine/vespa/client/go/internal/build" ) func NewGetClusterStateCmd() *cobra.Command { var ( curOptions Options ) cmd := &cobra.Command{ Use: "vespa-get-cluster-state [-h] [-v] [-f] [-c cluster]", Short: "Get the cluster state of a given cluster.", Long: `Usage: get-cluster-state [Options]`, Version: build.Version, Args: cobra.MaximumNArgs(0), CompletionOptions: cobra.CompletionOptions{DisableDefaultCmd: true}, Run: func(cmd *cobra.Command, args []string) { curOptions.NodeIndex = AllNodes runGetClusterState(&curOptions) }, } addCommonOptions(cmd, &curOptions) return cmd } func runGetClusterState(opts *Options) { if opts.Silent { trace.Silent() } if opts.NoColors || os.Getenv(envvars.TERM) == "" { color.NoColor = true } trace.Debug("run getClusterState with: ", opts) m := detectModel(opts) trace.Debug("model:", m) sss := m.findSelectedServices(opts) clusters := make(map[string]*ClusterState) for _, s := range sss { trace.Debug("found service: ", s) if clusters[s.cluster] == nil { state, _ := m.getClusterState(s.cluster) trace.Debug("cluster ", s.cluster, state) clusters[s.cluster] = state } } for k, v := range clusters { globalState := v.State.Generated.State if globalState == "up" { fmt.Printf("Cluster %s:\n", k) } else { fmt.Printf("Cluster %s is %s. Too few nodes available.\n", k, color.HiRedString("%s", globalState)) } for serviceType, serviceList := range v.Service { for dn, dv := range serviceList.Node { nodeState := dv.State.Generated.State if nodeState == "up" { fmt.Printf("%s/%s/%s: %v\n", k, serviceType, dn, nodeState) } else { fmt.Printf("%s/%s/%s: %v\n", k, serviceType, dn, color.HiRedString(nodeState)) } } } } }