-
Notifications
You must be signed in to change notification settings - Fork 10
/
template_helpers.go
70 lines (52 loc) · 1.75 KB
/
template_helpers.go
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
package main
import (
"net"
"sort"
"strings"
linuxproc "github.com/marc-barry/goprocinfo/linux"
)
// InterfaceRateStats implements sort.Interface for []InterfaceRateStat based on Iface (i.e. interface name).
type InterfaceRateStats []InterfaceRateStat
func (l InterfaceRateStats) Len() int { return len(l) }
func (l InterfaceRateStats) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l InterfaceRateStats) Less(i, j int) bool { return l[i].Iface < l[j].Iface }
// NetworkStats implements sort.Interface for []linuxproc.NetworkStat based on Iface (i.e. interface name).
type NetworkStats []linuxproc.NetworkStat
func (l NetworkStats) Len() int { return len(l) }
func (l NetworkStats) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l NetworkStats) Less(i, j int) bool { return l[i].Iface < l[j].Iface }
func getInterfaces() []net.Interface {
collectInterfaces()
return IfaceList.All()
}
func getInterfaceIPAddressesString(iface net.Interface) string {
addrs, err := iface.Addrs()
if err != nil {
return err.Error()
}
var addrStrings = make([]string, 0, len(addrs))
for _, addr := range addrs {
addrStrings = append(addrStrings, addr.String())
}
return strings.Join(addrStrings, ", ")
}
func getCPUInfo() *linuxproc.CPUInfo {
info, err := readCPUInfo()
if err != nil {
Log.WithField("error", err).Error("Error reading cpuinfo.")
}
return info
}
func getNetworkDeviceStats() []linuxproc.NetworkStat {
stats, err := readNetworkDeviceStats()
if err != nil {
Log.WithField("error", err).Error("Error reading network device stats.")
}
sort.Sort(NetworkStats(stats))
return stats
}
func getInterfaceRateStats() []InterfaceRateStat {
stats := copyInterfaceRateStats()
sort.Sort(InterfaceRateStats(stats))
return stats
}