Skip to content

Commit 5ae70cd

Browse files
committed
update
1 parent 4219dd1 commit 5ae70cd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+11798
-11
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/pi-monitor.iml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/watcherTasks.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

device/host.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/*
2+
* @Author: jia
3+
* @LastEditTime: 2020-12-17 09:21:59
4+
* @FilePath: /pi-monitor/device/host.go
5+
* @Date: 2020-12-17 09:17:06
6+
* @Software: VS Code
7+
*/
8+
package device
9+
10+
import (
11+
"bufio"
12+
"github.com/shirou/gopsutil/v3/host"
13+
log "github.com/sirupsen/logrus"
14+
"github.com/wonderivan/logger"
15+
"os"
16+
"os/exec"
17+
"pi-monitor/helper"
18+
"strings"
19+
"time"
20+
)
21+
22+
type Host struct {
23+
Hostname string // 主机名
24+
OS string // 系统版本
25+
Vendor string // 厂家
26+
Model string // 硬件版本
27+
Serial string // 序列号
28+
BootTime string // 启动时间
29+
Kernal string // 内核信息
30+
InterfaceNum int // 网卡数
31+
InterfaceNames []string // 网卡名称
32+
infostat *host.InfoStat
33+
}
34+
35+
var hostinfo *Host
36+
37+
func init() {
38+
hostinfo = new(Host)
39+
hostinfo.getInfostat()
40+
hostinfo.hostname()
41+
hostinfo.osVersion()
42+
hostinfo.hostVendor()
43+
hostinfo.serial()
44+
hostinfo.bootTime()
45+
hostinfo.kernel()
46+
hostinfo.InterfaceNum = GetNetCount()
47+
hostinfo.InterfaceNames = GetNetNames()
48+
}
49+
50+
func GetHost() *Host {
51+
return hostinfo
52+
}
53+
54+
func (this *Host) getInfostat() {
55+
stat, err := host.Info()
56+
if err != nil {
57+
log.Error(err)
58+
}
59+
this.infostat = stat
60+
}
61+
62+
func (this *Host) hostname() {
63+
this.Hostname = this.infostat.Hostname
64+
}
65+
66+
func (this *Host) osVersion() {
67+
this.OS = this.readOSRelease("PRETTY_NAME")
68+
if this.OS == "" {
69+
this.OS = this.infostat.OS
70+
}
71+
}
72+
73+
//getHostInfo 获取主机版本与厂家信息
74+
func (this *Host) hostVendor() {
75+
model := helper.ReadLine("/proc/device-tree/model")
76+
switch {
77+
case strings.Contains(model, "Raspberry"):
78+
this.Model = model
79+
this.Vendor = "Raspberry Pi"
80+
case strings.Contains(model, "Radxa"):
81+
this.Model = model
82+
this.Vendor = "Rock Pi"
83+
case strings.Contains(model, "FriendlyARM"):
84+
this.Model = model
85+
this.Vendor = "Nano Pi"
86+
default:
87+
this.Model = "unknown"
88+
this.Vendor = "unknown"
89+
}
90+
}
91+
92+
//getSerial 获取序列号
93+
func (this *Host) serial() {
94+
if helper.PathExists("/proc/device-tree/serial-number") {
95+
this.Serial = helper.ReadLine("/proc/device-tree/serial-number")
96+
} else if helper.PathExists("/proc/cpuinfo") {
97+
this.Serial = scanCpuInfo("Serial")
98+
} else {
99+
this.Serial = "unknown"
100+
}
101+
}
102+
103+
//bootTime 启动时间
104+
func (this *Host) bootTime() {
105+
this.BootTime = time.Unix(int64(this.infostat.BootTime), 0).Format("2006-01-02 15:04:05")
106+
}
107+
108+
//kernel 读取内核信息
109+
func (this *Host) kernel() {
110+
cmd := exec.Command("uname", "-a")
111+
stdout, err := cmd.Output()
112+
if err != nil {
113+
logger.Info(err)
114+
}
115+
this.Kernal = strings.Trim(string(stdout), "\n")
116+
}
117+
118+
//readOSRelease 读取/etc/os-release
119+
func (this *Host) readOSRelease(keyward string) string {
120+
if !helper.PathExists("/etc/os-release") {
121+
return ""
122+
}
123+
124+
file, err := os.Open("/etc/os-release")
125+
if err != nil {
126+
log.Fatal(err)
127+
return ""
128+
}
129+
defer file.Close()
130+
131+
var lineText string
132+
scanner := bufio.NewScanner(file)
133+
for scanner.Scan() {
134+
lineText = scanner.Text()
135+
if strings.Contains(lineText, keyward) {
136+
lineText = strings.Trim(strings.Split(lineText, "=")[1], "\"")
137+
break
138+
}
139+
}
140+
141+
return lineText
142+
}
143+
144+
func scanCpuInfo(keyward string) string {
145+
if !helper.PathExists("/proc/cpuinfo") {
146+
return ""
147+
}
148+
149+
file, err := os.Open("/proc/cpuinfo")
150+
if err != nil {
151+
log.Fatal(err)
152+
return ""
153+
}
154+
defer file.Close()
155+
156+
var lineText string
157+
scanner := bufio.NewScanner(file)
158+
for scanner.Scan() {
159+
lineText = scanner.Text()
160+
if strings.Contains(lineText, keyward) {
161+
lineText = strings.Trim(strings.Split(lineText, ":")[1], " ")
162+
break
163+
}
164+
}
165+
166+
return lineText
167+
}

