2ede93ef7209ea13faf7c93257ef930de3bfaa2a
[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: "Validate database healthiness",
40                 Long:  `Validate database healthiness`,
41                 Args:  cobra.ExactArgs(0),
42                 RunE: func(cmd *cobra.Command, args []string) error {
43                         out, err := runHealthCheck(dbCreateCb)
44                         cmd.Println(out)
45                         if err != nil {
46                                 cmd.PrintErrf("%s\n", buf.String())
47                         }
48                         return err
49                 },
50         }
51         cmd.SetOut(os.Stdout)
52         return cmd
53 }
54
55 func runHealthCheck(dbCreateCb DbCreateCb) (string, error) {
56         var anyErr error
57         var str string
58         var states []sdlgoredis.DbState
59         for _, dbInst := range dbCreateCb().Instances {
60                 state, err := dbInst.State()
61                 if err != nil {
62                         anyErr = err
63                 }
64                 states = append(states, *state)
65         }
66         str = writeStateResults(states)
67         return str, anyErr
68 }
69
70 func writeStateResults(dbStates []sdlgoredis.DbState) string {
71         var str string
72         var anyErr error
73         for i, dbState := range dbStates {
74                 if err := dbState.IsOnline(); err != nil {
75                         anyErr = err
76                 }
77                 str = str + fmt.Sprintf("  SDL DB backend #%d\n", (i+1))
78
79                 pAddr := dbState.PrimaryDbState.GetAddress()
80                 err := dbState.PrimaryDbState.IsOnline()
81                 if err == nil {
82                         str = str + fmt.Sprintf("    Primary (%s): OK\n", pAddr)
83                 } else {
84                         str = str + fmt.Sprintf("    Primary (%s): NOK\n", pAddr)
85                         str = str + fmt.Sprintf("      %s\n", err.Error())
86
87                 }
88                 if dbState.ReplicasDbState != nil {
89                         for j, rInfo := range dbState.ReplicasDbState.States {
90                                 err := rInfo.IsOnline()
91                                 if err == nil {
92                                         str = str + fmt.Sprintf("    Replica #%d (%s): OK\n", (j+1), rInfo.GetAddress())
93                                 } else {
94                                         str = str + fmt.Sprintf("    Replica #%d (%s): NOK\n", (j+1), rInfo.GetAddress())
95                                         str = str + fmt.Sprintf("      %s\n", err.Error())
96                                 }
97                         }
98
99                 }
100                 if dbState.SentinelsDbState != nil {
101                         for k, sInfo := range dbState.SentinelsDbState.States {
102                                 err := sInfo.IsOnline()
103                                 if err != nil {
104                                         str = str + fmt.Sprintf("    Sentinel #%d (%s): NOK\n", (k+1), sInfo.GetAddress())
105                                         str = str + fmt.Sprintf("      %s\n", err.Error())
106                                 }
107                         }
108                 }
109         }
110         if anyErr == nil {
111                 str = fmt.Sprintf("Overall status: OK\n\n") + str
112         } else {
113                 str = fmt.Sprintf("Overall status: NOK\n\n") + str
114         }
115         return str
116 }