Esempio n. 1
0
func ProcessNum(db mysql.Conn, cfg *base.Cfg) (*model.MetaData, error) {
	sql := "SHOW PROCESSLIST"
	rows, _, err := db.Query(sql)
	if err != nil {
		return nil, err
	}
	monitorName := "process_nums"
	threadNum := len(rows) - 1
	data := model.NewMetric(monitorName, cfg)
	data.SetValue(threadNum)
	return data, nil
}
Esempio n. 2
0
func MysqlAlive(cfg *base.Cfg, m *model.MysqlIns, ok bool) {
	data := model.NewMetric("mysql_alive_local", cfg)
	data.SetValue(1)
	if !ok {
		data.SetValue(0)
	}
	msg, err := SendData([]*model.MetaData{data}, cfg)
	if err != nil {
		log.Errorf("Send alive data failed: %v", err)
		return
	}
	log.Infof("Alive data response %v: %s", m, string(msg))
}
Esempio n. 3
0
func SlaveStatus(db mysql.Conn, cfg *base.Cfg) ([]*model.MetaData, error) {
	isSlave := model.NewMetric("Is_slave", cfg)
	row, res, err := db.QueryFirst("SHOW SLAVE STATUS")
	if err != nil {
		return nil, err
	}

	// be master
	if row == nil {
		isSlave.SetValue(0)
		isSlave.Tags = fmt.Sprintf("%s,role=%s", isSlave.Tags, "master")
		return []*model.MetaData{isSlave}, nil
	}

	// be slave
	isSlave.SetValue(1)
	isSlave.Tags = fmt.Sprintf("%s,role=%s", isSlave.Tags, "slaver")

	data := make([]*model.MetaData, len(SlaveStatusToSend))
	for i, s := range SlaveStatusToSend {
		data[i] = model.NewMetric(s, cfg)
		switch s {
		case "Slave_SQL_Running", "Slave_IO_Running":
			data[i].SetValue(0)
			v := row.Str(res.Map(s))
			if v == "Yes" {
				data[i].SetValue(1)
			}
		default:
			v, err := row.Int64Err(res.Map(s))
			if err != nil {
				data[i].SetValue(-1)
			} else {
				data[i].SetValue(v)
			}
		}
	}
	return append(data, isSlave), nil
}