func PingServer(instance *Instance) error {
	client, err := client.Connect(
		client.Host(instance.Host),
		client.Port(instance.Port),
		client.Password(instance.Password),
	)
	if err != nil {
		return err
	}

	return client.Ping()
}
Esempio n. 2
0
func main() {
	var configPath string

	flag.StringVar(&configPath, "config", "", "Path to redis config file")
	flag.Parse()

	if configPath == "" {
		log.Fatal("Missing flag --config")
	}

	config, err := redisconf.Load(configPath)
	if err != nil {
		log.Fatalf("Error loading Redis config file: %s", err)
	}

	port := config.Port()
	host := config.Host()
	password := config.Password()

	clientConnection, err := client.Connect(
		client.Port(port),
		client.Host(host),
		client.Password(password),
	)
	if err != nil {
		log.Fatalf("Error connecting to Redis: %s", err)
	}

	m, err := metrics.CPULoad(clientConnection)
	if err != nil {
		log.Fatalf("Error getting metrics: %s", err)
	}

	if err := json.NewEncoder(os.Stdout).Encode(m); err != nil {
		log.Fatalf("Error encoding json: %s", err)
	}
}
Esempio n. 3
0
func redisPIDProvider(instancePath string) process.PIDProvider {
	return func() (int, error) {
		instanceConf, err := redisconf.Load(path.Join(instancePath, "redis.conf"))
		if err != nil {
			return 0, err
		}

		client, err := client.Connect(
			client.Host("localhost"),
			client.Port(instanceConf.Port()),
			client.Password(instanceConf.Password()),
			client.CmdAliases(instanceConf.CommandAliases()),
		)
		if err != nil {
			return 0, err
		}

		pidfile, err := client.GetConfig("pidfile")
		if err != nil {
			return 0, err
		}
		return process.ReadPID(pidfile)
	}
}
			It("connects with no error", func() {
				_, err := client.Connect(client.Host(host), client.Port(port))
				Ω(err).ShouldNot(HaveOccurred())
			})

			Context("when the server has authentication enabled", func() {
				BeforeEach(func() {
					redisArgs = append(redisArgs, "--requirepass", "hello")
				})

				It("returns an error if the password is incorrect", func() {
					_, err := client.Connect(
						client.Host(host),
						client.Port(port),
						client.Password("wrong-password"),
					)
					Ω(err).Should(MatchError("ERR invalid password"))
				})

				It("works if the password is correct", func() {
					_, err := client.Connect(
						client.Host(host),
						client.Port(port),
						client.Password("hello"),
					)
					Ω(err).ShouldNot(HaveOccurred())
				})
			})
		})
	})
Esempio n. 5
0
func (b *instanceBackuper) instanceBackup(redisConfig instance.RedisConfig) BackupResult {
	result := BackupResult{
		RedisConfigPath: redisConfig.Path,
		NodeIP:          b.backupConfig.NodeIP,
	}

	logger := b.logger.WithData(
		lager.Data{
			"node_ip":           b.backupConfig.NodeIP,
			"redis_config_path": redisConfig.Path,
		},
	)

	logger.Info("instance-backup", lager.Data{"event": "starting"})

	logger.Info("instance-backup.locate-iid", lager.Data{"event": "starting"})
	instanceID, err := b.instanceIDLocator.LocateID(
		redisConfig.Path,
		b.backupConfig.NodeIP,
	)
	if err != nil {
		logger.Error("instance-backup.locate-iid", err, lager.Data{"event": "failed"})
		result.Err = err
		return result
	}

	result.InstanceID = instanceID

	logger.Info("instance-backup.locate-iid", lager.Data{
		"event":       "done",
		"instance_id": instanceID,
	})

	redisAddress := fmt.Sprintf("%s:%d", redisConfig.Conf.Host(), redisConfig.Conf.Port())
	logger.Info("instance-backup.redis-connect", lager.Data{
		"event":         "starting",
		"redis_address": redisAddress,
	})
	redisClient, err := b.redisClientProvider(
		redis.Host(redisConfig.Conf.Host()),
		redis.Port(redisConfig.Conf.Port()),
		redis.Password(redisConfig.Conf.Password()),
		redis.CmdAliases(redisConfig.Conf.CommandAliases()),
	)
	if err != nil {
		logger.Error("instance-backup.redis-connect", err, lager.Data{"event": "failed"})
		result.Err = err
		return result
	}
	defer func() {
		if err := redisClient.Disconnect(); err != nil {
			logger.Error("instance-backup.redis-disconnect", err, lager.Data{"event": "failed"})
		}
	}()
	logger.Info("instance-backup.redis-connect", lager.Data{
		"event":         "done",
		"redis_address": redisAddress,
	})

	targetPath := b.buildTargetPath(instanceID)
	logger.Info("instance-backup.redis-backup", lager.Data{
		"event":       "starting",
		"target_path": targetPath,
	})
	err = b.redisBackuper.Backup(redisClient, targetPath)
	if err != nil {
		logger.Error("instance-backup.redis-backup", err, lager.Data{"event": "failed"})
		result.Err = err
		return result
	}
	logger.Info("instance-backup.redis-backup", lager.Data{
		"event":       "done",
		"target_path": targetPath,
	})

	logger.Info("instance-backup", lager.Data{"event": "done"})
	return result
}