Python + Flask 实现Nginx Log信息统计
2022/1/7 7:06:22
本文主要是介绍Python + Flask 实现Nginx Log信息统计,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
今日分享内容是基于: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()): dict2[i]=int(dict1[i]) print(dict2) httpPost(dict(hostname=hostname,nginx_data=dict2)) time.sleep(3600)
Part2:接收端
@resource.route('/GetNginxResource',methods=['POST']) def GetNginxResource(): '''接收来自linux上传的数据''' query = request.get_json() hostname = query["hostname"] nginx_data = query["nginx_data"] createtime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) sql = 'insert into nginx_log_info (hostname,nginx_data,create_time) VALUES ' data = '("' + hostname + '","' + str(nginx_data) + '","' + str(createtime) + '"' end = data + ")" sql = sql + end print(sql) db = conndb() db.execute_sql(sql) data = {'code': 200, 'message': 'success', 'status': '10000'} return json.dumps(data)
Part3:展示端
这部分主要分为以下两块内容:
第一块是页面请求
<template> <div> <div class="crumbs"> <el-breadcrumb separator="/"> <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> Nginx Log信息 </el-breadcrumb-item> </el-breadcrumb> </div> <div class="container"> <div class="handle-box"> <el-input v-model="query.hostname" placeholder="环境" class="handle-input mr10" clearable @clear="clear_name"></el-input> <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button> </div> <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header"> <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column> <el-table-column prop="hostname" width="150" label="环境"></el-table-column> <el-table-column prop="nginx_data" width="800" label="nginx log统计信息"></el-table-column> <el-table-column prop="create_time" label="创建时间"></el-table-column> </el-table> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="query.pageIndex" :page-sizes="[5, 10, 20, 30]" :page-size="query.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="parseInt(pageTotal)"> </el-pagination> </div> </div> </template> <script> import server from '../api/request.js' export default { name: 'InterfaceMem', data () { return { query: { hostname: '', pageIndex: 1, pageSize: 10 }, tableData: [], pageTotal: 0 } }, created () { this.getNginxData() }, methods: { // 获取后端返回的真实数据 getNginxData () { server({url: '/getNginxList', data: this.query, method: 'post'}) .then(response => { console.log('**********') console.log(response) this.tableData = response.listdata console.log(this.tableData) console.log(this.tableData['nginx_data']) this.pageTotal = response.pageTotal || 5 }) }, // 触发搜索按钮 handleSearch () { server({url: '/getNginxList', data: this.query, method: 'post'}) .then(response => { console.log(response) this.tableData = response.listdata console.log(this.tableData) this.pageTotal = response.pageTotal || 5 }) }, // 分页导航 handleSizeChange (val) { // console.log(val) this.$set(this.query, 'pageSize', val) // console.log(this.query) this.getNginxData() }, // 翻页改变页码触发 handleCurrentChange (val) { this.$set(this.query, 'pageIndex', val) this.getNginxData() }, clear_name () { this.query.hostname = '' this.getNginxData() } } } </script> <style scoped> .handle-box { margin-bottom: 20px; } .handle-select { width: 120px; } .handle-input { width: 300px; display: inline-block; } .table { width: 100%; font-size: 14px; } .red { color: #ff0000; } .mr10 { margin-right: 10px; } .table-td-thumb { display: block; margin: auto; width: 40px; height: 40px; } </style>
第二块是后端请求处理
@resource.route('/getNginxList',methods=['POST']) def getNginxList(): '''fe的页面列表数据获取''' query = request.get_json() print(query) if (query["hostname"] == ""): sql1 = "select id,hostname,nginx_data,create_time from nginx_log_info order by id DESC limit " + str( (query['pageIndex'] - 1) * query["pageSize"]) + "," + str(query["pageSize"]) count_sql = "select count(*) from nginx_log_info" colume_sql = "select id from nginx_log_info" else: sql1 = "select id,hostname,nginx_data,create_time from nginx_log_info where hostname like '%" + str(query["hostname"]) + "%' order by id DESC" + " limit " + str( (query['pageIndex'] - 1) * query["pageSize"]) + "," + str(query["pageSize"]) count_sql = "select count(*) from nginx_log_info where hostname like '%" + str(query["hostname"]) + "%' order by id DESC" colume_sql = "select id from nginx_log_info" sql2 = "select id,hostname,nginx_data,create_time from nginx_log_info" db = conndb() listdata = db.get_data(sql1, sql2) db = conndb() result = db.get_data(count_sql, colume_sql) print(result) pageTotal = result[0]['id'] print(listdata) print(pageTotal) data = {'listdata': listdata, "pageTotal": pageTotal, "code": 200} return json.dumps(data)
Part4:页面展示
欢迎关注【无量测试之道】公众号,回复【领取资源】
Python+Unittest框架API自动化、
Python+Unittest框架API自动化、
Python+Pytest框架API自动化、
Python+Pandas+Pyecharts大数据分析、
Python+Selenium框架Web的UI自动化、
Python+Appium框架APP的UI自动化、
Python编程学习资源干货、
资源和代码 免费送啦~
文章下方有公众号二维码,可直接微信扫一扫关注即可。
备注:我的个人公众号已正式开通,致力于IT互联网技术的分享。
包含:数据分析、大数据、机器学习、测试开发、API接口自动化、测试运维、UI自动化、性能测试、代码检测、编程技术等。
微信搜索公众号:“无量测试之道”,或扫描下方二维码:
添加关注,让我们一起共同成长!
这篇关于Python + Flask 实现Nginx Log信息统计的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-29Nginx发布学习:从入门到实践的简单教程
- 2024-10-28Nginx发布:新手入门教程
- 2024-10-21nginx 怎么设置文件上传最大20M限制-icode9专业技术文章分享
- 2024-10-17关闭 nginx的命令是什么?-icode9专业技术文章分享
- 2024-09-17Nginx实用篇:实现负载均衡、限流与动静分离
- 2024-08-21宝塔nginx新增8022端口方法步骤-icode9专业技术文章分享
- 2024-08-21nginx配置,让ws升级为wss访问的方法步骤-icode9专业技术文章分享
- 2024-08-15nginx ws代理配置方法步骤-icode9专业技术文章分享
- 2024-08-14nginx 让访问带有/relid的地址返回404 ,例子 /relid-x-0.36-y-131.html-icode9专业技术文章分享
- 2024-08-14nginx 判断地址有/statics/的路径,指向到/home/html/statics/目录-icode9专业技术文章分享