Tobias von Dewitz 7 rokov pred
rodič
commit
64bd8701b9
5 zmenil súbory, kde vykonal 80 pridanie a 77 odobranie
  1. 8 5
      build-rpm.sh
  2. 5 3
      init.d/munchclient
  3. 10 0
      logrotate.d/munchclient
  4. 48 59
      main.go
  5. 9 10
      munchclient.toml

+ 8 - 5
build-rpm.sh

@@ -13,12 +13,13 @@ RPM_DIR=/opt/rpm.scraperwall.com/centos6
 
 
 rm -rf $DESTDIR
-install -v -d $DESTDIR/{usr/bin,etc/init.d,etc/default}
+install -d $DESTDIR/{usr/bin,etc/init.d,etc/default,etc/logrotate.d}
 make
 
 install -v -m 755 $BINARY $DESTDIR/usr/bin/
 install -v -m 644 defaults/$DEFAULTS_FILE $DESTDIR/etc/default/
-install -v -m 644 munchclient.toml /etc/
+install -v -m 644 logrotate.d/munchclient $DESTDIR/etc/logrotate.d/
+install -v -m 644 munchclient.toml $DESTDIR/etc/
 
 
 fpm -s dir -t $PKG_TYPE -C $DESTDIR --name $BINARY \
@@ -28,9 +29,11 @@ fpm -s dir -t $PKG_TYPE -C $DESTDIR --name $BINARY \
   --config-files "etc/default/$BINARY" \
   --config-files "etc/$CONFIG_FILE" \
   --rpm-init "init.d/$BINARY" \
-  -p rmps \
-	--rpm-sign
-  # --deb-systemd $SERVICE_FILE \
+  --rpm-trigger-after-install "[]munchclient: ./after-install-trigger.sh" \
+  --rpm-trigger-before-uninstall "[]munchclient: ./before-uninstall-trigger.sh" \
+  -p rpms \
+  -d go-daemon \
+  --rpm-sign
 
 ok=$?
 

+ 5 - 3
init.d/munchclient

@@ -21,12 +21,14 @@ RETVAL=0
 PIDFILE=/var/run/munchclient.pid
 
 prog=munchclient
-exec=/usr/bin/munchclient
+exec=/usr/bin/$prog
+cmd="/usr/bin/god --nohup --logfile /var/log/munchclient.log --pidfile $PIDFILE --rundir /tmp -- /usr/bin/munchclient -config /etc/munchclient.toml"
 lockfile=/var/lock/subsys/$prog
 
+
 # Source config
 if [ -f /etc/default/$prog ] ; then
-    . /etc/default/$prog
+    source /etc/default/$prog
 fi
 
 start() {
@@ -35,7 +37,7 @@ start() {
 	umask 077
 
         echo -n $"Starting ScraperWall request collector: "
-        daemon --pidfile="$PIDFILE" $exec -config /etc/munchclient.toml -pidfile "$PIDFILE"
+        daemon --pidfile="$PIDFILE" $cmd
         RETVAL=$?
         echo
         [ $RETVAL -eq 0 ] && touch $lockfile

+ 10 - 0
logrotate.d/munchclient

@@ -0,0 +1,10 @@
+/var/log/munchclient.log {
+    weekly
+    missingok
+    notifempty
+    rotate 4 
+    minsize 10000000
+    postrotate
+    pkill /etc/init.d/munchclient restart || true
+    endscript
+}

+ 48 - 59
main.go

@@ -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)

+ 9 - 10
munchclient.toml

@@ -1,12 +1,11 @@
-Live = true
-Interface = "eth0"
-SnapshotLen = 8192
-Filter = "tcp port 80 and dst 10.1.1.1"
-Promiscuous = false
-NatsURL = "nats://127.0.0.1:4222"
-NatsQueue = "requests"
-UseXForwardedAsSource = true
-PidFile = "/tmp/munchclient.pid"
-Quiet = true
+# Live = true
+# Interface = "eth0"
+# SnapshotLen = 8192
+# Filter = "tcp port 80 and dst 10.1.1.1"
+# Promiscuous = false
+# NatsURL = "nats://192.168.122.1:4222"
+# NatsQueue = "requests"
+# UseXForwardedAsSource = true
+# Quiet = true
 # SleepFor = 100µs
 # RequestsFile = requests.csv