/
main.go
262 lines (221 loc) · 7.06 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
package main
import (
"context"
"embed"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"time"
"github.com/ancientlore/flagcfg"
"github.com/ancientlore/kubismus"
"github.com/facebookgo/flagenv"
)
var (
addr string
cpuProfile string
memProfile string
cpus int
workingDir string
version bool
help bool
defaultCfg DefaultCfg
//go:embed media/*.png
media embed.FS
)
func init() {
// Set initial settings
defaultCfg.Init()
// http service/status address
flag.StringVar(&addr, "addr", ":8080", "HTTP service address for monitoring.")
// http post settings
flag.IntVar(&defaultCfg.Conns, "conns", defaultCfg.Conns, "Number of concurrent HTTP connections.")
flag.DurationVar((*time.Duration)(&defaultCfg.Timeout), "timeout", time.Duration(defaultCfg.Timeout), "HTTP timeout.")
flag.StringVar(&defaultCfg.FilesPat, "files", defaultCfg.FilesPat, "Pattern of files to post, like *.xml.")
flag.StringVar(&defaultCfg.Method, "method", defaultCfg.Method, "HTTP method.")
flag.StringVar(&defaultCfg.UseRequestID, "requestid", defaultCfg.UseRequestID, "Name of header to send a random GUID.")
flag.BoolVar(&defaultCfg.NoCompress, "nocompress", defaultCfg.NoCompress, "Disable HTTP compression.")
flag.BoolVar(&defaultCfg.NoKeepAlive, "nokeepalive", defaultCfg.NoKeepAlive, "Disable HTTP keep-alives.")
flag.BoolVar(&defaultCfg.FileInfo, "fileinfo", defaultCfg.FileInfo, "Whether to send file information headers.")
// processing
flag.DurationVar((*time.Duration)(&defaultCfg.SleepTime), "sleep", time.Duration(defaultCfg.SleepTime), "Interval to wait when no files are found.")
flag.Int64Var(&defaultCfg.MaxFileSize, "maxsize", defaultCfg.MaxFileSize, "Maximum file size to post.")
flag.IntVar(&defaultCfg.BatchSize, "batchsize", defaultCfg.BatchSize, "Readdir batch size.")
// headers
flag.StringVar(&defaultCfg.HeaderDelim, "hdrdelim", defaultCfg.HeaderDelim, "Delimiter for HTTP headers specified with -header.")
flag.StringVar(&defaultCfg.HeaderText, "headers", defaultCfg.HeaderText, "HTTP headers, delimited by -hdrdelim.")
// profiling
flag.StringVar(&cpuProfile, "cpuprofile", cpuProfile, "Write CPU profile to given file.")
flag.StringVar(&memProfile, "memprofile", memProfile, "Write memory profile to given file.")
// runtime
flag.IntVar(&cpus, "cpu", cpus, "Number of CPUs to use.")
flag.StringVar(&workingDir, "wd", workingDir, "Set the working directory.")
// help
flag.BoolVar(&version, "version", false, "Show version.")
flag.BoolVar(&help, "help", false, "Show help.")
}
func showHelp() {
fmt.Println(`
__ __ __ ______ __
________ __/ /_____ / /_ / / / / __ \/ /
/ __ / / / / __/ __ \/ __ \/ / / / /_/ / /
/ /_/ / /_/ / /_/ /_/ / / / / /_/ / _, _/ /___
\__,_/\__,_/\__/\____/_/ /_/\____/_/ |_/_____/
A tool to continuously post files found in a folder.
Usage:
autohurl [options] url1 [url2 ... urlN]
Example:
autohurl -method POST -files "*.xml" -conns 10
Options:`)
flag.PrintDefaults()
fmt.Println(`
All of the options can be set via environment variables prefixed with "AUTOHURL_" - for instance,
AUTOHURL_TIMEOUT can be set to "30s" to increase the default timeout.
Options can also be specified in a TOML configuration file named "autohurl.config". The location
of the file can be overridden with the AUTOHURL_CONFIG environment variable.`)
}
func showVersion() {
fmt.Printf("autohURL version %s\n", autohurlVersion)
}
func main() {
// Parse flags from command-line
flag.Parse()
// Parser flags from config
flagcfg.AddDefaults()
flagcfg.Parse()
// Parse flags from environment (using github.com/facebookgo/flagenv)
flagenv.Prefix = "AUTOHURL_"
flagenv.Parse()
if help {
showHelp()
return
}
if version {
showVersion()
return
}
// parse default headers - make sure they work
var err error
err = defaultCfg.ParseHeaders()
if err != nil {
log.Fatal(err)
}
// log.Printf("%#v", headers)
// setup number of CPUs
runtime.GOMAXPROCS(cpus)
// setup cpu profiling if desired
if cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer func() {
log.Print("Writing CPU profile to ", cpuProfile)
pprof.StopCPUProfile()
f.Close()
}()
}
// setup Kubismus
kubismus.Setup("autohURL", "/media/logo36.png")
kubismus.Note("Processors", fmt.Sprintf("%d of %d", runtime.GOMAXPROCS(0), runtime.NumCPU()))
http.Handle("/", http.HandlerFunc(kubismus.ServeHTTP))
http.Handle("/media/", http.FileServer(http.FS(media)))
// switch to working dir
if workingDir != "" {
err := os.Chdir(workingDir)
if err != nil {
log.Fatal(err)
}
}
wd, err := os.Getwd()
if err == nil {
kubismus.Note("Working Directory", wd)
}
// Print settings
fmt.Printf("addr = \"%s\"\n", addr)
fmt.Printf("cpu = %d\n", cpus)
fmt.Printf("wd = \"%s\"\n", workingDir)
fmt.Printf("cpuprofile = \"%s\"\n", cpuProfile)
fmt.Printf("memprofile = \"%s\"\n", memProfile)
fmt.Println()
// Print default folder settings
fmt.Printf("[folders.*]\n%s\n", defaultCfg.String())
// read folders
var cfg map[string]*FolderCfg
cfg, err = readConfig(flagcfg.Filename())
if err != nil {
log.Fatal(err)
}
if len(cfg) == 0 {
log.Fatal("No folders configured to watch")
}
// set up default settings
for i := range cfg {
cfg[i].SetDefaults(&defaultCfg)
err = cfg[i].ParseHeaders()
if err != nil {
log.Fatal(err)
}
fmt.Printf("[folders.%s]\n%s\n", i, cfg[i].String())
kubismus.Note("folders."+i, cfg[i].String())
kubismus.Define(i+"_Errors", kubismus.COUNT, i+": Errors")
kubismus.Define(i+"_Sent", kubismus.COUNT, i+": HTTP Posts")
kubismus.Define(i+"_Sent", kubismus.SUM, i+": Bytes Sent")
kubismus.Define(i+"_Received", kubismus.SUM, i+": Bytes Received")
kubismus.Define(i+"_ResponseTime", kubismus.AVERAGE, i+": Average Time (s)")
}
// setup the thread context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// spawn a function that updates the number of goroutines shown in the status page
go func() {
done := ctx.Done()
t := time.NewTicker(5 * time.Second)
defer t.Stop()
for {
select {
case <-done:
return
case <-t.C:
kubismus.Note("Goroutines", fmt.Sprintf("%d", runtime.NumGoroutine()))
}
}
}()
// handle kill signals
go func() {
// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, os.Kill)
// Block until a signal is received.
s := <-c
log.Print("Got signal ", s, ", canceling work")
cancel()
}()
// Build pipeline
for name, fldr := range cfg {
ch1 := readDir(ctx, name, fldr)
go doHTTP(ctx, name, fldr, ch1)
}
// write memory profile if configured
if memProfile != "" {
defer func() {
f, err := os.Create(memProfile)
if err != nil {
log.Print(err)
} else {
log.Print("Writing memory profile to ", memProfile)
pprof.WriteHeapProfile(f)
f.Close()
}
}()
}
// status web site
log.Fatal(http.ListenAndServe(addr, nil))
}