From 641eb4c04bd5fafab61300fd86e97580b3a5b35c Mon Sep 17 00:00:00 2001 From: Jared Watts Date: Thu, 27 Oct 2016 11:11:54 -0700 Subject: [PATCH] rook status command to show health and status of cluster --- cmd/rook/block-list_test.go | 5 +- cmd/rook/node-list_test.go | 5 +- cmd/rook/pool-list_test.go | 6 +- cmd/rook/rook.go | 1 + cmd/rook/status.go | 97 ++++++++++++++++++++++ cmd/rook/status_test.go | 95 ++++++++++++++++++++++ pkg/api/routes.go | 6 ++ pkg/api/status.go | 100 +++++++++++++++++++++++ pkg/api/status_test.go | 130 +++++++++++++++++++++++++++++ pkg/cephmgr/client/status.go | 131 ++++++++++++++++++++++++++++++ pkg/cephmgr/client/status_test.go | 46 +++++++++++ pkg/model/status.go | 64 +++++++++++++++ pkg/rook/client/block.go | 10 +-- pkg/rook/client/client.go | 1 + pkg/rook/client/node.go | 4 +- pkg/rook/client/pool.go | 6 +- pkg/rook/client/status.go | 26 ++++++ pkg/rook/test/mockclient.go | 9 ++ 18 files changed, 729 insertions(+), 13 deletions(-) create mode 100644 cmd/rook/status.go create mode 100644 cmd/rook/status_test.go create mode 100644 pkg/api/status.go create mode 100644 pkg/api/status_test.go create mode 100644 pkg/cephmgr/client/status.go create mode 100644 pkg/cephmgr/client/status_test.go create mode 100644 pkg/model/status.go create mode 100644 pkg/rook/client/status.go diff --git a/cmd/rook/block-list_test.go b/cmd/rook/block-list_test.go index 929c4857f..24c967abe 100644 --- a/cmd/rook/block-list_test.go +++ b/cmd/rook/block-list_test.go @@ -42,7 +42,10 @@ func TestListBlockImages(t *testing.T) { out, err := listBlocks(mockRBDSysBusPath, c, e) assert.Nil(t, err) - assert.Equal(t, "NAME POOL SIZE DEVICE MOUNT\nmyimage1 mypool1 1.00 KiB rbd5 /tmp/mymount1\n", out) + + expectedOut := "NAME POOL SIZE DEVICE MOUNT\n" + + "myimage1 mypool1 1.00 KiB rbd5 /tmp/mymount1\n" + assert.Equal(t, expectedOut, out) } func TestListBlockImagesFailure(t *testing.T) { diff --git a/cmd/rook/node-list_test.go b/cmd/rook/node-list_test.go index 89113f0d5..c72d91b88 100644 --- a/cmd/rook/node-list_test.go +++ b/cmd/rook/node-list_test.go @@ -32,7 +32,10 @@ func TestListNodes(t *testing.T) { out, err := listNodes(c) assert.Nil(t, err) - assert.Equal(t, "PUBLIC PRIVATE STATE CLUSTER SIZE LOCATION UPDATED \n187.1.2.3 10.0.0.100 OK cluster1 100 B root=default,dc=datacenter5 1s ago \n", out) + + expectedOut := "PUBLIC PRIVATE STATE CLUSTER SIZE LOCATION UPDATED \n" + + "187.1.2.3 10.0.0.100 OK cluster1 100 B root=default,dc=datacenter5 1s ago \n" + assert.Equal(t, expectedOut, out) } func TestListNodesError(t *testing.T) { diff --git a/cmd/rook/pool-list_test.go b/cmd/rook/pool-list_test.go index 52278b1da..78b4e61cb 100644 --- a/cmd/rook/pool-list_test.go +++ b/cmd/rook/pool-list_test.go @@ -37,7 +37,11 @@ func TestListPools(t *testing.T) { out, err := listPools(c) assert.Nil(t, err) - assert.Equal(t, "NAME NUMBER TYPE SIZE DATA CODING ALGORITHM\nreplPool1 0 replicated 3 \necPool1 1 erasure coded 2 1 jerasure::reed_sol_van\n", out) + + expectedOut := "NAME NUMBER TYPE SIZE DATA CODING ALGORITHM\n" + + "replPool1 0 replicated 3 \n" + + "ecPool1 1 erasure coded 2 1 jerasure::reed_sol_van\n" + assert.Equal(t, expectedOut, out) } func TestListPoolsError(t *testing.T) { diff --git a/cmd/rook/rook.go b/cmd/rook/rook.go index fa3bb3bc4..85d572b59 100644 --- a/cmd/rook/rook.go +++ b/cmd/rook/rook.go @@ -60,6 +60,7 @@ func addCommands() { rootCmd.AddCommand(nodeCmd) rootCmd.AddCommand(poolCmd) rootCmd.AddCommand(blockCmd) + rootCmd.AddCommand(statusCmd) } func NewTableWriter(buffer io.Writer) *tabwriter.Writer { diff --git a/cmd/rook/status.go b/cmd/rook/status.go new file mode 100644 index 000000000..ee181c374 --- /dev/null +++ b/cmd/rook/status.go @@ -0,0 +1,97 @@ +package rook + +import ( + "bytes" + "fmt" + "net/http" + "os" + + "github.com/rook/rook/pkg/model" + "github.com/rook/rook/pkg/rook/client" + "github.com/rook/rook/pkg/util/display" + "github.com/spf13/cobra" +) + +var statusCmd = &cobra.Command{ + Use: "status", + Short: "Outputs a summary of the status of the cluster", +} + +func init() { + statusCmd.RunE = getStatusEntry +} + +func getStatusEntry(cmd *cobra.Command, args []string) error { + + c := client.NewRookNetworkRestClient(client.GetRestURL(apiServerEndpoint), http.DefaultClient) + out, err := getStatus(c) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + + fmt.Print(out) + return nil +} + +func getStatus(c client.RookRestClient) (string, error) { + statusDetails, err := c.GetStatusDetails() + if err != nil { + return "", fmt.Errorf("failed to get status: %+v", err) + } + + var buffer bytes.Buffer + w := NewTableWriter(&buffer) + + // overall status + buffer.WriteString(fmt.Sprintf("OVERALL STATUS: %s\n", model.HealthStatusToString(statusDetails.OverallStatus))) + + // summary messages + buffer.WriteString("\n") + buffer.WriteString("SUMMARY:\n") + fmt.Fprintln(w, "SEVERITY\tMESSAGE") + for _, sm := range statusDetails.SummaryMessages { + fmt.Fprintf(w, "%s\t%s\n", model.HealthStatusToString(sm.Status), sm.Message) + } + w.Flush() + + // usage stats + buffer.WriteString("\n") + buffer.WriteString("USAGE:\n") + fmt.Fprintln(w, "TOTAL\tUSED\tDATA\tAVAILABLE") + fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", + display.BytesToString(statusDetails.Usage.TotalBytes), + display.BytesToString(statusDetails.Usage.UsedBytes), + display.BytesToString(statusDetails.Usage.DataBytes), + display.BytesToString(statusDetails.Usage.AvailableBytes)) + w.Flush() + + // monitors + buffer.WriteString("\n") + buffer.WriteString("MONITORS:\n") + fmt.Fprintln(w, "NAME\tADDRESS\tIN QUORUM\tSTATUS") + for _, m := range statusDetails.Monitors { + fmt.Fprintf(w, "%s\t%s\t%t\t%s\n", m.Name, m.Address, m.InQuorum, model.HealthStatusToString(m.Status)) + } + w.Flush() + + // OSDs + buffer.WriteString("\n") + buffer.WriteString("OSDs:\n") + fmt.Fprintln(w, "TOTAL\tUP\tIN\tFULL\tNEAR FULL") + fmt.Fprintf(w, "%d\t%d\t%d\t%t\t%t\n", statusDetails.OSDs.Total, statusDetails.OSDs.NumberUp, + statusDetails.OSDs.NumberIn, statusDetails.OSDs.Full, statusDetails.OSDs.NearFull) + w.Flush() + + // placement groups + buffer.WriteString("\n") + buffer.WriteString(fmt.Sprintf("PLACEMENT GROUPS (%d total):\n", statusDetails.PGs.Total)) + fmt.Fprintln(w, "STATE\tCOUNT") + for state, count := range statusDetails.PGs.StateCounts { + fmt.Fprintf(w, "%s\t%d\n", state, count) + } + w.Flush() + + w.Flush() + return buffer.String(), nil +} diff --git a/cmd/rook/status_test.go b/cmd/rook/status_test.go new file mode 100644 index 000000000..96b75df00 --- /dev/null +++ b/cmd/rook/status_test.go @@ -0,0 +1,95 @@ +package rook + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/rook/rook/pkg/model" + "github.com/rook/rook/pkg/rook/test" +) + +func TestGetStatus(t *testing.T) { + c := &test.MockRookRestClient{ + MockGetStatusDetails: func() (model.StatusDetails, error) { + return model.StatusDetails{ + OverallStatus: model.HealthWarning, + SummaryMessages: []model.StatusSummary{ + {Status: model.HealthWarning, Message: "a cluster warning"}, + {Status: model.HealthOK, Message: "a cluster OK message"}, + }, + Monitors: []model.MonitorSummary{ + {Name: "mon00", Address: "192.155.0.0", InQuorum: true, Status: model.HealthOK}, + {Name: "mon01", Address: "192.155.0.1", InQuorum: false, Status: model.HealthError}, + }, + OSDs: model.OSDSummary{ + Total: 5, NumberIn: 4, NumberUp: 4, Full: false, NearFull: true, + }, + PGs: model.PGSummary{ + Total: 100, + StateCounts: map[string]int{"state1": 50}, + }, + Usage: model.UsageSummary{ + TotalBytes: 1000, + DataBytes: 100, + UsedBytes: 200, + AvailableBytes: 700, + }, + }, nil + }, + } + + out, err := getStatus(c) + assert.Nil(t, err) + + expectedOut := "OVERALL STATUS: WARNING\n\n" + + "SUMMARY:\n" + + "SEVERITY MESSAGE\n" + + "WARNING a cluster warning\n" + + "OK a cluster OK message\n\n" + + "USAGE:\n" + + "TOTAL USED DATA AVAILABLE\n" + + "1000 B 200 B 100 B 700 B\n\n" + + "MONITORS:\n" + + "NAME ADDRESS IN QUORUM STATUS\n" + + "mon00 192.155.0.0 true OK\n" + + "mon01 192.155.0.1 false ERROR\n\n" + + "OSDs:\n" + + "TOTAL UP IN FULL NEAR FULL\n" + + "5 4 4 false true\n\n" + + "PLACEMENT GROUPS (100 total):\n" + + "STATE COUNT\n" + + "state1 50\n" + assert.Equal(t, expectedOut, out) +} + +func TestGetStatusEmptyResponse(t *testing.T) { + c := &test.MockRookRestClient{ + MockGetStatusDetails: func() (model.StatusDetails, error) { + return model.StatusDetails{ + OverallStatus: model.HealthUnknown, + SummaryMessages: []model.StatusSummary{}, + Monitors: []model.MonitorSummary{}, + PGs: model.PGSummary{StateCounts: map[string]int{}}, + }, nil + }, + } + + out, err := getStatus(c) + assert.Nil(t, err) + + expectedOut := "OVERALL STATUS: UNKNOWN\n" + + "\nSUMMARY:\n" + + "SEVERITY MESSAGE\n\n" + + "USAGE:\n" + + "TOTAL USED DATA AVAILABLE\n" + + "0 B 0 B 0 B 0 B\n\n" + + "MONITORS:\n" + + "NAME ADDRESS IN QUORUM STATUS\n\n" + + "OSDs:\n" + + "TOTAL UP IN FULL NEAR FULL\n" + + "0 0 0 false false\n\n" + + "PLACEMENT GROUPS (0 total):\n" + + "STATE COUNT\n" + assert.Equal(t, expectedOut, out) +} diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 5c2bc62ab..88b31cb42 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -2,6 +2,12 @@ package api func (h *Handler) GetRoutes() []Route { return []Route{ + { + "GetStatusDetails", + "GET", + "/status", + h.GetStatusDetails, + }, { "GetNodes", "GET", diff --git a/pkg/api/status.go b/pkg/api/status.go new file mode 100644 index 000000000..6121dcd60 --- /dev/null +++ b/pkg/api/status.go @@ -0,0 +1,100 @@ +package api + +import ( + "log" + "net/http" + + ceph "github.com/rook/rook/pkg/cephmgr/client" + "github.com/rook/rook/pkg/model" +) + +// Gets the status details of this cluster. +// GET +// /status +func (h *Handler) GetStatusDetails(w http.ResponseWriter, r *http.Request) { + adminConn, ok := h.handleConnectToCeph(w) + if !ok { + return + } + defer adminConn.Shutdown() + + cephStatus, err := ceph.Status(adminConn) + if err != nil { + log.Printf("failed to get status: %+v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + summaries := make([]model.StatusSummary, len(cephStatus.Health.Summary)) + for i, s := range cephStatus.Health.Summary { + summaries[i] = model.StatusSummary{ + Status: ceph.HealthToModelHealthStatus(s.Severity), + Message: s.Summary, + } + } + + // generate the monitor health summaries + monitors := make([]model.MonitorSummary, len(cephStatus.MonMap.Mons)) + for i, m := range cephStatus.MonMap.Mons { + monitors[i] = model.MonitorSummary{ + Name: m.Name, + Address: m.Address, + } + + // determine if the mon is in quorum + inQuorum := false + for _, qr := range cephStatus.Quorum { + if m.Rank == qr { + inQuorum = true + break + } + } + monitors[i].InQuorum = inQuorum + + // determine the mon's health status + monHealth := model.HealthUnknown + for _, mh := range ceph.GetMonitorHealthSummaries(cephStatus) { + if m.Name == mh.Name { + monHealth = ceph.HealthToModelHealthStatus(mh.Health) + break + } + } + monitors[i].Status = monHealth + } + + // generate the OSD health Summary + osdMap := cephStatus.OsdMap.OsdMap + osds := model.OSDSummary{ + Total: osdMap.NumOsd, + NumberIn: osdMap.NumInOsd, + NumberUp: osdMap.NumUpOsd, + Full: osdMap.Full, + NearFull: osdMap.NearFull, + } + + // generate the usage Summary + usageSummary := model.UsageSummary{ + DataBytes: cephStatus.PgMap.DataBytes, + UsedBytes: cephStatus.PgMap.UsedBytes, + AvailableBytes: cephStatus.PgMap.AvailableBytes, + TotalBytes: cephStatus.PgMap.TotalBytes, + } + + // generate the placement group health Summary + pgStates := make(map[string]int, len(cephStatus.PgMap.PgsByState)) + for _, pg := range cephStatus.PgMap.PgsByState { + pgStates[pg.StateName] = pg.Count + } + pgSummary := model.PGSummary{Total: cephStatus.PgMap.NumPgs, StateCounts: pgStates} + + statusDetails := model.StatusDetails{ + OverallStatus: ceph.HealthToModelHealthStatus(cephStatus.Health.OverallStatus), + SummaryMessages: summaries, + Monitors: monitors, + OSDs: osds, + PGs: pgSummary, + Usage: usageSummary, + } + + FormatJsonResponse(w, statusDetails) +} diff --git a/pkg/api/status_test.go b/pkg/api/status_test.go new file mode 100644 index 000000000..dd34d3231 --- /dev/null +++ b/pkg/api/status_test.go @@ -0,0 +1,130 @@ +package api + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "net/http/httptest" + "strings" + "testing" + + ceph "github.com/rook/rook/pkg/cephmgr/client" + testceph "github.com/rook/rook/pkg/cephmgr/client/test" + "github.com/rook/rook/pkg/cephmgr/test" + "github.com/rook/rook/pkg/clusterd" + "github.com/rook/rook/pkg/model" + "github.com/rook/rook/pkg/util" + "github.com/stretchr/testify/assert" +) + +const ( + // this JSON was generated from the mon_command "status", ExecuteMonCommand(conn, map[string]interface{}{"prefix": "status"}) + CephStatusResponseRaw = `{"health":{"health":{"health_services":[{"mons":[{"name":"mon0","kb_total":64891708,"kb_used":34813204,"kb_avail":26759160,"avail_percent":41,"last_updated":"2016-10-26 17:03:36.573444","store_stats":{"bytes_total":14871920,"bytes_sst":0,"bytes_log":2833842,"bytes_misc":12038078,"last_updated":"0.000000"},"health":"HEALTH_OK"}]}]},"timechecks":{"epoch":3,"round":0,"round_status":"finished"},"summary":[{"severity":"HEALTH_WARN","summary":"too many PGs per OSD (2048 > max 300)"}],"overall_status":"HEALTH_WARN","detail":[]},"fsid":"515d542a-fa63-496c-991d-cc8c1e156a3a","election_epoch":3,"quorum":[0],"quorum_names":["mon0"],"monmap":{"epoch":1,"fsid":"515d542a-fa63-496c-991d-cc8c1e156a3a","modified":"2016-10-26 16:10:36.449756","created":"2016-10-26 16:10:36.449756","mons":[{"rank":0,"name":"mon0","addr":"127.0.0.1:6790\/0"}]},"osdmap":{"osdmap":{"epoch":6,"num_osds":10,"num_up_osds":9,"num_in_osds":9,"full":false,"nearfull":true,"num_remapped_pgs":0}},"pgmap":{"pgs_by_state":[{"state_name":"active+clean","count":2048},{"state_name":"created+peering","count":100}],"version":600,"num_pgs":2148,"data_bytes":0,"bytes_used":39048007680,"bytes_avail":27401101312,"bytes_total":66449108992},"fsmap":{"epoch":1,"by_rank":[]},"mgrmap":{"active_gid":0,"active_name":"","standbys":[]}}` +) + +func TestGetStatusDetailsHandler(t *testing.T) { + etcdClient := util.NewMockEtcdClient() + context := &clusterd.Context{EtcdClient: etcdClient} + + req, err := http.NewRequest("GET", "http://10.0.0.100/status", nil) + if err != nil { + log.Fatal(err) + } + + cephFactory := &testceph.MockConnectionFactory{Fsid: "myfsid", SecretKey: "mykey"} + connFactory := &test.MockConnectionFactory{} + cephFactory.Conn = &testceph.MockConnection{ + MockMonCommand: func(args []byte) (buffer []byte, info string, err error) { + switch { + case strings.Index(string(args), "status") != -1: + return []byte(CephStatusResponseRaw), "info", nil + } + return nil, "", fmt.Errorf("unexpected mon_command '%s'", string(args)) + }, + } + connFactory.MockConnectAsAdmin = func(context *clusterd.Context, cephFactory ceph.ConnectionFactory) (ceph.Connection, error) { + return cephFactory.NewConnWithClusterAndUser("mycluster", "admin") + } + + // make a request to GetStatusDetails and verify the results + w := httptest.NewRecorder() + h := NewHandler(context, connFactory, cephFactory) + h.GetStatusDetails(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + expectedRespObj := model.StatusDetails{ + OverallStatus: model.HealthWarning, + SummaryMessages: []model.StatusSummary{ + {Status: model.HealthWarning, Message: "too many PGs per OSD (2048 > max 300)"}, + }, + Monitors: []model.MonitorSummary{ + {Name: "mon0", Address: "127.0.0.1:6790/0", InQuorum: true, Status: model.HealthOK}, + }, + OSDs: model.OSDSummary{ + Total: 10, NumberIn: 9, NumberUp: 9, Full: false, NearFull: true, + }, + PGs: model.PGSummary{ + Total: 2148, + StateCounts: map[string]int{"active+clean": 2048, "created+peering": 100}, + }, + Usage: model.UsageSummary{ + TotalBytes: 66449108992, + DataBytes: 0, + UsedBytes: 39048007680, + AvailableBytes: 27401101312, + }, + } + + VerifyStatusResponse(t, expectedRespObj, w) +} + +func TestGetStatusDetailsEmptyResponseFromCeph(t *testing.T) { + etcdClient := util.NewMockEtcdClient() + context := &clusterd.Context{EtcdClient: etcdClient} + + req, err := http.NewRequest("GET", "http://10.0.0.100/status", nil) + if err != nil { + log.Fatal(err) + } + + cephFactory := &testceph.MockConnectionFactory{Fsid: "myfsid", SecretKey: "mykey"} + connFactory := &test.MockConnectionFactory{} + cephFactory.Conn = &testceph.MockConnection{ + MockMonCommand: func(args []byte) (buffer []byte, info string, err error) { + switch { + case strings.Index(string(args), "status") != -1: + return []byte("{}"), "info", nil + } + return nil, "", fmt.Errorf("unexpected mon_command '%s'", string(args)) + }, + } + connFactory.MockConnectAsAdmin = func(context *clusterd.Context, cephFactory ceph.ConnectionFactory) (ceph.Connection, error) { + return cephFactory.NewConnWithClusterAndUser("mycluster", "admin") + } + + // make a request to GetStatusDetails and verify the results + w := httptest.NewRecorder() + h := NewHandler(context, connFactory, cephFactory) + h.GetStatusDetails(w, req) + assert.Equal(t, http.StatusOK, w.Code) + + expectedRespObj := model.StatusDetails{ + OverallStatus: model.HealthUnknown, + SummaryMessages: []model.StatusSummary{}, + Monitors: []model.MonitorSummary{}, + PGs: model.PGSummary{StateCounts: map[string]int{}}, + } + VerifyStatusResponse(t, expectedRespObj, w) +} + +func VerifyStatusResponse(t *testing.T, expectedRespObj model.StatusDetails, w *httptest.ResponseRecorder) { + // unmarshal the http response to get the actual object and compare it to the expected object + var actualResultObj model.StatusDetails + bodyBytes, err := ioutil.ReadAll(w.Body) + assert.Nil(t, err) + err = json.Unmarshal(bodyBytes, &actualResultObj) + assert.Nil(t, err) + assert.Equal(t, expectedRespObj, actualResultObj) +} diff --git a/pkg/cephmgr/client/status.go b/pkg/cephmgr/client/status.go new file mode 100644 index 000000000..17af6fd86 --- /dev/null +++ b/pkg/cephmgr/client/status.go @@ -0,0 +1,131 @@ +package client + +import ( + "encoding/json" + "fmt" + + "github.com/rook/rook/pkg/model" +) + +type CephStatus struct { + Health HealthStatus `json:"health"` + FSID string `json:"fsid"` + ElectionEpoch int `json:"election_epoch"` + Quorum []int `json:"quorum"` + QuorumNames []string `json:"quorum_names"` + MonMap MonMap `json:"monmap"` + OsdMap struct { + OsdMap OsdMap `json:"osdmap"` + } `json:"osdmap"` + PgMap PgMap `json:"pgmap"` +} + +type HealthStatus struct { + Details struct { + Services []map[string][]HealthService `json:"health_services"` + } `json:"health"` + Timechecks struct { + Epoch int `json:"epoch"` + Round int `json:"round"` + RoundStatus string `json:"round_status"` + } `json:"timechecks"` + Summary []HealthSummary `json:"summary"` + OverallStatus string `json:"overall_status"` + Detail []interface{} `json:"detail"` +} + +type HealthService struct { + Name string `json:"name"` + Health string `json:"health"` + KbTotal uint64 `json:"kb_total"` + KbUsed uint64 `json:"kb_used"` + KbAvailable uint64 `json:"kb_avail"` + AvailablePercent int `json:"avail_percent"` + LastUpdated string `json:"last_updated"` + StoreStats struct { + BytesTotal uint64 `json:"bytes_total"` + BytesSst uint64 `json:"bytes_sst"` + BytesLog uint64 `json:"bytes_log"` + BytesMisc uint64 `json:"bytes_misc"` + LastUpdated string `json:"last_updated"` + } `json:"store_stats"` +} + +type HealthSummary struct { + Severity string `json:"severity"` + Summary string `json:"summary"` +} + +type MonMap struct { + Epoch int `json:"epoch"` + FSID string `json:"fsid"` + CreatedTime string `json:"created"` + ModifiedTime string `json:"modified"` + Mons []MonMapEntry `json:"mons"` +} + +type OsdMap struct { + Epoch int `json:"epoch"` + NumOsd int `json:"num_osds"` + NumUpOsd int `json:"num_up_osds"` + NumInOsd int `json:"num_in_osds"` + Full bool `json:"full"` + NearFull bool `json:"nearfull"` + NumRemappedPgs int `json:"num_remapped_pgs"` +} + +type PgMap struct { + PgsByState []PgStateEntry `json:"pgs_by_state"` + Version int `json:"version"` + NumPgs int `json:"num_pgs"` + DataBytes uint64 `json:"data_bytes"` + UsedBytes uint64 `json:"bytes_used"` + AvailableBytes uint64 `json:"bytes_avail"` + TotalBytes uint64 `json:"bytes_total"` +} + +type PgStateEntry struct { + StateName string `json:"state_name"` + Count int `json:"count"` +} + +func Status(conn Connection) (CephStatus, error) { + cmd := map[string]interface{}{"prefix": "status"} + buf, err := ExecuteMonCommand(conn, cmd, "status") + if err != nil { + return CephStatus{}, fmt.Errorf("failed to get status: %+v", err) + } + + var status CephStatus + if err := json.Unmarshal(buf, &status); err != nil { + return CephStatus{}, fmt.Errorf("failed to unmarshal status response: %+v", err) + } + + return status, nil +} + +func HealthToModelHealthStatus(cephHealth string) model.HealthStatus { + switch cephHealth { + case "HEALTH_OK": + return model.HealthOK + case "HEALTH_WARN": + return model.HealthWarning + case "HEALTH_ERR": + return model.HealthError + default: + return model.HealthUnknown + } +} + +func GetMonitorHealthSummaries(cephStatus CephStatus) []HealthService { + // of all the available health services, we are looking for the one called "mons", + // which will then be a collection of monitor healths + for _, hs := range cephStatus.Health.Details.Services { + monsHealth, ok := hs["mons"] + if ok { + return monsHealth + } + } + + return nil +} diff --git a/pkg/cephmgr/client/status_test.go b/pkg/cephmgr/client/status_test.go new file mode 100644 index 000000000..179e64232 --- /dev/null +++ b/pkg/cephmgr/client/status_test.go @@ -0,0 +1,46 @@ +package client + +import ( + "encoding/json" + "testing" + + "github.com/stretchr/testify/assert" +) + +const ( + // this JSON was generated from the mon_command "status", ExecuteMonCommand(conn, map[string]interface{}{"prefix": "status"}) + CephStatusResponseRaw = `{"health":{"health":{"health_services":[{"mons":[{"name":"mon0","kb_total":64891708,"kb_used":34813204,"kb_avail":26759160,"avail_percent":41,"last_updated":"2016-10-26 17:03:36.573444","store_stats":{"bytes_total":14871920,"bytes_sst":0,"bytes_log":2833842,"bytes_misc":12038078,"last_updated":"0.000000"},"health":"HEALTH_OK"}]}]},"timechecks":{"epoch":3,"round":0,"round_status":"finished"},"summary":[{"severity":"HEALTH_WARN","summary":"too many PGs per OSD (2048 > max 300)"}],"overall_status":"HEALTH_WARN","detail":[]},"fsid":"515d542a-fa63-496c-991d-cc8c1e156a3a","election_epoch":3,"quorum":[0],"quorum_names":["mon0"],"monmap":{"epoch":1,"fsid":"515d542a-fa63-496c-991d-cc8c1e156a3a","modified":"2016-10-26 16:10:36.449756","created":"2016-10-26 16:10:36.449756","mons":[{"rank":0,"name":"mon0","addr":"127.0.0.1:6790\/0"}]},"osdmap":{"osdmap":{"epoch":6,"num_osds":10,"num_up_osds":9,"num_in_osds":9,"full":false,"nearfull":true,"num_remapped_pgs":0}},"pgmap":{"pgs_by_state":[{"state_name":"active+clean","count":2048}],"version":600,"num_pgs":2048,"data_bytes":0,"bytes_used":39048007680,"bytes_avail":27401101312,"bytes_total":66449108992},"fsmap":{"epoch":1,"by_rank":[]},"mgrmap":{"active_gid":0,"active_name":"","standbys":[]}}` +) + +func TestStatusMarshal(t *testing.T) { + var status CephStatus + err := json.Unmarshal([]byte(CephStatusResponseRaw), &status) + assert.Nil(t, err) + + // verify some health fields + assert.Equal(t, "HEALTH_WARN", status.Health.OverallStatus) + assert.Equal(t, 1, len(status.Health.Summary)) + assert.Equal(t, "HEALTH_WARN", status.Health.Summary[0].Severity) + assert.Equal(t, "too many PGs per OSD (2048 > max 300)", status.Health.Summary[0].Summary) + assert.Equal(t, "mon0", status.Health.Details.Services[0]["mons"][0].Name) + assert.Equal(t, 41, status.Health.Details.Services[0]["mons"][0].AvailablePercent) + assert.Equal(t, "HEALTH_OK", status.Health.Details.Services[0]["mons"][0].Health) + assert.Equal(t, uint64(2833842), status.Health.Details.Services[0]["mons"][0].StoreStats.BytesLog) + + // verify some Mon map fields + assert.Equal(t, 1, status.MonMap.Epoch) + assert.Equal(t, "mon0", status.MonMap.Mons[0].Name) + assert.Equal(t, "127.0.0.1:6790/0", status.MonMap.Mons[0].Address) + + // verify some OSD map fields + assert.Equal(t, 10, status.OsdMap.OsdMap.NumOsd) + assert.Equal(t, 9, status.OsdMap.OsdMap.NumUpOsd) + assert.False(t, status.OsdMap.OsdMap.Full) + assert.True(t, status.OsdMap.OsdMap.NearFull) + + // verify some PG map fields + assert.Equal(t, 2048, status.PgMap.NumPgs) + assert.Equal(t, uint64(39048007680), status.PgMap.UsedBytes) + assert.Equal(t, 2048, status.PgMap.PgsByState[0].Count) + assert.Equal(t, "active+clean", status.PgMap.PgsByState[0].StateName) +} diff --git a/pkg/model/status.go b/pkg/model/status.go new file mode 100644 index 000000000..da378a6ad --- /dev/null +++ b/pkg/model/status.go @@ -0,0 +1,64 @@ +package model + +type HealthStatus int + +const ( + HealthOK HealthStatus = iota + HealthWarning + HealthError + HealthUnknown +) + +type StatusDetails struct { + OverallStatus HealthStatus `json:"overall"` + SummaryMessages []StatusSummary `json:"summary"` + Monitors []MonitorSummary `json:"monitors"` + OSDs OSDSummary `json:"osd"` + PGs PGSummary `json:"pg"` + Usage UsageSummary `json:"usage"` +} + +type StatusSummary struct { + Status HealthStatus `json:"status"` + Message string `json:"message"` +} + +type MonitorSummary struct { + Name string `json:"name"` + Address string `json:"address"` + InQuorum bool `json:"inQuorum"` + Status HealthStatus `json:"status"` +} + +type OSDSummary struct { + Total int `json:"total"` + NumberIn int `json:"numIn"` + NumberUp int `json:"numUp"` + Full bool `json:"full"` + NearFull bool `json:"nearFull"` +} + +type PGSummary struct { + Total int `json:"total"` + StateCounts map[string]int `json:"stateCount"` +} + +type UsageSummary struct { + DataBytes uint64 `json:"data"` + UsedBytes uint64 `json:"used"` + AvailableBytes uint64 `json:"available"` + TotalBytes uint64 `json:"total"` +} + +func HealthStatusToString(hs HealthStatus) string { + switch hs { + case HealthOK: + return "OK" + case HealthWarning: + return "WARNING" + case HealthError: + return "ERROR" + default: + return "UNKNOWN" + } +} diff --git a/pkg/rook/client/block.go b/pkg/rook/client/block.go index 9e197bf10..199c23c35 100644 --- a/pkg/rook/client/block.go +++ b/pkg/rook/client/block.go @@ -9,12 +9,12 @@ import ( ) const ( - ImageQueryName = "image" - ImageMapInfoQueryName = "mapinfo" + imageQueryName = "image" + imageMapInfoQueryName = "mapinfo" ) func (c *RookNetworkRestClient) GetBlockImages() ([]model.BlockImage, error) { - body, err := c.DoGet(ImageQueryName) + body, err := c.DoGet(imageQueryName) if err != nil { return nil, err } @@ -34,7 +34,7 @@ func (c *RookNetworkRestClient) CreateBlockImage(newImage model.BlockImage) (str return "", err } - resp, err := c.DoPost(ImageQueryName, bytes.NewReader(body)) + resp, err := c.DoPost(imageQueryName, bytes.NewReader(body)) if err != nil { return "", err } @@ -43,7 +43,7 @@ func (c *RookNetworkRestClient) CreateBlockImage(newImage model.BlockImage) (str } func (c *RookNetworkRestClient) GetBlockImageMapInfo() (model.BlockImageMapInfo, error) { - body, err := c.DoGet(path.Join(ImageQueryName, ImageMapInfoQueryName)) + body, err := c.DoGet(path.Join(imageQueryName, imageMapInfoQueryName)) if err != nil { return model.BlockImageMapInfo{}, err } diff --git a/pkg/rook/client/client.go b/pkg/rook/client/client.go index f48ed56e0..25aa520d0 100644 --- a/pkg/rook/client/client.go +++ b/pkg/rook/client/client.go @@ -17,6 +17,7 @@ type RookRestClient interface { GetBlockImages() ([]model.BlockImage, error) CreateBlockImage(image model.BlockImage) (string, error) GetBlockImageMapInfo() (model.BlockImageMapInfo, error) + GetStatusDetails() (model.StatusDetails, error) } type RookNetworkRestClient struct { diff --git a/pkg/rook/client/node.go b/pkg/rook/client/node.go index f54956bd7..149d4089d 100644 --- a/pkg/rook/client/node.go +++ b/pkg/rook/client/node.go @@ -7,11 +7,11 @@ import ( ) const ( - NodeQueryName = "node" + nodeQueryName = "node" ) func (a *RookNetworkRestClient) GetNodes() ([]model.Node, error) { - body, err := a.DoGet(NodeQueryName) + body, err := a.DoGet(nodeQueryName) if err != nil { return nil, err } diff --git a/pkg/rook/client/pool.go b/pkg/rook/client/pool.go index 2e3000b0b..01a556426 100644 --- a/pkg/rook/client/pool.go +++ b/pkg/rook/client/pool.go @@ -8,11 +8,11 @@ import ( ) const ( - PoolQueryName = "pool" + poolQueryName = "pool" ) func (c *RookNetworkRestClient) GetPools() ([]model.Pool, error) { - body, err := c.DoGet(PoolQueryName) + body, err := c.DoGet(poolQueryName) if err != nil { return nil, err } @@ -32,7 +32,7 @@ func (c *RookNetworkRestClient) CreatePool(newPool model.Pool) (string, error) { return "", err } - resp, err := c.DoPost(PoolQueryName, bytes.NewReader(body)) + resp, err := c.DoPost(poolQueryName, bytes.NewReader(body)) if err != nil { return "", err } diff --git a/pkg/rook/client/status.go b/pkg/rook/client/status.go new file mode 100644 index 000000000..2d3e8a157 --- /dev/null +++ b/pkg/rook/client/status.go @@ -0,0 +1,26 @@ +package client + +import ( + "encoding/json" + + "github.com/rook/rook/pkg/model" +) + +const ( + statusQueryName = "status" +) + +func (a *RookNetworkRestClient) GetStatusDetails() (model.StatusDetails, error) { + body, err := a.DoGet(statusQueryName) + if err != nil { + return model.StatusDetails{}, err + } + + var statusDetails model.StatusDetails + err = json.Unmarshal(body, &statusDetails) + if err != nil { + return model.StatusDetails{}, err + } + + return statusDetails, nil +} diff --git a/pkg/rook/test/mockclient.go b/pkg/rook/test/mockclient.go index c7bdd8152..2e9fec5c2 100644 --- a/pkg/rook/test/mockclient.go +++ b/pkg/rook/test/mockclient.go @@ -21,6 +21,7 @@ type MockRookRestClient struct { MockGetBlockImages func() ([]model.BlockImage, error) MockCreateBlockImage func(image model.BlockImage) (string, error) MockGetBlockImageMapInfo func() (model.BlockImageMapInfo, error) + MockGetStatusDetails func() (model.StatusDetails, error) } func (m *MockRookRestClient) GetNodes() ([]model.Node, error) { @@ -71,6 +72,14 @@ func (m *MockRookRestClient) GetBlockImageMapInfo() (model.BlockImageMapInfo, er return model.BlockImageMapInfo{}, nil } +func (m *MockRookRestClient) GetStatusDetails() (model.StatusDetails, error) { + if m.MockGetStatusDetails != nil { + return m.MockGetStatusDetails() + } + + return model.StatusDetails{}, nil +} + func (m *MockRookRestClient) URL() string { return "" }