Skip to content

Commit

Permalink
separate diagnostics calls in new module
Browse files Browse the repository at this point in the history
  • Loading branch information
Omarabdul3ziz committed Dec 11, 2023
1 parent 7b955e0 commit f079563
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 31 deletions.
33 changes: 2 additions & 31 deletions cmds/modules/noded/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (

substrate "github.com/threefoldtech/tfchain/clients/tfchain-client-go"
"github.com/threefoldtech/tfgrid-sdk-go/rmb-sdk-go"
"github.com/threefoldtech/zos/cmds/modules/zbusdebug"
"github.com/threefoldtech/zos/pkg/app"
"github.com/threefoldtech/zos/pkg/capacity"
"github.com/threefoldtech/zos/pkg/diagnostics"
"github.com/threefoldtech/zos/pkg/environment"
"github.com/threefoldtech/zos/pkg/events"
"github.com/threefoldtech/zos/pkg/monitord"
Expand Down Expand Up @@ -193,36 +193,7 @@ func action(cli *cli.Context) error {
})

bus.WithHandler("zos.system.diagnostics", func(ctx context.Context, payload []byte) (interface{}, error) {
type moduleStatus struct {
Status zbus.Status `json:"status,omitempty"`
Err error `json:"error,omitempty"`
}
results := struct {
SystemStatusOk bool `json:"system_status_ok"`
Modules map[string]moduleStatus `json:"modules"`
}{
SystemStatusOk: true,
Modules: make(map[string]moduleStatus),
}

for module := range zbusdebug.PossibleModules {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)

status, err := redis.Status(ctx, module)
if err != nil {
results.SystemStatusOk = false
}

moduleStatus := moduleStatus{
Status: status,
Err: err,
}
results.Modules[module] = moduleStatus

cancel()
}

return results, nil
return diagnostics.GetSystemDiagnostics(ctx, redis)
})

bus.WithHandler("zos.system.dmi", func(ctx context.Context, payload []byte) (interface{}, error) {
Expand Down
77 changes: 77 additions & 0 deletions pkg/diagnostics/diagnostics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package diagnostics

import (
"context"
"sync"
"time"

"github.com/threefoldtech/zbus"
)

const callTimeout = 3 * time.Second

// Modules is all the registered modules on zbus
var Modules = []string{
"storage",
"node",
"identityd",
"vmd",
"flist",
"network",
"container",
"provision",
"gateway",
"qsfsd",
}

type moduleStatus struct {
Status zbus.Status `json:"status,omitempty"`
Err error `json:"error,omitempty"`
}

// Diagnostics show the health of zbus modules
type Diagnostics struct {
// SystemStatusOk is the overall system status
SystemStatusOk bool `json:"system_status_ok"`
// Modules is a list of modules with their objects and workers
Modules map[string]moduleStatus `json:"modules"`
}

func GetSystemDiagnostics(ctx context.Context, busClient zbus.Client) (Diagnostics, error) {
results := Diagnostics{
SystemStatusOk: true,
Modules: make(map[string]moduleStatus),
}

var wg sync.WaitGroup
for _, module := range Modules {

wg.Add(1)
go func(module string) {
defer wg.Done()
report := getModuleStatus(ctx, busClient, module)
results.Modules[module] = report

if report.Err != nil {
results.SystemStatusOk = false
}
}(module)

}

wg.Wait()

return results, nil

}

func getModuleStatus(ctx context.Context, busClient zbus.Client, module string) moduleStatus {
ctx, cancel := context.WithTimeout(ctx, callTimeout)
defer cancel()

status, err := busClient.Status(ctx, module)
return moduleStatus{
Status: status,
Err: err,
}
}

0 comments on commit f079563

Please sign in to comment.