Implement SDL CLI 'healthcheck' -command
[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         "bytes"
27         "fmt"
28         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/sdlgoredis"
29         "github.com/spf13/cobra"
30         "os"
31 )
32
33 func NewHealthCheckCmd() *cobra.Command {
34         return newHealthCheckCmd(newDatabase)
35 }
36
37 func newHealthCheckCmd(dbCreateCb DbCreateCb) *cobra.Command {
38         cmd := &cobra.Command{
39                 Use:   "healthcheck",
40                 Short: "healthcheck - validates database healthiness",
41                 Long:  `healthcheck - validates database healthiness`,
42                 RunE: func(cmd *cobra.Command, args []string) error {
43                         var buf bytes.Buffer
44                         sdlgoredis.SetDbLogger(&buf)
45                         out, err := runHealthCheck(dbCreateCb)
46                         cmd.Println(out)
47                         if err != nil {
48                                 cmd.PrintErrf("%s\n", buf.String())
49                         }
50                         return err
51                 },
52         }
53         cmd.SetOut(os.Stdout)
54         return cmd
55 }
56
57 func runHealthCheck(dbCreateCb DbCreateCb) (string, error) {
58         var anyErr error
59         var str string
60         var states []sdlgoredis.DbState
61         for _, dbInst := range dbCreateCb().Instances {
62                 info, err := dbInst.State()
63                 if err != nil {
64                         anyErr = fmt.Errorf("SDL CLI error: %v", err)
65                 }
66                 states = append(states, *info)
67         }
68         str = writeStateResults(states)
69         return str, anyErr
70 }
71
72 func writeStateResults(dbStates []sdlgoredis.DbState) string {
73         var str string
74         var anyErr error
75         for i, dbState := range dbStates {
76                 if err := dbState.IsOnline(); err != nil {
77                         anyErr = err
78                 }
79                 str = str + fmt.Sprintf("  SDL DB backend #%d\n", (i+1))
80                 mAddr := dbState.MasterDbState.GetAddress()
81                 err := dbState.MasterDbState.IsOnline()
82                 if err == nil {
83                         str = str + fmt.Sprintf("    Master (%s): OK\n", mAddr)
84                 } else {
85                         str = str + fmt.Sprintf("    Master (%s): NOK\n", mAddr)
86                         str = str + fmt.Sprintf("      %s\n", err.Error())
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 anyErr == nil {
101                 str = fmt.Sprintf("Overall status: OK\n\n") + str
102         } else {
103                 str = fmt.Sprintf("Overall status: NOK\n\n") + str
104         }
105         return str
106 }