前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx日志分析

nginx日志分析

作者头像
互联网老辛
发布2018-07-30 10:41:21
9740
发布2018-07-30 10:41:21
举报
文章被收录于专栏:互联网老辛

在nginx.conf中定义的日志格式如下:

代码语言:javascript
复制
    http {
        ...
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status [$request_body] $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        ...
    }

日志文件如下:

代码语言:javascript
复制
116.2.52.247 - - [26/Oct/2017:15:04:00 +0000] "POST /api/v1/f1_static/ HTTP/1.1" 200 [{\x22user_id\x22:\x229b999d46dd6149f49\x22}] 323 "http://www.abc.com/ProductPerspective/detail/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "-"116.2.52.247 - - [26/Oct/2017:15:04:00 +0000] "OPTIONS /api/v1/fund_info/ HTTP/1.1" 200 [-] 31 "http://www.abc.com/ProductPerspective/detail/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" "-"

nginx没有命令直接将日志按天分割,我们写了一个shell脚本,每日0点定时执行。

代码语言:javascript
复制
 #!/bin/bashlogs_path="/mydata/nginx/logs/"
 mv ${logs_path}access-web.log ${logs_path}access-web-$(date -d "yesterday" +"%Y%m%d").logmv ${logs_path}access-api.log ${logs_path}access-api-$(date -d "yesterday" +"%Y%m%d").log

cron:

代码语言:javascript
复制
0 0 * * * /mydata/nginx/nginx.log.sh

从nginx服务器将日志数据传输到日志服务器

代码语言:javascript
复制
[root@VM_231_116_centos ~]
root@10.105.83.34's password:
access-power-20170929.log 100%  126KB 125.8KB/s  00:00
access-web-20171016.log   100% 2616KB  2.6MB/s  00:00
access-power-20170907.log  100% 1687KB  1.7MB/s  00:00
access-api-20170911.log    100% 1209KB  1.2MB/s  00:00
access-power-20170930.log   100% 1354KB  1.3MB/s  00:00
access.log   100%  45MB  45.2MB/s  00:00
access-api-20170907.log  100% 2960KB  2.9MB/s  00:00
access-power-20170906.log  100%  669KB 669.1KB/s  00:01
access-api-20170904.log   100% 9186KB  9.0MB/s  00:00
  • 服务器之间文件(夹)复制
代码语言:javascript
复制
scp local_file remote_username@remote_ip:remote_folder  或者  
scp local_file remote_username@remote_ip:remote_file  
 scp -r local_folder remote_username@remote_ip:remote_folder

主要有几点:

  1. 逐行解析
  2. 正则匹配
  3. 日期的处理
  4. 批量写入数据库
代码语言:javascript
复制
import reimport timeimport osimport arrowimport pandas as pdimport jsonimport io_tosqlimport shutil 
from sqlalchemy import create_engine
engine_user_info = create_engine(    "mysql+pymysql://{}:{}@{}:{}/{}".format('usr', 'pwd', 'host','port', 'db'),
    connect_args={"charset": "utf8"}) 

def parse(filename):
 
    month_abr = {"Jan":"01", "Feb":"02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06",                 "Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"}
 
    dfs = [] 
    try:
 
        i = 0
        file = open(filename)        for line in file:
            pattern = "(\d+\.\d+\.\d+\.\d+).*?\[(.*?)\].*?(\w+) (/.*?) .*?\" (\d+) \[(.*?)\] (\d+) \"(.*?)\" \"(.*?)\" \"(.*?)\""
            s = re.search(pattern, line)            if s:
                remote_addr = s.group(1)
                local_time = s.group(2)
                request_method = s.group(3)
                request_url = s.group(4)
                status = s.group(5)
                request_body = s.group(6)
                body_bytes_sent = s.group(7)
                http_referer = s.group(8)
                http_user_agent = s.group(9)
                http_x_forwarded_for = s.group(10) 
                
                for mon in month_abr.keys():                    if mon in local_time:
                        local_time = local_time.replace(mon, month_abr[mon])                        break
 
                lt = arrow.get(local_time, "DD/MM/YYYY:HH:mm:ss")
                lt = lt.shift(hours=8)
                local_time = str(lt.datetime)
                i = i+1
                
 
                if request_body != '-':                    try:
                        request_body = request_body.replace(r'\x22', '"').replace("null", '""')
                        request_body_dict = json.loads(request_body)
                        fund_id = request_body_dict.get('fund_id', None)
                        user_id = request_body_dict.get('user_id', None)                        if user_id is None:
                            user_id = request_body_dict.get('userId', None)                    except Exception as e:
                        print("request_body:{}".format(request_body))
                        print(e)
                        fund_id = None
                        user_id = None
                else:
                    fund_id = None
                    user_id = None
 
                if request_method not in ("GET", "POST"):                    
                    continue
  
                df = pd.DataFrame({"remote_addr": [remote_addr], "request_method": [request_method], "local_time": [local_time],                                                "request_url": [request_url], "status": [status], "request_body": [request_body],                                                "body_bytes_sent": [body_bytes_sent], "http_referer": [http_referer],                                                "http_user_agent": [http_user_agent], "http_x_forwarded_for": [http_x_forwarded_for],                                                "fund_id": [fund_id], "user_id": [user_id]
                                                })
                df['create_at'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
                
                dfs.append(df) 
                
                if len(dfs) >= 100:
                    df_all = pd.concat(dfs)
                    df_all = df_all.drop_duplicates(subset=['remote_addr', 'request_url','local_time'])                    
                    df_all.to_sql("log_table", engine, if_exists="append", index=False)
                    print("写入长度为:" + str(len(df_all)))
                    dfs = []
  
        df_all = pd.concat(dfs)
        df_all = df_all.drop_duplicates(subset=['remote_addr', 'request_url','local_time'])
        df_all.to_sql("log_table", engine, if_exists="append", index=False) 
    except Exception as e:
        print(e)

日志结构化写入数据库后,到前端页面可以多维度展示

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-07-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 大数据架构师专家 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档