Skip to content

Commit 37375f0

Browse files
author
root
committed
update
1 parent 92b6782 commit 37375f0

File tree

4 files changed

+115
-4
lines changed

4 files changed

+115
-4
lines changed

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"pi-monitor/service"
5+
6+
"github.com/wonderivan/logger"
57
)
68

79
func main() {
8-
service.GetCPU()
10+
logger.Info(service.GetHost())
911
//route.Run()
1012
}

service/cpu.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ func freq() *Freq {
120120
}
121121

122122
func GetCPU() {
123+
logger.Info(cpu.Info())
124+
return
123125
time.Sleep(time.Second)
124126
for {
125127
logger.Info(cpuLoad())

service/host.go

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,117 @@
11
package service
22

3+
import (
4+
"bufio"
5+
"os"
6+
"os/exec"
7+
"strings"
8+
"time"
9+
10+
"github.com/shirou/gopsutil/v3/host"
11+
"github.com/wonderivan/logger"
12+
)
13+
314
type Host struct {
415
Hostname string
5-
IP string
6-
Uptime string
16+
Uptime *UpTime
17+
BootTime string
718
OS string
19+
Platform string
820
Kernal string
921
Version string
22+
Hardware string
23+
Serial string
24+
Model string
25+
}
26+
27+
type UpTime struct {
28+
Minute uint64
29+
Hour uint64
30+
Day uint64
31+
Year uint64
32+
}
33+
34+
func readKernal() string {
35+
cmd := exec.Command("uname", "-a")
36+
stdout, err := cmd.Output()
37+
if err != nil {
38+
logger.Info(err)
39+
return ""
40+
}
41+
return strings.Trim(string(stdout), "\n")
42+
}
43+
44+
func readInfo() map[string]string {
45+
var info = make(map[string]string)
46+
info["hardware"] = ""
47+
info["serial"] = ""
48+
info["model"] = ""
49+
50+
f, err := os.Open("/proc/cpuinfo")
51+
if err != nil {
52+
logger.Info(err)
53+
}
54+
defer f.Close()
55+
56+
scanner := bufio.NewScanner(f)
57+
for scanner.Scan() {
58+
line := scanner.Text()
59+
if strings.Contains(line, "Hardware") {
60+
info["hardware"] = strings.Trim(strings.Split(line, ":")[1], " ")
61+
}
62+
63+
if strings.Contains(line, "Serial") {
64+
info["serial"] = strings.Trim(strings.Split(line, ":")[1], " ")
65+
}
66+
67+
if strings.Contains(line, "Model") {
68+
info["model"] = strings.Trim(strings.Split(line, ":")[1], " ")
69+
}
70+
}
71+
72+
if scanner.Err() != nil {
73+
logger.Error(scanner.Err())
74+
}
75+
76+
return info
77+
}
78+
79+
func runningTime(t uint64) *UpTime {
80+
var mins = (t - (t % 60)) / 60
81+
var min = mins % 60
82+
var hours = (mins - min) / 60
83+
var hour = hours % 24
84+
var day = hours / 24
85+
var year = hours / 24 / 365
86+
87+
upTime := &UpTime{
88+
Minute: min,
89+
Hour: hour,
90+
Day: day,
91+
Year: year,
92+
}
93+
94+
return upTime
95+
}
96+
97+
func GetHost() *Host {
98+
info, err := host.Info()
99+
if err != nil {
100+
logger.Error(err)
101+
}
102+
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
10117
}

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ github.com/modern-go/reflect2
3333
# github.com/shirou/gopsutil/v3 v3.20.10
3434
github.com/shirou/gopsutil/v3/cpu
3535
github.com/shirou/gopsutil/v3/host
36+
github.com/shirou/gopsutil/v3/net
3637
github.com/shirou/gopsutil/v3/internal/common
3738
github.com/shirou/gopsutil/v3/process
3839
github.com/shirou/gopsutil/v3/mem
39-
github.com/shirou/gopsutil/v3/net
4040
# github.com/ugorji/go/codec v1.1.7
4141
github.com/ugorji/go/codec
4242
# github.com/wonderivan/logger v1.0.0

0 commit comments

Comments
 (0)