forked from JVaassen/domoticz-remote-Pi-Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiRemoteServer.py
272 lines (244 loc) · 9.84 KB
/
PiRemoteServer.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# PiReMonitor Server
#
# Author: Han Sytsma, based on JVaassen, based on Xorfor's PiMonitor
import os # read info from OS
# needs pip3 install paho-mqtt
import paho.mqtt.client as mqtt # MQTT Client
from datetime import timedelta # Calculate System uptime
import time # sleep funtion
# MQTT
MQTThost = "192.168.2.8" # Fill in the host
# Send data every
period = "60" # Send data every 60 seconds
# Domoticz indexes, creating with remark which sensor should be made in Domoticx
idxcorevoltage = "169" # Voltage sensor - Name: Core Voltage
idxsdram_c = "170" # Voltage sensor
idxsdram_i = "171" # Voltage sensor
idxsdram_p = "172" # Voltage sensor
idxcpucount = "173" # Custom sensor - axis Cores - NameL Amount Cores
idxcpucurrentspeed = "174" # Custom sensor - axis MHz - Name: CPU Current Speed
idxcpumemory = "175" # Custom sensor - axis MB - Name: CPU Memory
idxcputemperature = "176" # Temperature sensor - Name CPU Temperature
idxcpuuse = "177" # Percentage sensor - Nsme: CPU percentage
idxmemory = "178 " # Custom sensor - axis: MB - Name: Total Memory
idxgpumemory = "179" # Custom sensor - axis: MB - Name GPU Memory
idxgputemperature = "180" # Temperature sensor - Name: GPU Temperature
idxraminfo = "181" # Percentage sensor - Name: % Memory Used
idxnetworkconnections = "182" # Custom Sensor - axis: Interfaces - Name Network Interfaces
idxcpuuptime = "183" # Custom Sensor - axis: Days - Name System Uptime
# MQTT Publisher Activate
client = mqtt.Client("P1") # create new isntance
# client.publish(topic, message)
class output:
global _last_idle, _last_total
_last_idle = _last_total = 0
# Return % of CPU used by user
# Based on: https://rosettacode.org/wiki/Linux_CPU_utilization#Python
def getCPUuse(self):
global _last_idle, _last_total
try:
with open('/proc/stat') as f:
fields = [float(column) for column in f.readline().strip().split()[1:]]
idle, total = fields[3], sum(fields)
idle_delta, total_delta = idle - _last_idle, total - _last_total
_last_idle, _last_total = idle, total
res = round(100.0 * (1.0 - idle_delta / total_delta), 2 )
except:
res = 0.0
return res
def getCPUcount(self):
return os.cpu_count()
def getCPUuptime(self):
try:
with open('/proc/uptime','r') as f:
uptime_seconds = float(f.readline().split()[0])
res = str(timedelta(seconds = uptime_seconds))
except:
res = 0.0
return res
# Return number of network connections
def getNetworkConnections(self,state):
res = 0
try:
for line in os.popen("netstat -tun").readlines():
if line.find(state) >= 0:
res += 1
except:
res = 0
return res
# Return GPU temperature
def getGPUtemperature(self):
try:
res = os.popen("/opt/vc/bin/vcgencmd measure_temp").readline().replace("temp=", "").replace("'C\n", "")
if (res==""):
res = "0"
except:
res = "0"
return float(res)
def getGPUmemory(self):
try:
res = os.popen("/opt/vc/bin/vcgencmd get_mem gpu").readline().replace("gpu=", "").replace("M\n", "")
if (res==""):
res = 0
except:
res = "0"
return float(res)
def getCPUmemory(self):
try:
res = os.popen("/opt/vc/bin/vcgencmd get_mem arm").readline().replace("arm=", "").replace("M\n", "")
if (res==""):
res = 0
except:
res = "0"
return float(res)
# Return CPU temperature
def getCPUtemperature(self):
try:
res = os.popen("cat /sys/class/thermal/thermal_zone0/temp").readline()
if (res==""):
res = 0
except:
res = "0"
return round(float(res)/1000,1)
# Return CPU speed in Mhz
def getCPUcurrentSpeed(self):
try:
res = os.popen("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq").readline()
except:
res = "0"
return round(int(res)/1000)
# Return RAM information in a list
# Based on: https://gist.github.com/funvill/5252169
def getRAMinfo(self):
p = os.popen("free -b")
i = 0
while 1:
i = i + 1
line = p.readline()
if i == 2:
res = line.split()[1:4]
# Index 0: total RAM
# Index 1: used RAM
# Index 2: free RAM
return round(100 * int(res[1]) / int(res[0]), 2 )
# http://www.microcasts.tv/episodes/2014/03/15/memory-usage-on-the-raspberry-pi/
# https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=164787
# https://stackoverflow.com/questions/22102999/get-total-physical-memory-in-python/28161352
# https://stackoverflow.com/questions/17718449/determine-free-ram-in-python
# https://www.reddit.com/r/raspberry_pi/comments/60h5qv/trying_to_make_a_system_info_monitor_with_an_lcd/
# Get uptime of RPi
# Based on: http://cagewebdev.com/raspberry-pi-showing-some-system-info-with-a-python-script/
def getUpStats(self):
#Returns a tuple (uptime, 5 min load average)
try:
s = os.popen("uptime").readline()
load_split = s.split("load average: ")
load_five = float(load_split[1].split(",")[1])
up = load_split[0]
up_pos = up.rfind(",", 0, len(up)-4)
up = up[:up_pos].split("up ")[1]
return up
except:
return ""
# Get voltage
# Based on: https://www.raspberrypi.org/forums/viewtopic.php?t=30697
def getVoltage(self,p):
if p in ["core", "sdram_c", "sdram_i", "sdram_p"]:
try:
res = os.popen(
"/opt/vc/bin/vcgencmd measure_volts {}".format(p)).readline().replace("volt=", "").replace("V", "")
if (res==""):
res = 0
except:
res = "0"
else:
res = "0"
return float(res)
# ps aux | grep domoticz | awk '{sum=sum+$6}; END {print sum}'
def getDomoticzMemory(self):
try:
res = os.popen(
"ps aux | grep domoticz | awk '{sum=sum+$6}; END {print sum}'").readline().replace("\n", "")
except:
res = "0"
return float(res)
def encodee(self):
# Connect to broker
client.connect(MQTThost)
#
core = str(self.getVoltage("core"))
print( "VoltageCore,"+ core )
client.publish("domoticz/in",'{ "idx": ' + idxcorevoltage + ', "nvalue": 0, "svalue": "' + core + '" }' )
#
sdram_c = str(self.getVoltage("sdram_c"))
print( "VoltageSdRam_C,"+ sdram_c )
client.publish("domoticz/in",'{ "idx": ' + idxsdram_c + ', "nvalue": 0, "svalue": "' + sdram_c + '" }' )
#
sdram_i = str(self.getVoltage("sdram_i"))
print( "VoltageSdRam_I,"+ sdram_i )
client.publish("domoticz/in",'{ "idx": ' + idxsdram_i + ', "nvalue": 0, "svalue": "' + sdram_i + '" }' )
#
sdram_p = str(self.getVoltage("sdram_i"))
print( "VoltageSdRam_P,"+ sdram_p )
client.publish("domoticz/in",'{ "idx": ' + idxsdram_p + ', "nvalue": 0, "svalue": "' + sdram_p + '" }' )
#
cpucount = str(self.getCPUcount())
print( "CPUcount,"+ cpucount )
client.publish("domoticz/in",'{ "idx": ' + idxcpucount + ', "nvalue": 0, "svalue": "' + cpucount + '" }' )
#
cpucurrentspeed = str(self.getCPUcurrentSpeed())
print( "CPUcurrentSpeed,"+ cpucurrentspeed )
client.publish("domoticz/in",'{ "idx": ' + idxcpucurrentspeed + ', "nvalue": 0, "svalue": "' + cpucurrentspeed + '" }' )
#
cpumemory = str(self.getCPUmemory())
print( "CPUmemory,"+ cpumemory )
client.publish("domoticz/in",'{ "idx": ' + idxcpumemory + ', "nvalue": 0, "svalue": "' + cpumemory + '" }' )
#
cputemperature = str(self.getCPUtemperature())
print( "CPUtemperature,"+ cputemperature )
client.publish("domoticz/in",'{ "idx": ' + idxcputemperature + ', "nvalue": 0, "svalue": "' + cputemperature + '" }' )
#
cpuuse = str(self.getCPUuse())
print( "CPUuse,"+ cpuuse )
client.publish("domoticz/in",'{ "idx": ' + idxcpuuse + ', "nvalue": 0, "svalue": "' + cpuuse + '" }' )
#
memory = str(self.getDomoticzMemory())
print ("Memory,"+ memory )
client.publish("domoticz/in",'{ "idx": ' + idxmemory + ', "nvalue": 0, "svalue": "' + memory + '" }' )
#
gpumemory = str(self.getGPUmemory())
print ("GPUmemory,"+ gpumemory )
client.publish("domoticz/in",'{ "idx": ' + idxgpumemory + ', "nvalue": 0, "svalue": "' + gpumemory + '" }' )
#
gputemperature = str(self.getGPUtemperature())
print ("GPUtemperature,"+ gputemperature )
client.publish("domoticz/in",'{ "idx": ' + idxgputemperature + ', "nvalue": 0, "svalue": "' + gputemperature + '" }' )
#
raminfo = str(self.getRAMinfo())
print ("RAMinfo,"+ raminfo )
client.publish("domoticz/in",'{ "idx": ' + idxraminfo + ', "nvalue": 0, "svalue": "' + raminfo + '" }' )
#
networkconnections = str(self.getNetworkConnections("ESTABLISHED"))
print ("NetworkConnections,"+ networkconnections )
client.publish("domoticz/in",'{ "idx": ' + idxnetworkconnections + ', "nvalue": 0, "svalue": "' + networkconnections + '" }' )
#
cpuuptime = str(self.getCPUuptime())
print ("CPUuptime,"+ cpuuptime )
client.publish("domoticz/in",'{ "idx": ' + idxcpuuptime + ', "nvalue": 0, "svalue": "' + cpuuptime + '" }' )
#
# Disconnect fromo broker
client.disconnect(MQTThost)
return
def sender():
# Running as a deamon, comment out if you want to use crontab
while True:
MyO = output()
print (MyO.encodee())
time.sleep(float(period))
# Running in crontab, uncomment
#MyO = output()
#print (MyO.encodee())
if __name__ == '__main__':
sender()