设为首页 收藏本站
查看: 678|回复: 0

[经验分享] rsyslog收集nginx日志配置

[复制链接]

尚未签到

发表于 2015-12-23 10:39:08 | 显示全部楼层 |阅读模式
rsyslog日志收集配置
rsyslog服务器收集各服务器的日志,并汇总,再由logstash处理
请查看上一篇文章  http://bbotte.blog.iyunv.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                     #查看客户端输入的命令  
DSC0000.jpg
DSC0001.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补丁
  
# 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_image_filter_module # make # make install #以上是安装nginx的步骤,下面打补丁 # git clone https://github.com/splitice/nginx_syslog_patch # patch -p1  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_image_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_image_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"';  sendfileon;  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
DSC0002.jpg
如果感觉这篇文章比较乱,那么请了解一些关于rsyslog的配置,以便更灵活的操控日志的收集,上面需要改动的是不同web服务器的nginx日志存储到不同的文件或目录,在logstash配置文件中稍微修改即可
https://github.com/yaoweibin/nginx_syslog_patch
http://www.rsyslog.com/doc/property_replacer.html
http://www.logstashbook.com/TheLogstashBook_sample.pdf
http://blog.iyunv.com/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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-155156-1-1.html 上篇帖子: Nginx屏蔽个别User 下篇帖子: nginx日常应用之日志分割(四)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表