Exemplo n.º 1
0
func main() {
	flag.Parse()

	if *versionFlag {
		fmt.Println(shh.Version())
		os.Exit(0)
	}

	measurements := make(chan shh.Measurement, 100)
	config := shh.GetConfig()

	mp := shh.NewMultiPoller(measurements, config)

	signal.Notify(signalChannel, syscall.SIGINT)
	signal.Notify(signalChannel, syscall.SIGTERM)

	go func() {
		for sig := range signalChannel {
			now := time.Now()
			shh.ErrLogger.Println(slog.Context{"signal": sig, "finishing": now, "runtime": time.Since(config.Start)})
			mp.Exit()
			shh.ErrLogger.Fatalln(slog.Context{"signal": sig, "finished": time.Now(), "duration": time.Since(now)})
		}
	}()

	if config.ProfilePort != shh.DEFAULT_PROFILE_PORT {
		go func() {
			shh.Logger.Println(http.ListenAndServe("localhost:"+config.ProfilePort, nil))
		}()
	}

	ctx := slog.Context{"start": true, "interval": config.Interval}
	shh.Logger.Println(ctx)

	outputter, err := shh.NewOutputter(config.Outputter, measurements, config)
	if err != nil {
		shh.FatalError(ctx, err, "creating outputter")
	}
	outputter.Start()

	start := make(chan time.Time, 1)
	start <- time.Now()
	ticks := time.Tick(config.Interval)

	for {
		select {
		case tick := <-start:
			mp.Poll(tick)
			start = nil
		case tick := <-ticks:
			mp.Poll(tick)
		}
	}
}
Exemplo n.º 2
0
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "usage: %s [options] <metric-name> <value>\n", os.Args[0])
		flag.PrintDefaults()
	}

	flag.Parse()

	if *versionFlag {
		fmt.Println(shh.Version())
		os.Exit(0)
	}

	if flag.NArg() != 2 {
		die("")
	}

	metric := assertValidMetricName(flag.Arg(0))
	unit := assertValidUnit(*unitFlag)
	mmType := assertValidType(*measurementType)
	value := assertValidValue(flag.Arg(1))

	if !shh.MetricNameRegexp.MatchString(flag.Arg(0)) {
		die("ERROR: invalid metric name\n")
	}

	conn, err := getConnection(shh.GetEnvWithDefault("SHH_ADDRESS", *shhAddr))
	if err != nil {
		die(fmt.Sprintf("ERROR: couldn't get connection to %s: %s\n", *shhAddr, err))
	}
	conn.SetDeadline(time.Now().Add(time.Second * 5))
	line := formatLine(metric, value, mmType, unit)
	fmt.Printf(line)
	fmt.Fprintf(conn, line)
	conn.Close()
}