-
Notifications
You must be signed in to change notification settings - Fork 14
/
rpi_monitor.py
executable file
·346 lines (289 loc) · 10.1 KB
/
rpi_monitor.py
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python
import rrdtool
from rrdtool import update as rrd_update
import os, time
import psutil
RUNDIR = os.path.dirname(os.path.realpath(__file__))
RRDSDIR = RUNDIR + '/rrds'
CpuRRDFile = RRDSDIR + '/cpustatus.rrd'
MemRRDFile = RRDSDIR + '/meminfo.rrd'
UptimeRRDFile = RRDSDIR + '/uptime.rrd'
CPUCoresRRDFile = RRDSDIR + '/cpucores.rrd'
'''
1 day - 5-minute resolution
1 week - 15-minute resolution
1 month - 1-hour resolution
1 year - 6-hour resolution
RRA:AVERAGE:0.5:1:288
RRA:AVERAGE:0.5:3:672
RRA:AVERAGE:0.5:12:744
RRA:AVERAGE:0.5:72:1480
5-minute for 3 days
RRA:AVERAGE:0.5:1:864
15-minute for 2 weeks
RRA:AVERAGE:0.5:3:1344
1-hour for 2 months
RRA:AVERAGE:0.5:12:1488
3-hour for 1 year
RRA:AVERAGE:0.5:36:2960
'''
def floatFormat(num):
return "{0:.2f}".format(num)
#def updateCPURRD(ctemp, gtemp, cusage):
# global CpuRRDFile
# if not os.path.exists(CpuRRDFile):
# ret = rrdtool.create(CpuRRDFile, '--step', '300',
# 'DS:cputemp:GAUGE:600:30:100',
# 'DS:gputemp:GAUGE:600:30:100',
# 'DS:cpuUsage:GAUGE:600:0:100',
# 'RRA:AVERAGE:0.5:1:228',
# 'RRA:AVERAGE:0.5:3:672',
# 'RRA:AVERAGE:0.5:12:744')
# if ret:
# print rrdtool.error()
# return False
#
# #update data
# ret = rrd_update(CpuRRDFile, 'N:%s:%s:%s' %(ctemp, gtemp, cusage));
# if ret:
# print rrdtool.error()
# return False
#
# return True
def updateCPURRD(ctemp, cusage, pids):
if not os.path.exists(CpuRRDFile):
ret = rrdtool.create(CpuRRDFile, '--step', '300',
'DS:cputemp:GAUGE:600:0:100',
'DS:cpuUsage:GAUGE:600:0:100',
'DS:pids:GAUGE:600:0:U',
'RRA:AVERAGE:0.5:1:864',
'RRA:AVERAGE:0.5:3:1344',
'RRA:AVERAGE:0.5:12:1488',
'RRA:AVERAGE:0.5:36:2960')
if ret:
print(rrdtool.error())
return False
#update data
ret = rrd_update(CpuRRDFile, 'N:%s:%s:%s' %(ctemp, cusage, pids));
if ret:
print(rrdtool.error())
return False
return True
def updateCPUCoreRRD(cpu_cores, cpu_num):
if not os.path.exists(CPUCoresRRDFile):
core_DS=[]
for i in range(cpu_num):
core_DS.append("DS:core_" + str(i) + ":GAUGE:600:0:100")
core_DS.append("DS:core_avg:GAUGE:600:0:100")
ret = rrdtool.create(CPUCoresRRDFile, '--step', '300',
core_DS,
'RRA:AVERAGE:0.5:1:864',
'RRA:AVERAGE:0.5:3:1344',
'RRA:AVERAGE:0.5:12:1488',
'RRA:AVERAGE:0.5:36:2960')
if ret:
print(rrdtool.error())
return False
core_avg = sum(cpu_cores)/float(4)
#update data
core_data = "N"
for i in range(cpu_num):
core_data+= ":" + str(cpu_cores[i])
core_data+=":"+str(core_avg)
ret = rrd_update(CPUCoresRRDFile, core_data);
if ret:
print(rrdtool.error())
return False
return True
def updateUptimeRRD(uptime):
if not os.path.exists(UptimeRRDFile):
ret = rrdtool.create(UptimeRRDFile, '--step', '300',
'DS:uptime:GAUGE:600:0:U',
'RRA:LAST:0.5:1:864',
'RRA:LAST:0.5:3:1344',
'RRA:LAST:0.5:12:1488',
'RRA:LAST:0.5:36:2960')
if ret:
print(rrdtool.error())
return False
#update data
ret = rrd_update(UptimeRRDFile, 'N:%s' %(uptime));
if ret:
print(rrdtool.error())
return False
return True
def updateMemRRD(total, used, buf, cached, free):
if not os.path.exists(MemRRDFile):
ret = rrdtool.create(MemRRDFile, '--step', '300',
'DS:total:GAUGE:600:0:U',
'DS:used:GAUGE:600:0:U',
'DS:buf:GAUGE:600:0:U',
'DS:cached:GAUGE:600:0:U',
'DS:free:GAUGE:600:0:U',
'RRA:AVERAGE:0.5:1:864',
'RRA:AVERAGE:0.5:3:1344',
'RRA:AVERAGE:0.5:12:1488',
'RRA:AVERAGE:0.5:36:2960')
if ret:
print(rrdtool.error())
return False
#update data
ret = rrd_update(MemRRDFile, 'N:%s:%s:%s:%s:%s' %(total, used, buf, cached, free));
if ret:
print(rrdtool.error())
return False
return True
def updatePartitionRRD(index, total, used, free, percent):
MountRRDFile = RRDSDIR +'/' +'mount-%s.rrd' % (index)
if not os.path.exists(MountRRDFile):
ret = rrdtool.create(MountRRDFile, '--step', '300',
'DS:total:GAUGE:600:0:U',
'DS:used:GAUGE:600:0:U',
'DS:free:GAUGE:600:0:U',
'DS:percent:GAUGE:600:0:100',
'RRA:AVERAGE:0.5:1:864',
'RRA:AVERAGE:0.5:3:1344',
'RRA:AVERAGE:0.5:12:1488',
'RRA:AVERAGE:0.5:36:2960')
if ret:
print(rrdtool.error())
return False
#update data
ret = rrd_update(MountRRDFile, 'N:%s:%s:%s:%s' %(total, used, free, percent));
if ret:
print(rrdtool.error())
return False
return True
def updateNetRRD(net, send, recv):
NetRRDFile = RRDSDIR + '/' + 'interface-%s.rrd' % (net)
if not os.path.exists(NetRRDFile):
ret = rrdtool.create(NetRRDFile, '--step', '300',
'DS:send:DERIVE:600:0:U',
'DS:recv:DERIVE:600:0:U',
'RRA:AVERAGE:0.5:1:864',
'RRA:AVERAGE:0.5:3:1344',
'RRA:AVERAGE:0.5:12:1488',
'RRA:AVERAGE:0.5:36:2960')
if ret:
print(rrdtool.error())
return False
#update data
ret = rrd_update(NetRRDFile, 'N:%s:%s' %(send, recv));
if ret:
print(rrdtool.error())
return False
return True
def updateDiskRRD(name, rcount, wcount, rbytes, wbytes,
rtime, wtime):
DiskRRDFile = RRDSDIR +'/' +'hdd-%s.rrd' % (name)
if not os.path.exists(DiskRRDFile):
ret = rrdtool.create(DiskRRDFile, '--step', '300',
'DS:rcount:DERIVE:600:0:U',
'DS:wcount:DERIVE:600:0:U',
'DS:rbytes:DERIVE:600:0:U',
'DS:wbytes:DERIVE:600:0:U',
'DS:rtime:DERIVE:600:0:U',
'DS:wtime:DERIVE:600:0:U',
'RRA:AVERAGE:0.5:1:864',
'RRA:AVERAGE:0.5:3:1344',
'RRA:AVERAGE:0.5:12:1488',
'RRA:AVERAGE:0.5:36:2960')
if ret:
print(rrdtool.error())
return False
#update data
ret = rrd_update(DiskRRDFile,
'N:%s:%s:%s:%s:%s:%s' %(rcount, wcount, rbytes, wbytes, rtime, wtime));
if ret:
print(rrdtool.error())
return False
return True
def get_cpu_temp():
tempfile = '/sys/class/thermal/thermal_zone0/temp'
if os.path.exists(tempfile):
tf = open(tempfile)
cpu_temp = tf.read()
tf.close()
return float(cpu_temp)/1000
else:
return 0
#def get_gpu_temp():
# #cmd = subprocess.Popen(['sudo /opt/vc/bin/vcgencmd measure_temp'], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# #cmd.communicate('password\n')
# cmd = subprocess.Popen(['/opt/vc/bin/vcgencmd measure_temp'],
# shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
# gpu_temp = cmd.communicate()[0].replace( 'temp=', '' ).replace( '\'C', '' )
# return float(gpu_temp)
def get_cpuInfo():
time.sleep(3)
cpuUsage = psutil.cpu_percent()
cpuNum = psutil.cpu_count()
cTemp = get_cpu_temp()
pids = psutil.pids()
# gTemp = get_gpu_temp()
# if updateCPURRD(cTemp, gTemp, cpuUsage) == False:
if updateCPURRD(cTemp, cpuUsage, len(pids)) == False:
return False
if cpuNum > 1:
cpuCores = psutil.cpu_percent(percpu=True)
if updateCPUCoreRRD(cpuCores, cpuNum) == False:
return False
uptime = int(time.time() - psutil.boot_time())/60 #uptime in minutes
if updateUptimeRRD(uptime) == False:
return False
return True
def get_memInfo():
'''
svmem(total=508686336L, available=432787456L, percent=14.9, used=480034816L, free=28651520L, active=214945792, inactive=228995072, buffers=43900928L, cached=360235008)
'''
mem = psutil.virtual_memory()
if updateMemRRD(mem.total, mem.used,
mem.buffers, mem.cached, (mem.total - mem.used)) == False:
return False
return True
def get_mountInfo():
disks = psutil.disk_partitions()
index = ""
for d in disks:
mpoint = d.mountpoint
diskInfo = psutil.disk_usage(mpoint)
if mpoint == '/':
index = "root"
else:
lp = mpoint.split('/')
index = lp[ len(lp) - 1 ]
# updatePartitionRRD(index, dtotal, dused, dfree, float(diskInfo.percent))
updatePartitionRRD(index, diskInfo.total,
diskInfo.used, diskInfo.free, float(diskInfo.percent))
'''
sdiskusage(total=7764254720L, used=3972837376L, free=3429568512L, percent=51.2)
'''
def get_netInfo():
nets = psutil.net_io_counters(pernic=True)
for net in list(nets.keys()):
if net == 'lo':
continue
info = nets.get(net)
#snetio(bytes_sent=42988, bytes_recv=42988, packets_sent=595, packets_recv=595, errin=0, errout=0, dropin=0, dropout=0)
updateNetRRD(net, info.bytes_sent, info.bytes_recv)
def get_diskInfo():
disks = psutil.disk_io_counters(perdisk=True)
for dname in list(disks.keys()):
diskio = disks.get(dname)
#sdiskio(read_count=9837, write_count=43761, read_bytes=198268416, write_bytes=933855232, read_time=87870, write_time=35913990)
updateDiskRRD(dname, diskio.read_count, diskio.write_count,
diskio.read_bytes, diskio.write_bytes,
diskio.read_time, diskio.write_time)
def test():
print('test....')
#get_netInfo()
get_cpuInfo()
def main():
get_cpuInfo()
get_memInfo()
get_diskInfo()
get_mountInfo()
get_netInfo()
if __name__ == '__main__':
main()
#test()