host.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package host
  2. import (
  3. "context"
  4. "encoding/json"
  5. "os"
  6. "runtime"
  7. "time"
  8. "github.com/shirou/gopsutil/internal/common"
  9. )
  10. var invoke common.Invoker = common.Invoke{}
  11. // A HostInfoStat describes the host status.
  12. // This is not in the psutil but it useful.
  13. type InfoStat struct {
  14. Hostname string `json:"hostname"`
  15. Uptime uint64 `json:"uptime"`
  16. BootTime uint64 `json:"bootTime"`
  17. Procs uint64 `json:"procs"` // number of processes
  18. OS string `json:"os"` // ex: freebsd, linux
  19. Platform string `json:"platform"` // ex: ubuntu, linuxmint
  20. PlatformFamily string `json:"platformFamily"` // ex: debian, rhel
  21. PlatformVersion string `json:"platformVersion"` // version of the complete OS
  22. KernelVersion string `json:"kernelVersion"` // version of the OS kernel (if available)
  23. KernelArch string `json:"kernelArch"` // native cpu architecture queried at runtime, as returned by `uname -m` or empty string in case of error
  24. VirtualizationSystem string `json:"virtualizationSystem"`
  25. VirtualizationRole string `json:"virtualizationRole"` // guest or host
  26. HostID string `json:"hostid"` // ex: uuid
  27. }
  28. type UserStat struct {
  29. User string `json:"user"`
  30. Terminal string `json:"terminal"`
  31. Host string `json:"host"`
  32. Started int `json:"started"`
  33. }
  34. type TemperatureStat struct {
  35. SensorKey string `json:"sensorKey"`
  36. Temperature float64 `json:"sensorTemperature"`
  37. }
  38. func (h InfoStat) String() string {
  39. s, _ := json.Marshal(h)
  40. return string(s)
  41. }
  42. func (u UserStat) String() string {
  43. s, _ := json.Marshal(u)
  44. return string(s)
  45. }
  46. func (t TemperatureStat) String() string {
  47. s, _ := json.Marshal(t)
  48. return string(s)
  49. }
  50. func Info() (*InfoStat, error) {
  51. return InfoWithContext(context.Background())
  52. }
  53. func InfoWithContext(ctx context.Context) (*InfoStat, error) {
  54. var err error
  55. ret := &InfoStat{
  56. OS: runtime.GOOS,
  57. }
  58. ret.Hostname, err = os.Hostname()
  59. if err != nil && err != common.ErrNotImplementedError {
  60. return nil, err
  61. }
  62. ret.Platform, ret.PlatformFamily, ret.PlatformVersion, err = PlatformInformationWithContext(ctx)
  63. if err != nil && err != common.ErrNotImplementedError {
  64. return nil, err
  65. }
  66. ret.KernelVersion, err = KernelVersionWithContext(ctx)
  67. if err != nil && err != common.ErrNotImplementedError {
  68. return nil, err
  69. }
  70. ret.KernelArch, err = KernelArch()
  71. if err != nil && err != common.ErrNotImplementedError {
  72. return nil, err
  73. }
  74. ret.VirtualizationSystem, ret.VirtualizationRole, err = VirtualizationWithContext(ctx)
  75. if err != nil && err != common.ErrNotImplementedError {
  76. return nil, err
  77. }
  78. ret.BootTime, err = BootTimeWithContext(ctx)
  79. if err != nil && err != common.ErrNotImplementedError {
  80. return nil, err
  81. }
  82. ret.Uptime, err = UptimeWithContext(ctx)
  83. if err != nil && err != common.ErrNotImplementedError {
  84. return nil, err
  85. }
  86. ret.Procs, err = numProcs(ctx)
  87. if err != nil && err != common.ErrNotImplementedError {
  88. return nil, err
  89. }
  90. ret.HostID, err = HostIDWithContext(ctx)
  91. if err != nil && err != common.ErrNotImplementedError {
  92. return nil, err
  93. }
  94. return ret, nil
  95. }
  96. // BootTime returns the system boot time expressed in seconds since the epoch.
  97. func BootTime() (uint64, error) {
  98. return BootTimeWithContext(context.Background())
  99. }
  100. func Uptime() (uint64, error) {
  101. return UptimeWithContext(context.Background())
  102. }
  103. func Users() ([]UserStat, error) {
  104. return UsersWithContext(context.Background())
  105. }
  106. func PlatformInformation() (string, string, string, error) {
  107. return PlatformInformationWithContext(context.Background())
  108. }
  109. // HostID returns the unique host ID provided by the OS.
  110. func HostID() (string, error) {
  111. return HostIDWithContext(context.Background())
  112. }
  113. func Virtualization() (string, string, error) {
  114. return VirtualizationWithContext(context.Background())
  115. }
  116. func KernelVersion() (string, error) {
  117. return KernelVersionWithContext(context.Background())
  118. }
  119. func SensorsTemperatures() ([]TemperatureStat, error) {
  120. return SensorsTemperaturesWithContext(context.Background())
  121. }
  122. func timeSince(ts uint64) uint64 {
  123. return uint64(time.Now().Unix()) - ts
  124. }