Example #1
0
// getConnection returns the connection object for the given dsn
// In case a connection already exists it is reused
func (m *MetricSeter) getConnection(dsn string) (*sql.DB, error) {

	if db, ok := m.connections[dsn]; ok {
		return db, nil
	}

	db, err := mysql.Connect(dsn)
	if err != nil {
		return nil, err
	}

	m.connections[dsn] = db

	return db, nil
}
Example #2
0
// Fetch fetches status messages from a mysql host.
func (m *MetricSet) Fetch() (event common.MapStr, err error) {
	if m.db == nil {
		var err error
		m.db, err = mysql.Connect(m.dsn)
		if err != nil {
			return nil, errors.Wrap(err, "mysql-status connect to host")
		}
	}

	status, err := m.loadStatus(m.db)
	if err != nil {
		return nil, err
	}

	return eventMapping(status), nil
}
Example #3
0
// Fetches status messages from mysql hosts
func (m MetricSeter) Fetch() (events []common.MapStr, err error) {

	// Load status for all hosts and add it to events
	for _, host := range MetricSet.Module.GetHosts() {
		db, err := mysql.Connect(host)
		if err != nil {
			logp.Err("MySQL conenction error: %s", err)
		}

		status, err := m.loadStatus(db)

		if err != nil {
			return nil, err
		}

		event := eventMapping(status)
		events = append(events, event)
	}

	return events, nil
}
Example #4
0
// Setup any metric specific configuration
func (m *MetricSeter) Setup(ms *helper.MetricSet) error {

	// Additional configuration options
	config := struct {
		Username string `config:"username"`
		Password string `config:"password"`
	}{
		Username: "",
		Password: "",
	}

	for _, host := range ms.Config.Hosts {
		dsn := mysql.CreateDSN(host, config.Username, config.Password)

		db, err := mysql.Connect(dsn)
		if err != nil {
			logp.Err(err.Error())
		}

		m.connections[host] = db
	}

	return nil
}
Example #5
0
// Fetch fetches status messages from mysql hosts.
func (m *MetricSet) Fetch(host string) (event common.MapStr, err error) {
	// TODO (akroh): reading and writing to map are not concurrent-safe
	db, found := m.connections[host]
	if !found {
		dsn, found := m.hostToDSN[host]
		if !found {
			return nil, fmt.Errorf("DSN not found for host '%s'", host)
		}

		var err error
		db, err = mysql.Connect(dsn)
		if err != nil {
			return nil, errors.Wrap(err, "mysql-status connect to host")
		}
		m.connections[host] = db
	}

	status, err := m.loadStatus(db)
	if err != nil {
		return nil, err
	}

	return eventMapping(status), nil
}