-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctionalities.go
More file actions
113 lines (100 loc) · 2.54 KB
/
functionalities.go
File metadata and controls
113 lines (100 loc) · 2.54 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"net"
"net/http"
"runtime"
"time"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/mem"
psnet "github.com/shirou/gopsutil/net"
)
type NetworkData struct {
Bandwidth uint64 `json:"bandwidth"`
CPUUsage float64 `json:"cpuUsage"`
MemoryUsage float64 `json:"memoryUsage"`
DiskUsage float64 `json:"diskUsage"`
PingLatency float64 `json:"pingLatency"`
Timestamp time.Time `json:"timestamp"`
HostInfo HostInfo `json:"hostInfo"`
NetworkInfo NetInfo `json:"networkInfo"`
}
type HostInfo struct {
Hostname string `json:"hostname"`
Platform string `json:"platform"`
OS string `json:"os"`
Uptime uint64 `json:"uptime"`
}
type NetInfo struct {
IPAddress string `json:"ipAddress"`
MacAddress string `json:"macAddress"`
}
func getNetworkData() NetworkData {
return NetworkData{
Bandwidth: getBandwidthUsage(),
CPUUsage: getCPUUsage(),
MemoryUsage: getMemoryUsage(),
DiskUsage: getDiskUsage(),
PingLatency: getPingLatency("google.com"),
Timestamp: time.Now(),
HostInfo: getHostInfo(),
NetworkInfo: getNetworkInfo(),
}
}
func getBandwidthUsage() uint64 {
stats, _ := psnet.IOCounters(false)
return stats[0].BytesSent + stats[0].BytesRecv
}
func getCPUUsage() float64 {
percentage, _ := cpu.Percent(time.Second, false)
if len(percentage) > 0 {
return percentage[0]
}
return 0
}
func getMemoryUsage() float64 {
vm, _ := mem.VirtualMemory()
return vm.UsedPercent
}
func getDiskUsage() float64 {
usage, _ := disk.Usage("/")
return usage.UsedPercent
}
func getPingLatency(host string) float64 {
start := time.Now()
_, err := http.Get("http://" + host)
if err != nil {
return -1
}
return float64(time.Since(start).Milliseconds())
}
func getHostInfo() HostInfo {
hostInfo, _ := host.Info()
return HostInfo{
Hostname: hostInfo.Hostname,
Platform: hostInfo.Platform,
OS: fmt.Sprintf("%s %s", hostInfo.OS, runtime.GOARCH),
Uptime: hostInfo.Uptime,
}
}
func getNetworkInfo() NetInfo {
interfaces, _ := net.Interfaces()
for _, iface := range interfaces {
if iface.Flags&net.FlagUp != 0 && iface.Flags&net.FlagLoopback == 0 {
addrs, _ := iface.Addrs()
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return NetInfo{
IPAddress: ipnet.IP.String(),
MacAddress: iface.HardwareAddr.String(),
}
}
}
}
}
}
return NetInfo{}
}