Пример #1
0
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 = "RANDFILES_"
	flagenv.Parse()

	if help {
		showHelp()
		return
	}

	// setup number of CPUs
	runtime.GOMAXPROCS(cpus)

	name, _ := os.Hostname()

	http.Handle("/", http.HandlerFunc(kubismus.ServeHTTP))

	kubismus.Setup("randfiles", "")
	kubismus.Note("Host Name", name)
	kubismus.Note("CPUs", fmt.Sprintf("%d of %d", runtime.GOMAXPROCS(0), runtime.NumCPU()))
	kubismus.Note("Delay", delay.String())
	kubismus.Note("Writer threads", fmt.Sprintf("%d", threads))
	kubismus.Note("File size", fmt.Sprintf("%d to %d bytes", minSize, maxSize))
	kubismus.Note("File extension", ext)
	kubismus.Define("Data", kubismus.COUNT, "Files/second")
	kubismus.Define("Data", kubismus.SUM, "Bytes/second")

	// 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)
	}

	rand.Seed(time.Now().UnixNano())
	for i := 0; i < threads; i++ {
		go writeFiles(minSize, maxSize, delay, ext)
	}
	go calcMetrics()

	log.Fatal(http.ListenAndServe(addr, nil))
}
Пример #2
0
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 = "WEBNULL_"
	flagenv.Parse()

	if help {
		showHelp()
		return
	}

	// setup number of CPUs
	runtime.GOMAXPROCS(cpus)

	name, _ := os.Hostname()

	http.Handle("/status/", http.StripPrefix("/status", http.HandlerFunc(kubismus.ServeHTTP)))
	http.Handle("/", kubismus.HttpRequestMetric("Requests", handleRequest()))
	http.HandleFunc("/media/", ServeHTTP)

	kubismus.Setup("/web/null", "/media/null.png")
	kubismus.Note("Host Name", name)
	kubismus.Note("CPUs", fmt.Sprintf("%d of %d", runtime.GOMAXPROCS(0), runtime.NumCPU()))
	kubismus.Define("Requests", kubismus.COUNT, "Requests/second")
	kubismus.Define("Requests", kubismus.SUM, "Bytes/second")

	go calcMetrics()

	log.Fatal(http.ListenAndServe(addr, nil))
}
Пример #3
0
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 = "HURL_"
	flagenv.Parse()

	if help {
		showHelp()
		return
	}

	if version {
		showVersion()
		return
	}

	if flag.NArg() < 1 {
		fmt.Fprintf(os.Stderr, "Please provide a URL to fetch.\n")
		return
	}

	err := 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()
		}()
	}

	// create HTTP transport and client
	transport = &http.Transport{DisableKeepAlives: noKeepAlive, MaxIdleConnsPerHost: conns, DisableCompression: noCompress, ResponseHeaderTimeout: timeout}
	client = &http.Client{Transport: transport, Timeout: timeout}

	// setup Kubismus
	kubismus.Setup("hURL", "/media/logo36.png")
	kubismus.Define("Sent", kubismus.COUNT, "HTTP Posts")
	kubismus.Define("Sent", kubismus.SUM, "Bytes Sent")
	kubismus.Define("Received", kubismus.SUM, "Bytes Received")
	kubismus.Define("Received100", kubismus.COUNT, "1xx Responses")
	kubismus.Define("Received200", kubismus.COUNT, "2xx Responses")
	kubismus.Define("Received300", kubismus.COUNT, "3xx Responses")
	kubismus.Define("Received400", kubismus.COUNT, "4xx Responses")
	kubismus.Define("Received500", kubismus.COUNT, "5xx Responses")
	kubismus.Define("Error", kubismus.COUNT, "Communication Errors")
	kubismus.Define("ResponseTime", kubismus.AVERAGE, "Average Time (s)")
	kubismus.Note("Concurrent Connections", strconv.Itoa(conns))
	kubismus.Note("HTTP Method", method)
	kubismus.Note("Timeout", timeout.String())
	kubismus.Note("Processors", fmt.Sprintf("%d of %d", runtime.GOMAXPROCS(0), runtime.NumCPU()))
	kubismus.Note("Data files", strings.Join(strings.Split(filesPat, ","), "\n"))
	kubismus.Note("URLs", strings.Join(flag.Args(), "\n"))
	kubismus.Note("Discard files", strconv.FormatBool(discard))
	http.Handle("/", http.HandlerFunc(kubismus.ServeHTTP))
	http.HandleFunc("/media/", ServeHTTP)

	// 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)
	}

	// 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()))
			}
		}
	}()

	// spawn the status web site
	go func() {
		log.Fatal(http.ListenAndServe(addr, nil))
	}()

	// 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
	var patList []string
	if filesPat != "" {
		patList = strings.Split(filesPat, ",")
	}
	ch1 := loopCount(ctx, loop)
	ch2 := loopUrls(ctx, method, flag.Args(), ch1)
	ch3 := loopFiles(ctx, patList, ch2)
	doHttp(ctx, conns, ch3)

	// write memory profile if configured
	if memProfile != "" {
		f, err := os.Create(memProfile)
		if err != nil {
			log.Print(err)
		} else {
			log.Print("Writing memory profile to ", memProfile)
			pprof.WriteHeapProfile(f)
			f.Close()
		}
	}
}
Пример #4
0
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.HandleFunc("/media/", ServeHTTP)

	// 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))
}
Пример #5
0
// program start
func main() {
	var err error

	sconf := service.Config{
		Name:        "BabelPoxy",
		DisplayName: "Babel Proxy",
		Description: "babelproxy converts RESTful calls into Babel calls.",
	}

	// 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 = "BABELPROXY_"
	flagenv.Parse()

	// show help if asked
	if help {
		fmt.Fprintln(os.Stderr, `Usage of babelproxy:
  babelproxy [options] <filepatterns>

"For the faasti RESTafarians! (Feel no way, we maas.)"

Example:
  babelproxy *.babel

Options include:`)
		flag.PrintDefaults()
		fmt.Fprintln(os.Stderr, `
All of the options can be overridden in the configuration file. To override
the location of the configuration file, set the BABELPROXY_CONFIG environment
variable. When running as a service, all options must be specified in the
configuration file or environment variables.`)
		return
	}

	// show version if asked
	if ver {
		fmt.Printf("babelproxy Version %s\n", BABELPROXY_VERSION)
		return
	}

	// default the pubaddr
	if strings.TrimSpace(conf.PubAddr) == "" {
		conf.PubAddr = conf.RestAddr
	}

	// handle kill signals
	go func() {
		// setup cpu profiling if desired
		var f io.WriteCloser
		var err error
		if conf.CpuProfile != "" {
			f, err = os.Create(conf.CpuProfile)
			if err != nil {
				log.Fatal(err)
			}
			pprof.StartCPUProfile(f)
		}

		// 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")

		if conf.CpuProfile != "" {
			log.Print("Writing CPU profile to ", conf.CpuProfile)
			pprof.StopCPUProfile()
			f.Close()
		}

		// write memory profile if configured
		if conf.MemProfile != "" {
			f, err := os.Create(conf.MemProfile)
			if err != nil {
				log.Print(err)
			} else {
				log.Print("Writing memory profile to ", conf.MemProfile)
				pprof.WriteHeapProfile(f)
				f.Close()
			}
		}
		os.Exit(0)
	}()

	// setup number of CPUs
	runtime.GOMAXPROCS(conf.Cpus)

	// set working directory
	if conf.WorkingDir != "" {
		err = os.Chdir(conf.WorkingDir)
		if err != nil {
			log.Fatal(err)
		}
	}

	// override configured arguments (file list) with command line
	if flag.NArg() > 0 {
		conf.Args = flag.Args()
	}

	var i svcImpl
	svcBroxy, err = service.New(&i, &sconf)
	if err != nil {
		log.Fatal(err)
	}
	/*
		svcLogger, err = svcBroxy.Logger(nil)
		if err != nil {
			log.Fatal(err)
		}
	*/
	if control != "" {
		err = service.Control(svcBroxy, control)
		if err != nil {
			log.Fatal(err)
		}
		return
	}

	if service.Interactive() {
		i.doWork()
	} else {
		err = svcBroxy.Run()
		if err != nil {
			//svcLogger.Error(err.Error())
			log.Println(err)
		}
	}
}
Пример #6
0
func init() {
	const (
		name        = "HashSrv"
		displayName = "HashSrv"
		desc        = "HashSrv is a service for hashing, compressing, encrypting, and encoding things."
	)

	var help bool
	var ver bool
	var noisy bool
	var svcInstall bool
	var svcRemove bool
	var svcStart bool
	var svcStop bool

	flag.BoolVar(&noisy, "noisy", false, "Enable logging")
	flag.BoolVar(&help, "help", false, "Show command help")
	flag.BoolVar(&ver, "version", false, "Show version")
	flag.StringVar(&cpuprofile, "cpuprofile", "", "Write CPU profile to file")
	flag.StringVar(&memprofile, "memprofile", "", "Write memory profile to file")
	flag.BoolVar(&svcInstall, "install", false, "Install HashSrv as a service")
	flag.BoolVar(&svcRemove, "remove", false, "Remove the HashSrv service")
	flag.BoolVar(&svcRun, "run", false, "Run HashSrv standalone (not as a service)")
	flag.BoolVar(&svcStart, "start", false, "Start the HashSrv service")
	flag.BoolVar(&svcStop, "stop", false, "Stop the HashSrv service")
	flag.StringVar(&hostAddr, "addr", ":9009", "Address to host the service on")
	flag.Parse()
	flagcfg.AddDefaults()
	flagcfg.Parse()
	flagenv.Prefix = "HASHSRV_"
	flagenv.Parse()

	if help {
		usage()
		os.Exit(0)
	}

	if ver {
		fmt.Printf("HashSrv Version %s\n", HASHSRV_VERSION)
		os.Exit(0)
	}

	var err error
	var i impl
	wsHashSrv, err = service.New(i, &service.Config{Name: name, DisplayName: displayName, Description: desc})
	if err != nil {
		log.Fatal(err)
	}
	wsLogger, err = wsHashSrv.Logger(nil)
	if err != nil {
		log.Fatal(err)
	}

	if svcInstall == true && svcRemove == true {
		log.Fatalln("Options -install and -remove cannot be used together.")
	} else if svcInstall == true {
		err = wsHashSrv.Install()
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("Service \"%s\" installed.\n", displayName)
		os.Exit(0)
	} else if svcRemove == true {
		err = wsHashSrv.Uninstall()
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("Service \"%s\" removed.\n", displayName)
		os.Exit(0)
	} else if svcStart == true {
		err = wsHashSrv.Start()
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("Service \"%s\" started.\n", displayName)
		os.Exit(0)
	} else if svcStop == true {
		err = wsHashSrv.Stop()
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("Service \"%s\" stopped.\n", displayName)
		os.Exit(0)
	}

	if noisy == false {
		log.SetOutput(ioutil.Discard)
	}
}