Example #1
0
func main() {
	var (
		databaseMasterHost = flag.String("database-master-host", "localhost", "Database master host")
		databaseMasterPort = flag.Int("database-master-port", 3306, "Database master port")
		mh                 = flag.String("mh", "localhost", "a shortcut for database-master-host")
	)
	envflag.SetDebugEnabled(true)
	if err := envflag.Parse(); err != nil {
		panic(err)
	}
	fmt.Println("RESULT: ", *databaseMasterHost, ":", *databaseMasterPort, *mh)
}
Example #2
0
func main() {
	var (
		meterId       = flag.String("meter-id", "REQUIRED", "ID of the meter to read from")
		libratoUser   = flag.String("librato-user", "REQUIRED", "User for Librato")
		libratoToken  = flag.String("librato-token", "REQUIRED", "Token for Librato")
		libratoSource = flag.String("librato-source", "REQUIRED", "Source name for librato")
		gasPriceIn    = flag.String("gas-price", "OPTIONAL", "Price of gas Will report as additional metric")
	)
	if err := envflag.Parse(); err != nil {
		panic(err)
	}
	gasPriceInFl, err := strconv.ParseFloat(*gasPriceIn, 64)
	if err != nil {
		fmt.Println("gas-price has to be a number")
		os.Exit(1)
	}
	gasPrice = gasPriceInFl
	if *meterId == "REQUIRED" {
		fmt.Println("meter-id is a required field")
		os.Exit(1)
	}
	if *libratoUser == "REQUIRED" {
		fmt.Println("librato-user is a required field")
		os.Exit(1)
	}
	if *libratoToken == "REQUIRED" {
		fmt.Println("librato-token is a required field")
		os.Exit(1)
	}
	if *libratoSource == "REQUIRED" {
		fmt.Println("librato-source is a required field")
		os.Exit(1)
	}

	metrics := librato.NewSimpleMetrics(*libratoUser, *libratoToken, *libratoSource)
	defer metrics.Wait()
	defer metrics.Close()
	consumptionCounter = metrics.GetCounter("sensor.utility.gas.cf")
	costGauge = metrics.GetGauge("sensor.utility.gas.spend")

	cmdName := "rtlamr"
	cmdArgs := []string{"-format=json", "-filterid=" + *meterId}

	cmd := exec.Command(cmdName, cmdArgs...)
	cmdReader, err := cmd.StdoutPipe()
	if err != nil {
		panic(err)
	}

	scanner := bufio.NewScanner(cmdReader)
	go func() {
		for scanner.Scan() {
			text := scanner.Text()
			line := &MeterRead{}
			if err := json.Unmarshal([]byte(text), &line); err != nil {
				fmt.Fprintln(os.Stderr, fmt.Sprintf("Error unmarshaling line (%s):| %s", err, text))
				continue
			}
			fmt.Print("reading received: ")
			processLine(line)
		}
	}()

	err = cmd.Start()
	if err != nil {
		fmt.Fprintln(os.Stderr, "Error starting Cmd", err)
		os.Exit(1)
	}

	err = cmd.Wait()
	if err != nil {
		time.Sleep(2000) // let the scanner finish. there's gotta be a better way
		fmt.Fprintln(os.Stderr, "Command exited", err)
		os.Exit(1)
	}
}