nginx错误日志入库小程序
2018-02-27 14:20
190 查看
nginx错误日志入库小程序
为了独立记录nginx_error日志来自定义一些ip策略阻挡攻击,这里把所有nginx服务器的错误日志入库来进行统一处理,代码如下,然后结合supervisor来使用:#!/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = 'xiaojinsong' # __time__ = '2018/2/27' from __future__ import print_function import pyinotify import re import os import sys import MySQLdb import socket class ProcessTransientFile(pyinotify.ProcessEvent): def process_IN_MODIFY(self, event): line = file.readline() # print(line) if line: intodb(line) def nginxLogAnalyzer(line): # print(line,end='') m = re.match(r'(\d{4}\/\d{2}\/\d{2}\ \d{2}\:\d{2}\:\d{2}) ([^,]*),\s(.*)', line) dic1 = {} v = m.group(3).split(', ') dict1 = dict([(a.split(': ')[0].strip(), a.split(': ')[1].strip()) for a in v]) if dict1['request']: x = dict1['request'] dict1.pop('request') dict1['method'] = x.split(' ')[0].strip('"') dict1['request'] = x.split(' ')[1].strip('"') dict1['http_version'] = x.split(' ')[2].strip('"') dict1['time'] = m.group(1) dict1['errortype'] = m.group(2) hn = socket.gethostname() dict1['nginxIP'] = socket.gethostbyname(hn) return dict1 def intodb(line): dict1 = nginxLogAnalyzer(line) field = [] cur.execute("describe nginx_error") field = cur.fetchall() mylist = [] for fie in field: mylist.append(fie[0]) for key in dict1.keys(): if key not in mylist: try: sql = "alter table nginx_error add column %s varchar(30) DEFAULT NULL" % key cur.execute(sql) conn.commit() except Exception, e: conn.rollback() print(e) placeholders = ', '.join(['%s'] * len(dict1)) columns = ', '.join(dict1.keys()) sql = "INSERT INTO nginx_error (%s) VALUES (%s)" % (columns, placeholders) try: cur.execute(sql, dict1.values()) conn.commit() except Exception, e: conn.rollback() print(e) if __name__ == '__main__': conn = MySQLdb.connect(host='xxx.xxx.xxxx', port=3306, user='nginx', passwd='nginx', db='nginx', charset="utf8") # cur = conn.cursor(MySQLdb.cursors.DictCursor) cur = conn.cursor() cur.execute("SET NAMES utf8") filename = sys.argv[1] file = open(filename, 'r') st_results = os.stat(filename) st_size = st_results[6] file.seek(st_size) wm = pyinotify.WatchManager() notifier = pyinotify.Notifier(wm) wm.watch_transient_file(filename, pyinotify.IN_MODIFY, ProcessTransientFile) notifier.loop()
相关文章推荐
- nginx访问日志,错误日志参数说明
- 使用Logstash分析纪录Nginx服务器访问及错误日志
- [经验之谈]程序之中如何通过日志跟踪错误信息
- 安装sql server 2000时又出现:安装程序配置服务器失败。参考服务器错误日志和 C:\WINNT\sqlstp.log 了解更多信息。
- nginx开启PHP错误日志
- FFMPEG推流nginx失败,FFMPEG日志显示rtmp server requested close错误
- SQL安装时出错,提示:安装程序配置服务器失败 参考服务器错误日志。针对其中一种情况的解决方法!
- Nginx错误日志
- nginx php-fpm 输出php错误日志(转)
- How to handle crash problem? (1.结合Dr.Watson系统日志和Vc6来定位多线程环境下程序异常退出的错误)
- Nginx反向代理Nodejs – log4js日志IP显示错误
- java程序系统错误处理方法之 日志
- docker中运行lnmp环境界面502,nginx错误日志提示/run/php/php7.0-fpm.sock failed
- vc release错误追踪位置,程序不弹错直接退出,后生成日志文件。
- nginx、tomcat、php日志轮转程序
- 实战:结合Dr.Watson系统日志和Vc6来定位多线程环境下程序异常退出的错误
- nginx php-fpm 输出php错误日志
- 安装程序配置服务器失败。参考服务器错误日志和C:/WINDOWS/sqlstp.log
- 实战:结合Dr.Watson系统日志和Vc6来定位多线程环境下程序异常退出的错误
- nginx日志错误日志说明