-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate diagnostics calls in new module
- Loading branch information
1 parent
7b955e0
commit f079563
Showing
2 changed files
with
79 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |