storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/pkg/madmin/info-commands.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2017 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package madmin
    19  
    20  import (
    21  	"context"
    22  	"encoding/json"
    23  	"net/http"
    24  	"runtime"
    25  	"time"
    26  )
    27  
    28  // BackendType - represents different backend types.
    29  type BackendType int
    30  
    31  // Enum for different backend types.
    32  const (
    33  	Unknown BackendType = iota
    34  	// Filesystem backend.
    35  	FS
    36  	// Multi disk Erasure (single, distributed) backend.
    37  	Erasure
    38  	// Gateway to other storage
    39  	Gateway
    40  
    41  	// Add your own backend.
    42  )
    43  
    44  // ItemState - represents the status of any item in offline,init,online state
    45  type ItemState string
    46  
    47  const (
    48  
    49  	// ItemOffline indicates that the item is offline
    50  	ItemOffline = ItemState("offline")
    51  	// ItemInitializing indicates that the item is still in initialization phase
    52  	ItemInitializing = ItemState("initializing")
    53  	// ItemOnline indicates that the item is online
    54  	ItemOnline = ItemState("online")
    55  )
    56  
    57  // StorageInfo - represents total capacity of underlying storage.
    58  type StorageInfo struct {
    59  	Disks []Disk
    60  
    61  	// Backend type.
    62  	Backend BackendInfo
    63  }
    64  
    65  // BackendInfo - contains info of the underlying backend
    66  type BackendInfo struct {
    67  	// Represents various backend types, currently on FS, Erasure and Gateway
    68  	Type BackendType
    69  
    70  	// Following fields are only meaningful if BackendType is Gateway.
    71  	GatewayOnline bool
    72  
    73  	// Following fields are only meaningful if BackendType is Erasure.
    74  	OnlineDisks  BackendDisks // Online disks during server startup.
    75  	OfflineDisks BackendDisks // Offline disks during server startup.
    76  
    77  	// Following fields are only meaningful if BackendType is Erasure.
    78  	StandardSCData   []int // Data disks for currently configured Standard storage class.
    79  	StandardSCParity int   // Parity disks for currently configured Standard storage class.
    80  	RRSCData         []int // Data disks for currently configured Reduced Redundancy storage class.
    81  	RRSCParity       int   // Parity disks for currently configured Reduced Redundancy storage class.
    82  }
    83  
    84  // BackendDisks - represents the map of endpoint-disks.
    85  type BackendDisks map[string]int
    86  
    87  // Sum - Return the sum of the disks in the endpoint-disk map.
    88  func (d1 BackendDisks) Sum() (sum int) {
    89  	for _, count := range d1 {
    90  		sum += count
    91  	}
    92  	return sum
    93  }
    94  
    95  // Merge - Reduces two endpoint-disk maps.
    96  func (d1 BackendDisks) Merge(d2 BackendDisks) BackendDisks {
    97  	if len(d2) == 0 {
    98  		d2 = make(BackendDisks)
    99  	}
   100  	var merged = make(BackendDisks)
   101  	for i1, v1 := range d1 {
   102  		if v2, ok := d2[i1]; ok {
   103  			merged[i1] = v2 + v1
   104  			continue
   105  		}
   106  		merged[i1] = v1
   107  	}
   108  	return merged
   109  }
   110  
   111  // StorageInfo - Connect to a minio server and call Storage Info Management API
   112  // to fetch server's information represented by StorageInfo structure
   113  func (adm *AdminClient) StorageInfo(ctx context.Context) (StorageInfo, error) {
   114  	resp, err := adm.executeMethod(ctx, http.MethodGet, requestData{relPath: adminAPIPrefix + "/storageinfo"})
   115  	defer closeResponse(resp)
   116  	if err != nil {
   117  		return StorageInfo{}, err
   118  	}
   119  
   120  	// Check response http status code
   121  	if resp.StatusCode != http.StatusOK {
   122  		return StorageInfo{}, httpRespToErrorResponse(resp)
   123  	}
   124  
   125  	// Unmarshal the server's json response
   126  	var storageInfo StorageInfo
   127  	if err = json.NewDecoder(resp.Body).Decode(&storageInfo); err != nil {
   128  		return StorageInfo{}, err
   129  	}
   130  
   131  	return storageInfo, nil
   132  }
   133  
   134  // BucketUsageInfo - bucket usage info provides
   135  // - total size of the bucket
   136  // - total objects in a bucket
   137  // - object size histogram per bucket
   138  type BucketUsageInfo struct {
   139  	Size                    uint64 `json:"size"`
   140  	ReplicationPendingSize  uint64 `json:"objectsPendingReplicationTotalSize"`
   141  	ReplicationFailedSize   uint64 `json:"objectsFailedReplicationTotalSize"`
   142  	ReplicatedSize          uint64 `json:"objectsReplicatedTotalSize"`
   143  	ReplicaSize             uint64 `json:"objectReplicaTotalSize"`
   144  	ReplicationPendingCount uint64 `json:"objectsPendingReplicationCount"`
   145  	ReplicationFailedCount  uint64 `json:"objectsFailedReplicationCount"`
   146  
   147  	ObjectsCount         uint64            `json:"objectsCount"`
   148  	ObjectSizesHistogram map[string]uint64 `json:"objectsSizesHistogram"`
   149  }
   150  
   151  // DataUsageInfo represents data usage stats of the underlying Object API
   152  type DataUsageInfo struct {
   153  	// LastUpdate is the timestamp of when the data usage info was last updated.
   154  	// This does not indicate a full scan.
   155  	LastUpdate time.Time `json:"lastUpdate"`
   156  
   157  	// Objects total count across all buckets
   158  	ObjectsTotalCount uint64 `json:"objectsCount"`
   159  
   160  	// Objects total size across all buckets
   161  	ObjectsTotalSize uint64 `json:"objectsTotalSize"`
   162  
   163  	// Total Size for objects that have not yet been replicated
   164  	ReplicationPendingSize uint64 `json:"objectsPendingReplicationTotalSize"`
   165  
   166  	// Total size for objects that have witness one or more failures and will be retried
   167  	ReplicationFailedSize uint64 `json:"objectsFailedReplicationTotalSize"`
   168  
   169  	// Total size for objects that have been replicated to destination
   170  	ReplicatedSize uint64 `json:"objectsReplicatedTotalSize"`
   171  
   172  	// Total size for objects that are replicas
   173  	ReplicaSize uint64 `json:"objectsReplicaTotalSize"`
   174  
   175  	// Total number of objects pending replication
   176  	ReplicationPendingCount uint64 `json:"objectsPendingReplicationCount"`
   177  
   178  	// Total number of objects that failed replication
   179  	ReplicationFailedCount uint64 `json:"objectsFailedReplicationCount"`
   180  
   181  	// Total number of buckets in this cluster
   182  	BucketsCount uint64 `json:"bucketsCount"`
   183  
   184  	// Buckets usage info provides following information across all buckets
   185  	// - total size of the bucket
   186  	// - total objects in a bucket
   187  	// - object size histogram per bucket
   188  	BucketsUsage map[string]BucketUsageInfo `json:"bucketsUsageInfo"`
   189  
   190  	// Deprecated kept here for backward compatibility reasons.
   191  	BucketSizes map[string]uint64 `json:"bucketsSizes"`
   192  }
   193  
   194  // DataUsageInfo - returns data usage of the current object API
   195  func (adm *AdminClient) DataUsageInfo(ctx context.Context) (DataUsageInfo, error) {
   196  	resp, err := adm.executeMethod(ctx, http.MethodGet, requestData{relPath: adminAPIPrefix + "/datausageinfo"})
   197  	defer closeResponse(resp)
   198  	if err != nil {
   199  		return DataUsageInfo{}, err
   200  	}
   201  
   202  	// Check response http status code
   203  	if resp.StatusCode != http.StatusOK {
   204  		return DataUsageInfo{}, httpRespToErrorResponse(resp)
   205  	}
   206  
   207  	// Unmarshal the server's json response
   208  	var dataUsageInfo DataUsageInfo
   209  	if err = json.NewDecoder(resp.Body).Decode(&dataUsageInfo); err != nil {
   210  		return DataUsageInfo{}, err
   211  	}
   212  
   213  	return dataUsageInfo, nil
   214  }
   215  
   216  // InfoMessage container to hold server admin related information.
   217  type InfoMessage struct {
   218  	Mode         string             `json:"mode,omitempty"`
   219  	Domain       []string           `json:"domain,omitempty"`
   220  	Region       string             `json:"region,omitempty"`
   221  	SQSARN       []string           `json:"sqsARN,omitempty"`
   222  	DeploymentID string             `json:"deploymentID,omitempty"`
   223  	Buckets      Buckets            `json:"buckets,omitempty"`
   224  	Objects      Objects            `json:"objects,omitempty"`
   225  	Usage        Usage              `json:"usage,omitempty"`
   226  	Services     Services           `json:"services,omitempty"`
   227  	Backend      interface{}        `json:"backend,omitempty"`
   228  	Servers      []ServerProperties `json:"servers,omitempty"`
   229  }
   230  
   231  // Services contains different services information
   232  type Services struct {
   233  	KMS           KMS                           `json:"kms,omitempty"`
   234  	LDAP          LDAP                          `json:"ldap,omitempty"`
   235  	Logger        []Logger                      `json:"logger,omitempty"`
   236  	Audit         []Audit                       `json:"audit,omitempty"`
   237  	Notifications []map[string][]TargetIDStatus `json:"notifications,omitempty"`
   238  }
   239  
   240  // Buckets contains the number of buckets
   241  type Buckets struct {
   242  	Count uint64 `json:"count"`
   243  	Error string `json:"error,omitempty"`
   244  }
   245  
   246  // Objects contains the number of objects
   247  type Objects struct {
   248  	Count uint64 `json:"count"`
   249  	Error string `json:"error,omitempty"`
   250  }
   251  
   252  // Usage contains the total size used
   253  type Usage struct {
   254  	Size  uint64 `json:"size"`
   255  	Error string `json:"error,omitempty"`
   256  }
   257  
   258  // KMS contains KMS status information
   259  type KMS struct {
   260  	Status  string `json:"status,omitempty"`
   261  	Encrypt string `json:"encrypt,omitempty"`
   262  	Decrypt string `json:"decrypt,omitempty"`
   263  }
   264  
   265  // LDAP contains ldap status
   266  type LDAP struct {
   267  	Status string `json:"status,omitempty"`
   268  }
   269  
   270  // Status of endpoint
   271  type Status struct {
   272  	Status string `json:"status,omitempty"`
   273  }
   274  
   275  // Audit contains audit logger status
   276  type Audit map[string]Status
   277  
   278  // Logger contains logger status
   279  type Logger map[string]Status
   280  
   281  // TargetIDStatus containsid and status
   282  type TargetIDStatus map[string]Status
   283  
   284  // backendType - indicates the type of backend storage
   285  type backendType string
   286  
   287  const (
   288  	// FsType - Backend is FS Type
   289  	FsType = backendType("FS")
   290  	// ErasureType - Backend is Erasure type
   291  	ErasureType = backendType("Erasure")
   292  )
   293  
   294  // FSBackend contains specific FS storage information
   295  type FSBackend struct {
   296  	Type backendType `json:"backendType,omitempty"`
   297  }
   298  
   299  // ErasureBackend contains specific erasure storage information
   300  type ErasureBackend struct {
   301  	Type         backendType `json:"backendType,omitempty"`
   302  	OnlineDisks  int         `json:"onlineDisks,omitempty"`
   303  	OfflineDisks int         `json:"offlineDisks,omitempty"`
   304  	// Parity disks for currently configured Standard storage class.
   305  	StandardSCParity int `json:"standardSCParity,omitempty"`
   306  	// Parity disks for currently configured Reduced Redundancy storage class.
   307  	RRSCParity int `json:"rrSCParity,omitempty"`
   308  }
   309  
   310  // ServerProperties holds server information
   311  type ServerProperties struct {
   312  	State      string            `json:"state,omitempty"`
   313  	Endpoint   string            `json:"endpoint,omitempty"`
   314  	Uptime     int64             `json:"uptime,omitempty"`
   315  	Version    string            `json:"version,omitempty"`
   316  	CommitID   string            `json:"commitID,omitempty"`
   317  	Network    map[string]string `json:"network,omitempty"`
   318  	Disks      []Disk            `json:"drives,omitempty"`
   319  	PoolNumber int               `json:"poolNumber,omitempty"`
   320  	MemStats   runtime.MemStats  `json:"mem_stats"`
   321  }
   322  
   323  // DiskMetrics has the information about XL Storage APIs
   324  // the number of calls of each API and the moving average of
   325  // the duration of each API.
   326  type DiskMetrics struct {
   327  	APILatencies map[string]string `json:"apiLatencies,omitempty"`
   328  	APICalls     map[string]uint64 `json:"apiCalls,omitempty"`
   329  }
   330  
   331  // Disk holds Disk information
   332  type Disk struct {
   333  	Endpoint        string       `json:"endpoint,omitempty"`
   334  	RootDisk        bool         `json:"rootDisk,omitempty"`
   335  	DrivePath       string       `json:"path,omitempty"`
   336  	Healing         bool         `json:"healing,omitempty"`
   337  	State           string       `json:"state,omitempty"`
   338  	UUID            string       `json:"uuid,omitempty"`
   339  	Model           string       `json:"model,omitempty"`
   340  	TotalSpace      uint64       `json:"totalspace,omitempty"`
   341  	UsedSpace       uint64       `json:"usedspace,omitempty"`
   342  	AvailableSpace  uint64       `json:"availspace,omitempty"`
   343  	ReadThroughput  float64      `json:"readthroughput,omitempty"`
   344  	WriteThroughPut float64      `json:"writethroughput,omitempty"`
   345  	ReadLatency     float64      `json:"readlatency,omitempty"`
   346  	WriteLatency    float64      `json:"writelatency,omitempty"`
   347  	Utilization     float64      `json:"utilization,omitempty"`
   348  	Metrics         *DiskMetrics `json:"metrics,omitempty"`
   349  	HealInfo        *HealingDisk `json:"heal_info,omitempty"`
   350  
   351  	// Indexes, will be -1 until assigned a set.
   352  	PoolIndex int `json:"pool_index"`
   353  	SetIndex  int `json:"set_index"`
   354  	DiskIndex int `json:"disk_index"`
   355  }
   356  
   357  // ServerInfo - Connect to a minio server and call Server Admin Info Management API
   358  // to fetch server's information represented by infoMessage structure
   359  func (adm *AdminClient) ServerInfo(ctx context.Context) (InfoMessage, error) {
   360  	resp, err := adm.executeMethod(ctx,
   361  		http.MethodGet,
   362  		requestData{relPath: adminAPIPrefix + "/info"},
   363  	)
   364  	defer closeResponse(resp)
   365  	if err != nil {
   366  		return InfoMessage{}, err
   367  	}
   368  
   369  	// Check response http status code
   370  	if resp.StatusCode != http.StatusOK {
   371  		return InfoMessage{}, httpRespToErrorResponse(resp)
   372  	}
   373  
   374  	// Unmarshal the server's json response
   375  	var message InfoMessage
   376  	if err = json.NewDecoder(resp.Body).Decode(&message); err != nil {
   377  		return InfoMessage{}, err
   378  	}
   379  
   380  	return message, nil
   381  }