/* Refresh a server object */
func (sm *ServerMonitor) refresh() error {
	err := sm.Conn.Ping()
	if err != nil {
		return err
	}
	sv, err := dbhelper.GetVariables(sm.Conn)
	if err != nil {
		return err
	}
	sm.BinlogPos = sv["GTID_BINLOG_POS"]
	sm.Strict = sv["GTID_STRICT_MODE"]
	sm.LogBin = sv["LOG_BIN"]
	sm.ReadOnly = sv["READ_ONLY"]
	sm.CurrentGtid = sv["GTID_CURRENT_POS"]
	sm.SlaveGtid = sv["GTID_SLAVE_POS"]
	sid, _ := strconv.ParseUint(sv["SERVER_ID"], 10, 0)
	sm.ServerId = uint(sid)
	slaveStatus, err := dbhelper.GetSlaveStatus(sm.Conn)
	if err != nil {
		return err
	}
	sm.UsingGtid = slaveStatus.Using_Gtid
	sm.IOThread = slaveStatus.Slave_IO_Running
	sm.SQLThread = slaveStatus.Slave_SQL_Running
	sm.Delay = slaveStatus.Seconds_Behind_Master
	sm.MasterServerId = slaveStatus.Master_Server_Id
	sm.MasterHost = slaveStatus.Master_Host
	return err
}
/* Refresh a server object */
func (server *ServerMonitor) refresh() error {
	err := server.Conn.Ping()
	if err != nil {
		// we want the failed state for masters to be set by the monitor
		if server.State != stateMaster {
			server.State = stateFailed
			// remove from slave list
			server.delete(&slaves)
		}
		return err
	}
	sv, err := dbhelper.GetVariables(server.Conn)
	if err != nil {
		return err
	}
	server.PrevState = server.State
	server.BinlogPos = sv["GTID_BINLOG_POS"]
	server.Strict = sv["GTID_STRICT_MODE"]
	server.LogBin = sv["LOG_BIN"]
	server.ReadOnly = sv["READ_ONLY"]
	server.CurrentGtid = sv["GTID_CURRENT_POS"]
	server.SlaveGtid = sv["GTID_SLAVE_POS"]
	sid, _ := strconv.ParseUint(sv["SERVER_ID"], 10, 0)
	server.ServerID = uint(sid)
	slaveStatus, err := dbhelper.GetSlaveStatus(server.Conn)
	if err != nil {
		// If we reached this stage with a previously failed server, reintroduce
		// it as unconnected server.
		if server.State == stateFailed {
			server.State = stateUnconn
			if autorejoin {
				if verbose {
					logprint("INFO : Rejoining previously failed server", server.URL)
				}
				err := server.rejoin()
				if err != nil {
					logprint("ERROR: Failed to autojoin previously failed server", server.URL)
				}
			}
		}
		return err
	}
	server.UsingGtid = slaveStatus.Using_Gtid
	server.IOThread = slaveStatus.Slave_IO_Running
	server.SQLThread = slaveStatus.Slave_SQL_Running
	server.Delay = slaveStatus.Seconds_Behind_Master
	server.MasterServerID = slaveStatus.Master_Server_Id
	server.MasterHost = slaveStatus.Master_Host
	// In case of state change, reintroduce the server in the slave list
	if server.PrevState == stateFailed || server.PrevState == stateUnconn {
		server.State = stateSlave
		slaves = append(slaves, server)
	}
	return err
}
Exemple #3
0
func main() {

	flag.Parse()
	if *version == true {
		fmt.Println("MariaDB Tools version 0.0.1")
		os.Exit(0)
	}
	var address string
	if *socket != "" {
		address = "unix(" + *socket + ")"
	}
	if *host != "" {
		address = "tcp(" + *host + ":" + *port + ")"
	}

	db, _ := sqlx.Open("mysql", *user+":"+*password+"@"+address+"/")
	err := db.Ping()
	if err != nil {
		log.Fatal(err)
	}

	defer db.Close()

	status = dbhelper.GetStatusAsInt(db)
	variable, _ = dbhelper.GetVariables(db)

	out, err := exec.Command("uname", "-srm").Output()
	if err != nil {
		log.Fatal(err)
	}
	hostname, _ := os.Hostname()
	fmt.Printf("### MariaDB Server report for host %s\n", hostname)
	fmt.Printf("### %-25s%s", "Kernel version", out)
	fmt.Printf("### %-25s%s\n", "System Time", time.Now().Format("2006-01-02 at 03:04 (MST)"))
	fmt.Println(common.DrawHashline("General", 60))
	var server_version string
	db.QueryRow("SELECT VERSION()").Scan(&server_version)
	pPrintStr("Version", server_version)
	now := time.Now().Unix()
	uptime := status["UPTIME"]
	start_time := time.Unix(now-uptime, 0).Local()
	pPrintStr("Started", humanize.Time(start_time))
	var count int64
	db.Get(&count, "SELECT COUNT(*) FROM information_schema.schemata")
	pPrintInt("Databases", count)
	db.Get(&count, "SELECT COUNT(*) FROM information_schema.tables")
	pPrintInt("Tables", count) /* Potentially unsafe for large systems */
	pPrintStr("Datadir", variable["DATADIR"])
	pPrintStr("Binary Log", variable["LOG_BIN"])
	if variable["LOG_BIN"] == "ON" {
		pPrintStr("Binlog writes per hour", humanize.IBytes(uint64(status["BINLOG_BYTES_WRITTEN"]/status["UPTIME"])*3600))
	}
	// Add stuff for slow logs
	slaveStatus, err := dbhelper.GetSlaveStatus(db)
	if err != nil {
		slaveIO := slaveStatus.Slave_IO_Running
		slaveSQL := slaveStatus.Slave_SQL_Running
		var slaveState string
		if slaveIO == "Yes" && slaveSQL == "Yes" {
			slaveState = "Slave configured, threads running"
		} else {
			slaveState = "Slave configured, threads stopped"
		}
		pPrintStr("Replication", slaveState)
	} else {
		pPrintStr("Replication", "Not configured")
	}

	// InnoDB
	fmt.Println(common.DrawHashline("InnoDB", 60))
	ibps := humanize.IBytes(common.StrtoUint(variable["INNODB_BUFFER_POOL_SIZE"]))
	pPrintStr("InnoDB Buffer Pool", ibps)
	ibpsPages := float64(status["INNODB_BUFFER_POOL_PAGES_TOTAL"])
	ibpsFree := float64(status["INNODB_BUFFER_POOL_PAGES_FREE"])
	ibpsUsed := common.DecimaltoPctLow(ibpsFree, ibpsPages)
	pPrintStr("InnoDB Buffer Used", strconv.Itoa(ibpsUsed)+"%")
	ibpsDirty := float64(status["INNODB_BUFFER_POOL_PAGES_DIRTY"])
	ibpsDirtyPct := common.DecimaltoPct(ibpsDirty, ibpsPages)
	pPrintStr("InnoDB Buffer Dirty", strconv.Itoa(ibpsDirtyPct)+"%")
	pPrintStr("InnoDB Log Files", string(variable["INNODB_LOG_FILES_IN_GROUP"])+" files of "+humanize.IBytes(common.StrtoUint(variable["INNODB_LOG_FILE_SIZE"])))
	pPrintStr("InnoDB log writes per hour", humanize.IBytes(uint64(status["INNODB_OS_LOG_WRITTEN"]/status["UPTIME"])*3600))
	pPrintStr("InnoDB Log Buffer", humanize.IBytes(common.StrtoUint(variable["INNODB_LOG_BUFFER_SIZE"])))
	var iftc string
	switch variable["INNODB_FLUSH_LOG_AT_TRX_COMMIT"] {
	case "0":
		iftc = "0 - Flush log and write buffer every sec"
	case "1":
		iftc = "1 - Write buffer and Flush log at each trx commit"
	case "2":
		iftc = "2 - Write buffer at each trx commit, Flush log every sec"
	}
	pPrintStr("InnoDB Flush Log", iftc)
	ifm := variable["INNODB_FLUSH_METHOD"]
	if ifm == "" {
		ifm = "fsync"
	}
	pPrintStr("InnoDB Flush Method", ifm)
	pPrintStr("InnoDB IO Capacity", variable["INNODB_IO_CAPACITY"])
	// MyISAM
	fmt.Println(common.DrawHashline("MyISAM", 60))
	kbs := humanize.IBytes(common.StrtoUint(variable["KEY_BUFFER_SIZE"]))
	pPrintStr("MyISAM Key Cache", kbs)
	kbs_free := float64(status["KEY_BLOCKS_UNUSED"])
	kbs_used := float64(status["KEY_BLOCKS_USED"])
	kbsUsedPct := int(((1 - (kbs_free / (kbs_free + kbs_used))) * 100) + 0.5)
	pPrintStr("MyISAM Cache Used", strconv.Itoa(kbsUsedPct)+"%")
	// Handlers
	pPrintInt("Open tables", status["OPEN_TABLES"])
	pPrintInt("Open files", status["OPEN_FILES"])
}