functions.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package main
  2. import (
  3. "errors"
  4. "github.com/shirou/gopsutil/cpu"
  5. "github.com/shirou/gopsutil/disk"
  6. "github.com/shirou/gopsutil/mem"
  7. "github.com/shirou/gopsutil/net"
  8. "github.com/shirou/gopsutil/process"
  9. "time"
  10. )
  11. type MemInfo struct {
  12. Total uint64
  13. Free uint64
  14. Used uint64
  15. Cached uint64
  16. UsedPercent float64
  17. }
  18. /**
  19. returns: Total, Free, Used, Cached, UsedPercent
  20. */
  21. func getMem() (*MemInfo, error) {
  22. m, e := mem.VirtualMemory()
  23. if e == nil {
  24. data := &MemInfo{}
  25. data.Total = m.Total
  26. data.Free = m.Free
  27. data.Used = m.Used
  28. data.Cached = m.Buffers + m.Cached
  29. data.UsedPercent = m.UsedPercent
  30. return data, nil
  31. }
  32. return &MemInfo{}, errors.New("could not get memory data")
  33. }
  34. type CPUInfo struct {
  35. ModelName string
  36. Cores int32
  37. Mhz float64
  38. CacheSize int32
  39. }
  40. func getCPU() (*[]CPUInfo, error) {
  41. cpus, e := cpu.Info()
  42. if e == nil {
  43. data := make([]CPUInfo, len(cpus))
  44. for i := 0; i < len(cpus); i++ {
  45. c := cpus[i]
  46. data[i].ModelName = c.ModelName
  47. data[i].Cores = c.Cores
  48. data[i].Mhz = c.Mhz
  49. data[i].CacheSize = c.CacheSize
  50. }
  51. return &data, nil
  52. }
  53. return &[]CPUInfo{}, errors.New("could not found CPU info")
  54. }
  55. type Process struct {
  56. ProcessName string
  57. ProcessCPUPercent float64
  58. ProcessMemoryPercent float32
  59. ProcessUserName string
  60. }
  61. //TODO: 返回值,错误处理,范围
  62. func getAllProcess() *[]Process {
  63. pi, _ := process.Pids()
  64. data := make([]Process, len(pi))
  65. for i := 0; i < len(pi); i++ {
  66. p, _ := process.NewProcess(pi[i])
  67. pn, _ := p.Name()
  68. pc, _ := p.CPUPercent()
  69. pm, _ := p.MemoryPercent()
  70. pu, _ := p.Username()
  71. data[i].ProcessName = pn
  72. data[i].ProcessCPUPercent = pc
  73. data[i].ProcessMemoryPercent = pm
  74. data[i].ProcessUserName = pu
  75. }
  76. return &data
  77. }
  78. //TODO: per nic
  79. /**
  80. bytesRecv: 23530889
  81. bytesSent: 6687155
  82. dropin: 0
  83. dropout: 0
  84. errin: 0
  85. errout: 0
  86. fifoin: 0
  87. fifoout: 0
  88. NetIO: "all"
  89. packetsRecv: 305999
  90. packetsSent: 96951
  91. */
  92. type NetIO struct {
  93. BytesRecv uint64
  94. BytesSent uint64
  95. PacketsRecv uint64
  96. PacketsSent uint64
  97. }
  98. func getNetIO() (*NetIO, error) {
  99. io := &NetIO{}
  100. b, e := net.IOCounters(false)
  101. if e == nil {
  102. data := b[0]
  103. io.BytesRecv = data.BytesRecv
  104. io.BytesSent = data.BytesSent
  105. io.PacketsRecv = data.PacketsRecv
  106. io.PacketsSent = data.PacketsSent
  107. return io, nil
  108. }
  109. return &NetIO{}, errors.New("could not get net IO")
  110. }
  111. // 获取CPU百分比
  112. func getCPUPercent() (float64, error) {
  113. a, e := cpu.Percent(time.Duration(time.Second), false)
  114. if e == nil {
  115. return a[0], nil
  116. } else {
  117. return 0, errors.New("could not get cpu percent")
  118. }
  119. }
  120. /**
  121. device: "/dev/dm-0"
  122. fstype: "xfs"
  123. mountpoint: "/"
  124. opts: "rw,relatime"
  125. */
  126. func getDiskPartition() ([]disk.PartitionStat, error) {
  127. d, err := disk.Partitions(false)
  128. if err == nil {
  129. return d, nil
  130. }
  131. return nil, errors.New("could not get disk partition")
  132. }
  133. /**
  134. free: 15353872384
  135. fstype: "xfs"
  136. inodesFree: 8837809
  137. inodesTotal: 8910848
  138. inodesUsed: 73039
  139. inodesUsedPercent: 0.8196638524189842
  140. path: "/"
  141. total: 18238930944
  142. used: 2885058560
  143. usedPercent: 15.818134126710358
  144. */
  145. type DiskUsage struct {
  146. Path string
  147. FsType string
  148. Total uint64
  149. Free uint64
  150. Used uint64
  151. UsedPercent float64
  152. }
  153. func getDiskUsage(path string) (*DiskUsage, error) {
  154. d, err := disk.Usage(path)
  155. if err != nil {
  156. return nil, errors.New("could not get disk usage")
  157. }
  158. usage := &DiskUsage{}
  159. usage.Path = d.Path
  160. usage.FsType = d.Fstype
  161. usage.Total = d.Total
  162. usage.Free = d.Free
  163. usage.Used = d.Used
  164. usage.UsedPercent = d.UsedPercent
  165. return usage, nil
  166. }
  167. type NetInterface struct {
  168. Name string
  169. HardwareAddr string
  170. }
  171. func getFirstNetInterface() (*NetInterface, error) {
  172. n, e := net.Interfaces()
  173. data := &NetInterface{}
  174. if e != nil {
  175. return data, nil
  176. }
  177. for _, v := range n {
  178. if v.HardwareAddr == "" {
  179. continue
  180. }
  181. data.Name = v.Name
  182. data.HardwareAddr = v.HardwareAddr
  183. }
  184. return data, nil
  185. }