info.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2017 The go-vgo Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // https://github.com/go-vgo/gt/blob/master/LICENSE
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. package info
  11. import (
  12. "errors"
  13. "fmt"
  14. "sync"
  15. "github.com/shirou/gopsutil/cpu"
  16. "github.com/shirou/gopsutil/disk"
  17. "github.com/shirou/gopsutil/host"
  18. "github.com/shirou/gopsutil/mem"
  19. )
  20. var (
  21. lck sync.RWMutex
  22. // InitMemUsed init mem used
  23. InitMemUsed uint64
  24. // InitDiskUsed init disk used
  25. InitDiskUsed uint64
  26. )
  27. func init() {
  28. lck.Lock()
  29. InitMemUsed, _ = MemUsed()
  30. InitDiskUsed, _ = DiskUsed()
  31. lck.Unlock()
  32. }
  33. // MemPercent returns the amount of use memory in percent.
  34. func MemPercent() (string, error) {
  35. memInfo, err := mem.VirtualMemory()
  36. useMem := fmt.Sprintf("%.2f", memInfo.UsedPercent)
  37. return useMem, err
  38. }
  39. // MemUsed returns the amount of used memory in bytes.
  40. func MemUsed() (uint64, error) {
  41. memInfo, err := mem.VirtualMemory()
  42. if err != nil {
  43. return 0, err
  44. }
  45. return memInfo.Used, err
  46. }
  47. // UsedMem returns the amount of riot used memory in bytes
  48. // after init() func.
  49. func UsedMem() (uint64, error) {
  50. memInfo, err := mem.VirtualMemory()
  51. // memInfo, err := MemUsed()
  52. if err != nil {
  53. return 0, err
  54. }
  55. return memInfo.Used - InitMemUsed, err
  56. }
  57. // MemTotal returns the amount of total memory in bytes.
  58. func MemTotal() (uint64, error) {
  59. memInfo, err := mem.VirtualMemory()
  60. if err != nil {
  61. return 0, err
  62. }
  63. return memInfo.Total, err
  64. }
  65. // MemFree returns the amount of free memory in bytes.
  66. func MemFree() (uint64, error) {
  67. memInfo, err := mem.VirtualMemory()
  68. if err != nil {
  69. return 0, err
  70. }
  71. return memInfo.Free, err
  72. }
  73. // ToKB bytes to kb
  74. func ToKB(data uint64) uint64 {
  75. return data / 1024
  76. }
  77. // ToMB bytes to mb
  78. func ToMB(data uint64) uint64 {
  79. return data / 1024 / 1024
  80. }
  81. // ToGB bytes to gb
  82. func ToGB(data uint64) uint64 {
  83. return data / 1024 / 1024 / 1024
  84. }
  85. // Disk init the disk
  86. func Disk(pt ...bool) ([]*disk.UsageStat, error) {
  87. var ptBool bool
  88. if len(pt) > 0 {
  89. ptBool = pt[0]
  90. }
  91. var usage []*disk.UsageStat
  92. parts, err := disk.Partitions(ptBool)
  93. for _, part := range parts {
  94. use, err := disk.Usage(part.Mountpoint)
  95. if err != nil {
  96. return usage, err
  97. }
  98. usage = append(usage, use)
  99. // printUsage(use)
  100. }
  101. return usage, err
  102. }
  103. // DiskPercent returns the amount of use disk in percent.
  104. func DiskPercent() (string, error) {
  105. usage, err := Disk()
  106. if len(usage) > 0 {
  107. useDisk := fmt.Sprintf("%.2f", usage[0].UsedPercent)
  108. return useDisk, err
  109. }
  110. return "0.00", err
  111. }
  112. // DiskUsed returns the amount of use disk in bytes.
  113. func DiskUsed() (uint64, error) {
  114. usage, err := Disk()
  115. // for i := 0; i < len(usage); i++ {
  116. if len(usage) > 0 {
  117. useDisk := usage[0].Used
  118. return useDisk, err
  119. }
  120. return 0, err
  121. }
  122. // UsedDisk returns the amount of use disk in bytes
  123. // after init() func.
  124. func UsedDisk() (uint64, error) {
  125. diskUsed, err := DiskUsed()
  126. if err != nil {
  127. return 0, err
  128. }
  129. return diskUsed - InitDiskUsed, err
  130. }
  131. // DiskTotal returns the amount of total disk in bytes.
  132. func DiskTotal() (uint64, error) {
  133. usage, err := Disk()
  134. // for i := 0; i < len(usage); i++ {
  135. if len(usage) > 0 {
  136. totalDisk := usage[0].Total
  137. return totalDisk, err
  138. }
  139. return 0, err
  140. }
  141. // DiskFree returns the amount of free disk in bytes.
  142. func DiskFree() (uint64, error) {
  143. usage, err := Disk()
  144. // for i := 0; i < len(usage); i++ {
  145. if len(usage) > 0 {
  146. freeDisk := usage[0].Free
  147. return freeDisk, err
  148. }
  149. return 0, err
  150. }
  151. // CPUInfo returns the cpu info
  152. func CPUInfo(args ...int) (string, error) {
  153. info, err := cpu.Info()
  154. if err != nil {
  155. return "", err
  156. }
  157. if len(info) == 0 {
  158. return "", errors.New("no CPU detected")
  159. }
  160. if len(args) > 0 {
  161. return info[args[0]].ModelName, nil
  162. }
  163. return info[0].ModelName, nil
  164. }
  165. // CPUPercent returns the amount of use cpu in percent.
  166. func CPUPercent() ([]float64, error) {
  167. used, err := cpu.Percent(0, true)
  168. return used, err
  169. }
  170. // Uptime returns the system uptime in seconds.
  171. func Uptime() (uptime uint64, err error) {
  172. hostInfo, err := host.Info()
  173. if err != nil {
  174. return 0, err
  175. }
  176. return hostInfo.Uptime, nil
  177. }
  178. // PlatformInfo fetches system platform information.
  179. func PlatformInfo() (platform, family, osVersion string, err error) {
  180. platform, family, osVersion, err = host.PlatformInformation()
  181. return
  182. }
  183. // Platform returns the platform name and OS Version.
  184. func Platform() (string, error) {
  185. platform, _, osVersion, err := host.PlatformInformation()
  186. return platform + " " + osVersion, err
  187. }
  188. // KernelVer returns the kernel version as a string.
  189. func KernelVer() (string, error) {
  190. return host.KernelVersion()
  191. }