Skip to content

Commit ea9bfd1

Browse files
committed
update
1 parent 91141a9 commit ea9bfd1

26 files changed

+5413
-182
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
# pi-monitor
2+
pi-monitor是一个WEB端的树莓派监控工具,使用go语言编写,方便安装使用。

api/api.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ type statistics struct {
1818
Host *service.Host
1919
CPU *service.CPU
2020
Memory *service.Memory
21+
Net *service.Net
2122
}
2223

23-
func Collect(g *gin.Context) {
24-
g.JSON(http.StatusOK, gin.H{
24+
func Collect(c *gin.Context) {
25+
c.JSON(http.StatusOK, gin.H{
2526
"Host": service.GetHost(),
2627
"CPU": service.GetCPU(),
2728
"Memory": service.GetMem(),
29+
"Net": service.GetNet(),
2830
})
2931
}

main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
*/
88
package main
99

10-
import "pi-monitor/route"
10+
import (
11+
"pi-monitor/route"
12+
)
1113

1214
func main() {
13-
//service.GetMem()
15+
//service.InitNetStat()
16+
//service.GetNet()
1417
route.Run()
1518
}

route/route.go

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,13 @@
88
package route
99

1010
import (
11-
"net/http"
1211
"pi-monitor/api"
1312
"pi-monitor/service"
14-
15-
"github.com/gin-gonic/gin"
1613
)
1714

1815
func route() {
1916
server.Gin.LoadHTMLGlob("views/**/*")
2017
server.Gin.Static("static/", "static/")
21-
22-
host := service.GetHost()
23-
24-
data := gin.H{}
25-
data["title"] = "Raspberrypi Monitor"
26-
data["boottime"] = host.BootTime
27-
data["version"] = host.Version
28-
data["serial"] = host.Serial
29-
data["hostname"] = host.Hostname
30-
data["platform"] = host.Platform
31-
data["kernal"] = host.Kernal
32-
data["hardware"] = host.Hardware
33-
data["model"] = host.Model
34-
data["os"] = host.OS
35-
36-
server.Gin.GET("/", func(c *gin.Context) {
37-
c.HTML(http.StatusOK, "home.tmpl", data)
38-
})
39-
18+
server.Gin.GET("/", service.Index)
4019
server.Gin.GET("/api/get", api.Collect)
4120
}

service/host.go

Lines changed: 168 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"bufio"
5+
"log"
56
"os"
67
"os/exec"
78
"strings"
@@ -12,16 +13,14 @@ import (
1213
)
1314

1415
type 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

2726
type 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
}

service/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ package service
22

33
func init() {
44
initLastTimes()
5+
getHost()
6+
// initNetStat()
57
}

service/mem.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Memory struct {
1717
Used uint64
1818
Free uint64
1919
Shared uint64
20-
Buffer uint64
20+
Cached uint64
2121
Available uint64
2222
UsedPercent float64
2323
SwapTotal uint64
@@ -36,7 +36,7 @@ func GetMem() *Memory {
3636
Used: v.Used / 1024 / 1024,
3737
Free: v.Free / 1024 / 1024,
3838
Shared: v.Shared / 1024 / 1024,
39-
Buffer: v.Buffers / 1024 / 1024,
39+
Cached: v.Cached / 1024 / 1024,
4040
Available: v.Available / 1024 / 1024,
4141
UsedPercent: v.UsedPercent,
4242
SwapTotal: v.SwapTotal / 1024 / 1024,

0 commit comments

Comments
 (0)