-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhost.go
58 lines (49 loc) · 1005 Bytes
/
host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package goinfo
import (
"fmt"
"io/ioutil"
"os"
"time"
)
// HostName host name and boot time struct.
type HostName struct {
Name string `json:"name"`
Boot string `json:"boot"`
Uptime string `json:"uptime"`
}
// String format the host struct.
func (h *HostName) String() (host string) {
if h == nil {
return
}
return fmt.Sprintf("user:%s, boot:%s, uptime:%s", h.Name, h.Boot, h.Uptime)
}
// Host getting host info by reading linux /proc/uptime file.
func (x *Xminfo) Host() (host *HostName, err error) {
host = new(HostName)
b, err := ioutil.ReadFile(gHostFile)
if err != nil {
return
}
length := len(b)
for i := 0; i < length; i++ {
if b[i] == ' ' {
b = b[0:i]
break
}
}
t := string(b) + "s"
// system running time.
d, err := time.ParseDuration(t)
if err != nil {
return
}
host.Uptime = d.String()
// getting system booting time.
host.Boot = time.Now().Add(-d).Format(gTimeFarmat)
host.Name, err = os.Hostname()
if err != nil {
return
}
return
}