Example #1
0
/*
   Returns true if plugin is installed on master.
*/
func PluginIsInstalled(pluginName string) (bool, error) {
	pluginName = strings.ToLower(pluginName)

	db, err := database.NewConnection()
	defer db.Close()

	var nrOfRows int

	err = db.QueryRow("SELECT COUNT(*) FROM plugins WHERE name=? AND installedOnMaster=1",
		pluginName).Scan(&nrOfRows)

	switch {
	case err != nil:
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not execute sql query")
		return false, err
	case nrOfRows == 1:
		return true, nil
	case nrOfRows == 0:
		return false, nil
	default:
		return false, err
	}
}
Example #2
0
func (slave *Slave) Load(HWaddr string) {
	slave.lock.Lock()
	defer slave.lock.Unlock()

	db, err := database.NewConnection()
	defer db.Close()

	stmt, err := db.Prepare("SELECT HWaddr, concat('node-',convert(id,CHAR(5))), INET_NTOA(170787072 + id), port, username, password FROM devices WHERE HWaddr=?")
	if err != nil {
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not prepare sql statement")
		return
	}

	err = stmt.QueryRow(strings.Replace(HWaddr, ":", "", -1)).Scan(&slave.HardwareAddress, &slave.Hostname, &slave.IPAddress, &slave.Port, &slave.UserName, &slave.Password)
	switch {
	case err == sql.ErrNoRows:
		Log.WithFields(log.Fields{
			"mac": HWaddr,
		}).Info("Device not in database")
		break
	case err != nil:
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not execute sql query")
		break
	default:
		Log.WithFields(log.Fields{
			"mac": HWaddr,
		}).Info("Device loaded from database")
		break
	}
}
Example #3
0
func SetPluginInstalled(pluginName string) error {
	pluginName = strings.ToLower(pluginName)

	db, err := database.NewConnection()
	defer db.Close()

	stmt, err := db.Prepare("UPDATE plugins SET installedOnMaster=1 WHERE name=(?)")
	if err != nil {
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not prepare sql query")
		return err
	}

	_, err = stmt.Exec(pluginName)

	if err != nil {
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could execute sql query")
		return err
	}

	Log.WithFields(log.Fields{
		"plugin": pluginName,
	}).Info("Plugin set to be installed on master")
	return nil
}
Example #4
0
func (slave *Slave) Store() {
	slave.lock.Lock()
	defer slave.lock.Unlock()

	db, err := database.NewConnection()
	defer db.Close()

	//INET_ATON is to convert ip to int (array-TO-number)
	stmt, err := db.Prepare("INSERT INTO devices SET hwaddr=?, port=?, hname=?, username=?, password=?")
	if err != nil {
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not prepare sql statement")
		return
	}

	_, err = stmt.Exec(strings.Replace(slave.HardwareAddress, ":", "", -1), slave.Port, slave.Hostname, slave.UserName, slave.Password)
	if err != nil {
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not execute the sql query")
		return
	}

	Log.WithFields(log.Fields{
		"mac": slave.HardwareAddress,
	}).Info("added device")
}
Example #5
0
func getAllStoredDevices() ([]*Slave, error) {
	db, err := database.NewConnection()
	defer db.Close()

	rows, err := db.Query("SELECT HWaddr, concat('node-',convert(id,CHAR(5))), INET_NTOA(170787072 + id), port, username, password FROM devices")
	if err != nil {
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not execute sql query")
		return nil, err
	}
	defer rows.Close()

	var ds []*Slave
	for rows.Next() {
		slave := &Slave{
			Connected: false, // have no idea really ..
			lock:      &sync.Mutex{},
		}
		rows.Scan(&slave.HardwareAddress, &slave.Hostname, &slave.IPAddress, &slave.Port, &slave.UserName, &slave.Password)
		ds = append(ds, slave)
	}

	return ds, nil
}
Example #6
0
func (slave *Slave) setPluginInstalled(plugin string) {

	db, err := database.NewConnection()
	defer db.Close()

	stmt, err := db.Prepare("INSERT INTO installedPlugins_slave (hwaddr, plugin) VALUES ((?),(?))")
	defer stmt.Close()
	if err != nil {
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not prepare sql query")
	}
	_, err = stmt.Exec(slave.HardwareAddress, plugin)
	if err != nil {
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not execute sql query")
	}
}
Example #7
0
func (slave *Slave) Delete() {
	slave.lock.Lock()
	defer slave.lock.Unlock()

	db, err := database.NewConnection()
	defer db.Close()

	stmt, err := db.Prepare("DELETE FROM devices WHERE hwaddr=?")
	if err != nil {
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not prepare sql statement")
		return
	}

	device, err := stmt.Exec(strings.Replace(slave.HardwareAddress, ":", "", -1))
	if err != nil {
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not execute the sql query")
		return
	}

	affect, err := device.RowsAffected()
	if err != nil {
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not calculate number of affected rows")
		return
	}
	if affect == 0 {
		Log.WithFields(log.Fields{
			"mac": slave.HardwareAddress,
		}).Info("Device not deleted")
	} else {
		Log.WithFields(log.Fields{
			"mac": slave.HardwareAddress,
		}).Info("Device deleted")
	}
}
Example #8
0
func (slave *Slave) pluginIsInstalled(pluginName string) bool {

	db, err := database.NewConnection()
	defer db.Close()

	var hwaddr, plugin string

	err = db.QueryRow("SELECT hwaddr, plugin FROM installedPlugins_slave WHERE hwaddr=? AND plugin=?",
		strings.Replace(slave.HardwareAddress, ":", "", -1), pluginName).Scan(&hwaddr, &plugin)

	switch {
	case err == sql.ErrNoRows:
		return false
	case err != nil:
		Log.WithFields(log.Fields{
			"error": err,
		}).Fatal("Could not execute sql query")
		return false
	default:
		return true
	}
}