2 Copyright (c) 2021 AT&T Intellectual Property.
3 Copyright (c) 2018-2021 Nokia.
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 * platform project (RICP).
27 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/sdlgoredis"
28 "github.com/spf13/cobra"
33 rootCmd.AddCommand(newHealthCheckCmd(newDatabase))
36 func newHealthCheckCmd(dbCreateCb DbCreateCb) *cobra.Command {
37 cmd := &cobra.Command{
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)
46 cmd.PrintErrf("%s\n", buf.String())
55 func runHealthCheck(dbCreateCb DbCreateCb) (string, error) {
58 var states []sdlgoredis.DbState
59 for _, dbInst := range dbCreateCb().Instances {
60 info, err := dbInst.State()
64 states = append(states, *info)
66 str = writeStateResults(states)
70 func writeStateResults(dbStates []sdlgoredis.DbState) string {
73 for i, dbState := range dbStates {
74 if err := dbState.IsOnline(); err != nil {
77 str = str + fmt.Sprintf(" SDL DB backend #%d\n", (i+1))
78 mAddr := dbState.MasterDbState.GetAddress()
79 err := dbState.MasterDbState.IsOnline()
81 str = str + fmt.Sprintf(" Master (%s): OK\n", mAddr)
83 str = str + fmt.Sprintf(" Master (%s): NOK\n", mAddr)
84 str = str + fmt.Sprintf(" %s\n", err.Error())
86 if dbState.ReplicasDbState != nil {
87 for j, rInfo := range dbState.ReplicasDbState.States {
88 err := rInfo.IsOnline()
90 str = str + fmt.Sprintf(" Replica #%d (%s): OK\n", (j+1), rInfo.GetAddress())
92 str = str + fmt.Sprintf(" Replica #%d (%s): NOK\n", (j+1), rInfo.GetAddress())
93 str = str + fmt.Sprintf(" %s\n", err.Error())
97 if dbState.SentinelsDbState != nil {
98 for k, sInfo := range dbState.SentinelsDbState.States {
99 err := sInfo.IsOnline()
101 str = str + fmt.Sprintf(" Sentinel #%d (%s): NOK\n", (k+1), sInfo.GetAddress())
102 str = str + fmt.Sprintf(" %s\n", err.Error())
108 str = fmt.Sprintf("Overall status: OK\n\n") + str
110 str = fmt.Sprintf("Overall status: NOK\n\n") + str