host_openbsd.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // +build openbsd
  2. package host
  3. import (
  4. "bytes"
  5. "context"
  6. "encoding/binary"
  7. "io/ioutil"
  8. "os"
  9. "strings"
  10. "unsafe"
  11. "github.com/shirou/gopsutil/internal/common"
  12. "github.com/shirou/gopsutil/process"
  13. "golang.org/x/sys/unix"
  14. )
  15. const (
  16. UTNameSize = 32 /* see MAXLOGNAME in <sys/param.h> */
  17. UTLineSize = 8
  18. UTHostSize = 16
  19. )
  20. func HostIDWithContext(ctx context.Context) (string, error) {
  21. return "", common.ErrNotImplementedError
  22. }
  23. func numProcs(ctx context.Context) (uint64, error) {
  24. procs, err := process.PidsWithContext(ctx)
  25. if err != nil {
  26. return 0, err
  27. }
  28. return uint64(len(procs)), nil
  29. }
  30. func PlatformInformationWithContext(ctx context.Context) (string, string, string, error) {
  31. platform := ""
  32. family := ""
  33. version := ""
  34. p, err := unix.Sysctl("kern.ostype")
  35. if err == nil {
  36. platform = strings.ToLower(p)
  37. }
  38. v, err := unix.Sysctl("kern.osrelease")
  39. if err == nil {
  40. version = strings.ToLower(v)
  41. }
  42. return platform, family, version, nil
  43. }
  44. func VirtualizationWithContext(ctx context.Context) (string, string, error) {
  45. return "", "", common.ErrNotImplementedError
  46. }
  47. func UsersWithContext(ctx context.Context) ([]UserStat, error) {
  48. var ret []UserStat
  49. utmpfile := "/var/run/utmp"
  50. file, err := os.Open(utmpfile)
  51. if err != nil {
  52. return ret, err
  53. }
  54. defer file.Close()
  55. buf, err := ioutil.ReadAll(file)
  56. if err != nil {
  57. return ret, err
  58. }
  59. u := Utmp{}
  60. entrySize := int(unsafe.Sizeof(u))
  61. count := len(buf) / entrySize
  62. for i := 0; i < count; i++ {
  63. b := buf[i*entrySize : i*entrySize+entrySize]
  64. var u Utmp
  65. br := bytes.NewReader(b)
  66. err := binary.Read(br, binary.LittleEndian, &u)
  67. if err != nil || u.Time == 0 || u.Name[0] == 0 {
  68. continue
  69. }
  70. user := UserStat{
  71. User: common.IntToString(u.Name[:]),
  72. Terminal: common.IntToString(u.Line[:]),
  73. Host: common.IntToString(u.Host[:]),
  74. Started: int(u.Time),
  75. }
  76. ret = append(ret, user)
  77. }
  78. return ret, nil
  79. }
  80. func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, error) {
  81. return []TemperatureStat{}, common.ErrNotImplementedError
  82. }
  83. func KernelVersionWithContext(ctx context.Context) (string, error) {
  84. _, _, version, err := PlatformInformationWithContext(ctx)
  85. return version, err
  86. }