github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/system/disk/disk.go (about)

     1  package disk
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"github.com/isyscore/isc-gobase/system/common"
     7  )
     8  
     9  var invoke common.Invoker = common.Invoke{}
    10  
    11  type UsageStat struct {
    12  	Path              string  `json:"path"`
    13  	Fstype            string  `json:"fstype"`
    14  	Total             uint64  `json:"total"`
    15  	Free              uint64  `json:"free"`
    16  	Used              uint64  `json:"used"`
    17  	UsedPercent       float64 `json:"usedPercent"`
    18  	InodesTotal       uint64  `json:"inodesTotal"`
    19  	InodesUsed        uint64  `json:"inodesUsed"`
    20  	InodesFree        uint64  `json:"inodesFree"`
    21  	InodesUsedPercent float64 `json:"inodesUsedPercent"`
    22  }
    23  
    24  type PartitionStat struct {
    25  	Device     string `json:"device"`
    26  	Mountpoint string `json:"mountpoint"`
    27  	Fstype     string `json:"fstype"`
    28  	Opts       string `json:"opts"`
    29  }
    30  
    31  type IOCountersStat struct {
    32  	ReadCount        uint64 `json:"readCount"`
    33  	MergedReadCount  uint64 `json:"mergedReadCount"`
    34  	WriteCount       uint64 `json:"writeCount"`
    35  	MergedWriteCount uint64 `json:"mergedWriteCount"`
    36  	ReadBytes        uint64 `json:"readBytes"`
    37  	WriteBytes       uint64 `json:"writeBytes"`
    38  	ReadTime         uint64 `json:"readTime"`
    39  	WriteTime        uint64 `json:"writeTime"`
    40  	IopsInProgress   uint64 `json:"iopsInProgress"`
    41  	IoTime           uint64 `json:"ioTime"`
    42  	WeightedIO       uint64 `json:"weightedIO"`
    43  	Name             string `json:"name"`
    44  	SerialNumber     string `json:"serialNumber"`
    45  	Label            string `json:"label"`
    46  }
    47  
    48  func (d UsageStat) String() string {
    49  	s, _ := json.Marshal(d)
    50  	return string(s)
    51  }
    52  
    53  func (d PartitionStat) String() string {
    54  	s, _ := json.Marshal(d)
    55  	return string(s)
    56  }
    57  
    58  func (d IOCountersStat) String() string {
    59  	s, _ := json.Marshal(d)
    60  	return string(s)
    61  }
    62  
    63  // Usage returns a file system usage. path is a filesystem path such
    64  // as "/", not device file path like "/dev/vda1".  If you want to use
    65  // a return value of disk.Partitions, use "Mountpoint" not "Device".
    66  func Usage(path string) (*UsageStat, error) {
    67  	return UsageWithContext(context.Background(), path)
    68  }
    69  
    70  // Partitions returns disk partitions. If all is false, returns
    71  // physical devices only (e.g. hard disks, cd-rom drives, USB keys)
    72  // and ignore all others (e.g. memory partitions such as /dev/shm)
    73  //
    74  // 'all' argument is ignored for BSD, see: https://github.com/giampaolo/psutil/issues/906
    75  func Partitions(all bool) ([]PartitionStat, error) {
    76  	return PartitionsWithContext(context.Background(), all)
    77  }
    78  
    79  func IOCounters(names ...string) (map[string]IOCountersStat, error) {
    80  	return IOCountersWithContext(context.Background(), names...)
    81  }