Пример #1
0
// Dump the script to create the table
func dumpCreateTable(w io.Writer, db mysql.Conn, table string) {
	fmt.Fprintf(w, "\n--\n")
	fmt.Fprintf(w, "-- Table structure for table `%s`\n", table)
	fmt.Fprintf(w, "--\n\n")
	fmt.Fprintf(w, "DROP TABLE IF EXISTS `%s`;\n", table)
	row, _, err := db.QueryFirst("SHOW CREATE TABLE `%s`", table)
	checkError(err)
	fmt.Fprintf(w, "%s;\n", row.Str(1))
}
Пример #2
0
func innodbStatus(m *MysqlIns, db mysql.Conn) ([]*MetaData, error) {
	status, _, err := db.QueryFirst("SHOW /*!50000 ENGINE */ INNODB STATUS")
	if err != nil {
		return nil, err
	}
	ctn := status.Str(2)
	rows := strings.Split(ctn, "\n")
	return parseInnodbStatus(m, rows)
}
Пример #3
0
func (m MySQLPlugin) fetchShowInnodbStatus(db mysql.Conn, stat map[string]float64) error {
	row, _, err := db.QueryFirst("SHOW /*!50000 ENGINE*/ INNODB STATUS")
	if err != nil {
		log.Fatalln("FetchMetrics (InnoDB Status): ", err)
		return err
	}

	if len(row) > 0 {
		parseInnodbStatus(string(row[len(row)-1].([]byte)), &stat)
	} else {
		log.Fatalln("FetchMetrics (InnoDB Status): row length is too small: ", len(row))
	}
	return nil
}
Пример #4
0
// Get the table data
func dumpTableData(w io.Writer, db mysql.Conn, table string) {
	fmt.Fprintf(w, "\n--\n-- Dumping data for table `%s`\n--\n\n", table)

	rowCnt, _, err := db.QueryFirst(getSelectCountQueryFor(db, table))
	checkError(err)
	if rowCnt.Int(0) == 0 {
		fmt.Fprintf(w, "--\n-- Empty table\n--\n\n")
		return
	} else {
		fmt.Fprintf(w, "--\n-- %d rows\n--\n\n", rowCnt.Int(0))
	}

	fmt.Fprintf(w, "LOCK TABLES `%s` WRITE;\n", table)
	query := fmt.Sprintf("INSERT INTO `%s` VALUES", table)
	rows := make([]string, 0)

	res, err := db.Start(getSelectQueryFor(db, table))
	checkError(err)
	row := res.MakeRow()

	for {
		err = res.ScanRow(row)
		if err == io.EOF {
			break
		}
		checkError(err)

		vals := make([]string, 0)
		for k, col := range row {
			val := "NULL"
			if col != nil {
				val = fmt.Sprintf("'%s'", db.EscapeString(row.Str(k)))
			}
			vals = append(vals, val)
		}

		rows = append(rows, fmt.Sprintf("( %s )", strings.Join(vals, ", ")))
		if len(rows) >= 100 {
			fmt.Fprintf(w, "%s\n%s;\n", query, strings.Join(rows, ",\n"))
			rows = make([]string, 0)
		}
	}

	if len(rows) > 0 {
		fmt.Fprintf(w, "%s\n%s;\n", query, strings.Join(rows, ",\n"))
	}

	fmt.Fprintf(w, "\nUNLOCK TABLES;\n")
}
Пример #5
0
func slaveStatus(m *MysqlIns, db mysql.Conn) ([]*MetaData, error) {

	isSlave := NewMetric("Is_slave")

	row, res, err := db.QueryFirst("SHOW SLAVE STATUS")
	if err != nil {
		return nil, err
	}

	// be master
	if row == nil {
		isSlave.SetValue(0)
		return []*MetaData{isSlave}, nil
	}

	// be slave
	isSlave.SetValue(1)

	data := make([]*MetaData, len(SlaveStatusToSend))
	for i, s := range SlaveStatusToSend {
		data[i] = NewMetric(s)
		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
}
Пример #6
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
}