一种syslog-ng收集nginx日志的方式

syslog-ng实践

一、数据流图

 

二、基本流程 

1
2
3
4
5
6
7
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、所需下载文件

1
2
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、开始安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
安装其他依赖软件包
 
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、下载参考配置文件,(仅是用于参考使用)

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

2、参考内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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、相关说明

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

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

1
2
3
4
5
6
7
8
9
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文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
## nginx logs
template nginx_format_center { template("$PROGRAM $MSG $SOURCEIP\n"); 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文件内容

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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

1
systemctl start syslog-ng.service

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

1
2
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、下载参考配置文件,(仅是用于参考使用)

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

2、参考内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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、相关说明

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

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

1
2
3
4
5
6
7
8
9
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的域名。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
## nginx logs
template nginx_format { template("$PROGRAM $MSG\n"); 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文件内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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

1
systemctl start syslog-ng.service

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

1
2
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文件

1
2
3
4
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文件

1
2
3
4
5
6
7
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中。

 

posted @   MR__Wang  阅读(751)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
历史上的今天:
2018-08-07 python 删除2天前后缀为.log的文件
2018-08-07 HTTP header location 重定向 URL
点击右上角即可分享
微信分享提示