今日分享内容是基于:Python + Flask 实现接口接收 Disk 信息 来进一步分享如何接收 Nginx Log 的信息。
原理:
通过 Python 调用 Shell 脚本去执行 Nginx Log 的相关命令,然后进行处理再请求 Requests 库来向后端定义好的接口来推送数据并展示。
Part1:收集端
import os
import requests
import json
import time
url="http://10.8.31.61:5555/GetNginxResource"
nginx_data={}
def exec_cmd(logname):
nginx_cmd = "awk -F' ' '{print $21}' " + logname + " |sort |uniq -c"
response = os.popen(nginx_cmd)
nginx_data['data']=str(response.read()).replace("\n","")
#print(nginx_data)
response.close()
return nginx_data
def httpPost(datas):
header = {"Content-Type":"application/json"}
resp_content = requests.post(url=url,data=json.dumps(datas),headers=header)
#resp_content = json.loads(resp_content.content)
print(resp_content.text)
if __name__ == '__main__':
hostname=str(os.popen("hostname |awk -F'.' '{print $1}' |awk -F'-' '{print $2}'").read()).replace("\n","")
while(1):
data=exec_cmd("/home/q/nginx/logs/access_ars2.beta.shangri-la.com.log")
data1=data['data'].split(",")
dict1={}
for i in data1:
data2=i.strip().split(" ")
if(len(data2)>=2):
key,value=data2[1],data2[0]
dict1[key]=value
else:
pass
listkey=['200','204','206','301','302','304','400','401','403','404','415','499','500','503']
dict2={}
for i in listkey:
if(i in dict1.keys()):