Files
mesh-catalog/modules/networkmanager/cmd/uplink-tools/tools.go
T
jochen 1dec6eb02c M7 networkmanager: its tools and health
The bundle serves uplink_sign_in, uplink_names_again, uplink_rating_again,
uplink_publish_again and uplink_health: the read of the reaction, the three acts the
parts do by themselves, and the watcher process's health, which also gives the names
again where the resolver no longer holds them.

Builds: design 08 sign-in-page §13, §23
2026-11-24 21:25:17 +01:00

145 lines
6.1 KiB
Go

package main
// networkmanager's own tools, served by the bundle (design 08 sign-in-page §23): the read of the sign-in reaction, the
// three acts that start by hand what the two parts do by themselves, and the health tool of the watcher's process.
import (
"fmt"
"slices"
"strings"
"git.novox.be/novox/mesh-networking/modules/mesh-connectivity-contracts/go/seats/noderesolver"
stdio "git.novox.be/novox/mesh-sdk/go"
"git.novox.be/novox/mesh-sdk/go/health"
)
// signInRead is uplink_sign_in's answer.
type signInRead struct {
Watcher *watcherState `json:"watcher,omitempty"`
WatcherError string `json:"watcher_error,omitempty"`
Waiting int `json:"waiting"`
RefusedSince int `json:"publishes_refused_since_start"`
Names *NamesCall `json:"names,omitempty"`
ResolverRefused *string `json:"resolver_refused,omitempty"`
ResolverError string `json:"resolver_error,omitempty"`
}
func (b *bundle) tools() []stdio.Tool {
return []stdio.Tool{
{Name: "uplink_sign_in",
Description: "The network's sign-in page as this machine reacts to it: whether the watcher hears the network " +
"manager, the last rating it heard and when, the sign-in asked now (network, link, since), the asks opened " +
"and ended since its start, the last page opened with its outcome, the last notification, the last session " +
"start and what it did, the kept events dropped past the bound of 256, the events waiting to be published " +
"and the publishes refused since the bundle's start, the last uplink-names call with its result, and the " +
"resolver's refusal of the names where a holder of node-resolver runs here. (r)",
Run: func(map[string]any) (any, error) { return b.signIn(), nil }},
{Name: "uplink_names_again",
Description: "Give this machine's resolver the network manager's check host now, as the bundle does at its " +
"moments: calls node-resolver's uplink-names on this node and answers the call as names.json keeps it. (a)",
Run: func(map[string]any) (any, error) { return b.names.give("uplink_names_again"), nil }},
{Name: "uplink_rating_again",
Description: "Make the watcher subscribe on the system bus again, read the network manager's rating now and " +
"act on it as on a change; answers the watcher's state after it. (a)",
Run: func(map[string]any) (any, error) {
var s watcherState
if err := b.folder.askWatcher(askRatingAgain, &s); err != nil {
return nil, err
}
return s, nil
}},
{Name: "uplink_publish_again",
Description: "Publish the kept sign-in events now, in order under their own message ids, as the bundle does at " +
"its moments: answers how many were published and how many still wait. (a)",
Run: func(map[string]any) (any, error) { return b.pub.publishKept("uplink_publish_again"), nil }},
{Name: "uplink_health",
Description: "Whether the sign-in reaction works, as the watcher process's health check (novox/hq ADR 0240): " +
"unhealthy while the watcher's socket does not answer, while the watcher is not subscribed on the system " +
"bus, or while the last uplink-names call stands failed, naming which; no holder of node-resolver is " +
"healthy. Where a holder runs and the resolver no longer holds the names last given, it gives them again. (r)",
Run: func(map[string]any) (any, error) { return b.health(), nil }},
}
}
// signIn reads the watcher's state, the folder and, where a holder of node-resolver runs here, the resolver's refusal.
func (b *bundle) signIn() signInRead {
var out signInRead
var s watcherState
if err := b.folder.askWatcher(askState, &s); err != nil {
out.WatcherError = err.Error()
} else {
out.Watcher = &s
}
if kept, err := b.folder.pending(); err == nil {
out.Waiting = len(kept)
}
out.RefusedSince = b.pub.refusedSince()
out.Names, _ = b.folder.names()
routes, held, err := b.resolverRoutes()
switch {
case err != nil:
out.ResolverError = err.Error()
case held:
out.ResolverRefused = &routes.UplinkNames.Refused
}
return out
}
// resolverRoutes is node-resolver's routes on this node, held false when no module holds the seat here.
func (b *bundle) resolverRoutes() (noderesolver.RoutesAnswer, bool, error) {
holder, err := b.names.resolverHolder()
if err != nil || holder == "" {
return noderesolver.RoutesAnswer{}, false, err
}
raw, err := b.names.ask("seat:"+noderesolver.Seat+"."+noderesolver.VerbRoutes+"@"+b.folder.Node, struct{}{})
if err != nil {
return noderesolver.RoutesAnswer{}, true, fmt.Errorf("%s/%s.%s: %w", b.folder.Node, noderesolver.Seat,
noderesolver.VerbRoutes, err)
}
routes, _, err := noderesolver.DecodeRoutesAnswer(raw)
return routes, true, err
}
// health is the watcher process's health, as its health tool answers it (§23). At this look, where a holder of
// node-resolver runs and its routes no longer hold the names last given, the names are given again (§13).
func (b *bundle) health() health.Answer {
var faults []string
var s watcherState
if err := b.folder.askWatcher(askState, &s); err != nil {
faults = append(faults, err.Error())
} else if !s.Subscribed {
faults = append(faults, "the watcher is not subscribed on the system bus")
}
call, err := b.folder.names()
if err != nil {
faults = append(faults, "names.json does not read: "+err.Error())
}
if call != nil && !call.Failed() && len(call.Names) > 0 {
if routes, held, err := b.resolverRoutes(); err == nil && held && !containsAll(routes.UplinkNames.Names, call.Names) {
journal("the resolver no longer holds %v: giving them again", call.Names)
given := b.names.give("the health look")
call = &given
}
}
if call != nil && call.Failed() {
faults = append(faults, "the last uplink-names call failed: "+call.Result)
}
if len(faults) > 0 {
return health.Unhealthy(strings.Join(faults, "; ") + ".")
}
why := "the watcher hears the network manager"
if call != nil {
why += "; the last uplink-names call: " + call.Result
}
return health.Healthy(why + ".")
}
func containsAll(have, want []string) bool {
for _, w := range want {
if !slices.Contains(have, w) {
return false
}
}
return true
}