|
| 1 | +package agent |
| 2 | + |
| 3 | +import ( |
| 4 | + "cmp" |
| 5 | + "fmt" |
| 6 | + "slices" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/stackvista/stackstate-cli/generated/stackstate_api" |
| 11 | + "github.com/stackvista/stackstate-cli/internal/common" |
| 12 | + "github.com/stackvista/stackstate-cli/internal/di" |
| 13 | + "github.com/stackvista/stackstate-cli/internal/printer" |
| 14 | +) |
| 15 | + |
| 16 | +func ListCommand(deps *di.Deps) *cobra.Command { |
| 17 | + cmd := &cobra.Command{ |
| 18 | + Use: "list", |
| 19 | + Short: "List all registered agents", |
| 20 | + Long: "List all registered agents.", |
| 21 | + RunE: deps.CmdRunEWithApi(RunListCommand), |
| 22 | + } |
| 23 | + |
| 24 | + return cmd |
| 25 | +} |
| 26 | + |
| 27 | +const toMB = 1024 * 1024 |
| 28 | + |
| 29 | +func RunListCommand(cmd *cobra.Command, cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo) common.CLIError { |
| 30 | + agents, resp, err := api.AgentRegistrationsApi.AllAgentRegistrations(cli.Context).Execute() |
| 31 | + |
| 32 | + if err != nil { |
| 33 | + return common.NewResponseError(err, resp) |
| 34 | + } |
| 35 | + |
| 36 | + agentList := agents.Agents |
| 37 | + |
| 38 | + slices.SortFunc(agentList, func(a, b stackstate_api.AgentRegistration) int { |
| 39 | + if n := cmp.Compare(a.Lease, b.Lease); n != 0 { |
| 40 | + return n |
| 41 | + } |
| 42 | + // If leases are equal, order by registration moment |
| 43 | + return cmp.Compare(a.RegisteredEpochMs, b.RegisteredEpochMs) |
| 44 | + }) |
| 45 | + |
| 46 | + var active = 0 |
| 47 | + var limited = 0 |
| 48 | + var stale = 0 |
| 49 | + |
| 50 | + for _, agent := range agentList { |
| 51 | + switch agent.Lease { |
| 52 | + case stackstate_api.AGENTLEASE_ACTIVE: |
| 53 | + active++ |
| 54 | + case stackstate_api.AGENTLEASE_LIMITED: |
| 55 | + limited++ |
| 56 | + case stackstate_api.AGENTLEASE_STALE: |
| 57 | + stale++ |
| 58 | + default: |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if cli.IsJson() { |
| 63 | + cli.Printer.PrintJson(map[string]interface{}{ |
| 64 | + "agents": agentList, |
| 65 | + }) |
| 66 | + } else { |
| 67 | + data := make([][]interface{}, len(agentList)) |
| 68 | + |
| 69 | + for i, agent := range agentList { |
| 70 | + var info = agent.AgentData |
| 71 | + if info == nil { |
| 72 | + info = &stackstate_api.AgentData{Platform: "", CoreCount: 0, MemoryBytes: 0, KernelVersion: ""} |
| 73 | + } |
| 74 | + |
| 75 | + data[i] = []interface{}{ |
| 76 | + agent.AgentId, |
| 77 | + agent.Lease, |
| 78 | + time.UnixMilli(agent.RegisteredEpochMs).Format("2006-01-02 15:04:05 MST"), |
| 79 | + time.UnixMilli(agent.LeaseUntilEpochMs).Format("2006-01-02 15:04:05 MST"), |
| 80 | + fmt.Sprintf("%v", info.CoreCount), |
| 81 | + fmt.Sprintf("%vMB", info.MemoryBytes/toMB), |
| 82 | + info.Platform, |
| 83 | + info.KernelVersion, |
| 84 | + } |
| 85 | + } |
| 86 | + cli.Printer.Table(printer.TableData{ |
| 87 | + Header: []string{"Host", "Lease", "Registered", "Last Lease", "CPUS", "Memory", "Platform", "Kernel"}, |
| 88 | + Data: data, |
| 89 | + }) |
| 90 | + |
| 91 | + cli.Printer.PrintLn("") |
| 92 | + cli.Printer.PrintLn(fmt.Sprintf("Totals: %d active, %d limited, %d stale", active, limited, stale)) |
| 93 | + } |
| 94 | + |
| 95 | + return nil |
| 96 | +} |
0 commit comments