This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
/
batchinsights.go
156 lines (131 loc) · 3.86 KB
/
batchinsights.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package batchinsights
import (
"fmt"
"runtime"
"time"
"github.com/Azure/batch-insights/pkg/cpu"
"github.com/Azure/batch-insights/pkg/disk"
"github.com/Azure/batch-insights/pkg/utils"
"github.com/dustin/go-humanize"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/net"
)
func getSamplingRate(rate time.Duration) time.Duration {
if rate <= time.Duration(0) {
return DefaultSamplingRate
}
return rate
}
// ListenForStats Start the sanpling of node metrics
func ListenForStats(config Config) {
var netIO = utils.IOAggregator{}
var gpuStatsCollector = NewGPUStatsCollector()
defer gpuStatsCollector.Shutdown()
var appInsightsService = createAppInsightsService(config)
for range time.Tick(getSamplingRate(config.SamplingRate)) {
gpuStatsCollector.GetStats()
var stats = NodeStats{}
if !config.Disable.Memory {
v, err := mem.VirtualMemory()
if err == nil {
stats.Memory = v
} else {
fmt.Println(err)
}
}
if !config.Disable.CPU {
cpus, err := cpu.PerCpuPercent()
if err == nil {
stats.CPUPercents = cpus
} else {
fmt.Println(err)
}
}
if !config.Disable.DiskUsage {
stats.DiskUsage = disk.GetDiskUsage()
}
if !config.Disable.DiskIO {
stats.DiskIO = disk.DiskIO()
}
if !config.Disable.NetworkIO {
stats.NetIO = getNetIO(&netIO)
}
if !config.Disable.GPU {
stats.Gpus = gpuStatsCollector.GetStats()
}
processes, err := ListProcesses(config.Processes)
if err == nil {
stats.Processes = processes
} else {
fmt.Println(err)
}
if appInsightsService != nil {
appInsightsService.UploadStats(stats)
} else {
printStats(stats)
}
}
}
func getNetIO(diskIO *utils.IOAggregator) *utils.IOStats {
var counters, err = net.IOCounters(false)
if err != nil {
fmt.Println(err)
} else if len(counters) >= 1 {
var stats = diskIO.UpdateAggregates(counters[0].BytesRecv, counters[0].BytesSent)
return &stats
}
return nil
}
// PrintSystemInfo print system info needed
func PrintSystemInfo() {
fmt.Printf("System information:\n")
fmt.Printf(" OS: %s\n", runtime.GOOS)
}
func getConfiguration() {
}
func printStats(stats NodeStats) {
fmt.Printf("========================= Stats =========================\n")
fmt.Printf("Cpu percent: %f%%, %v cpu(s)\n", avg(stats.CPUPercents), len(stats.CPUPercents))
fmt.Printf("Memory used: %s/%s\n", humanize.Bytes(stats.Memory.Used), humanize.Bytes(stats.Memory.Total))
if len(stats.DiskUsage) > 0 {
fmt.Printf("Disk usage:\n")
for _, usage := range stats.DiskUsage {
fmt.Printf(" - %s: %s/%s (%v%%)\n", usage.Path, humanize.Bytes(usage.Used), humanize.Bytes(usage.Total), usage.UsedPercent)
}
}
if stats.DiskIO != nil {
fmt.Printf("Disk IO: R:%sps, W:%sps\n", humanize.Bytes(stats.DiskIO.ReadBps), humanize.Bytes(stats.DiskIO.WriteBps))
}
if stats.NetIO != nil {
fmt.Printf("NET IO: R:%sps, S:%sps\n", humanize.Bytes(stats.NetIO.ReadBps), humanize.Bytes(stats.NetIO.WriteBps))
}
if len(stats.Gpus) > 0 {
fmt.Printf("GPU(s) usage:\n")
for _, usage := range stats.Gpus {
fmt.Printf(" - GPU: %f%%, Memory: %f%%\n", usage.GPU, usage.Memory)
}
}
if len(stats.Processes) > 0 {
fmt.Printf("Tracked processes:\n")
for _, process := range stats.Processes {
fmt.Printf(" - %s (%d), CPU: %f%%, Memory: %s\n", process.name, process.pid, process.cpu, humanize.Bytes(process.memory))
}
}
fmt.Println()
fmt.Println()
}
func avg(array []float64) float64 {
var total float64
for _, value := range array {
total += value
}
return total / float64(len(array))
}
func createAppInsightsService(config Config) *AppInsightsService {
if config.InstrumentationKey != "" {
service := NewAppInsightsService(config.InstrumentationKey, config.PoolID, config.NodeID, config.Aggregation)
return &service
}
fmt.Println("APP_INSIGHTS_INSTRUMENTATION_KEY is not set; will not upload to Application Insights")
return nil
}