forked from myENA/consul-zombie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
258 lines (219 loc) · 6.51 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// list and offer to kill dead consul services
package main
import (
"flag"
"fmt"
"log"
"os"
"regexp"
"github.com/hashicorp/consul/api"
"github.com/olekukonko/tablewriter"
)
// this is the default port for talking to remote consul agents
const (
defaultPort = 8500
)
type verbosityLevel uint8
func (v verbosityLevel) is(other verbosityLevel) bool {
return v == other
}
func (v verbosityLevel) allows(other verbosityLevel) bool {
return v >= other
}
const (
verbosityLevel0 verbosityLevel = iota
verbosityLevel1
verbosityLevel2
verbosityLevel3
)
func usage(code int) {
fmt.Println("usage: zombie [options] (hunt|kill|search)")
fmt.Println("Search (hunt) or deregister (kill) services: zombie -h for options.")
os.Exit(code)
}
func main() {
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
serviceString := fs.String("s", "", "Limit search by service address (regexp)")
tag := fs.String("t", "", "Limit search by tag")
force := fs.Bool("f", false, "Force killing of all matches, including healthy services")
localAddr := fs.String("local-addr", os.Getenv(api.HTTPAddrEnvName), "Address with port of \"local\" agent. Used to list services.")
remotePort := fs.Int("remote-port", defaultPort, "Port to use when connecting to remote agents")
token := fs.String("token", os.Getenv(api.HTTPTokenEnvName), "ACL token. Used in all api queries.")
v1 := fs.Bool("v", false, "Verbose")
v2 := fs.Bool("vv", false, "Really verbose")
v3 := fs.Bool("vvv", false, "Extremely verbose")
if err := fs.Parse(os.Args[1:]); err != nil {
log.Printf("Error parsing args: %s", err)
os.Exit(1)
}
var verbosity verbosityLevel
if *v3 {
verbosity = verbosityLevel3
} else if *v2 {
verbosity = verbosityLevel2
} else if *v1 {
verbosity = verbosityLevel1
}
// show usage if there are not command line args
args := fs.Args()
if len(args) == 0 {
usage(0)
}
cmd := args[0]
switch cmd {
// define a couple synonyms to "hunt" as well
case "hunt", "find", "search":
serviceList := getList(*localAddr, *token, *serviceString, *tag)
printList(serviceList, verbosity)
case "kill":
serviceList := getList(*localAddr, *token, *serviceString, *tag)
deregister(*remotePort, *token, serviceList, *force)
default:
usage(1)
}
}
// get a client handle for a specified address (or the local agent if "")
func getClient(address, token string) (*api.Client, error) {
config := api.DefaultConfig()
if address != "" {
config.Address = address
}
if token != "" {
config.Token = token
}
return api.NewClient(config)
}
// get a list of all services, limit to those matching the search criteria
func getList(localAddr, token, serviceString, tag string) []*api.ServiceEntry {
client, err := getClient(localAddr, token)
if err != nil {
log.Fatalf("Unable to get a consul client connection: %s\n", err)
}
serviceList, _, err := client.Catalog().Services(nil)
if err != nil {
log.Fatalf("Unable to get list of services from catalog: %s", err)
}
nodeServiceMap := make(map[string]map[string]*api.ServiceEntry)
for svc := range serviceList {
entries, _, err := client.Health().Service(svc, tag, false, nil)
if err != nil {
log.Fatalf("Unable to query for service \"%s\" health: %s", svc, err)
}
for _, entry := range entries {
if _, ok := nodeServiceMap[entry.Node.Node]; !ok {
nodeServiceMap[entry.Node.Node] = make(map[string]*api.ServiceEntry)
}
nodeServiceMap[entry.Node.Node][entry.Service.ID] = entry
}
}
seOut := make([]*api.ServiceEntry, 0)
if serviceString == "" {
for _, services := range nodeServiceMap {
for _, service := range services {
seOut = append(seOut, service)
}
}
} else {
var (
serviceID string
services map[string]*api.ServiceEntry
service *api.ServiceEntry
)
re := regexp.MustCompile(serviceString)
for _, services = range nodeServiceMap {
for serviceID, service = range services {
if re.FindString(serviceID) != "" || re.FindString(service.Service.Service) != "" {
seOut = append(seOut, service)
}
}
}
}
return seOut
}
// A service entry is considered healty if all the eligible checks are passing.
// serfChecks are not eligible
func isHealthy(se *api.ServiceEntry) bool {
if se == nil || se.Checks == nil || len(se.Checks) == 0 {
// No checks = failing
return false
}
healthy := true
eligible := 0
for _, c := range se.Checks {
if c.Name == "serfHealth" {
continue
}
// All found checks have to be passing
healthy = healthy && (c.Status == api.HealthPassing)
eligible++
}
// No eligible checks were found
if eligible == 0 {
return false
}
return healthy
}
// display a list of matching services
func printList(serviceList []*api.ServiceEntry, v verbosityLevel) {
var header, footer []string
var headerLen int
table := tablewriter.NewWriter(os.Stdout)
header = []string{"node", "id", "name", "address", "state"}
headerLen = len(header)
footer = make([]string, headerLen)
table.SetHeader(header)
healthy := 0
unhealthy := 0
for _, se := range serviceList {
isHealthy := isHealthy(se)
if isHealthy {
healthy++
} else {
unhealthy++
}
switch true {
case v.allows(verbosityLevel3), v.allows(verbosityLevel2), v.allows(verbosityLevel1):
table.Append([]string{
se.Node.Node,
se.Service.ID,
se.Service.Service,
fmt.Sprintf("%s:%d", se.Service.Address, se.Service.Port),
fmt.Sprintf("healthy=%t", isHealthy),
})
default:
if !isHealthy {
table.Append([]string{
se.Node.Node,
se.Service.ID,
se.Service.Service,
fmt.Sprintf("%s:%d", se.Service.Address, se.Service.Port),
fmt.Sprintf("healthy=%t", isHealthy),
})
}
}
}
footer[0] = "summary"
footer[headerLen-1] = fmt.Sprintf("total: %d", healthy+unhealthy)
footer[headerLen-2] = fmt.Sprintf("healthy: %d", healthy)
footer[headerLen-3] = fmt.Sprintf("unhealthy: %d", unhealthy)
table.SetFooter(footer)
table.Render()
}
// kill those services that are failing in the passed list, or all if force is true
func deregister(remotePort int, token string, serviceList []*api.ServiceEntry, force bool) {
for _, se := range serviceList {
if !isHealthy(se) || force {
fullAddress := fmt.Sprintf("%s:%d", se.Node.Address, remotePort)
fmt.Printf("Deregistering %s: %s (%s)\n", se.Service.Service, se.Service.ID, fullAddress)
client, err := getClient(fullAddress, token)
if err != nil {
log.Fatalf("Unable to get consul client: %s\n", err)
}
agent := client.Agent()
err = agent.ServiceDeregister(se.Service.ID)
if err != nil {
log.Printf("Unable to deregister: %s\n", err)
}
}
}
}