-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpi_console_only.go
208 lines (171 loc) · 5.94 KB
/
rpi_console_only.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"bytes"
"fmt"
"log"
"net/http"
"strconv"
"time"
linuxproc "github.com/c9s/goprocinfo/linux"
)
//
// cpu usage values are read from the /proc/stat pseudo-file with the help of the goprocinfo package...
// For the calculation two measurements are neaded: 'current' and 'previous'...
// More at: func calcSingleCoreUsage(curr, prev)...
//
type MyCPUStats struct {
Cpu0 float32
Cpu1 float32
Cpu2 float32
Cpu3 float32
}
//
// memory values are read from the /proc/meminfo pseudo-file with the help of the goprocinfo package...
// how are they calculated? Like 'htop' command, see question:
// - http://stackoverflow.com/questions/41224738/how-to-calculate-memory-usage-from-proc-meminfo-like-htop/
//
type MyMemoInfo struct {
TotalMachine uint64
TotalUsed uint64
Buffers uint64
Cached uint64
NonCacheNonBuffers uint64
}
func main() {
time_interval := 1 // this number represents seconds
push_to_influx := false
print_std_out := true
influxUrl := "http://10.143.0.218:8086"
cpuDBname := "pi_cpu"
memoDBname := "pi_memo"
currCPUStats := ReadCPUStats()
prevCPUStats := ReadCPUStats()
info := ReadMemoInfo()
for {
time.Sleep(time.Second * time.Duration(time_interval))
//
// CPU stuff below...
//
currCPUStats = ReadCPUStats()
coreStats := calcMyCPUStats(currCPUStats, prevCPUStats)
if print_std_out {
//fmt.Printf("CPU stats: %+v", coreStats)
// calculate % CPU usage
cpu := (coreStats.Cpu0 + coreStats.Cpu1 + coreStats.Cpu2 + coreStats.Cpu3) / 4
fmt.Printf("CPU: %+v", cpu)
}
if push_to_influx {
url := influxUrl + "/write?db=" + cpuDBname
body := []byte("cpu0,coreID=0 value=" + strconv.FormatFloat(float64(coreStats.Cpu0), 'f', -1, 32) + "\n" +
"cpu1,coreID=1 value=" + strconv.FormatFloat(float64(coreStats.Cpu0), 'f', -1, 32) + "\n" +
"cpu2,coreID=2 value=" + strconv.FormatFloat(float64(coreStats.Cpu0), 'f', -1, 32) + "\n" +
"cpu3,coreID=3 value=" + strconv.FormatFloat(float64(coreStats.Cpu0), 'f', -1, 32))
// fmt.Printf("%s", body)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
hc := http.Client{}
_, err = hc.Do(req)
if err != nil {
log.Fatal("could not send POST")
}
// fmt.Println(resp)
}
prevCPUStats = currCPUStats
//
// Memory stuff below...
//
info = ReadMemoInfo()
var mmInfo MyMemoInfo
mmInfo.TotalMachine = info.MemTotal
mmInfo.TotalUsed = info.MemTotal - info.MemFree
mmInfo.Buffers = info.Buffers
mmInfo.Cached = info.Cached + info.SReclaimable - info.Shmem
mmInfo.NonCacheNonBuffers = mmInfo.TotalUsed - (mmInfo.Buffers + mmInfo.Cached)
if print_std_out {
// fmt.Printf(" | Memory info: %+v\n", mmInfo)
fmt.Printf(" | Memory: %+v\n", mmInfo.TotalUsed)
}
if push_to_influx {
url := influxUrl + "/write?db=" + memoDBname
body := []byte("TotalMachine,memTag=TotalMachine value=" + strconv.Itoa(int(mmInfo.TotalMachine)) + "\n" +
"TotalUsed,memTag=TotalUsed value=" + strconv.Itoa(int(mmInfo.TotalUsed)) + "\n" +
"Buffers,memTag=Buffers value=" + strconv.Itoa(int(mmInfo.Buffers)) + "\n" +
"Cached,memTag=Cached value=" + strconv.Itoa(int(mmInfo.Cached)) + "\n" +
"NonCacheNonBuffers,memTag=NonCacheNonBuffers value=" + strconv.Itoa(int(mmInfo.NonCacheNonBuffers)))
// fmt.Printf("%s", body)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
hc := http.Client{}
_, err = hc.Do(req)
if err != nil {
log.Fatal("could not send POST")
}
// fmt.Println(resp)
}
}
}
func ReadCPUStats() *linuxproc.Stat {
stat, err := linuxproc.ReadStat("/proc/stat")
if err != nil {
log.Fatal("stat read fail")
}
// fmt.Println(stat)
return stat
}
func calcMyCPUStats(curr, prev *linuxproc.Stat) *MyCPUStats {
var stats MyCPUStats
// fmt.Println("cpu0: ", calcSingleCoreUsage(curr.CPUStats[0], prev.CPUStats[0]))
// fmt.Println("cpu1: ", calcSingleCoreUsage(curr.CPUStats[1], prev.CPUStats[1]))
// fmt.Println("cpu2: ", calcSingleCoreUsage(curr.CPUStats[2], prev.CPUStats[2]))
// fmt.Println("cpu3: ", calcSingleCoreUsage(curr.CPUStats[3], prev.CPUStats[3]))
stats.Cpu0 = calcSingleCoreUsage(curr.CPUStats[0], prev.CPUStats[0])
stats.Cpu1 = calcSingleCoreUsage(curr.CPUStats[1], prev.CPUStats[1])
stats.Cpu2 = calcSingleCoreUsage(curr.CPUStats[2], prev.CPUStats[2])
stats.Cpu3 = calcSingleCoreUsage(curr.CPUStats[3], prev.CPUStats[3])
// fmt.Printf("CPU stats: %+v", stats)
return &stats
}
func calcSingleCoreUsage(curr, prev linuxproc.CPUStat) float32 {
/*
* source: http://stackoverflow.com/questions/23367857/accurate-calculation-of-cpu-usage-given-in-percentage-in-linux
*
* PrevIdle = previdle + previowait
* Idle = idle + iowait
*
* PrevNonIdle = prevuser + prevnice + prevsystem + previrq + prevsoftirq + prevsteal
* NonIdle = user + nice + system + irq + softirq + steal
*
* PrevTotal = PrevIdle + PrevNonIdle
* Total = Idle + NonIdle
*
* # differentiate: actual value minus the previous one
* totald = Total - PrevTotal
* idled = Idle - PrevIdle
*
* CPU_Percentage = (totald - idled)/totald
*/
// fmt.Printf("curr:\n %+v\n", curr)
// fmt.Printf("prev:\n %+v\n", prev)
PrevIdle := prev.Idle + prev.IOWait
Idle := curr.Idle + curr.IOWait
PrevNonIdle := prev.User + prev.Nice + prev.System + prev.IRQ + prev.SoftIRQ + prev.Steal
NonIdle := curr.User + curr.Nice + curr.System + curr.IRQ + curr.SoftIRQ + curr.Steal
PrevTotal := PrevIdle + PrevNonIdle
Total := Idle + NonIdle
// fmt.Println(PrevIdle, Idle, PrevNonIdle, NonIdle, PrevTotal, Total)
// differentiate: actual value minus the previous one
totald := Total - PrevTotal
idled := Idle - PrevIdle
CPU_Percentage := (float32(totald) - float32(idled)) / float32(totald)
return CPU_Percentage
}
//
// Memory
//
//
func ReadMemoInfo() *linuxproc.MemInfo {
info, err := linuxproc.ReadMemInfo("/proc/meminfo")
if err != nil {
log.Fatal("info read fail")
}
// fmt.Printf("Memory info struct:\n%+v", info)
return info
}