Implement 'get' and 'get keys' -SDL CLI commands
[ric-plt/sdlgo.git] / internal / cli / healthcheck.go
1 /*
2    Copyright (c) 2021 AT&T Intellectual Property.
3    Copyright (c) 2018-2021 Nokia.
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 */
17
18 /*
19  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20  * platform project (RICP).
21  */
22
23 package cli
24
25 import (
26         "fmt"
27         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/sdlgoredis"
28         "github.com/spf13/cobra"
29         "os"
30 )
31
32 func init() {
33         rootCmd.AddCommand(newHealthCheckCmd(newDatabase))
34 }
35
36 func newHealthCheckCmd(dbCreateCb DbCreateCb) *cobra.Command {
37         cmd := &cobra.Command{
38                 Use:   "healthcheck",
39                 Short: "healthcheck - validates database healthiness",
40                 Long:  `healthcheck - validates database healthiness`,
41                 RunE: func(cmd *cobra.Command, args []string) error {
42                         out, err := runHealthCheck(dbCreateCb)
43                         cmd.Println(out)
44                         if err != nil {
45                                 cmd.PrintErrf("%s\n", buf.String())
46                         }
47                         return err
48                 },
49         }
50         cmd.SetOut(os.Stdout)
51         return cmd
52 }
53
54 func runHealthCheck(dbCreateCb DbCreateCb) (string, error) {
55         var anyErr error
56         var str string
57         var states []sdlgoredis.DbState
58         for _, dbInst := range dbCreateCb().Instances {
59                 info, err := dbInst.State()
60                 if err != nil {
61                         anyErr = fmt.Errorf("SDL CLI error: %v", err)
62                 }
63                 states = append(states, *info)
64         }
65         str = writeStateResults(states)
66         return str, anyErr
67 }
68
69 func writeStateResults(dbStates []sdlgoredis.DbState) string {
70         var str string
71         var anyErr error
72         for i, dbState := range dbStates {
73                 if err := dbState.IsOnline(); err != nil {
74                         anyErr = err
75                 }
76                 str = str + fmt.Sprintf("  SDL DB backend #%d\n", (i+1))
77                 mAddr := dbState.MasterDbState.GetAddress()
78                 err := dbState.MasterDbState.IsOnline()
79                 if err == nil {
80                         str = str + fmt.Sprintf("    Master (%s): OK\n", mAddr)
81                 } else {
82                         str = str + fmt.Sprintf("    Master (%s): NOK\n", mAddr)
83                         str = str + fmt.Sprintf("      %s\n", err.Error())
84                 }
85                 if dbState.ReplicasDbState != nil {
86                         for j, rInfo := range dbState.ReplicasDbState.States {
87                                 err := rInfo.IsOnline()
88                                 if err == nil {
89                                         str = str + fmt.Sprintf("    Replica #%d (%s): OK\n", (j+1), rInfo.GetAddress())
90                                 } else {
91                                         str = str + fmt.Sprintf("    Replica #%d (%s): NOK\n", (j+1), rInfo.GetAddress())
92                                         str = str + fmt.Sprintf("      %s\n", err.Error())
93                                 }
94                         }
95                 }
96                 if dbState.SentinelsDbState != nil {
97                         for k, sInfo := range dbState.SentinelsDbState.States {
98                                 err := sInfo.IsOnline()
99                                 if err != nil {
100                                         str = str + fmt.Sprintf("    Sentinel #%d (%s): NOK\n", (k+1), sInfo.GetAddress())
101                                         str = str + fmt.Sprintf("      %s\n", err.Error())
102                                 }
103                         }
104                 }
105         }
106         if anyErr == nil {
107                 str = fmt.Sprintf("Overall status: OK\n\n") + str
108         } else {
109                 str = fmt.Sprintf("Overall status: NOK\n\n") + str
110         }
111         return str
112 }