@@ -2,6 +2,7 @@ package service
22
33import (
44 "bufio"
5+ "log"
56 "os"
67 "os/exec"
78 "strings"
@@ -12,16 +13,14 @@ import (
1213)
1314
1415type Host struct {
15- Hostname string
16- Uptime * UpTime
17- BootTime string
18- OS string
19- Platform string
20- Kernal string
21- Version string
22- Hardware string
23- Serial string
24- Model string
16+ Hostname string // 主机名
17+ OS string // 系统版本
18+ Vendor string // 厂家
19+ Model string // 硬件版本
20+ Serial string // 序列号
21+ BootTime string // 启动时间
22+ Kernal string // 内核信息
23+ InterfaceNum int // 网卡数
2524}
2625
2726type UpTime struct {
@@ -31,47 +30,170 @@ type UpTime struct {
3130 Year uint64
3231}
3332
34- func readKernal () string {
33+ var hostInfo * Host
34+
35+ //hostname 获取主机名
36+ func hostname () {
37+ var name string
38+ name , err := os .Hostname ()
39+ if err != nil {
40+ logger .Error (err )
41+ }
42+ hostInfo .Hostname = name
43+ }
44+
45+ //osVersion 获取操作系统版本
46+ func osVersion () {
47+ hostInfo .OS = readOSRelease ("PRETTY_NAME" )
48+ }
49+
50+ //getHostInfo 获取主机版本与厂家信息
51+ func hostVendor () {
52+ model := readLine ("/proc/device-tree/model" )
53+ switch {
54+ case strings .Contains (model , "Raspberry" ):
55+ hostInfo .Model = model
56+ hostInfo .Vendor = "Raspberry Pi"
57+ case strings .Contains (model , "Radxa" ):
58+ hostInfo .Model = model
59+ hostInfo .Vendor = "Rock Pi"
60+ case strings .Contains (model , "FriendlyARM" ):
61+ hostInfo .Model = model
62+ hostInfo .Vendor = "Nano Pi"
63+ default :
64+ hostInfo .Model = "unknown"
65+ hostInfo .Vendor = "unknown"
66+ }
67+ }
68+
69+ //getSerial 获取序列号
70+ func serial () {
71+ if PathExists ("/proc/device-tree/serial-number" ) {
72+ hostInfo .Serial = readLine ("/proc/device-tree/serial-number" )
73+ } else if PathExists ("/proc/cpuinfo" ) {
74+ hostInfo .Serial = scanCpuInfo ("Serial" )
75+ } else {
76+ hostInfo .Serial = "unknown"
77+ }
78+ }
79+
80+ //bootTime 启动时间
81+ func bootTime (t uint64 ) {
82+ hostInfo .BootTime = time .Unix (int64 (t ), 0 ).Format ("2006-01-02 15:04:05" )
83+ }
84+
85+ //kernel 读取内核信息
86+ func kernel () {
3587 cmd := exec .Command ("uname" , "-a" )
3688 stdout , err := cmd .Output ()
3789 if err != nil {
3890 logger .Info (err )
39- return ""
4091 }
41- return strings .Trim (string (stdout ), "\n " )
92+ hostInfo . Kernal = strings .Trim (string (stdout ), "\n " )
4293}
4394
44- func readInfo () map [string ]string {
45- var info = make (map [string ]string )
46- info ["hardware" ] = ""
47- info ["serial" ] = ""
48- info ["model" ] = ""
95+ //readLine 读取文件第一行
96+ func readLine (path string ) string {
97+ file , err := os .Open (path )
98+ if err != nil {
99+ log .Fatal (err )
100+ }
101+ defer file .Close ()
49102
50- f , err := os .Open ("/proc/cpuinfo" )
103+ var lineText string
104+ scanner := bufio .NewScanner (file )
105+ scanner .Scan ()
106+ lineText = scanner .Text ()
107+ return lineText [:len (lineText )- 1 ]
108+ }
109+
110+ //readOSRelease 读取/etc/os-release
111+ func readOSRelease (keyward string ) string {
112+ file , err := os .Open ("/etc/os-release" )
51113 if err != nil {
52- logger . Info (err )
114+ log . Fatal (err )
53115 }
54- defer f .Close ()
116+ defer file .Close ()
55117
56- scanner := bufio .NewScanner (f )
118+ var lineText string
119+ scanner := bufio .NewScanner (file )
57120 for scanner .Scan () {
58- line := scanner .Text ()
59- if strings .Contains (line , "Hardware" ) {
60- info ["hardware" ] = strings .Trim (strings .Split (line , ":" )[1 ], " " )
121+ lineText = scanner .Text ()
122+ if strings .Contains (lineText , keyward ) {
123+ lineText = strings .Trim (strings .Split (lineText , "=" )[1 ], "\" " )
124+ break
61125 }
126+ }
62127
63- if strings .Contains (line , "Serial" ) {
64- info ["serial" ] = strings .Trim (strings .Split (line , ":" )[1 ], " " )
65- }
128+ return lineText
129+ }
130+
131+ func scanCpuInfo (keyward string ) string {
132+ file , err := os .Open ("/proc/cpuinfo" )
133+ if err != nil {
134+ log .Fatal (err )
135+ }
136+ defer file .Close ()
66137
67- if strings .Contains (line , "Model" ) {
68- info ["model" ] = strings .Trim (strings .Split (line , ":" )[1 ], " " )
138+ var lineText string
139+ scanner := bufio .NewScanner (file )
140+ for scanner .Scan () {
141+ lineText = scanner .Text ()
142+ if strings .Contains (lineText , keyward ) {
143+ lineText = strings .Trim (strings .Split (lineText , ":" )[1 ], " " )
144+ break
69145 }
70146 }
71147
72- if scanner .Err () != nil {
73- logger .Error (scanner .Err ())
148+ return lineText
149+ }
150+
151+ func getInfo (path string ) string {
152+
153+ file , err := os .Open (path )
154+ if err != nil {
155+ log .Fatal (err )
74156 }
157+ defer file .Close ()
158+ var lineText string
159+ scanner := bufio .NewScanner (file )
160+ scanner .Scan ()
161+ lineText = scanner .Text ()
162+
163+ return lineText [:len (lineText )- 1 ]
164+ }
165+
166+ func readInfo () map [string ]string {
167+ var info = make (map [string ]string )
168+ info ["hardware" ] = ""
169+ info ["serial" ] = getInfo ("/proc/device-tree/serial-number" )
170+ info ["model" ] = getInfo ("/proc/device-tree/model" )
171+
172+ // f, err := os.Open("/proc/cpuinfo")
173+ // if err != nil {
174+ // logger.Info(err)
175+ // }
176+ // defer f.Close()
177+
178+ // scanner := bufio.NewScanner(f)
179+ // for scanner.Scan() {
180+ // line := scanner.Text()
181+ // if strings.Contains(line, "Hardware") {
182+ // info["hardware"] = strings.Trim(strings.Split(line, ":")[1], " ")
183+ // }
184+
185+ // if strings.Contains(line, "Serial") {
186+ // info["serial"] = strings.Trim(strings.Split(line, ":")[1], " ")
187+ // }
188+
189+ // if strings.Contains(line, "Model") {
190+ // info["model"] = strings.Trim(strings.Split(line, ":")[1], " ")
191+ // }
192+ // }
193+
194+ // if scanner.Err() != nil {
195+ // logger.Error(scanner.Err())
196+ // }
75197
76198 return info
77199}
@@ -94,24 +216,23 @@ func runningTime(t uint64) *UpTime {
94216 return upTime
95217}
96218
97- func GetHost () * Host {
219+ func getHost () {
98220 info , err := host .Info ()
99221 if err != nil {
100222 logger .Error (err )
101223 }
102224
103- boardInfo := readInfo ()
104-
105- host := & Host {
106- Hostname : info .Hostname ,
107- OS : info .OS ,
108- Platform : info .Platform ,
109- Hardware : boardInfo ["hardware" ],
110- Serial : boardInfo ["serial" ],
111- Model : boardInfo ["model" ],
112- Uptime : runningTime (info .Uptime ),
113- BootTime : time .Unix (int64 (info .BootTime ), 0 ).Format ("2006-01-02 15:04:05" ),
114- Kernal : readKernal (),
115- }
116- return host
225+ hostInfo = & Host {}
226+
227+ osVersion ()
228+ hostname ()
229+ hostVendor ()
230+ serial ()
231+ bootTime (info .BootTime )
232+ kernel ()
233+ hostInfo .InterfaceNum = len (GetNet ().Interface )
234+ }
235+
236+ func GetHost () * Host {
237+ return hostInfo
117238}
0 commit comments