-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
326 lines (321 loc) · 8.33 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"errors"
"fmt"
"github.com/qiniuts/logctl/api"
"gopkg.in/urfave/cli.v2"
"os"
"strconv"
"time"
)
func normalizeDate(str string) (string, error) {
// 没有指定时区,格式化为 0800
dfs := []string{"20060102T15:04"}
for _, df := range dfs {
t, err := time.Parse(df, str)
if err == nil {
return t.Format("2006-01-02T15:04:05+0800"), err
}
}
// 指定了时区
dfs = []string{"2006-01-02T15:04:05-0700"}
for _, df := range dfs {
t, err := time.Parse(df, str)
if err == nil {
return t.Format("2006-01-02T15:04:05-0700"), err
}
}
return "", errors.New(fmt.Sprintf(" %s : %s ", "时间格式不正确", str))
}
func main() {
app := &cli.App{
Name: "logctl",
Usage: "query logs from logdb",
UsageText: " command [command options] [arguments...] ",
Version: "0.0.4",
Commands: []*cli.Command{
{
Name: "account",
Usage: "设置后续查询时需要的 ak sk 以及方便使用的别名",
ArgsUsage: "<ak> <sk> <name>",
Action: func(c *cli.Context) error {
if c.Args().Len() == 3 {
api.Account(c.Args().Get(0), c.Args().Get(1), c.Args().Get(2))
return nil
}
return errors.New("参数错误: <ak> <sk> <name> ")
},
},
{
Name: "users",
Usage: "查看已设置账号列表",
Action: func(c *cli.Context) error {
api.UserList()
return nil
},
},
{
Name: "switch",
Usage: "通过别名,切换不同的账号",
ArgsUsage: "<name>",
Action: func(c *cli.Context) error {
if c.Args().Len() == 1 {
api.Switch(c.Args().Get(0))
return nil
}
return errors.New("参数错误: <name> ")
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "列取当前账号下所有的仓库",
ArgsUsage: " ",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Value: false,
Usage: "verbose",
},
},
Action: func(c *cli.Context) error {
api.ListRepos(c.Bool("v"))
return nil
},
},
{
Name: "repo",
Usage: "设置查询日志所在的仓库(请在查询前设置)",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "refresh",
Aliases: []string{"r"},
Value: false,
},
},
Action: func(c *cli.Context) error {
api.SetRepo(c.Args().Get(0), c.Bool("refresh"))
return nil
},
},
{
Name: "reqid",
Usage: "通过 reqid 查询日志。若未提供 [field:],查看是否有 reqid、respheader 字段",
ArgsUsage: " [<field>:]<reqid> ",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "field",
Aliases: []string{"f"},
Value: "*",
Usage: "显示哪些字段,默认 * ,即全部。以逗号 , 分割,忽略空格。如 \"*, F1\"",
},
&cli.StringFlag{
Name: "split",
Value: "\t",
Usage: "显示字段分隔符",
},
&cli.BoolFlag{
Name: "debug",
},
},
Action: func(c *cli.Context) error {
arg := &api.CtlArg{
Field: c.String("field"),
Split: c.String("split"),
Debug: c.Bool("debug"),
}
api.QueryReqid(arg, c.Args().Get(0))
return nil
},
},
{
Name: "query",
Aliases: []string{"q"},
Usage: "在时间范围内查询 logdb 内的日志",
ArgsUsage: " <query> ",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "order",
Aliases: []string{"o"},
Usage: "排序字段. 必须包含 :desc 或 :asc, 如 timestamp:desc 。默认按 date 数据类型降序排列",
},
&cli.StringFlag{
Name: "start",
Aliases: []string{"s"},
Usage: "查询日志的开始时间,格式要求 logdb 能够正确识别,如: 20060102T15:04,2017-04-06T17:40:30+0800",
},
&cli.StringFlag{
Name: "end",
Aliases: []string{"e"},
Usage: "查询日志的终止时间,格式要求 logdb 能够正确识别,如: 20060102T15:04,2017-04-06T16:40:30+0800",
},
&cli.Float64Flag{
Name: "day",
Aliases: []string{"d"},
Usage: "从当前时间往前推指定天,如 2.5",
DefaultText: "无",
},
&cli.Float64Flag{
Name: "hour",
Aliases: []string{"H"},
Usage: "从当前时间往前推指定小时,如 2.5",
DefaultText: "无",
},
&cli.Float64Flag{
Name: "minute",
Aliases: []string{"m"},
Usage: "从当前时间往前推指定分钟,如 30",
DefaultText: "无",
},
&cli.StringFlag{
Name: "field",
Aliases: []string{"f"},
Value: "*",
Usage: "显示哪些字段,默认 * ,即全部。以逗号 , 分割,忽略空格。如 \"*, F1\"",
},
&cli.StringFlag{
Name: "split",
Value: "\t",
Usage: "显示字段分隔符",
},
&cli.IntFlag{
Name: "head",
Aliases: []string{"l"},
Usage: "显示前多少行",
DefaultText: "无",
},
&cli.BoolFlag{
Name: "debug",
Value: false,
Usage: "显示参数信息",
},
},
Action: func(c *cli.Context) (err error) {
start := c.String("start")
end := c.String("end")
if len(start) != 0 {
start, err = normalizeDate(start)
if err != nil {
fmt.Println(err)
return nil
}
}
if len(end) != 0 {
end, err = normalizeDate(end)
if err != nil {
fmt.Println(err)
return nil
}
}
if (len(start) == 0) && (len(end) == 0) {
day := c.Float64("day")
hour := c.Float64("hour")
minute := c.Float64("minute")
m := day*24*60 + hour*60 + minute
// 浮点数,不能通过 m != 0 判断
if m > 0.05 {
start = time.Now().Add(-time.Duration(m) * time.Minute).Format("2006-01-02T15:04:05-0700")
end = time.Now().Format("2006-01-02T15:04:05-0700")
}
}
arg := &api.CtlArg{
Field: c.String("field"),
Sort: c.String("order"),
Start: start,
End: end,
Split: c.String("split"),
Head: c.Int("head"),
Debug: c.Bool("debug"),
}
query := c.Args().Get(0)
api.Query(&query, arg)
return nil
},
},
{
Name: "sample",
Aliases: []string{"s"},
Usage: "显示两条日志作为样例",
Action: func(c *cli.Context) error {
api.QuerySample()
return nil
},
},
{
Name: "clear",
Usage: "清理保存在临时文件中的信息",
Action: func(c *cli.Context) error {
api.Clear()
return nil
},
},
{
Name: "deluser",
Usage: "通过别名,删除账号信息",
ArgsUsage: "<name>",
Action: func(c *cli.Context) error {
if c.Args().Len() == 1 {
api.Deluser(c.Args().Get(0))
return nil
}
return errors.New("参数错误: <name> ")
},
},
{
Name: "histogram",
Aliases: []string{"g"},
Usage: "在时间范围内查询 logdb 内的日志",
Hidden: true,
ArgsUsage: " <query> ",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "start",
Aliases: []string{"s"},
Usage: "查询日志的开始时间,如: 2017-04-06T17:40:30+0800",
},
&cli.StringFlag{
Name: "end",
Aliases: []string{"e"},
Usage: "查询日志的终止时间,如: 2017-04-06T16:40:30+0800",
},
&cli.StringFlag{
Name: "field",
Aliases: []string{"f"},
Usage: "以哪个字段排序,要求字段的数据类型为 date",
},
&cli.BoolFlag{
Name: "debug",
Value: false,
Usage: "显示参数信息",
},
},
Action: func(c *cli.Context) error {
arg := &api.CtlArg{
Field: c.String("field"),
Start: c.String("start"),
End: c.String("end"),
Debug: c.Bool("debug"),
}
query := c.Args().Get(0)
api.QueryHistogram(&query, arg)
return nil
},
},
{
Name: "range",
Usage: "设置默认查询时间范围,单位 分钟,默认 5 分钟",
Action: func(c *cli.Context) error {
i, err := strconv.Atoi(c.Args().Get(0))
if err == nil && i > 0 {
api.SetTimeRange(i)
} else {
fmt.Println(" range must be an integer and greater than 0 ")
}
return nil
},
},
},
}
app.Run(os.Args)
}