Пример #1
0
func main() {
	log.AddFilter("stderr", l4g.DEBUG,
		l4g.NewFormatLogWriter(os.Stderr, "[%d %t] [%L] %M"))
	defer log.Close()
	ParseArgs()

	// var rootDb *mymysql.MySQL
	// var leafDb *mymysql.MySQL

	// verify database connectability
	// if *rootHost != "" {
	// 	rootAddr := fmt.Sprintf("%v:%v", *rootHost, *rootPort)
	// 	rootDb = mymysql.New("tcp", "", rootAddr, *rootUser, *rootPass)
	// 	rootDb.Register("set names utf8")
	// 	rows, _, err := rootDb.QueryAC("SELECT * FROM mysql.user")
	// 	if err != nil {
	// 		log.Error(err)
	// 		return
	// 	}
	// 	// defer log.Info(rootDb)
	// 	// defer rootDb.Close()
	// 	// defer log.Info(rootDb)
	// 	log.Info(len(rows))
	// 	for i, _ := range rows {
	// 		log.Info("%v\n", i)
	// 	}
	// 	log.Close()
	// }
}
Пример #2
0
func main() {
	// parse command line flags
	parseFlags()
	// init logger
	log.AddFilter(*logFilename, logLevel,
		l4g.NewFormatLogWriter(logFile, "[%d %t] [%L] %M"))
	defer log.Close()
	sqlog.AddFilter(*sqlogFilename, sqlogLevel,
		l4g.NewFormatLogWriter(sqlogFile, "[%d %t] %M"))
	defer sqlog.Close()
	// global error catching
	defer func() {
		exit(recover())
	}()
	createPidfile()
	// spawn os signal handler
	go func() {
		for {
			switch sig := (<-signal.Incoming).(os.UnixSignal); sig {
			case os.SIGINT, os.SIGHUP, os.SIGQUIT, os.SIGTERM, os.SIGKILL:
				{
					exit(fmt.Sprintf("%v received", sig))
				}
			}
		}
	}()

	// query slave status from MySQL instance
	db := mysql.New("tcp", "",
		server.Host+":"+strconv.Itoa(server.Port), server.User, server.Pass)
	defer db.Close()
	db.Debug = false
	log.Info("starting %v...", cmdname)
	if *batchMode {
		err := db.Connect()
		if err != nil {
			panic(fmt.Sprintf("can't connect to %v on port %v: %v",
				server.Host, server.Port, err))
		}
		processRplStatus(db)
	} else {
		for {
			if !db.IsConnected() {
				log.Info("connecting to %v port %v", server.Host, server.Port)
				err := db.Connect()
				if err != nil {
					log.Warn("can't connect to %v on port %v: %v",
						server.Host, server.Port, err)
					log.Warn("retry in %v seconds...", *retryInterval)
					// sleep for timeInterval minute(s)
					time.Sleep(int64(*retryInterval) * 1e9)
					continue
				}
				log.Info("connection established. start monitoring.")
			}
			slave, reconnect := processRplStatus(db)
			if !slave {
				// target server is not eligible to be monitored
				break
			}
			if reconnect {
				log.Warn("retry in %v seconds...", *retryInterval)
				db.Close()
				time.Sleep(int64(*retryInterval) * 1e9)
			} else {
				time.Sleep(int64(*interval) * 1e9)
			}
		}
	}
	exit(nil)
}