-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
365 lines (295 loc) · 10.2 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// A webapp for looking at and searching through files.
package main
import (
"fmt"
"io/ioutil"
"log"
"net"
"os"
"strings"
"sync"
"github.com/mitchellh/mapstructure"
"github.com/pelletier/go-toml"
flag "github.com/spf13/pflag"
)
const scriptDescription = `
Usage: tailon -c <config file>
Usage: tailon [options] <filespec> [<filespec> ...]
Tailon is a webapp for looking at and searching through files and streams.
`
const scriptEpilog = `
Tailon can be configured through a config file or with command-line flags.
The command-line interface expects one or more filespec arguments, which
specify the files to be served. The expected format is:
[alias=name,group=name]<spec>
where <spec> can be a file name, glob or directory. The optional 'alias='
and 'group=' specifiers change the display name of the files in the UI and
the group in which they appear.
A file specifier points to a single, possibly non-existent file. The file
name in the UI can be overwritten with 'alias='. For example:
tailon alias=error.log,/var/log/apache/error.log
A glob evaluates to the list of files that match a shell file name pattern.
The pattern is evaluated each time the file list is refreshed. An 'alias='
specifier overwrites the parent directory of each matched file in the UI.
tailon "/var/log/apache/*.log" "alias=nginx,/var/log/nginx/*.log"
If a directory is given, all files under it are served recursively.
tailon /var/log/apache/ /var/log/nginx/
Example usage:
tailon file1.txt file2.txt file3.txt
tailon alias=messages,/var/log/messages "/var/log/*.log"
tailon -b localhost:8080,localhost:8081 -c config.toml
For information on usage through the configuration file, please refer to the
'--help-config' option.
`
const configFileHelp = `
Tailon can be configured through a TOML config file. The config file allows
more configurability than the command-line interface.
# The <title> of the index page.
title = "Tailon"
# The root of the web application.
relative-root = "/"
# The addresses to listen on. Can be an address:port combination or an unix socket.
listen-addr = [":8080"]
# Allow download of know files (only those matched by a filespec).
allow-download = true
# Commands that will appear in the UI.
allow-commands = ["tail", "grep", "grep -v", "sed", "awk"]
`
const defaultTomlConfig = `
title = "Tailon"
relative-root = "/"
listen-addr = [":8080"]
allow-download = true
lines-of-history = 0
lines-to-tail = 100
allow-commands = ["tail", "grep", "grep -v", "sed", "awk"]
[files]
file1 = "alias=test,group=test_group,log.log"
file2 = "alias=test1,group=test_group1,log.log"
[commands]
[commands.tail]
action = ["tail", "-n", "$lines", "-F", "$path"]
[commands.grep]
stdin = "tail"
action = ["grep", "-e", "$script"]
default = ".*"
[commands.sed]
stdin = "tail"
action = ["sed", "-u", "-e", "$script"]
default = "s/.*/&/"
[commands.awk]
stdin = "tail"
action = ["awk", "--sandbox", "$script"]
default = "{print $0; fflush()}"
[commands."grep -v"]
stdin = "tail"
action = ["grep", "-v", "--text", "--line-buffered", "--color=never", "-e", "$script"]
default = "^$"
`
// CommandSpec defines a command that the server can execute.
type CommandSpec struct {
Stdin string
Action []string
Default string
}
func parseTomlConfig(config string) (*toml.Tree, map[string]CommandSpec, []FileSpec) {
cfg, err := toml.Load(config)
if err != nil {
log.Fatal("Error parsing config: ", err)
}
commands := make(map[string]CommandSpec)
cfgCommands := cfg.Get("commands").(*toml.Tree).ToMap()
for key, value := range cfgCommands {
command := CommandSpec{}
err := mapstructure.Decode(value, &command)
if err != nil {
log.Fatal(err)
}
commands[key] = command
}
cfgFiles := cfg.Get("files").(*toml.Tree).ToMap()
filespecs := make([]FileSpec, 0)
for _, value := range cfgFiles {
if value == nil {
continue
}
strvalue := fmt.Sprint(value)
if file, err := parseFileSpec(strvalue); err != nil {
fmt.Fprintf(os.Stderr, "Error parsing argument '%s': %s\n", strvalue, err)
os.Exit(1)
} else {
filespecs = append(filespecs, file)
}
}
return cfg, commands, filespecs
}
// FileSpec is an instance of a file to be monitored. These are mapped to
// os.Args or the [files] elements in the config file.
type FileSpec struct {
Path string
Type string
Alias string
Group string
}
// Parse a string into a filespec. Example inputs are:
// alias=1,group=2,/var/log/messages
// /var/log/
// /var/log/*
func parseFileSpec(spec string) (FileSpec, error) {
var filespec FileSpec
var path string
parts := strings.Split(spec, ",")
if length := len(parts); length == 1 {
path = parts[0]
} else {
// The last part is the path. We'll probably need a more robust
// solution in the future.
path, parts = parts[len(parts)-1], parts[:len(parts)-1]
}
if strings.ContainsAny(path, "*?[]") {
filespec.Type = "glob"
} else {
stat, err := os.Lstat(path)
if os.IsNotExist(err) || stat.Mode().IsRegular() {
filespec.Type = "file"
} else if stat.Mode().IsDir() {
filespec.Type = "dir"
}
}
for _, part := range parts {
if strings.HasPrefix(part, "group=") {
group := strings.SplitN(part, "=", 2)[1]
group = strings.Trim(group, "'\" ")
filespec.Group = group
} else if strings.HasPrefix(part, "alias=") {
filespec.Alias = strings.SplitN(part, "=", 2)[1]
}
}
if filespec.Type == "" {
filespec.Type = "file"
}
filespec.Path = path
return filespec, nil
}
// Config contains all backend and frontend configuration options and relevant state.
type Config struct {
RelativeRoot string
BindAddr []string
ConfigPath string
WrapLinesInitial bool
TailLinesInitial int
LinesToTail int64
LinesOfHistory int64
AllowCommandNames []string
AllowDownload bool
CommandSpecs map[string]CommandSpec
CommandScripts map[string]string
FileSpecs []FileSpec
}
func makeConfig(configContent string) *Config {
defaults, commandSpecs, fileSpecs := parseTomlConfig(configContent)
// Convert the list of bind addresses from []interface{} to []string.
addrsA := defaults.Get("listen-addr").([]interface{})
addrsB := make([]string, len(addrsA))
for i := range addrsA {
addrsB[i] = addrsA[i].(string)
}
config := Config{
BindAddr: addrsB,
RelativeRoot: defaults.Get("relative-root").(string),
LinesToTail: defaults.Get("lines-to-tail").(int64),
LinesOfHistory: defaults.Get("lines-of-history").(int64),
AllowDownload: defaults.Get("allow-download").(bool),
CommandSpecs: commandSpecs,
FileSpecs: fileSpecs,
}
mapstructure.Decode(defaults.Get("allow-commands"), &config.AllowCommandNames)
return &config
}
var config = &Config{}
func main() {
config = makeConfig(defaultTomlConfig)
printHelp := flag.BoolP("help", "h", false, "Show this help message and exit")
printConfigHelp := flag.BoolP("help-config", "e", false, "Show configuration file help and exit")
bindAddr := flag.StringP("bind", "b", strings.Join(config.BindAddr, ","), "Listen on the specified address and port")
flag.StringVarP(&config.RelativeRoot, "relative-root", "r", config.RelativeRoot, "Webapp relative root.")
flag.BoolVarP(&config.AllowDownload, "allow-download", "a", config.AllowDownload, "Allow file downloads.")
flag.Int64Var(&config.LinesToTail, "lines-to-tail", config.LinesToTail, "No. of lines to tail.")
flag.Int64Var(&config.LinesOfHistory, "history-lines", config.LinesOfHistory, "No. of history lines to tail.")
flag.StringVarP(&config.ConfigPath, "config", "c", "", "Config.toml file location.")
flag.Parse()
flag.Usage = func() {
fmt.Fprintln(os.Stderr, strings.TrimLeft(scriptDescription, "\n"))
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, strings.TrimRight(scriptEpilog, "\n"))
os.Exit(0)
}
if *printHelp {
flag.Usage()
os.Exit(0)
}
if *printConfigHelp {
fmt.Fprintf(os.Stderr, "%s\n\n%s\n", strings.Trim(configFileHelp, "\n"), strings.Trim(defaultTomlConfig, "\n"))
os.Exit(0)
}
config.BindAddr = strings.Split(*bindAddr, ",")
// If a configuration file is specified, read all options from it. This discards all options set on the command-line.
if config.ConfigPath != "" {
if b, err := ioutil.ReadFile(config.ConfigPath); err != nil {
fmt.Fprintf(os.Stderr, "Error reading config file '%s': %s\n", config.ConfigPath, err)
os.Exit(1)
} else {
config = makeConfig(string(b))
}
}
// Ensure that relative root is always '/' or '/$arg/'.
config.RelativeRoot = "/" + strings.TrimLeft(config.RelativeRoot, "/")
config.RelativeRoot = strings.TrimRight(config.RelativeRoot, "/") + "/"
// Handle command-line file specs
filespecs := make([]FileSpec, len(flag.Args()))
for _, spec := range flag.Args() {
if filespec, err := parseFileSpec(spec); err != nil {
fmt.Fprintf(os.Stderr, "Error parsing argument '%s': %s\n", spec, err)
os.Exit(1)
} else {
config.FileSpecs = append(filespecs, filespec) // from joshuaboniface fork
filespecs = append(filespecs, filespec) // original one
config.FileSpecs = filespecs // this wasn't here. It was on 319
}
}
//config.FileSpecs = filespecs was here
if len(config.FileSpecs) == 0 {
fmt.Fprintln(os.Stderr, "No files specified on command-line or in config file")
os.Exit(2)
}
config.CommandScripts = make(map[string]string)
for cmd, values := range config.CommandSpecs {
config.CommandScripts[cmd] = values.Default
}
log.Print("Generating initial file listing...")
createListing(config.FileSpecs)
var wg sync.WaitGroup
for _, addr := range config.BindAddr {
wg.Add(1)
go startServer(config, addr)
}
wg.Wait()
}
func startServer(config *Config, bindAddr string) {
loggerHTML := log.New(os.Stdout, "", log.LstdFlags)
loggerHTML.Printf("Server start, relative-root: %s, bind-addr: %s\n", config.RelativeRoot, bindAddr)
server := setupServer(config, bindAddr, loggerHTML)
if strings.Contains(bindAddr, ":") {
server.ListenAndServe()
} else {
os.Remove(bindAddr)
unixAddr, _ := net.ResolveUnixAddr("unix", bindAddr)
unixListener, err := net.ListenUnix("unix", unixAddr)
unixListener.SetUnlinkOnClose(true)
if err != nil {
panic(err)
}
defer unixListener.Close()
server.Serve(unixListener)
}
}