/* * MinIO Cloud Storage, (C) 2017 MinIO, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package madmin import ( "encoding/json" "errors" "io/ioutil" "net/http" "net/url" "strconv" humanize "github.com/dustin/go-humanize" "github.com/minio/minio/pkg/cpu" "github.com/minio/minio/pkg/disk" "github.com/minio/minio/pkg/mem" ) const ( // DefaultNetPerfSize - default payload size used for network performance. DefaultNetPerfSize = 100 * humanize.MiByte // DefaultDrivePerfSize - default file size for testing drive performance DefaultDrivePerfSize = 100 * humanize.MiByte ) // BackendType - represents different backend types. type BackendType int // Enum for different backend types. const ( Unknown BackendType = iota // Filesystem backend. FS // Multi disk Erasure (single, distributed) backend. Erasure // Add your own backend. ) // DriveInfo - represents each drive info, describing // status, uuid and endpoint. type DriveInfo HealDriveInfo // StorageInfo - represents total capacity of underlying storage. type StorageInfo struct { Used []uint64 // Used total used per disk. Total []uint64 // Total disk space per disk. Available []uint64 // Total disk space available per disk. MountPaths []string // Disk mountpoints // Backend type. Backend struct { // Represents various backend types, currently on FS and Erasure. Type BackendType // Following fields are only meaningful if BackendType is Erasure. OnlineDisks BackendDisks // Online disks during server startup. OfflineDisks BackendDisks // Offline disks during server startup. StandardSCData int // Data disks for currently configured Standard storage class. StandardSCParity int // Parity disks for currently configured Standard storage class. RRSCData int // Data disks for currently configured Reduced Redundancy storage class. RRSCParity int // Parity disks for currently configured Reduced Redundancy storage class. // List of all disk status, this is only meaningful if BackendType is Erasure. Sets [][]DriveInfo } } // BackendDisks - represents the map of endpoint-disks. type BackendDisks map[string]int // Sum - Return the sum of the disks in the endpoint-disk map. func (d1 BackendDisks) Sum() (sum int) { for _, count := range d1 { sum += count } return sum } // Merge - Reduces two endpoint-disk maps. func (d1 BackendDisks) Merge(d2 BackendDisks) BackendDisks { if len(d2) == 0 { d2 = make(BackendDisks) } for i1, v1 := range d1 { if v2, ok := d2[i1]; ok { d2[i1] = v2 + v1 continue } d2[i1] = v1 } return d2 } // StorageInfo - Connect to a minio server and call Storage Info Management API // to fetch server's information represented by StorageInfo structure func (adm *AdminClient) StorageInfo() (StorageInfo, error) { resp, err := adm.executeMethod("GET", requestData{relPath: adminAPIPrefix + "/storageinfo"}) defer closeResponse(resp) if err != nil { return StorageInfo{}, err } // Check response http status code if resp.StatusCode != http.StatusOK { return StorageInfo{}, httpRespToErrorResponse(resp) } // Unmarshal the server's json response var storageInfo StorageInfo respBytes, err := ioutil.ReadAll(resp.Body) if err != nil { return StorageInfo{}, err } err = json.Unmarshal(respBytes, &storageInfo) if err != nil { return StorageInfo{}, err } return storageInfo, nil } // ServerDrivesPerfInfo holds informantion about address and write speed of // all drives in a single server node type ServerDrivesPerfInfo struct { Addr string `json:"addr"` Error string `json:"error,omitempty"` Perf []disk.Performance `json:"perf"` Size int64 `json:"size,omitempty"` } // ServerDrivesPerfInfo - Returns drive's read and write performance information func (adm *AdminClient) ServerDrivesPerfInfo(size int64) ([]ServerDrivesPerfInfo, error) { v := url.Values{} v.Set("perfType", string("drive")) v.Set("size", strconv.FormatInt(size, 10)) resp, err := adm.executeMethod("GET", requestData{ relPath: adminAPIPrefix + "/performance", queryValues: v, }) defer closeResponse(resp) if err != nil { return nil, err } // Check response http status code if resp.StatusCode != http.StatusOK { return nil, httpRespToErrorResponse(resp) } // Unmarshal the server's json response var info []ServerDrivesPerfInfo respBytes, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } err = json.Unmarshal(respBytes, &info) if err != nil { return nil, err } return info, nil } // ServerCPULoadInfo holds information about address and cpu load of // a single server node type ServerCPULoadInfo struct { Addr string `json:"addr"` Error string `json:"error,omitempty"` Load []cpu.Load `json:"load"` HistoricLoad []cpu.Load `json:"historicLoad"` } // ServerCPULoadInfo - Returns cpu utilization information func (adm *AdminClient) ServerCPULoadInfo() ([]ServerCPULoadInfo, error) { v := url.Values{} v.Set("perfType", string("cpu")) resp, err := adm.executeMethod("GET", requestData{ relPath: adminAPIPrefix + "/performance", queryValues: v, }) defer closeResponse(resp) if err != nil { return nil, err } // Check response http status code if resp.StatusCode != http.StatusOK { return nil, httpRespToErrorResponse(resp) } // Unmarshal the server's json response var info []ServerCPULoadInfo respBytes, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } err = json.Unmarshal(respBytes, &info) if err != nil { return nil, err } return info, nil } // ServerMemUsageInfo holds information about address and memory utilization of // a single server node type ServerMemUsageInfo struct { Addr string `json:"addr"` Error string `json:"error,omitempty"` Usage []mem.Usage `json:"usage"` HistoricUsage []mem.Usage `json:"historicUsage"` } // ServerMemUsageInfo - Returns mem utilization information func (adm *AdminClient) ServerMemUsageInfo() ([]ServerMemUsageInfo, error) { v := url.Values{} v.Set("perfType", string("mem")) resp, err := adm.executeMethod("GET", requestData{ relPath: adminAPIPrefix + "/performance", queryValues: v, }) defer closeResponse(resp) if err != nil { return nil, err } // Check response http status code if resp.StatusCode != http.StatusOK { return nil, httpRespToErrorResponse(resp) } // Unmarshal the server's json response var info []ServerMemUsageInfo respBytes, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } err = json.Unmarshal(respBytes, &info) if err != nil { return nil, err } return info, nil } // NetPerfInfo network performance information. type NetPerfInfo struct { Addr string `json:"addr"` ReadThroughput uint64 `json:"readThroughput"` Error string `json:"error,omitempty"` } // NetPerfInfo - Returns network performance information of all cluster nodes. func (adm *AdminClient) NetPerfInfo(size int) (map[string][]NetPerfInfo, error) { v := url.Values{} v.Set("perfType", "net") if size > 0 { v.Set("size", strconv.Itoa(size)) } resp, err := adm.executeMethod("GET", requestData{ relPath: adminAPIPrefix + "/performance", queryValues: v, }) defer closeResponse(resp) if err != nil { return nil, err } // Check response http status code if resp.StatusCode == http.StatusMethodNotAllowed { return nil, errors.New("NetPerfInfo is meant for multi-node MinIO deployments") } if resp.StatusCode != http.StatusOK { return nil, httpRespToErrorResponse(resp) } // Unmarshal the server's json response info := map[string][]NetPerfInfo{} respBytes, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } err = json.Unmarshal(respBytes, &info) if err != nil { return nil, err } return info, nil } // InfoMessage container to hold server admin related information. type InfoMessage struct { Mode string `json:"mode"` Domain []string `json:"domain,omitempty"` Region string `json:"region,omitempty"` SQSARN []string `json:"sqsARN,omitempty"` DeploymentID string `json:"deploymentID"` Buckets Buckets `json:"buckets"` Objects Objects `json:"objects"` Usage Usage `json:"usage"` Services Services `json:"services"` Backend interface{} `json:"backend"` Servers []ServerProperties `json:"servers"` } // Services contains different services information type Services struct { Vault Vault `json:"vault"` LDAP LDAP `json:"ldap"` Logger []Logger `json:"logger,omitempty"` Audit []Audit `json:"audit,omitempty"` Notifications []map[string][]TargetIDStatus `json:"notifications"` } // Buckets contains the number of buckets type Buckets struct { Count int `json:"count"` } // Objects contains the number of objects type Objects struct { Count int `json:"count"` } // Usage contains the tottal size used type Usage struct { Size uint64 `json:"size"` } // Vault - Fetches the Vault status type Vault struct { Status string `json:"status,omitempty"` Encrypt string `json:"encryp,omitempty"` Decrypt string `json:"decrypt,omitempty"` Update string `json:"update,omitempty"` } // LDAP contains ldap status type LDAP struct { Status string `json:"status,omitempty"` } // Status of endpoint type Status struct { Status string `json:"status"` } // Audit contains audit logger status type Audit map[string]Status // Logger contains logger status type Logger map[string]Status // TargetIDStatus containsid and status type TargetIDStatus map[string]Status // backendType - indicates the type of backend storage type backendType string const ( // FsType - Backend is FS Type FsType = backendType("FS") // ErasureType - Backend is Erasure type ErasureType = backendType("Erasure") ) // FsBackend contains specific FS storage information type FsBackend struct { Type backendType `json:"backendType"` } // XlBackend contains specific erasure storage information type XlBackend struct { Type backendType `json:"backendType"` OnlineDisks int `json:"onlineDisks"` OfflineDisks int `json:"offlineDisks"` // Data disks for currently configured Standard storage class. StandardSCData int `json:"standardSCData"` // Parity disks for currently configured Standard storage class. StandardSCParity int `json:"standardSCParity"` // Data disks for currently configured Reduced Redundancy storage class. RRSCData int `json:"rrSCData"` // Parity disks for currently configured Reduced Redundancy storage class. RRSCParity int `json:"rrSCParity"` } // ServerProperties holds server information type ServerProperties struct { State string `json:"state"` Endpoint string `json:"endpoint"` Uptime int64 `json:"uptime"` Version string `json:"version"` CommitID string `json:"commitID"` Network map[string]string `json:"network"` Disks []Disk `json:"disks"` } // Disk holds Disk information type Disk struct { DrivePath string `json:"path"` State string `json:"state"` UUID string `json:"uuid,omitempty"` Model string `json:"model,omitempty"` TotalSpace uint64 `json:"totalspace"` UsedSpace uint64 `json:"usedspace"` ReadThroughput float64 `json:"readthroughput,omitempty"` WriteThroughPut float64 `json:"writethroughput,omitempty"` ReadLatency float64 `json:"readlatency,omitempty"` WriteLatency float64 `json:"writelatency,omitempty"` Utilization float64 `json:"utilization,omitempty"` } // ServerInfo - Connect to a minio server and call Server Admin Info Management API // to fetch server's information represented by infoMessage structure func (adm *AdminClient) ServerInfo() (InfoMessage, error) { resp, err := adm.executeMethod("GET", requestData{relPath: adminAPIPrefix + "/info"}) defer closeResponse(resp) if err != nil { return InfoMessage{}, err } // Check response http status code if resp.StatusCode != http.StatusOK { return InfoMessage{}, httpRespToErrorResponse(resp) } // Unmarshal the server's json response var message InfoMessage respBytes, err := ioutil.ReadAll(resp.Body) if err != nil { return InfoMessage{}, err } err = json.Unmarshal(respBytes, &message) if err != nil { return InfoMessage{}, err } return message, nil }