Added filter and sort options for last, check-in, reverse, etc.

This commit is contained in:
Gregory Kurtzer
2022-01-05 05:20:07 +00:00
parent 1ccc65a91e
commit 6afb3f3e25
3 changed files with 67 additions and 19 deletions

View File

@@ -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) {

View File

@@ -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.

View File

@@ -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