|
@@ -10,9 +10,7 @@ import (
|
|
|
"net"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
- "os/signal"
|
|
|
"strings"
|
|
|
- "syscall"
|
|
|
"time"
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
@@ -24,7 +22,6 @@ import (
|
|
|
|
|
|
"git.scraperwall.com/scw/data"
|
|
|
"git.scraperwall.com/scw/ip"
|
|
|
- "git.scraperwall.com/scw/pidfile"
|
|
|
)
|
|
|
|
|
|
var (
|
|
@@ -38,7 +35,6 @@ var (
|
|
|
sleepFor = flag.Duration("sleep", 0, "Sleep this long between sending data (only when replaying a file)")
|
|
|
requestsFile = flag.String("requests", "", "CSV file containing requests (IP and URL)")
|
|
|
useXForwardedAsSource = flag.Bool("use-x-forwarded", false, "Use the IP address in X-Forwarded-For as source")
|
|
|
- pidFile = flag.String("pidfile", "/var/run/munchclient.pid", "The location of the PID file")
|
|
|
configFile = flag.String("config", "", "The location of the TOML config file")
|
|
|
beQuiet = flag.Bool("quiet", true, "Be quiet")
|
|
|
doVersion = flag.Bool("version", false, "Show version information")
|
|
@@ -68,47 +64,29 @@ type Config struct {
|
|
|
SleepFor time.Duration
|
|
|
RequestsFile string
|
|
|
UseXForwardedAsSource bool
|
|
|
- PidFile string
|
|
|
Quiet bool
|
|
|
}
|
|
|
|
|
|
+func (c Config) print() {
|
|
|
+ fmt.Printf("Live: %t\n", c.Live)
|
|
|
+ fmt.Printf("Interface: %s\n", c.Interface)
|
|
|
+ fmt.Printf("SnapshotLen: %d\n", c.SnapshotLen)
|
|
|
+ fmt.Printf("Filter: %s\n", c.Filter)
|
|
|
+ fmt.Printf("Promiscuous: %t\n", c.Promiscuous)
|
|
|
+ fmt.Printf("NatsURL: %s\n", c.NatsURL)
|
|
|
+ fmt.Printf("NatsQueue: %s\n", c.NatsQueue)
|
|
|
+ fmt.Printf("SleepFor: %s\n", c.SleepFor.String())
|
|
|
+ fmt.Printf("RequestsFile: %s\n", c.RequestsFile)
|
|
|
+ fmt.Printf("UseXForwardedAsSource: %t\n", c.UseXForwardedAsSource)
|
|
|
+ fmt.Printf("Quiet: %t\n", c.Quiet)
|
|
|
+}
|
|
|
+
|
|
|
func init() {
|
|
|
flag.Parse()
|
|
|
|
|
|
nats.RegisterEncoder(protobuf.PROTOBUF_ENCODER, &protobuf.ProtobufEncoder{})
|
|
|
}
|
|
|
|
|
|
-func loadConfig() {
|
|
|
-
|
|
|
- // initialize with values from the command line / environment
|
|
|
- config.Live = *doLiveCapture
|
|
|
- config.Interface = *iface
|
|
|
- config.SnapshotLen = *snapshotLen
|
|
|
- config.Filter = *filter
|
|
|
- config.Promiscuous = *promiscuous
|
|
|
- config.NatsURL = *natsURL
|
|
|
- config.NatsQueue = *natsQueue
|
|
|
- config.SleepFor = *sleepFor
|
|
|
- config.RequestsFile = *requestsFile
|
|
|
- config.UseXForwardedAsSource = *useXForwardedAsSource
|
|
|
- config.PidFile = *pidFile
|
|
|
- config.Quiet = *beQuiet
|
|
|
-
|
|
|
- if *configFile == "" {
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- _, err := os.Stat(*configFile)
|
|
|
- if err != nil {
|
|
|
- log.Printf("%s: %s\n", *configFile, err)
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- if _, err = toml.DecodeFile(*configFile, &config); err != nil {
|
|
|
- log.Printf("%s: %s\n", *configFile, err)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
func main() {
|
|
|
if *doVersion {
|
|
|
version()
|
|
@@ -117,29 +95,6 @@ func main() {
|
|
|
|
|
|
loadConfig()
|
|
|
|
|
|
- // Clean up before exiting
|
|
|
- //
|
|
|
- sigs := make(chan os.Signal, 1)
|
|
|
- signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
- go func() {
|
|
|
- <-sigs
|
|
|
- pidfile.Remove(config.PidFile)
|
|
|
- os.Exit(0)
|
|
|
- }()
|
|
|
-
|
|
|
- // Write the PID file
|
|
|
- //
|
|
|
- err := pidfile.Write(config.PidFile)
|
|
|
- // if the PID file is stale remove it and create a new one with the current PID
|
|
|
- if err == pidfile.ErrFileStale {
|
|
|
- err2 := os.Remove(config.PidFile)
|
|
|
- if err2 != nil {
|
|
|
- log.Fatalf("%s: %s\n", config.PidFile, err)
|
|
|
- }
|
|
|
- } else if err != nil { // permission denied or program is still running
|
|
|
- log.Fatalf("%s: %s\n", config.PidFile, err)
|
|
|
- }
|
|
|
-
|
|
|
// Output how many requests per second were sent
|
|
|
if !config.Quiet {
|
|
|
go func(c *uint64) {
|
|
@@ -366,6 +321,40 @@ func replayFile() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func loadConfig() {
|
|
|
+
|
|
|
+ // initialize with values from the command line / environment
|
|
|
+ config.Live = *doLiveCapture
|
|
|
+ config.Interface = *iface
|
|
|
+ config.SnapshotLen = *snapshotLen
|
|
|
+ config.Filter = *filter
|
|
|
+ config.Promiscuous = *promiscuous
|
|
|
+ config.NatsURL = *natsURL
|
|
|
+ config.NatsQueue = *natsQueue
|
|
|
+ config.SleepFor = *sleepFor
|
|
|
+ config.RequestsFile = *requestsFile
|
|
|
+ config.UseXForwardedAsSource = *useXForwardedAsSource
|
|
|
+ config.Quiet = *beQuiet
|
|
|
+
|
|
|
+ if *configFile == "" {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ _, err := os.Stat(*configFile)
|
|
|
+ if err != nil {
|
|
|
+ log.Printf("%s: %s\n", *configFile, err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if _, err = toml.DecodeFile(*configFile, &config); err != nil {
|
|
|
+ log.Printf("%s: %s\n", *configFile, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ if !config.Quiet {
|
|
|
+ config.print()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// version outputs build information
|
|
|
func version() {
|
|
|
fmt.Printf("munchclient %s, built on %s\n", Version, BuildDate)
|