zoukankan      html  css  js  c++  java
  • 一种syslog-ng收集nginx日志的方式

    syslog-ng实践

    一、数据流图

    二、基本流程 

    1、syslog-ng-agent 在节点端生成fifo文件(Xapp.log)。
    
    2、app生成log内容写入到指定的文件(Xapp.log)中。
    
    3、syslog-ng-agent实时的读取日志文件(Xapp.log),并根据定义的规则将文件中的内容进行解析和过滤。
    
    4、syslog-ng-agent会将解析和过滤的文件按照配置存储规则发送到本地和远端center指定位置生成文件存储。
    

     三、安装syslog-ng 

         1、所需下载文件

    syslog-ng-3.8.1.tar.gz
    eventlog-0.2.13.tar.gz

      下载地址链接:https://pan.baidu.com/s/1xVtvC0Qu_TyTEevXxqK4_Q 提取码:z5td

         将下载的文件存放在 /usr/local/src/

     2、开始安装

    安装其他依赖软件包
    
    yum install libesmtp libesmtp-devel
    yum install libgnomeui-devel
    yum install pcre pcre-devel
    yum install json-c json-c-devel
    
    安装eventlog
    
    cd /usr/local/src/
    tar -zxvf eventlog-0.2.13.tar.gz
    cd eventlog-0.2.13
    ./configure --prefix=/usr/local/eventlog-0.2.13
    make && make install
    ln -s /usr/local/eventlog-0.2.13 /usr/local/eventlog
    export PKG_CONFIG_PATH=/usr/local/eventlog/lib/pkgconfig/
    
    安装 syslog-ng
    tar -zxvf syslog-ng-3.8.1.tar.gz
    cd syslog-ng-3.8.1
    ./configure --prefix=/usr/local/syslog-ng-3.8.1 --enable-ssl --enable-smtp --enable-pcre --enable-json
    make && make install
    ln -s /usr/local/syslog-ng-3.8.1 /usr/local/syslog-ng
    

    四、配置syslog-ng-center

      1、下载参考配置文件,(仅是用于参考使用)

    链接:https://pan.baidu.com/s/1l-_jvCgI4d2jTkWfkUYyww 
    提取码:2dmg

    2、参考内容

    tree 
    .
    ├── conf
    │   ├── history-center.conf
    │   ├── history-cmd.conf
    │   ├── internal.conf
    │   ├── local.conf
    │   ├── nginx-center.conf
    │   ├── nginx.conf
    │   ├── project-center.conf
    │   └── project.conf
    ├── global.conf
    ├── include
    ├── init.sh
    ├── patterndb.d
    │   └── nginx.xml
    ├── scl.conf
    ├── syslog-ng
    ├── syslog-ng.conf
    ├── syslog-ng.logrotate
    ├── syslog-ng.logrotate.V6
    ├── syslog-ng.logrotate.V7
    ├── syslog-ng.rsyslog
    └── syslog-ng.service
    
    3 directories, 19 files
    

    3、相关说明

     conf 目录下的文件如
        history-center.conf  指定开启命令执行的收集
        history 指定center和本地的位置以及过滤和解析
        patterndb.d 目录下文件
        nginx.xml 是用于nginx日志的解析使用
        syslog-ng.con  配置加载conf 目录中的配置文件
    
    安装启停脚本
      # ./init.sh install 是安装syslog-ng启停服务和指定conf的配置文件
    

     4、本参考文件 开启nginx 的center解析。

    egrep -v '^#|^$' syslog-ng.conf
    @version: 3.8
    include "global.conf";
    include "conf/nginx-center.conf";
    include "include/*.conf";
     
    ./init.sh install
    syslog-ng install
    Created symlink from /etc/systemd/system/multi-user.target.wants/syslog-ng.service to /etc/systemd/system/syslog-ng.service

         nginx-center.conf文件内容

    ## nginx logs
    template nginx_format_center { template("$PROGRAM $MSG $SOURCEIP
    "); template_escape(no); };
    parser nginx_center { db_parser(file("/home/server_config/syslog-ng/patterndb.d/nginx.xml")); };
    include "include/*.ngxc";
    
    source s_nginx_access_center {
    	tcp(ip(0.0.0.0) port(51451) max-connections(10240) log_iw_size(1024000) keep_hostname(no));
    };
    destination d_nginx_access_center {file("/home/logs/logcenter/nginx/${N.vhost}/$YEAR/$MONTH/$DAY/access_$HOUR.log" perm(0644) dir_perm(0755) create_dirs(yes) template(nginx_format_center));};
    log {source(s_nginx_access_center);parser(nginx_center);destination(d_nginx_access_center);flags(final);};
    
    source s_nginx_error_center {
    	tcp(ip(0.0.0.0) port(51452) max-connections(10240) log_iw_size(1024000) keep_hostname(no));};
    destination p_nginx_error_center {file("/home/logs/logcenter/nginx/${N.vhost}/$YEAR/$MONTH/$DAY/error_$HOUR.log" perm(0644) dir_perm(0755) create_dirs(yes) template(nginx_format_center));};
    destination d_nginx_error_center {file("/home/logs/logcenter/nginx/$YEAR/$MONTH/$DAY/error_$HOUR.log" perm(0644) dir_perm(0755) create_dirs(yes) template(nginx_format_center));};
    log {source(s_nginx_error_center);parser(nginx_center);destination(p_nginx_error_center);destination(d_nginx_error_center);flags(final);};
    

      nginx.xml文件内容

      

    <?xml version='1.0' encoding='UTF-8'?>
    <patterndb version="4" pub_date="2014-03-13">
    
    	<ruleset name="Nginx log">
    		<rules>
    			<rule id="nginx_access" provider="lucky">
    				<description>Nginx / Http Access Log /</description>
    				<patterns>
    					<pattern>@ESTRING:N.identd: @@ESTRING:N.username: @@QSTRING:N.timestamp:[]@ @QSTRING:N.query:"@ @NUMBER:N.statusCode@ @NUMBER:N.bytesOut@ @QSTRING:N.referer:"@ @QSTRING:N.requestClientApplication:"@ @QSTRING:N.forwardedfor:"@ "@HOSTNAME:N.vhost@" @QSTRING:N.vport:"@@ANYSTRING@</pattern>
    				</patterns>
    				<values>
    				</values>
    				<examples>
    					<example>
    						<test_message>- - [13/Mar/2014:15:32:05 +0800] "GET /favicon.ico HTTP/1.1" 200 99999 "http://www.hw801.com/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)" "-" "www.hw801.com" "80" 0.000</test_message>
    						<test_values>
    							<test_value name="N.identd">-</test_value>
    							<test_value name="N.username">-</test_value>
    							<test_value name="N.timestamp">13/Mar/2014:15:32:05 +0800</test_value>
    							<test_value name="N.query">GET /favicon.ico HTTP/1.1</test_value>
    							<test_value name="N.statusCode">200</test_value>
    							<test_value name="N.bytesOut">99999</test_value>
    							<test_value name="N.referer">http://www.hw801.com/</test_value>
    							<test_value name="N.requestClientApplication">Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)</test_value>
    							<test_value name="N.forwardedfor">-</test_value>
    							<test_value name="N.vhost">www.hw801.com</test_value>
    							<test_value name="N.vport">80</test_value>
    						</test_values>
    					</example>
    				</examples>
    			</rule>
    			<rule id="nginx_error" provider="hw801">
    				<description>Nginx / Http error Log /</description>
    				<patterns>
    					<pattern>@ESTRING::,@ @ESTRING::,@ server: @ESTRING:N.vhost:,@@ANYSTRING@</pattern>
    				</patterns>
    				<values>
    				</values>
    			</rule>
    		</rules> 
    	</ruleset>
    </patterndb>

        5、启动syslog-ng-center

    systemctl start syslog-ng.service
    

     6、查看syslog-ng nginx的center接收端口是否开启

    netstat -natp|grep 51451
    tcp        0      0 0.0.0.0:51451           0.0.0.0:*               LISTEN      31111/syslog-ng 

    五、配置syslog-ng-anent

         过程同配置syslog-ng-center中1、2、3 相同

    1、下载参考配置文件,(仅是用于参考使用)

    链接:https://pan.baidu.com/s/1l-_jvCgI4d2jTkWfkUYyww 
    提取码:2dmg

    2、参考内容

    tree 
    .
    ├── conf
    │   ├── history-center.conf
    │   ├── history-cmd.conf
    │   ├── internal.conf
    │   ├── local.conf
    │   ├── nginx-center.conf
    │   ├── nginx.conf
    │   ├── project-center.conf
    │   └── project.conf
    ├── global.conf
    ├── include
    ├── init.sh
    ├── patterndb.d
    │   └── nginx.xml
    ├── scl.conf
    ├── syslog-ng
    ├── syslog-ng.conf
    ├── syslog-ng.logrotate
    ├── syslog-ng.logrotate.V6
    ├── syslog-ng.logrotate.V7
    ├── syslog-ng.rsyslog
    └── syslog-ng.service
    
    3 directories, 19 files
    

    3、相关说明

     conf 目录下的文件如
        history-center.conf  指定开启命令执行的收集
        history 指定center和本地的位置以及过滤和解析
        patterndb.d 目录下文件
        nginx.xml 是用于nginx日志的解析使用
        syslog-ng.con  配置加载conf 目录中的配置文件
    
    安装启停脚本
      # ./init.sh install 是安装syslog-ng启停服务和指定conf的配置文件
    

     4、本参考文件 开启nginx 的解析。

    egrep -v '^#|^$' syslog-ng.conf
    @version: 3.8
    include "global.conf";
    include "conf/nginx.conf";
    include "include/*.conf";
     
    ./init.sh install
    syslog-ng install
    Created symlink from /etc/systemd/system/multi-user.target.wants/syslog-ng.service to /etc/systemd/system/syslog-ng.service

         nginx.conf文件内容,192.168.1.200 为center的地址,51451端口是center的端口。hw801是match的域名。

    ## nginx logs
    template nginx_format { template("$PROGRAM $MSG
    "); template_escape(no); };
    parser nginx { db_parser(file("/home/server_config/syslog-ng/patterndb.d/nginx.xml")); };
    include "include/*.ngx";
    source s_nginx_access {
    	#file("/home/logs/nginx/access_log" follow_freq(1));
    	pipe("/home/logs/nginx/access_log");
    };
    destination d_nginx_access_local {file("/home/logs/nginx/${N.vhost}/$YEAR/$MONTH/$DAY/access_$HOUR.log" perm(0644) dir_perm(0755) create_dirs(yes) template(nginx_format));};
    destination d_nginx_access_remote {tcp('192.168.1.200' port(51451));};
    filter f_nginx_access {match(".hw801.(com|net|cn|org|com.cn|net.cn)$" value("N.vhost"));};
    log {source(s_nginx_access);parser(nginx);filter(f_nginx_access);destination(d_nginx_access_local);dest
    ination(d_nginx_access_remote);flags(final);};
    destination d_nginx_oth_access {file("/home/logs/nginx/$YEAR/$MONTH/$DAY/access_$HOUR.log" perm(0644) dir_perm(0755) create_dirs(yes) template(nginx_format));};
    log {source(s_nginx_access);destination(d_nginx_oth_access);flags(final);};
    source s_nginx_error {
    	#file("/home/logs/nginx/error_log" follow_freq(1));
    	pipe("/home/logs/nginx/error_log");
    };
    destination p_nginx_error_local {file("/home/logs/nginx/${N.vhost}/$YEAR/$MONTH/$DAY/error_$HOUR.log" perm(0644) dir_perm(0755) create_dirs(yes) template(nginx_format));};
    destination d_nginx_error_local {file("/home/logs/nginx/$YEAR/$MONTH/$DAY/error_$HOUR.log" perm(0644) dir_perm(0755) create_dirs(yes) template(nginx_format));};
    destination d_nginx_error_remote {tcp('192.168.1.200' port(51452));} ;
    filter f_nginx_error {match(".hw801.(com|net|cn|org|com.cn|net.cn)$" value("N.vhost"));};
    log {source(s_nginx_error);parser(nginx);filter(f_nginx_error);destination(p_nginx_error_local);destination(d_nginx_error_local);destination(d_nginx_error_remote);flags(final);};

      nginx.xml文件内容

    <?xml version='1.0' encoding='UTF-8'?>
    <patterndb version="4" pub_date="2014-03-13">
    
    	<ruleset name="Nginx log">
    		<rules>
    			<rule id="nginx_access" provider="lucky">
    				<description>Nginx / Http Access Log /</description>
    				<patterns>
    					<pattern>@ESTRING:N.identd: @@ESTRING:N.username: @@QSTRING:N.timestamp:[]@ @QSTRING:N.query:"@ @NUMBER:N.statusCode@ @NUMBER:N.bytesOut@ @QSTRING:N.referer:"@ @QSTRING:N.requestClientApplication:"@ @QSTRING:N.forwardedfor:"@ "@HOSTNAME:N.vhost@" @QSTRING:N.vport:"@@ANYSTRING@</pattern>
    				</patterns>
    				<values>
    				</values>
    				<examples>
    					<example>
    						<test_message>- - [13/Mar/2014:15:32:05 +0800] "GET /favicon.ico HTTP/1.1" 200 99999 "http://www.hw801.com/" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)" "-" "www.hw801.com" "80" 0.000</test_message>
    						<test_values>
    							<test_value name="N.identd">-</test_value>
    							<test_value name="N.username">-</test_value>
    							<test_value name="N.timestamp">13/Mar/2014:15:32:05 +0800</test_value>
    							<test_value name="N.query">GET /favicon.ico HTTP/1.1</test_value>
    							<test_value name="N.statusCode">200</test_value>
    							<test_value name="N.bytesOut">99999</test_value>
    							<test_value name="N.referer">http://www.hw801.com/</test_value>
    							<test_value name="N.requestClientApplication">Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)</test_value>
    							<test_value name="N.forwardedfor">-</test_value>
    							<test_value name="N.vhost">www.hw801.com</test_value>
    							<test_value name="N.vport">80</test_value>
    						</test_values>
    					</example>
    				</examples>
    			</rule>
    			<rule id="nginx_error" provider="hw801">
    				<description>Nginx / Http error Log /</description>
    				<patterns>
    					<pattern>@ESTRING::,@ @ESTRING::,@ server: @ESTRING:N.vhost:,@@ANYSTRING@</pattern>
    				</patterns>
    				<values>
    				</values>
    			</rule>
    		</rules> 
    	</ruleset>
    </patterndb>

        5、启动syslog-ng-agent

    systemctl start syslog-ng.service
    

     6、查看syslog-ng nginx是否开启

    ps -ef|grep syslog-ng|grep -v grep
    root     31111     1  0 16:55 ?        00:00:00 /usr/local/syslog-ng/sbin/syslog-ng -F -p /var/run/syslog-ng.pid
    

     7、查看是否生成nginx的fifo文件

    ll /home/logs/nginx/
    total 2
    prw------- 1 root root   0 Aug  7 17:12 access_log
    prw------- 1 root root   0 Jun 22 19:00 error_log
    

    六、配置nginx

    1、配置nginx的日志格式和日志路径,日志路径为上面的fifo文件

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
    		  '"$host" "$server_port" $request_time';
    
    access_log  /home/logs/nginx/access_log  main;
    error_log  /home/logs/nginx/error_log;
    

    2、启动nginx,后访问nginx即可获取到日志信息。

    值得注意的地方:

         1、访问如果上syslog-ng.conf中匹配到的域名,会依据匹配规则在本地和center生成对应目录及按照小时切割日志内容。

         2、若没有匹配到规则,会在本地和center的生成没有匹配到的目录,同样也是按照小时切割的日志内容。

         3、syslog-ng-agent可以部署到多台服务器上,指定同一个center即可。

    至此我们实现了app.log的收集到center中。

    下一次,在写下 center的日志如何收集到es中。

  • 相关阅读:
    [LeetCode] Implement Queue using Stacks 用栈来实现队列
    [LeetCode] Power of Two 判断2的次方数
    [LeetCode] 230. Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
    cvReleaseImage 释放内存出错
    FlyCapture2 fc2Image OpenCV IplImage Conversion 两种图像格式之间的转换
    FlyCapture2 Qt5 MinGW Configuration
    [LeetCode] 14. Longest Common Prefix 最长共同前缀
    [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
    [LeetCode] 229. Majority Element II 求大多数之二
    [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表
  • 原文地址:https://www.cnblogs.com/xzlive/p/15112118.html
Copyright © 2011-2022 走看看