device/host_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
@Time : 2020/12/17 10:28 上午
3+
@Author : jia
4+
@File : host_test.go.go
5+
@Software : GoLand
6+
*/
7+
8+
package device
9+
10+
import (
11+
"fmt"
12+
"testing"
13+
)
14+
15+
func TestGetHost(t *testing.T) {
16+
fmt.Println(GetHost())
17+
}

device/net.go

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
@Time : 2020/12/17 9:41 上午
3+
@Author : jia
4+
@File : net.go.go
5+
@Software : GoLand
6+
*/
7+
8+
package device
9+
10+
import (
11+
"github.com/shirou/gopsutil/v3/net"
12+
log "github.com/sirupsen/logrus"
13+
"time"
14+
)
15+
16+
type Net struct {
17+
Interface []*Interface
18+
Names []string
19+
Count int
20+
}
21+
22+
type Interface struct {
23+
Name string
24+
HardwareAddr string
25+
Addrs net.InterfaceAddrList
26+
BytesSent uint64
27+
BytesRecv uint64
28+
PacketsSent uint64
29+
PacketsRecv uint64
30+
Send float64
31+
Recv float64
32+
}
33+
34+
var (
35+
lastInterface map[string]*Interface
36+
lastTime time.Time
37+
)
38+
39+
func init() {
40+
lastInterface = interfaceStat()
41+
lastTime = time.Now()
42+
}
43+
44+
func GetInterfaceStat() {
45+
currentInterface := interfaceStat()
46+
timeNow := time.Now()
47+
for _, interfaceStat := range currentInterface {
48+
last := lastInterface[interfaceStat.Name]
49+
var recv, send, diff float64 = 0, 0, 0
50+
51+
diff = float64(timeNow.UnixNano()/1e6-lastTime.UnixNano()/1e6) / 1000
52+
recv = float64(interfaceStat.BytesRecv-last.BytesRecv) / diff / 1024
53+
send = float64(interfaceStat.BytesSent-last.BytesSent) / diff / 1024
54+
currentInterface[interfaceStat.Name].Recv = recv
55+
currentInterface[interfaceStat.Name].Send = send
56+
}
57+
58+
lastInterface = currentInterface
59+
lastTime = timeNow
60+
}
61+
62+
func GetNetCount() int {
63+
return len(lastInterface)
64+
}
65+
66+
func GetNetNames() []string {
67+
var names []string
68+
for _, v := range lastInterface {
69+
names = append(names, v.Name)
70+
}
71+
return nil
72+
}
73+
74+
func interfaceStat() map[string]*Interface {
75+
var interfaceStatList = make(map[string]*Interface)
76+
InterfaceStatList := loadInterfaceStatList()
77+
ioCounters := loadCounters()
78+
for _, interfaceStat := range InterfaceStatList {
79+
ioCounter := getCounter(interfaceStat.Name, ioCounters)
80+
i := &Interface{
81+
Name: interfaceStat.Name,
82+
HardwareAddr: interfaceStat.HardwareAddr,
83+
Addrs: interfaceStat.Addrs,
84+
BytesRecv: ioCounter.BytesRecv,
85+
BytesSent: ioCounter.BytesSent,
86+
PacketsRecv: ioCounter.PacketsRecv,
87+
PacketsSent: ioCounter.PacketsSent,
88+
Recv: 0,
89+
Send: 0,
90+
}
91+
92+
interfaceStatList[interfaceStat.Name] = i
93+
}
94+
return interfaceStatList
95+
}
96+
97+
func loadInterfaceStatList() net.InterfaceStatList {
98+
net.Interfaces()
99+
interfaceList, err := net.Interfaces()
100+
if err != nil {
101+
log.Error(err)
102+
}
103+
return interfaceList
104+
}
105+
106+
func loadCounters() []net.IOCountersStat {
107+
ioCountersStat, err := net.IOCounters(false)
108+
if err != nil {
109+
log.Error(err)
110+
}
111+
return ioCountersStat
112+
}
113+
114+
func getCounter(name string, ioCounters []net.IOCountersStat) net.IOCountersStat {
115+
for _, value := range ioCounters {
116+
if value.Name == name {
117+
return value
118+
}
119+
}
120+
121+
return net.IOCountersStat{}
122+
}

0 commit comments

Comments
 (0)