storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/healthinfo_linux.go (about)

     1  //go:build linux
     2  // +build linux
     3  
     4  /*
     5   * MinIO Cloud Storage, (C) 2020 MinIO, Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   *
    19   */
    20  
    21  package cmd
    22  
    23  import (
    24  	"context"
    25  	"fmt"
    26  	"net/http"
    27  	"strings"
    28  
    29  	diskhw "github.com/shirou/gopsutil/v3/disk"
    30  	"github.com/shirou/gopsutil/v3/host"
    31  
    32  	"storj.io/minio/pkg/madmin"
    33  	"storj.io/minio/pkg/smart"
    34  )
    35  
    36  func getLocalOsInfo(ctx context.Context, r *http.Request) madmin.ServerOsInfo {
    37  	addr := r.Host
    38  	if globalIsDistErasure {
    39  		addr = globalLocalNodeName
    40  	}
    41  
    42  	srvrOsInfo := madmin.ServerOsInfo{Addr: addr}
    43  	var err error
    44  
    45  	srvrOsInfo.Info, err = host.InfoWithContext(ctx)
    46  	if err != nil {
    47  		return madmin.ServerOsInfo{
    48  			Addr:  addr,
    49  			Error: fmt.Sprintf("info: %v", err),
    50  		}
    51  	}
    52  
    53  	srvrOsInfo.Sensors, err = host.SensorsTemperaturesWithContext(ctx)
    54  	if err != nil {
    55  		// Set error only when it's not of WARNINGS type
    56  		if _, isWarning := err.(*host.Warnings); !isWarning {
    57  			srvrOsInfo.Error = fmt.Sprintf("sensors-temp: %v", err)
    58  		}
    59  	}
    60  
    61  	// ignore user err, as it cannot be obtained reliably inside containers
    62  	srvrOsInfo.Users, _ = host.UsersWithContext(ctx)
    63  
    64  	return srvrOsInfo
    65  }
    66  
    67  func getLocalDiskHwInfo(ctx context.Context, r *http.Request) madmin.ServerDiskHwInfo {
    68  	addr := r.Host
    69  	if globalIsDistErasure {
    70  		addr = globalLocalNodeName
    71  	}
    72  
    73  	parts, err := diskhw.PartitionsWithContext(ctx, true)
    74  	if err != nil {
    75  		return madmin.ServerDiskHwInfo{
    76  			Addr:  addr,
    77  			Error: fmt.Sprintf("partitions: %v", err),
    78  		}
    79  	}
    80  
    81  	drives := []string{}
    82  	paths := []string{}
    83  	partitions := []madmin.PartitionStat{}
    84  
    85  	for _, part := range parts {
    86  		device := part.Device
    87  		path := part.Mountpoint
    88  		if strings.Index(device, "/dev/") == 0 {
    89  			if strings.Contains(device, "loop") {
    90  				continue
    91  			}
    92  
    93  			if strings.Contains(device, "/dev/fuse") {
    94  				continue
    95  			}
    96  
    97  			drives = append(drives, device)
    98  			paths = append(paths, path)
    99  			smartInfo, err := smart.GetInfo(device)
   100  			if err != nil {
   101  				smartInfo.Error = fmt.Sprintf("smart: %v", err)
   102  			}
   103  			partition := madmin.PartitionStat{
   104  				Device:     part.Device,
   105  				Mountpoint: part.Mountpoint,
   106  				Fstype:     part.Fstype,
   107  				Opts:       strings.Join(part.Opts, ","),
   108  				SmartInfo:  smartInfo,
   109  			}
   110  			partitions = append(partitions, partition)
   111  		}
   112  	}
   113  
   114  	ioCounters, err := diskhw.IOCountersWithContext(ctx, drives...)
   115  	if err != nil {
   116  		return madmin.ServerDiskHwInfo{
   117  			Addr:  addr,
   118  			Error: fmt.Sprintf("iocounters: %v", err),
   119  		}
   120  	}
   121  	usages := []*diskhw.UsageStat{}
   122  	for _, path := range paths {
   123  		usage, err := diskhw.UsageWithContext(ctx, path)
   124  		if err != nil {
   125  			return madmin.ServerDiskHwInfo{
   126  				Addr:  addr,
   127  				Error: fmt.Sprintf("usage: %v", err),
   128  			}
   129  		}
   130  		usages = append(usages, usage)
   131  	}
   132  
   133  	return madmin.ServerDiskHwInfo{
   134  		Addr:       addr,
   135  		Usage:      usages,
   136  		Partitions: partitions,
   137  		Counters:   ioCounters,
   138  		Error:      "",
   139  	}
   140  }