예제 #1
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)
	}
}
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/pivotal-cf/redis-cpu/metrics"
	"github.com/pivotal-cf/redis-cpu/metrics/fakes"
)

var _ = Describe("CPULoad", func() {
	var client *fakes.FakeClient

	BeforeEach(func() {
		client = new(fakes.FakeClient)
		client.InfoReturns(map[string]string{"used_cpu_sys": "123.45"}, nil)
	})

	It("calls client.Info()", func() {
		metrics.CPULoad(client)
		Expect(client.InfoCallCount()).To(Equal(1))
	})

	It("does not return an error", func() {
		_, err := metrics.CPULoad(client)
		Expect(err).ToNot(HaveOccurred())
	})

	It("reports the right CPU load metric", func() {
		m, _ := metrics.CPULoad(client)
		Expect(m.Key).To(Equal("cpu_load"))
		Expect(m.Value).To(Equal(123.45))
		Expect(m.Unit).To(Equal("load"))
	})