updated 2019/7/15 前兩天onenet做了升級玄坦,API未向前兼容,重新寫了腳本豺总。同時之前的ip138獲取公網(wǎng)IP的功能因?qū)Ψ狡帘卧穸鼡Q為sohu
最近領導安排我參加重慶物聯(lián)網(wǎng)項目,為了熟悉情況表伦,業(yè)余時間做了一個簡單的“物聯(lián)網(wǎng)”設備信息管理功能赂弓。
大致的流程是:樹莓派上Python腳本用crontab每隔5分鐘采集一次CPU信息和內(nèi)存信息,并通過onenet的數(shù)據(jù)上報接口上報到onenet平臺盈魁,再在onenet平臺配置觸發(fā)器,目前配置的是CPU使用率超過50%則發(fā)送一封告警郵件赤套。
onenet平臺目前是免費的,注冊開發(fā)者賬號后還需要做的事情包括:注冊一個產(chǎn)品宣脉,注冊好產(chǎn)品后平臺會分配一個API-KEY剔氏。然后在這個產(chǎn)品下注冊一個設備,注冊好設備后平臺會分配一個設備ID谈跛。這樣就可以進行信息上報了。
樹莓派部分:首先需要寫一個Python腳本蜡励,腳本做2件事情阻桅,1是采集CPU和內(nèi)存的信息,2是將這些信息通過接口上報到onenet平臺。
腳本如下:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib
import psutil
import requests
import socket
import re
'''
采集樹莓派cpu和內(nèi)存信息上報到ONENET設備管理平臺(HTTP)
'''
# 獲取內(nèi)存信息
def get_memory_info():
memory_info = {}
mem = psutil.virtual_memory()
memory_info['mem_total'] = round(float(mem.total) / 1000000000, 3)
memory_info['mem_free'] = round(float(mem.free) / 1000000000, 3)
memory_info['mem_usage_percent'] = int(round(mem.percent))
return memory_info
# 獲取CPU信息
def get_cpu_usage_percent():
cpu_usage_percent = psutil.cpu_percent(interval=1)
return cpu_usage_percent
# 獲取內(nèi)網(wǎng)IP
def get_intranet_ip():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('8.8.8.8', 80))
ip = s.getsockname()[0]
finally:
s.close()
return ip
# 獲取公網(wǎng)IP
def get_internet_ip():
url = urllib.request.urlopen("http://txt.go.sohu.com/ip/soip")
text = url.read()
ip = re.findall(r'\d+.\d+.\d+.\d+', text.decode("UTF-8"))
return ip[0]
def send_to_iot_platform(memory_info):
# 信息上報到'ONENET'平臺
url = "http://api.heclouds.com/devices/25700221/datapoints"
params = {
"datastreams": [
{
"id": "pi_info",
"datapoints": [
{
"at": "",
"value": {
"CPUUsage": get_cpu_usage_percent(),
"IntranetIP": get_intranet_ip(),
"InternetIP": get_internet_ip(),
"MemoryFree": memory_info['mem_free'],
"MemoryTotal": memory_info['mem_total'],
"MemoryUsage": memory_info['mem_usage_percent']
}
}
]
}
]
}
headers = {
"api-key": "這里填寫分配的api-key",
"Content-type": "application/json"
}
response = requests.post(url, json=params, headers=headers)
return response.text
def main():
memory_info = get_memory_info()
print('Memory Total(G):', memory_info['mem_total'])
print('Memory Free(G):', memory_info['mem_free'])
print('Memory Usage Percent(%):', memory_info['mem_usage_percent'])
print('CPU Usage Percent(%):', get_cpu_usage_percent())
print('Intranet IP:', get_intranet_ip())
print('Internet IP:', get_internet_ip())
print("onenet resp:", send_to_iot_platform(memory_info))
if __name__ == "__main__":
main()
腳本寫好后,再在crontab中配置尤揣,每5分鐘上報一次:
*/5 * * * * python /home/pi/Documents/memcpu.py >> /var/log/mycron.log
全部搞定后就可以在onenet平臺上查詢到上報的信息了:
最后北戏,再配置一個觸發(fā)器漫蛔,CPU使用率超過10%時發(fā)送郵件告警。
好了蠕嫁,試一下是否可以正常通過觸發(fā)器來發(fā)送郵件吧毯盈。
首先寫一個死循環(huán)讓CPU飆高一點。
while True:
print("123")
果然收到郵件:
也可以通過onenet的圖標看出這段時間內(nèi)CPU飆升的情況:
目前場景比較簡單赘阀,主要上報的指標有:CPU使用率,內(nèi)存使用率幅慌,內(nèi)存大小轰豆,內(nèi)存空閑大小,內(nèi)網(wǎng)IP酸休,公網(wǎng)IP。目前其實就內(nèi)網(wǎng)IP這個有用一點菩咨,在DHCP環(huán)境下經(jīng)常無法快速知道給PI分配了哪個IP陡厘。
復雜一點么可以做個機房溫度傳感器,買個樹莓派的溫度傳感器糙置,然后同樣用python將機房溫度上報給onenet平臺,當溫度超過某個指標后自動告警标捺。
onenet也支持開發(fā)web或者wap應用揉抵,用于展現(xiàn)數(shù)據(jù)。也提供了一個快速開發(fā)模板冤今,因此也做了一個簡單的H5 APP:
鏈接 點擊這里
樣例如下:
最后來個樹莓派3在安靜地工作中的圖 XD