-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
alloc_checks.go
162 lines (138 loc) · 3.9 KB
/
alloc_checks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package command
import (
"fmt"
"strings"
"time"
"github.com/hashicorp/nomad/api"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
)
type AllocChecksCommand struct {
Meta
}
func (c *AllocChecksCommand) Help() string {
helpText := `
Usage: nomad alloc checks [options] <allocation>
Alias: nomad checks
Outputs the latest health check status information for services in the allocation
using the Nomad service discovery provider.
General Options:
` + generalOptionsUsage(usageOptsDefault) + `
Checks Specific Options:
-verbose
Show full information.
`
return strings.TrimSpace(helpText)
}
func (c *AllocChecksCommand) Synopsis() string {
return "Outputs service health check status information."
}
func (c *AllocChecksCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-verbose": complete.PredictNothing,
})
}
func (c *AllocChecksCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictFunc(func(a complete.Args) []string {
client, err := c.Meta.Client()
if err != nil {
return nil
}
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.Allocs, nil)
if err != nil {
return nil
}
return resp.Matches[contexts.Allocs]
})
}
func (c *AllocChecksCommand) Name() string {
return "alloc checks"
}
func (c *AllocChecksCommand) Run(args []string) int {
var verbose bool
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.BoolVar(&verbose, "verbose", false, "")
if err := flags.Parse(args); err != nil {
return 1
}
args = flags.Args()
if numArgs := len(args); numArgs < 1 {
c.Ui.Error("An allocation ID is required")
c.Ui.Error(commandErrorText(c))
return 1
} else if numArgs > 1 {
c.Ui.Error("This command takes one argument (allocation ID)")
c.Ui.Error(commandErrorText(c))
return 1
}
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %v", err))
return 1
}
allocID := args[0]
// Truncate the id unless full length is requested
length := shortId
if verbose {
length = fullId
}
// Query the allocation info
if len(allocID) == 1 {
c.Ui.Error("Alloc ID must contain at least two characters.")
return 1
}
allocID = sanitizeUUIDPrefix(allocID)
allocations, _, err := client.Allocations().PrefixList(allocID)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying allocation: %v", err))
return 1
}
if len(allocations) == 0 {
c.Ui.Error(fmt.Sprintf("No allocation(s) with prefix or id %q found", allocID))
return 1
}
if len(allocations) > 1 {
out := formatAllocListStubs(allocations, verbose, length)
c.Ui.Error(fmt.Sprintf("Prefix matched multiple allocations\n\n%s", out))
return 1
}
// prefix lookup matched single allocation (happy path), lookup the checks
q := &api.QueryOptions{Namespace: allocations[0].Namespace}
checks, err := client.Allocations().Checks(allocations[0].ID, q)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying allocation checks: %s", err))
return 1
}
c.Ui.Output(fmt.Sprintf("Status of %d Nomad Service Checks", len(checks)))
c.Ui.Output("")
pair := func(key, value string) string { return fmt.Sprintf("%s|=|%s", key, value) }
taskFmt := func(s string) string {
if s == "" {
return "(group)"
}
return s
}
for _, check := range checks {
list := []string{
pair("ID", check.ID),
pair("Name", check.Check),
pair("Group", check.Group),
pair("Task", taskFmt(check.Task)),
pair("Service", check.Service),
pair("Status", check.Status),
}
if check.StatusCode > 0 {
list = append(list, pair("StatusCode", fmt.Sprintf("%d", check.StatusCode)))
}
list = append(list,
pair("Mode", check.Mode),
pair("Timestamp", formatTaskTimes(time.Unix(check.Timestamp, 0))),
pair("Output", check.Output),
)
c.Ui.Output(formatList(list))
c.Ui.Output("")
}
return 0
}