github.com/noironetworks/cilium-net@v1.6.12/cilium-health/cmd/get.go (about)

     1  // Copyright 2017 Authors of Cilium
     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  package cmd
    16  
    17  import (
    18  	"fmt"
    19  	"os"
    20  	"text/tabwriter"
    21  
    22  	ciliumClient "github.com/cilium/cilium/pkg/client"
    23  	"github.com/cilium/cilium/pkg/command"
    24  
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  // healthGetCmd represents the get command
    29  var healthGetCmd = &cobra.Command{
    30  	Use:     "get",
    31  	Aliases: []string{"inspect, show"},
    32  	Short:   "Display local cilium agent status",
    33  	Run: func(cmd *cobra.Command, args []string) {
    34  		result, err := client.Restapi.GetHealthz(nil)
    35  		if err != nil {
    36  			Fatalf("Cannot get health for local instance: %s\n", err)
    37  		}
    38  		sr := result.Payload
    39  
    40  		if command.OutputJSON() {
    41  			if err := command.PrintOutput(sr); err != nil {
    42  				os.Exit(1)
    43  			}
    44  		} else {
    45  			w := tabwriter.NewWriter(os.Stdout, 2, 0, 3, ' ', 0)
    46  			fmt.Fprintf(w, "Daemon uptime:\t%s\n", sr.Uptime)
    47  			load := sr.SystemLoad
    48  			fmt.Fprintf(w, "Node load:\t%s %s %s\n",
    49  				load.Last1min, load.Last5min, load.Last15min)
    50  			ciliumClient.FormatStatusResponse(w, sr.Cilium, false, false, false, false)
    51  			w.Flush()
    52  		}
    53  	},
    54  }
    55  
    56  func init() {
    57  	rootCmd.AddCommand(healthGetCmd)
    58  	command.AddJSONOutput(healthGetCmd)
    59  }