This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
278 lines (259 loc) · 9.81 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"fmt"
"strings"
"github.com/hico-horiuchi/ohgi/ohgi"
"github.com/hico-horiuchi/ohgibone/sensu"
"github.com/spf13/cobra"
)
var version string
func main() {
var (
age int
config string
consumers int
datacenter string
delete bool
expiration string
limit int
messages int
offset int
reason string
results bool
summarize string
)
rootCmd := &cobra.Command{
Use: "ohgi",
Short: "Sensu command-line tool by Golang",
Long: "Sensu command-line tool by Golang\nhttps://github.com/hico-horiuchi/ohgi",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
ohgi.Initialize()
sensu.DefaultAPI = ohgi.LoadConfig(config, datacenter)
},
}
rootCmd.PersistentFlags().StringVarP(&config, "config", "C", "", "Specify a configuration file")
rootCmd.PersistentFlags().StringVarP(&datacenter, "datacenter", "x", "", "Specify a datacenter")
clientsCmd := &cobra.Command{
Use: "clients [client]",
Short: "List and delete client(s) information",
Long: "clients Returns a list of clients\nclients [client] Returns a client",
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 0:
fmt.Print(ohgi.GetClients(sensu.DefaultAPI, limit, offset))
case 1:
if delete {
fmt.Print(ohgi.DeleteClientsClient(sensu.DefaultAPI, args[0]))
} else {
if strings.Contains(args[0], "*") {
fmt.Print(ohgi.GetClientsWildcard(sensu.DefaultAPI, args[0]))
} else {
fmt.Print(ohgi.GetClientsClient(sensu.DefaultAPI, args[0]))
}
}
}
},
}
clientsCmd.Flags().IntVarP(&limit, "limit", "l", -1, "The number of clients to return")
clientsCmd.Flags().IntVarP(&offset, "offset", "o", -1, "The number of clients to offset before returning items")
clientsCmd.Flags().BoolVarP(&delete, "delete", "d", false, "Removes a client, resolving its current events")
rootCmd.AddCommand(clientsCmd)
rootCmd.AddCommand(&cobra.Command{
Use: "jit [client] [address]",
Short: "Dynamically created clients, added to the client registry",
Long: "jit [client] [address] Create or update client data\njit [client] [address] [subscriptions] Create or update client data",
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 2:
fmt.Print(ohgi.PostClients(sensu.DefaultAPI, args[0], args[1], []string{}))
case 3:
fmt.Print(ohgi.PostClients(sensu.DefaultAPI, args[0], args[1], strings.Split(args[2], ",")))
default:
cmd.Help()
}
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "history [client]",
Short: "Returns the history for a client",
Long: "history [client] Returns the history for a client",
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 1:
fmt.Print(ohgi.GetClientsHistory(sensu.DefaultAPI, args[0]))
default:
cmd.Help()
}
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "checks [check]",
Short: "List locally defined checks and request executions",
Long: "checks Returns the list of checks\nchecks [check] Returns a check",
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 0:
fmt.Print(ohgi.GetChecks(sensu.DefaultAPI))
case 1:
if strings.Contains(args[0], "*") {
fmt.Print(ohgi.GetChecksWildcard(sensu.DefaultAPI, args[0]))
} else {
fmt.Print(ohgi.GetChecksCheck(sensu.DefaultAPI, args[0]))
}
}
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "request [check] [subscribers]",
Short: "Issues a check execution request",
Long: "request [check] Issues a check execution request\nrequest [check] [subscribers] Issues a check execution request",
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 1:
fmt.Print(ohgi.PostRequest(sensu.DefaultAPI, args[0], []string{}))
case 2:
fmt.Print(ohgi.PostRequest(sensu.DefaultAPI, args[0], strings.Split(args[1], ",")))
default:
cmd.Help()
}
},
})
eventsCmd := &cobra.Command{
Use: "events [client] [check]",
Short: "List and resolve current events",
Long: "events Returns the list of current events\nevents [client] Returns the list of current events for a given client\nevents [client] [check] Returns an event for a given client & check name",
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 0:
fmt.Print(ohgi.GetEvents(sensu.DefaultAPI))
case 1:
if strings.Contains(args[0], "*") {
fmt.Print(ohgi.GetEventsWildcard(sensu.DefaultAPI, args[0]))
} else {
fmt.Print(ohgi.GetEventsClient(sensu.DefaultAPI, args[0]))
}
case 2:
if delete {
fmt.Print(ohgi.DeleteEventsClientCheck(sensu.DefaultAPI, args[0], args[1]))
} else {
fmt.Print(ohgi.GetEventsClientCheck(sensu.DefaultAPI, args[0], args[1]))
}
}
},
}
eventsCmd.Flags().BoolVarP(&delete, "delete", "d", false, "Resolves an event for a given check on a given client")
rootCmd.AddCommand(eventsCmd)
rootCmd.AddCommand(&cobra.Command{
Use: "resolve [client] [check]",
Short: "Resolves an event",
Long: "resolve [client] [check] Resolves an event",
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 2:
fmt.Print(ohgi.PostResolve(sensu.DefaultAPI, args[0], args[1]))
default:
cmd.Help()
}
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "results [client] [check]",
Short: "List current check results",
Long: "results Returns a list of current check results for all clients\nresults [client] Returns a list of current check results for a given client\nresults [client] [check] Returns a check result for a given client & check name",
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 0:
fmt.Print(ohgi.GetResults(sensu.DefaultAPI))
case 1:
if strings.Contains(args[0], "*") {
fmt.Print(ohgi.GetResultsWildcard(sensu.DefaultAPI, args[0]))
} else {
fmt.Print(ohgi.GetResultsClient(sensu.DefaultAPI, args[0]))
}
case 2:
fmt.Print(ohgi.GetResultsClientCheck(sensu.DefaultAPI, args[0], args[1]))
}
},
})
aggregatesCmd := &cobra.Command{
Use: "aggregates [check] [issued]",
Short: "List and delete check aggregates",
Long: "aggregates Returns the list of aggregates\naggregates [check] Returns the list of aggregates for a given check\naggregates [check] [issued] Returns an aggregate",
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 0:
fmt.Print(ohgi.GetAggregates(sensu.DefaultAPI, limit, offset))
case 1:
if delete {
fmt.Print(ohgi.DeleteAggregatesCheck(sensu.DefaultAPI, args[0]))
} else {
fmt.Print(ohgi.GetAggregatesCheck(sensu.DefaultAPI, args[0], age))
}
case 2:
fmt.Print(ohgi.GetAggregatesCheckIssued(sensu.DefaultAPI, args[0], args[1], summarize, results))
}
},
}
aggregatesCmd.Flags().IntVarP(&limit, "limit", "l", -1, "The number of aggregates to return")
aggregatesCmd.Flags().IntVarP(&offset, "offset", "o", -1, "The number of aggregates to offset before returning items")
aggregatesCmd.Flags().IntVarP(&age, "age", "a", -1, "The number of seconds old an aggregate must be to be listed")
aggregatesCmd.Flags().BoolVarP(&delete, "delete", "d", false, "Deletes all aggregates for a check")
aggregatesCmd.Flags().StringVarP(&summarize, "summarize", "s", "", "Summarizes the output field in the event data")
aggregatesCmd.Flags().BoolVarP(&results, "results", "r", false, "Return the raw result data")
rootCmd.AddCommand(aggregatesCmd)
silenceCmd := &cobra.Command{
Use: "silence [client] [check]",
Short: "Create, list, and delete silence stashes",
Long: "silence Returns a list of silence stashes\nsilence [client] Create a silence stash\nsilence [client] [check] Create a silence stash",
Run: func(cmd *cobra.Command, args []string) {
switch len(args) {
case 0:
fmt.Print(ohgi.GetSilence(sensu.DefaultAPI))
case 1:
if delete {
fmt.Print(ohgi.DeleteSilence(sensu.DefaultAPI, args[0], ""))
} else {
fmt.Print(ohgi.PostSilence(sensu.DefaultAPI, args[0], "", expiration, reason))
}
case 2:
if delete {
fmt.Print(ohgi.DeleteSilence(sensu.DefaultAPI, args[0], args[1]))
} else {
fmt.Print(ohgi.PostSilence(sensu.DefaultAPI, args[0], args[1], expiration, reason))
}
}
},
}
silenceCmd.Flags().StringVarP(&expiration, "expiration", "e", "", "e.g. 15m, 1h, 1d")
silenceCmd.Flags().StringVarP(&reason, "reason", "r", "", "Enter a reason")
silenceCmd.Flags().BoolVarP(&delete, "delete", "d", false, "Remove silence stash")
rootCmd.AddCommand(silenceCmd)
healthCmd := &cobra.Command{
Use: "health",
Short: "Check the status of the API's transport & Redis connections, and query the transport's status",
Long: "health Returns health information on transport & Redis connections",
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(ohgi.GetHealth(sensu.DefaultAPI, consumers, messages))
},
}
healthCmd.Flags().IntVarP(&consumers, "consumers", "c", 1, "The minimum number of transport consumers to be considered healthy")
healthCmd.Flags().IntVarP(&messages, "messages", "m", 0, "The maximum ammount of transport queued messages to be considered healthy")
rootCmd.AddCommand(healthCmd)
rootCmd.AddCommand(&cobra.Command{
Use: "info",
Short: "List the Sensu version and the transport and Redis connection information",
Long: "info Returns information on the API",
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(ohgi.GetInfo(sensu.DefaultAPI))
},
})
rootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Print and check the version of ohgi",
Long: "version Print and check the version of ohgi",
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(ohgi.Version(version))
},
})
rootCmd.Execute()
}