admin: service status uses new response format (#1979)

server & service fields are added to JSON response
This commit is contained in:
Anis Elleuch
2017-01-23 17:01:21 -08:00
committed by Harshavardhana
parent 76180184a5
commit caf6bfb76f
7 changed files with 152 additions and 31 deletions
+59 -13
View File
@@ -19,6 +19,7 @@ package cmd
import (
"encoding/json"
"fmt"
"net/url"
humanize "github.com/dustin/go-humanize"
"github.com/fatih/color"
@@ -80,22 +81,38 @@ type backendStatus struct {
Backend interface{} `json:"backend"`
}
type serverVersion struct {
Version string `json:"version"`
CommitID string `json:"commitID"`
}
// serviceStatusMessage container to hold service status information.
type serviceStatusMessage struct {
Status string `json:"status"`
StorageInfo backendStatus `json:"storageInfo"`
Status string `json:"status"`
Service bool `json:"service"`
StorageInfo backendStatus `json:"storageInfo"`
ServerVersion serverVersion `json:"server"`
}
// String colorized service status message.
func (u serviceStatusMessage) String() string {
msg := fmt.Sprintf("Total: %s, Free: %s.",
func (u serviceStatusMessage) String() (msg string) {
defer func() {
msg = console.Colorize("ServiceStatus", msg)
}()
// When service is offline
if !u.Service {
msg = "The server is offline."
return
}
// Online service, get backend information
msg = fmt.Sprintf("Total: %s, Free: %s.",
humanize.IBytes(uint64(u.StorageInfo.Total)),
humanize.IBytes(uint64(u.StorageInfo.Free)),
)
if v, ok := u.StorageInfo.Backend.(xlBackend); ok {
msg += fmt.Sprintf(" Online Disks: %d, Offline Disks: %d.\n", v.OnlineDisks, v.OfflineDisks)
}
return console.Colorize("ServiceStatus", msg)
return
}
// JSON jsonified service status Message message.
@@ -131,18 +148,42 @@ func mainAdminServiceStatus(ctx *cli.Context) error {
// Fetch the storage info of the specified Minio server
st, e := client.ServiceStatus()
fatalIf(probe.NewError(e), "Cannot get service status.")
storageInfo := backendStatus{
Total: st.Total,
Free: st.Free,
// Check the availability of the server: online or offline. A server is considered
// offline if we can't get any response or we get a bad format response
var serviceOffline bool
switch v := e.(type) {
case *json.SyntaxError:
serviceOffline = true
case *url.Error:
if v.Timeout() {
serviceOffline = true
}
}
if serviceOffline {
printMsg(serviceStatusMessage{Service: false})
return nil
}
if st.Backend.Type == madmin.XL {
// If the error is not nil and not unrecognizable, just print it and exit
fatalIf(probe.NewError(e), "Cannot get service status.")
// Construct the version response
version := serverVersion{
Version: st.ServerVersion.Version,
CommitID: st.ServerVersion.CommitID,
}
// Construct the backend status
storageInfo := backendStatus{
Total: st.StorageInfo.Total,
Free: st.StorageInfo.Free,
}
if st.StorageInfo.Backend.Type == madmin.XL {
storageInfo.Backend = xlBackend{
Type: xlType,
OnlineDisks: st.Backend.OnlineDisks,
OfflineDisks: st.Backend.OfflineDisks,
OnlineDisks: st.StorageInfo.Backend.OnlineDisks,
OfflineDisks: st.StorageInfo.Backend.OfflineDisks,
}
} else {
storageInfo.Backend = fsBackend{
@@ -150,7 +191,12 @@ func mainAdminServiceStatus(ctx *cli.Context) error {
}
}
printMsg(serviceStatusMessage{StorageInfo: storageInfo})
// Print the whole response
printMsg(serviceStatusMessage{
Service: true,
StorageInfo: storageInfo,
ServerVersion: version,
})
return nil
}
+3
View File
@@ -95,6 +95,9 @@ func newAdminFactory() func(config *Config) (*madmin.AdminClient, *probe.Error)
// Set custom transport.
api.SetCustomTransport(transport)
// Set client timeout
api.SetTimeout(30 * time.Second)
// Set app info.
api.SetAppInfo(config.AppName, config.AppVersion)
+29 -8
View File
@@ -38,8 +38,11 @@ func main() {
| Service operations|LockInfo operations|Healing operations|
|:---|:---|:---|
|[`ServiceStatus`](#ServiceStatus)| | |
|[`ServiceRestart`](#ServiceRestart)| | |
|[`ServiceStatus`](#ServiceStatus)| [`ListLocks`](#ListLocks)| [`ListObjectsHeal`](#ListObjectsHeal)|
|[`ServiceRestart`](#ServiceRestart)| [`ClearLocks`](#ClearLocks)| [`ListBucketsHeal`](#ListBucketsHeal)|
| | |[`HealBucket`](#HealBucket) |
| | |[`HealObject`](#HealObject)|
| | |[`HealFormat`](#HealFormat)|
## 1. Constructor
<a name="Minio"></a>
@@ -71,9 +74,11 @@ Fetch service status, replies disk space used, backend type and total disks offl
| Param | Type | Description |
|---|---|---|
|`st.Total` | _int64_ | Total disk space. |
|`st.Free` | _int64_ | Free disk space. |
|`st.Backend`| _struct{}_ | Represents backend type embedded structure. |
|`st.ServerVersion.Version` | _string_ | Server version. |
|`st.ServerVersion.CommitID` | _string_ | Server commit id. |
|`st.StorageInfo.Total` | _int64_ | Total disk space. |
|`st.StorageInfo.Free` | _int64_ | Free disk space. |
|`st.StorageInfo.Backend`| _struct{}_ | Represents backend type embedded structure. |
| Param | Type | Description |
|---|---|---|
@@ -185,14 +190,14 @@ __Example__
}
```
<a name="ListBucketsList"></a>
### ListBucketsList() error
<a name="ListBucketsHeal"></a>
### ListBucketsHeal() error
If successful returns information on the list of buckets that need healing.
__Example__
``` go
// List buckets that need healing
// List buckets that need healing
healBucketsList, err := madmClnt.ListBucketsHeal()
if err != nil {
fmt.Println(err)
@@ -244,3 +249,19 @@ __Example__
log.Println("successfully healed mybucket/myobject")
```
<a name="HealFormat"></a>
### HealFormat() error
Heal storage format on available disks. This is used when disks were replaced or were found with missing format. This is supported only for erasure-coded backend.
__Example__
``` go
err := madmClnt.HealFormat()
if err != nil {
log.Fatalln(err)
}
log.Println("successfully healed storage format on available disks.")
```
+8
View File
@@ -31,6 +31,7 @@ import (
"regexp"
"runtime"
"strings"
"time"
"github.com/minio/minio-go/pkg/s3signer"
"github.com/minio/minio-go/pkg/s3utils"
@@ -164,6 +165,13 @@ func (c *AdminClient) SetCustomTransport(customHTTPTransport http.RoundTripper)
}
}
// SetTimeout - set http client timeout.
func (c *AdminClient) SetTimeout(timeout time.Duration) {
if c.httpClient != nil {
c.httpClient.Timeout = timeout
}
}
// TraceOn - enable HTTP tracing.
func (c *AdminClient) TraceOn(outputStream io.Writer) {
// if outputStream is nil then default to os.Stdout.
+29
View File
@@ -403,3 +403,32 @@ func (adm *AdminClient) HealObject(bucket, object string, dryrun bool) error {
return nil
}
// HealFormat - heal storage format on available disks.
func (adm *AdminClient) HealFormat() error {
queryVal := url.Values{}
queryVal.Set("heal", "")
// Set x-minio-operation to format.
hdrs := make(http.Header)
hdrs.Set(minioAdminOpHeader, "format")
reqData := requestData{
queryValues: queryVal,
customHeaders: hdrs,
}
// Execute POST on /?heal to heal storage format.
resp, err := adm.executeMethod("POST", reqData)
defer closeResponse(resp)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New("Got HTTP Status: " + resp.Status)
}
return nil
}
+21 -7
View File
@@ -41,8 +41,8 @@ const (
// Add your own backend.
)
// ServiceStatusMetadata - represents total capacity of underlying storage.
type ServiceStatusMetadata struct {
// StorageInfo - represents total capacity of underlying storage.
type StorageInfo struct {
// Total disk space.
Total int64
// Free available disk space.
@@ -60,10 +60,23 @@ type ServiceStatusMetadata struct {
}
}
// ServerVersion - server version
type ServerVersion struct {
Version string `json:"version"`
CommitID string `json:"commitID"`
}
// ServiceStatusMetadata - contains the response of service status API
type ServiceStatusMetadata struct {
StorageInfo StorageInfo `json:"storageInfo"`
ServerVersion ServerVersion `json:"serverVersion"`
}
// ServiceStatus - Connect to a minio server and call Service Status Management API
// to fetch server's storage information represented by ServiceStatusMetadata structure
func (adm *AdminClient) ServiceStatus() (ServiceStatusMetadata, error) {
// Prepare web service request
reqData := requestData{}
reqData.queryValues = make(url.Values)
reqData.queryValues.Set("service", "")
@@ -72,29 +85,30 @@ func (adm *AdminClient) ServiceStatus() (ServiceStatusMetadata, error) {
// Execute GET on bucket to list objects.
resp, err := adm.executeMethod("GET", reqData)
defer closeResponse(resp)
if err != nil {
return ServiceStatusMetadata{}, err
}
// Check response http status code
if resp.StatusCode != http.StatusOK {
return ServiceStatusMetadata{}, errors.New("Got HTTP Status: " + resp.Status)
}
// Unmarshal the server's json response
var serviceStatus ServiceStatusMetadata
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return ServiceStatusMetadata{}, err
}
var storageInfo ServiceStatusMetadata
err = json.Unmarshal(respBytes, &storageInfo)
err = json.Unmarshal(respBytes, &serviceStatus)
if err != nil {
return ServiceStatusMetadata{}, err
}
return storageInfo, nil
return serviceStatus, nil
}
// ServiceRestart - Call Service Restart API to restart a specified Minio server
+3 -3
View File
@@ -80,10 +80,10 @@
"revisionTime": "2016-08-18T00:31:20Z"
},
{
"checksumSHA1": "gsJXCQU0qB6V7hdxCpJ64duQwn0=",
"checksumSHA1": "TVcuuhRHLezVlDqOpPMIpUZUJ+s=",
"path": "github.com/minio/minio/pkg/madmin",
"revision": "80f13878776a1944934cc8a6fb4666c145355400",
"revisionTime": "2017-01-19T19:32:13Z"
"revision": "fc6f804865937fccb211f4b46da0c103f2416108",
"revisionTime": "2017-01-23T17:55:34Z"
},
{
"path": "github.com/minio/minio/pkg/probe",