From 6afb3f3e25e3efbb88988de583768f7f8abcfa40 Mon Sep 17 00:00:00 2001 From: Gregory Kurtzer Date: Wed, 5 Jan 2022 05:20:07 +0000 Subject: [PATCH] Added filter and sort options for last, check-in, reverse, etc. --- internal/app/wwctl/node/status/main.go | 68 ++++++++++++++++++++------ internal/app/wwctl/node/status/root.go | 15 ++++-- internal/pkg/warewulfd/status.go | 3 ++ 3 files changed, 67 insertions(+), 19 deletions(-) diff --git a/internal/app/wwctl/node/status/main.go b/internal/app/wwctl/node/status/main.go index 26ee342a..af872a4d 100644 --- a/internal/app/wwctl/node/status/main.go +++ b/internal/app/wwctl/node/status/main.go @@ -22,6 +22,7 @@ type allStatus struct { } type NodeStatus struct { + NodeName string `json:"node name"` Stage string `json:"stage"` Sent string `json:"sent"` Ipaddr string `json:"ipaddr"` @@ -79,8 +80,9 @@ func CobraRunE(cmd *cobra.Command, args []string) error { fmt.Printf("%-20s %-20s %-25s %-10s\n", "NODENAME", "STAGE", "SENT", "LASTSEEN (s)") fmt.Printf("%s\n", strings.Repeat("=", 80)) - keys := make([]string, 0, len(nodeStatus.Nodes)) + keys := make([]*NodeStatus, 0, len(nodeStatus.Nodes)) + wwlog.Printf(wwlog.VERBOSE, "Building sort index\n") if len(args) > 0 { tmpMap := make(map[string]bool) nodeList := hostlist.Expand(args) @@ -91,31 +93,69 @@ func CobraRunE(cmd *cobra.Command, args []string) error { for name := range nodeStatus.Nodes { if _, ok := tmpMap[name]; ok { - keys = append(keys, name) + keys = append(keys, nodeStatus.Nodes[name]) } } } else { - for k := range nodeStatus.Nodes { - keys = append(keys, k) + + for name := range nodeStatus.Nodes { + keys = append(keys, nodeStatus.Nodes[name]) } } - sort.Strings(keys) + wwlog.Printf(wwlog.VERBOSE, "Sorting index\n") + if SetSortLast { + sort.Slice(keys, func(i, j int) bool { + if keys[i].Lastseen > keys[j].Lastseen { + return true + } else if keys[i].Lastseen < keys[j].Lastseen { + return false + } else { + if keys[i].NodeName < keys[j].NodeName { + return true + } else { + return false + } + } + //return keys[i].Lastseen > keys[j].Lastseen + }) + } else { + sort.Slice(keys, func(i, j int) bool { + return keys[i].NodeName < keys[j].NodeName + }) + } - for _, id := range keys { - if SetTime > 0 && rightnow-nodeStatus.Nodes[id].Lastseen < SetTime { + if SetSortReverse { + var tmpsort []*NodeStatus + + wwlog.Printf(wwlog.VERBOSE, "Reversing sort order\n") + + for l := len(keys) - 1; l >= 0; l-- { + tmpsort = append(tmpsort, keys[l]) + } + + keys = tmpsort + } + + wwlog.Printf(wwlog.VERBOSE, "Printing results\n") + for _, o := range keys { + if SetTime > 0 && o.Lastseen < SetTime { continue } - if nodeStatus.Nodes[id].Lastseen > 0 { - if rightnow-nodeStatus.Nodes[id].Lastseen >= int64(controller.Warewulf.UpdateInterval*2) { - color.Red("%-20s %-20s %-25s %-10d\n", id, nodeStatus.Nodes[id].Stage, nodeStatus.Nodes[id].Sent, rightnow-nodeStatus.Nodes[id].Lastseen) - } else if rightnow-nodeStatus.Nodes[id].Lastseen >= int64(controller.Warewulf.UpdateInterval) { - color.Yellow("%-20s %-20s %-25s %-10d\n", id, nodeStatus.Nodes[id].Stage, nodeStatus.Nodes[id].Sent, rightnow-nodeStatus.Nodes[id].Lastseen) + + if o.Lastseen > 0 { + if SetUnknown { + continue + } + if rightnow-o.Lastseen >= int64(controller.Warewulf.UpdateInterval*2) { + color.Red("%-20s %-20s %-25s %-10d\n", o.NodeName, o.Stage, o.Sent, rightnow-o.Lastseen) + } else if rightnow-o.Lastseen >= int64(controller.Warewulf.UpdateInterval) { + color.Yellow("%-20s %-20s %-25s %-10d\n", o.NodeName, o.Stage, o.Sent, rightnow-o.Lastseen) } else { - fmt.Printf("%-20s %-20s %-25s %-10d\n", id, nodeStatus.Nodes[id].Stage, nodeStatus.Nodes[id].Sent, rightnow-nodeStatus.Nodes[id].Lastseen) + fmt.Printf("%-20s %-20s %-25s %-10d\n", o.NodeName, o.Stage, o.Sent, rightnow-o.Lastseen) } } else { - color.HiBlack("%-20s %-20s %-25s %-10s\n", id, "--", "--", "--") + color.HiBlack("%-20s %-20s %-25s %-10s\n", o.NodeName, "--", "--", "--") } if count+4 >= height && SetWatch { if count+1 != len(keys) { diff --git a/internal/app/wwctl/node/status/root.go b/internal/app/wwctl/node/status/root.go index 7e9b0eba..4871c13b 100644 --- a/internal/app/wwctl/node/status/root.go +++ b/internal/app/wwctl/node/status/root.go @@ -11,16 +11,21 @@ var ( RunE: CobraRunE, Args: cobra.MinimumNArgs(0), } - SetWatch bool - SetUpdate int - SetTime int64 + SetWatch bool + SetUpdate int + SetTime int64 + SetSortLast bool + SetSortReverse bool + SetUnknown bool ) func init() { baseCmd.PersistentFlags().BoolVarP(&SetWatch, "watch", "w", false, "Watch the status automatically") - baseCmd.PersistentFlags().IntVarP(&SetUpdate, "update", "u", 500, "Set the update frequency for 'watch' (ms)") + baseCmd.PersistentFlags().IntVarP(&SetUpdate, "update", "U", 500, "Set the update frequency for 'watch' (ms)") baseCmd.PersistentFlags().Int64VarP(&SetTime, "time", "t", 0, "Filter by last checkin time (seconds)") - + baseCmd.PersistentFlags().BoolVarP(&SetSortLast, "last", "l", false, "Sort by the last check-in time") + baseCmd.PersistentFlags().BoolVarP(&SetSortReverse, "reverse", "r", false, "Reverse the sort order") + baseCmd.PersistentFlags().BoolVarP(&SetUnknown, "unknown", "u", false, "Only show nodes of unknown status") } // GetRootCommand returns the root cobra.Command for the application. diff --git a/internal/pkg/warewulfd/status.go b/internal/pkg/warewulfd/status.go index 7711bbeb..3a1191b3 100644 --- a/internal/pkg/warewulfd/status.go +++ b/internal/pkg/warewulfd/status.go @@ -15,6 +15,7 @@ type allStatus struct { } type NodeStatus struct { + NodeName string `json:"node name"` Stage string `json:"stage"` Sent string `json:"sent"` Ipaddr string `json:"ipaddr"` @@ -49,6 +50,7 @@ func LoadNodeStatus() error { for _, n := range nodes { if _, ok := statusDB.Nodes[n.Id.Get()]; !ok { newDB.Nodes[n.Id.Get()] = &NodeStatus{} + newDB.Nodes[n.Id.Get()].NodeName = n.Id.Get() } else { newDB.Nodes[n.Id.Get()] = statusDB.Nodes[n.Id.Get()] } @@ -64,6 +66,7 @@ func updateStatus(nodeID, stage, sent, ipaddr string) { wwlog.Printf(wwlog.DEBUG, "Updating node status data: %s\n", nodeID) var n NodeStatus + n.NodeName = nodeID n.Stage = stage n.Lastseen = rightnow n.Sent = sent