disk.go 3.1 KB

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