...
1 package host
2
3 import (
4 "context"
5 "encoding/json"
6 "os"
7 "runtime"
8 "time"
9
10 "github.com/shirou/gopsutil/internal/common"
11 )
12
13 var invoke common.Invoker = common.Invoke{}
14
15
16
17 type InfoStat struct {
18 Hostname string `json:"hostname"`
19 Uptime uint64 `json:"uptime"`
20 BootTime uint64 `json:"bootTime"`
21 Procs uint64 `json:"procs"`
22 OS string `json:"os"`
23 Platform string `json:"platform"`
24 PlatformFamily string `json:"platformFamily"`
25 PlatformVersion string `json:"platformVersion"`
26 KernelVersion string `json:"kernelVersion"`
27 KernelArch string `json:"kernelArch"`
28 VirtualizationSystem string `json:"virtualizationSystem"`
29 VirtualizationRole string `json:"virtualizationRole"`
30 HostID string `json:"hostid"`
31 }
32
33 type UserStat struct {
34 User string `json:"user"`
35 Terminal string `json:"terminal"`
36 Host string `json:"host"`
37 Started int `json:"started"`
38 }
39
40 type TemperatureStat struct {
41 SensorKey string `json:"sensorKey"`
42 Temperature float64 `json:"sensorTemperature"`
43 }
44
45 func (h InfoStat) String() string {
46 s, _ := json.Marshal(h)
47 return string(s)
48 }
49
50 func (u UserStat) String() string {
51 s, _ := json.Marshal(u)
52 return string(s)
53 }
54
55 func (t TemperatureStat) String() string {
56 s, _ := json.Marshal(t)
57 return string(s)
58 }
59
60 func Info() (*InfoStat, error) {
61 return InfoWithContext(context.Background())
62 }
63
64 func InfoWithContext(ctx context.Context) (*InfoStat, error) {
65 var err error
66 ret := &InfoStat{
67 OS: runtime.GOOS,
68 }
69
70 ret.Hostname, err = os.Hostname()
71 if err != nil && err != common.ErrNotImplementedError {
72 return nil, err
73 }
74
75 ret.Platform, ret.PlatformFamily, ret.PlatformVersion, err = PlatformInformationWithContext(ctx)
76 if err != nil && err != common.ErrNotImplementedError {
77 return nil, err
78 }
79
80 ret.KernelVersion, err = KernelVersionWithContext(ctx)
81 if err != nil && err != common.ErrNotImplementedError {
82 return nil, err
83 }
84
85 ret.KernelArch, err = KernelArch()
86 if err != nil && err != common.ErrNotImplementedError {
87 return nil, err
88 }
89
90 ret.VirtualizationSystem, ret.VirtualizationRole, err = VirtualizationWithContext(ctx)
91 if err != nil && err != common.ErrNotImplementedError {
92 return nil, err
93 }
94
95 ret.BootTime, err = BootTimeWithContext(ctx)
96 if err != nil && err != common.ErrNotImplementedError {
97 return nil, err
98 }
99
100 ret.Uptime, err = UptimeWithContext(ctx)
101 if err != nil && err != common.ErrNotImplementedError {
102 return nil, err
103 }
104
105 ret.Procs, err = numProcs(ctx)
106 if err != nil && err != common.ErrNotImplementedError {
107 return nil, err
108 }
109
110 ret.HostID, err = HostIDWithContext(ctx)
111 if err != nil && err != common.ErrNotImplementedError {
112 return nil, err
113 }
114
115 return ret, nil
116 }
117
118
119 func BootTime() (uint64, error) {
120 return BootTimeWithContext(context.Background())
121 }
122
123 func Uptime() (uint64, error) {
124 return UptimeWithContext(context.Background())
125 }
126
127 func Users() ([]UserStat, error) {
128 return UsersWithContext(context.Background())
129 }
130
131 func PlatformInformation() (string, string, string, error) {
132 return PlatformInformationWithContext(context.Background())
133 }
134
135
136 func HostID() (string, error) {
137 return HostIDWithContext(context.Background())
138 }
139
140 func Virtualization() (string, string, error) {
141 return VirtualizationWithContext(context.Background())
142 }
143
144 func KernelVersion() (string, error) {
145 return KernelVersionWithContext(context.Background())
146 }
147
148 func SensorsTemperatures() ([]TemperatureStat, error) {
149 return SensorsTemperaturesWithContext(context.Background())
150 }
151
152 func timeSince(ts uint64) uint64 {
153 return uint64(time.Now().Unix()) - ts
154 }
155
View as plain text