rsyslog收集nginx日誌配置

rsyslog日誌收集配置

rsyslog服務器收集各服務器的日誌,並彙總,再由logstash處理

請查看上一篇文章 http://bbotte.blog.51cto.com/6205307/1613571 

客戶端/發送端  web服務器

# yum install rsyslog -y
# vim /etc/rsyslog.conf
*.* @192.168.10.1:514
# vim /etc/bashrc                              #收集其他服務器的操作命令
export PROMPT_COMMAND='{ msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd`]"$msg"; }'
# . /etc/bashrc
# crontab -e
*/1 * * * * /bin/echo `date`
# service rsyslog restart


服務器/收集端  rsyslog收集,logstash服務器

# yum install rsyslog -y
# vim /etc/rsyslog.conf
$ModLoad imudp                                 #啓用udp,514端口收集日誌
$UDPServerRun 514
$template logformat,"%FROMHOST-IP% %structured-data% %timegenerated% %msg%\n"  #定義日誌模板
$template DynFile,"/var/log/%$year%%$month%%$day%.log"                         #定義日誌路徑
:rawmsg, contains, "CROND" ?DynFile;logformat   #含有"CROND"日誌,輸出爲/var/log/%$year%%$month%%$day%.log
:rawmsg, contains, "CROND" ~ 
# service rsyslog restart
# tail -f /var/log/20150212.log                 #查看crontab的log
# tail -f /var/log/messages                     #查看客戶端輸入的命令

 wKiom1Te4-XQsGLOAALgGQ3vQjw715.jpg

  

wKiom1Te4_biIFQGAAPf5ZZeBmg351.jpg

#rsyslog測試log傳送完畢,下面是用rsyslog收集其他服務器日誌:

# vim /etc/sysconfig/rsyslog
SYSLOGD_OPTIONS="-c 5 -Q -x"
# vim /etc/rsyslog.conf                         #把下面幾行註釋
#$template logformat,"%FROMHOST-IP% %structured-data% %timegenerated% %msg%\n"
#$template DynFile,"/var/log/%$year%%$month%%$day%.log"
#:rawmsg, contains, "CROND" ?DynFile;logformat 
# service rsyslog restart
#現在服務端收集客戶端日誌,並保存在/var/log各日誌文件中,tail -f 查看

 

rsyslog其他配置選項

日誌級別:

———————————————————————-

debug       –有調式信息的,日誌信息最多

info        –一般信息的日誌,最常用

notice      –最具有重要性的普通條件的信息

warning     –警告級別

err         –錯誤級別,阻止某個功能或者模塊不能正常工作的信息

crit        –嚴重級別,阻止整個系統或者整個軟件不能正常工作的信息

alert       –需要立刻修改的信息

emerg       –內核崩潰等嚴重信息

none        –什麼都不記錄

從上到下,級別從低到高,記錄的信息越來越少


#過濾日誌, 由:號開頭

:msg, contains, “error” /var/log/error.log

:msg, contains, “error” ~         # 忽略包含error的日誌

#如果要把不同服務器發送過來的日誌保存到不同的文件, 可以這樣操作:

:fromhost-ip, isequal, “192.168.10.2″ /var/log/host1002.log

:FROMHOST-IP, isequal, “192.168.10.3″ /var/log/host1003.log

 

#現在是要把web服務器的nginx日誌收集到logstash服務器上,nginx原生不支持syslog,所以要打補丁

#爲nginx打syslog補丁

#下面是nginx1.4.7版本,其他版本請查看此鏈接 https://github.com/splitice/nginx_syslog_patch 

# tar -xzf nginx-1.4.7.tar.gz
# cd nginx-1.4.7
# ./configure --user=www --group=www --prefix=/usr/local/nginx \
--with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module \
--with-pcre=/root/pcre-8.35 --with-http_realip_module --with-http_p_w_picpath_filter_module #這個是測試,我是把pcre-8.35解壓到root目錄
# make
# make install
#以上是安裝nginx的步驟,下面打補丁
# git clone https://github.com/splitice/nginx_syslog_patch
# patch -p1 < /root/nginx-1.4.7/nginx_syslog_patch/syslog-1.4.0.patch
patching file src/core/ngx_cycle.c
patching file src/core/ngx_log.c
patching file src/core/ngx_log.h
patching file src/http/modules/ngx_http_log_module.c
patching file src/http/ngx_http_core_module.c
Hunk #2 succeeded at 4895 (offset 2 lines).
Hunk #3 succeeded at 4913 (offset 2 lines).
Hunk #4 succeeded at 4952 (offset 2 lines).
patching file src/http/ngx_http_request.c
Hunk #1 succeeded at 517 (offset -14 lines).
Hunk #2 succeeded at 798 (offset -23 lines).
Hunk #3 succeeded at 2002 (offset -23 lines).
# ./configure --user=www --group=www --prefix=/usr/local/nginx \
--with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module \
--with-pcre=/root/pcre-8.35 --with-http_realip_module --with-http_p_w_picpath_filter_module \
--add-module=/root/nginx-1.4.7/nginx_syslog_patch/
# make
# make install
# /usr/local/nginx/sbin/nginx -V                             #查看編譯的配置參數
nginx version: nginx/1.4.7
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) 
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/usr/local/nginx \
--with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module \
--with-pcre=/root/pcre-8.35 --with-http_realip_module --with-http_p_w_picpath_filter_module \
--add-module=/root/nginx-1.4.7/nginx_syslog_patch/
# grep -v ^.*# /usr/local/nginx/conf/nginx.conf|sed '/^$/d'  #nginx配置
worker_processes  1;
syslog local6 nginx;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
index index.html;
root /var/www;
access_log  syslog:notice|logs/host1.access.log main;
error_log   syslog:notice|logs/host1.error.log;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

 

#現在的話,nginx日誌有3份,一份位於/usr/local/nginx/logs,一份在/var/log/messages裏面,刷新nginx首頁,查看日誌

#當然,在syslog收集端也有一份nginx的訪問日誌

# tail -f /var/log/messages

#刷新下面頁面,

http://192.168.10.1/index.html#/dashboard/file/logstash.json

wKiom1Te5V2hBpChAAPHTLmBq5E874.jpg

如果感覺這篇文章比較亂,那麼請了解一些關於rsyslog的配置,以便更靈活的操控日誌的收集,上面需要改動的是不同web服務器的nginx日誌存儲到不同的文件或目錄,在logstash配置文件中稍微修改即可。

如果不希望爲nginx打補丁的方式,可以參考下一篇文章 http://bbotte.blog.51cto.com/6205307/1615477 


參考

https://github.com/yaoweibin/nginx_syslog_patch 

http://www.rsyslog.com/doc/property_replacer.html 

http://www.logstashbook.com/TheLogstashBook_sample.pdf  

http://blog.chinaunix.net/uid-21807675-id-1814878.html 

http://my.oschina.net/duxuefeng/blog/317570 

http://www.cnblogs.com/blueswu/p/3564763.html 

http://blog.clanzx.net/2013/12/31/rsyslog.html 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章