Implement SDL CLI 'get namespaces' -command
[ric-plt/sdlgo.git] / internal / cli / healthcheck.go
index 94467a9..de78ba9 100644 (file)
 package cli
 
 import (
-       "bytes"
        "fmt"
        "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/sdlgoredis"
        "github.com/spf13/cobra"
        "os"
 )
 
-func NewHealthCheckCmd() *cobra.Command {
-       return newHealthCheckCmd(newDatabase)
+func init() {
+       rootCmd.AddCommand(newHealthCheckCmd(newDatabase))
 }
 
 func newHealthCheckCmd(dbCreateCb DbCreateCb) *cobra.Command {
        cmd := &cobra.Command{
                Use:   "healthcheck",
-               Short: "healthcheck - validates database healthiness",
-               Long:  `healthcheck - validates database healthiness`,
+               Short: "Validate database healthiness",
+               Long:  `Validate database healthiness`,
+               Args:  cobra.ExactArgs(0),
                RunE: func(cmd *cobra.Command, args []string) error {
-                       var buf bytes.Buffer
-                       sdlgoredis.SetDbLogger(&buf)
                        out, err := runHealthCheck(dbCreateCb)
                        cmd.Println(out)
                        if err != nil {
@@ -61,7 +59,7 @@ func runHealthCheck(dbCreateCb DbCreateCb) (string, error) {
        for _, dbInst := range dbCreateCb().Instances {
                info, err := dbInst.State()
                if err != nil {
-                       anyErr = fmt.Errorf("SDL CLI error: %v", err)
+                       anyErr = err
                }
                states = append(states, *info)
        }
@@ -96,6 +94,15 @@ func writeStateResults(dbStates []sdlgoredis.DbState) string {
                                }
                        }
                }
+               if dbState.SentinelsDbState != nil {
+                       for k, sInfo := range dbState.SentinelsDbState.States {
+                               err := sInfo.IsOnline()
+                               if err != nil {
+                                       str = str + fmt.Sprintf("    Sentinel #%d (%s): NOK\n", (k+1), sInfo.GetAddress())
+                                       str = str + fmt.Sprintf("      %s\n", err.Error())
+                               }
+                       }
+               }
        }
        if anyErr == nil {
                str = fmt.Sprintf("Overall status: OK\n\n") + str