server.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package utils
  2. import (
  3. "fmt"
  4. "github.com/shirou/gopsutil/cpu"
  5. "github.com/shirou/gopsutil/disk"
  6. "github.com/shirou/gopsutil/load"
  7. "github.com/shirou/gopsutil/mem"
  8. "runtime"
  9. "time"
  10. )
  11. const (
  12. B = 1
  13. KB = 1024 * B
  14. MB = 1024 * KB
  15. GB = 1024 * MB
  16. )
  17. //服务器硬盘使用量
  18. func DiskCheck() {
  19. u, _ := disk.Usage("/")
  20. usedMB := int(u.Used) / MB
  21. usedGB := int(u.Used) / GB
  22. totalMB := int(u.Total) / MB
  23. totalGB := int(u.Total) / GB
  24. usedPercent := int(u.UsedPercent)
  25. fmt.Printf("Free space: %dMB (%dGB) / %dMB (%dGB) | Used: %d%%\n", usedMB, usedGB, totalMB, totalGB, usedPercent)
  26. }
  27. //OS
  28. func OSCheck() {
  29. fmt.Printf("goOs:%s,compiler:%s,numCpu:%d,version:%s,numGoroutine:%d\n", runtime.GOOS, runtime.Compiler, runtime.NumCPU(), runtime.Version(), runtime.NumGoroutine())
  30. }
  31. //CPU 使用量
  32. func CPUCheck() {
  33. cores, _ := cpu.Counts(false)
  34. cpus, err := cpu.Percent(time.Duration(200)*time.Millisecond, true)
  35. if err == nil {
  36. for i, c := range cpus {
  37. fmt.Printf("cpu%d : %f%%\n", i, c)
  38. }
  39. }
  40. a, _ := load.Avg()
  41. l1 := a.Load1
  42. l5 := a.Load5
  43. l15 := a.Load15
  44. fmt.Println(l1)
  45. fmt.Println(l5)
  46. fmt.Println(l15)
  47. fmt.Println(cores)
  48. }
  49. //内存使用量
  50. func RAMCheck() {
  51. u, _ := mem.VirtualMemory()
  52. usedMB := int(u.Used) / MB
  53. totalMB := int(u.Total) / MB
  54. usedPercent := int(u.UsedPercent)
  55. fmt.Printf("usedMB:%d,totalMB:%d,usedPercent:%d", usedMB, totalMB, usedPercent)
  56. }