Query the system status https://maze.io/system.v0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

system_linux.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package system
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net"
  6. "path/filepath"
  7. "strconv"
  8. "strings"
  9. "syscall"
  10. "time"
  11. )
  12. func init() {
  13. kernel.ticks = 100 // C.sysconf(C._SC_CLK_TCK)
  14. sysinfo := syscall.Sysinfo_t{}
  15. if err := syscall.Sysinfo(&sysinfo); err != nil {
  16. panic(err)
  17. }
  18. kernel.bootTime = time.Now().Add(-time.Duration(sysinfo.Uptime) * time.Second)
  19. }
  20. func cats(name string) (string, error) {
  21. b, err := ioutil.ReadFile(name)
  22. if err != nil {
  23. return "", err
  24. }
  25. return strings.TrimSpace(string(b)), nil
  26. }
  27. type procUnmarshaler interface {
  28. unmarshalProc(string) error
  29. }
  30. func procMap(base string, maps map[string]interface{}) error {
  31. for name, item := range maps {
  32. s, err := cats(filepath.Join(base, name))
  33. if err != nil {
  34. return err
  35. }
  36. switch v := item.(type) {
  37. case *bool:
  38. *v = s != "0"
  39. case *string:
  40. *v = s
  41. case *[]string:
  42. *v = strings.Fields(s)
  43. case *int8, *int16, *int32, *int64:
  44. var i int64
  45. if i, err = strconv.ParseInt(s, 10, 64); err != nil {
  46. return err
  47. }
  48. switch v := item.(type) {
  49. case *int8:
  50. *v = int8(i)
  51. case *int16:
  52. *v = int16(i)
  53. case *int32:
  54. *v = int32(i)
  55. case *int64:
  56. *v = i
  57. }
  58. case *uint8, *uint16, *uint32, *uint64:
  59. var i uint64
  60. if i, err = strconv.ParseUint(s, 10, 64); err != nil {
  61. return err
  62. }
  63. switch v := item.(type) {
  64. case *uint8:
  65. *v = uint8(i)
  66. case *uint16:
  67. *v = uint16(i)
  68. case *uint32:
  69. *v = uint32(i)
  70. case *uint64:
  71. *v = i
  72. }
  73. case *net.HardwareAddr:
  74. *v, _ = net.ParseMAC(s)
  75. case *time.Duration:
  76. var i uint64
  77. if i, err = strconv.ParseUint(s, 10, 64); err != nil {
  78. return err
  79. }
  80. *v = time.Duration(i) * time.Millisecond
  81. default:
  82. if u, ok := item.(procUnmarshaler); ok {
  83. if err = u.unmarshalProc(s); err != nil {
  84. return err
  85. }
  86. continue
  87. }
  88. return fmt.Errorf(`unsupported type %T in procMap`, v)
  89. }
  90. }
  91. return nil
  92. }