Exemple #1
0
func main() {
	var hostport, configfile string
	var step int
	flag.StringVar(&hostport, "address", "localhost:12345",
		"address to listen on for metrics json")
	flag.StringVar(&configfile, "cnf", "",
		"config file to grab thresholds from")
	flag.IntVar(&step, "step", 0, "seconds for a cycle of metric checks")
	flag.Parse()

	stepTime := time.Second * time.Duration(step)

	cnf, err := metricchecks.FileToConfig(configfile)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	c, err := metricchecks.New(hostport, cnf)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	err = c.NewScopeAndPackage()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	err = c.InsertMetricValuesFromJSON()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}

	_, err = c.CheckAll()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	if step > 0 {
		ticker := time.NewTicker(stepTime)
		for _ = range ticker.C {
			err := c.NewScopeAndPackage()
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				return
			}
			err = c.InsertMetricValuesFromJSON()
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				return
			}
			_, err = c.CheckAll()
			if err != nil {
				fmt.Fprintln(os.Stderr, err)
				return
			}
		}
	}
}
Exemple #2
0
func main() {
	var user, password, host, address, cnf, form, checkConfigFile string
	var stepSec int
	var servermode, human, loop bool
	var checkConfig *conf.ConfigFile

	m := metrics.NewMetricContext("system")

	flag.StringVar(&user, "u", "root", "user using database")
	flag.StringVar(&password, "p", "", "password for database")
	flag.StringVar(&host, "h", "",
		"address and protocol of the database to connect to. leave blank for tcp(127.0.0.1:3306)")
	flag.BoolVar(&servermode, "server", false,
		"Runs continously and exposes metrics as JSON on HTTP")
	flag.StringVar(&address, "address", ":12345",
		"address to listen on for http if running in server mode")
	flag.IntVar(&stepSec, "step", 2, "metrics are collected every step seconds")
	flag.StringVar(&cnf, "cnf", "/root/.my.cnf", "configuration file")
	flag.StringVar(&form, "form", "graphite", "output format of metrics to stdout")
	flag.BoolVar(&human, "human", false,
		"Makes output in MB for human readable sizes")
	flag.BoolVar(&loop, "loop", false, "loop on collecting metrics")
	flag.StringVar(&checkConfigFile, "check", "", "config file to check metrics with")
	flag.Parse()

	if servermode {
		go func() {
			http.HandleFunc("/api/v1/metrics.json/", m.HttpJsonHandler)
			log.Fatal(http.ListenAndServe(address, nil))
		}()
	}
	step := time.Millisecond * time.Duration(stepSec) * 1000

	var err error
	var c metricchecks.Checker
	checkConfig = conf.NewConfigFile()
	if checkConfigFile != "" {
		cnf, err := metricchecks.FileToConfig(checkConfigFile)
		if err != nil {
			checkConfigFile = ""
		} else {
			checkConfig = cnf
		}
	}

	c, err = metricchecks.New("", checkConfig)
	if err != nil {
		checkConfigFile = ""
	}

	//initialize metrics collectors to not loop and collect
	sqlstatDBs, err := dbstat.New(m, user, password, host, cnf)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	sqlstatTables, err := tablestat.New(m, user, password, host, cnf)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	sqlstatUsers, err := userstat.New(m, user, password, host, cnf)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	sqlstatDBs.Collect()
	sqlstatTables.Collect()
	sqlstatUsers.Collect()

	if checkConfigFile != "" {
		checkMetrics(c, m)
	}
	outputTableMetrics(sqlstatDBs, sqlstatTables, m, form)
	outputUserMetrics(sqlstatUsers, m, form)
	if loop {
		ticker := time.NewTicker(step)
		for _ = range ticker.C {
			sqlstatDBs.Collect()
			sqlstatTables.Collect()
			sqlstatUsers.Collect()
			outputTableMetrics(sqlstatDBs, sqlstatTables, m, form)
			outputUserMetrics(sqlstatUsers, m, form)
		}
	}
	sqlstatDBs.Close()
	sqlstatTables.Close()
	sqlstatUsers.Close()
}