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

[经验分享] rsyslog+loganalyzer+mysql部署日志服务器

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-2-23 09:20:47 | 显示全部楼层 |阅读模式
rsyslog是一个用来管理系统日志的开源程序,是早前syslog的升级版,对原有的日志系统进行了功能的扩展。  rsyslog守护进程可以被配置成两种环境,一种是配置成日志收集服务器,rsyslog进程可以从网络中收集其它主机上的日志数据,这些主机会将日志配置为发送到另外的远程服务器。另外就是可以配置为客户端,用来过滤和发送内部日志数据到本地文件夹(如/var/log)或一台远程rsyslog服务器上。

一、rsyslog特性     
    多线程
    支持通过TCP,SSL,TLS,RELP协议实现日志数据的可靠传输
    支持输出日志到MySQL, PGSQL, Oracle等多种关系型数据中
    强大的过滤器,可实现过滤系统信息中的任意部分
    自定义输出格式
    适用于企业级别日志记录需求

二、rsyslog配置
   rsyslog的主配置文件:/etc/rsyslog.conf
   1、定义过滤和输出规则的格式为:
      facility.priority   Target
      ⑴facility:设施,产生日志消息的子系统,从功能或程序上分类
         可选值:auth,authpriv,cron,daemon,ftp,kern,lpr,mail,mark,news,security,syslog,user,uucp,local0~local7
         指定设施时可以使用通配符:
           *:所有
           f1,f2,f3,...:列表
           !:取反
      ⑵priority:日志级别
         从低到高依次为:debug(7),info(6),notice(5),warning(4),err(3),crit(2),alert(1),emerg(0)      
         通配符:
           *:所有级别
           none:没有任何级别
         示例:
           mail.info:info级别及比info级别更高级别的日志消息都会被记录
           mail.=info:仅记录info级别
           mail.!info:除了info级别的都会被记录
           *.info:所有facility的info(及以上)级别
           mail.*:mail的所有级别
           mail.notice;news.info:
           mail,news.info:mail和news的info(及以上)级别
       ⑶Target:

          文件路径:例如/var/log/messages
            系统日志是比较重要的信息,一般是同步写入磁盘,但这样也会影响性能,路径前若带有“-”则表示异步写入
          用户:*,当前系统上所有已登录的用户
          日志服务器:@SERVER_IP
          管道:| COMMAND
   2、启用日志服务器功能
       # Provides UDP syslog reception
       $ModLoad imudp
       $UDPServerRun 514

       # Provides TCP syslog reception
       $ModLoad imtcp
       $InputTCPServerRun 514
   3、日志消息的格式
       时间  主机  进程(PID): 事件

   配置日志服务器示例:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
[iyunv@node2 ~]# rpm -q rsyslog   #linux系统上默认已安装rsyslog
rsyslog-5.8.10-10.el6_6.x86_64
[iyunv@node2 ~]# rpm -ql rsyslog
/etc/logrotate.d/syslog
/etc/pki/rsyslog
/etc/rc.d/init.d/rsyslog   #服务脚本
/etc/rsyslog.conf   #主配置文件
/etc/rsyslog.d
/etc/sysconfig/rsyslog
/lib64/rsyslog
...
[iyunv@node2 ~]# vim /etc/rsyslog.conf
...
#### MODULES ####

$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imklog   # provides kernel logging support (previously done by rklogd)
#$ModLoad immark  # provides --MARK-- message capability

# Provides UDP syslog reception
$ModLoad imudp   #启用imudp和imtcp模块,并监听514端口
$UDPServerRun 514

# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514
...
...
#### RULES ####

# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.*                                                 /dev/console

# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none                /var/log/messages

# The authpriv file has restricted access.
authpriv.*                                              /var/log/secure

# Log all the mail messages in one place.
mail.*                                                  -/var/log/maillog  #前面的“-”表示异步写入磁盘


# Log cron stuff
cron.*                                                  /var/log/cron

# Everybody gets emergency messages
*.emerg                                                 *

# Save news errors of level crit and higher in a special file.
uucp,news.crit                                          /var/log/spooler

# Save boot messages also to boot.log
local7.*                                                /var/log/boot.lo
...
[iyunv@node2 ~]# service rsyslog restart   #重启rsyslog服务
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]
[iyunv@node2 ~]# netstat -tuanp | grep 'rsyslogd'
tcp        0      0 0.0.0.0:514                 0.0.0.0:*                   LISTEN      5063/rsyslogd      
tcp        0      0 :::514                      :::*                        LISTEN      5063/rsyslogd      
udp        0      0 0.0.0.0:514                 0.0.0.0:*                               5063/rsyslogd      
udp        0      0 :::514                      :::*                                    5063/rsyslogd



1
2
3
4
5
6
7
8
9
[iyunv@node3 ~]# vim /etc/rsyslog.conf   #在另一台客户机上将target指向日志服务器地址
...
*.info;mail.none;authpriv.none;cron.none                @192.168.30.20
...
[iyunv@node3 ~]# service rsyslog restart
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]
[iyunv@node3 ~]# yum -y install tree   #测试:执行一个安装操作
...



1
2
3
4
5
[iyunv@node2 ~]# tail /var/log/messages  #在日志服务器上已能看到对应的日志信息
...
Feb 20 23:56:06 node3 kernel: imklog 5.8.10, log source = /proc/kmsg started.
Feb 20 23:56:06 node3 rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="5442" x-info="http://www.rsyslog.com"] start
Feb 20 23:56:19 node3 yum[5447]: Installed: tree-1.5.3-3.el6.x86_64



   4、rsyslog支持将日志存储于MySQL服务器中:

     ①安装rsyslog-mysql包;
     ②创建rsyslog依赖的数据库:
        # mysql < /usr/share/doc/rsyslog-5.8.10/createDB.sql
     ③启用相关模块
        在#### Modules #####段启用模块:
         $ModLoad ommysql
        在####rules####段中定义记录日志信息于数据库中
         facility.priority :ommysql:SERVER_IP,DATABASE,USERNAME,PASSWORD
     ④重启rsyslog服务
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
[iyunv@node2 ~]# yum -y install rsyslog-mysql
...
[iyunv@node2 ~]# rpm -ql rsyslog-mysql
/lib64/rsyslog/ommysql.so
/usr/share/doc/rsyslog-mysql-5.8.10
/usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql
[iyunv@node2 ~]# mysql < /usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql
[iyunv@node2 ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 5.5.36-MariaDB-log MariaDB Server

Copyright (c) 2000, 2014, Oracle, Monty Program Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| Syslog             |
| mysql              |
| performance_schema |
| test               |
| vsftpd             |
+--------------------+
6 rows in set (0.26 sec)

MariaDB [(none)]> use Syslog
Database changed
MariaDB [Syslog]> show tables;
+------------------------+
| Tables_in_Syslog       |
+------------------------+
| SystemEvents           |
| SystemEventsProperties |
+------------------------+
2 rows in set (0.00 sec)

MariaDB [Syslog]> grant all on Syslog.* to loguser@127.0.0.1 identified by 'logpass';
Query OK, 0 rows affected (0.24 sec)

MariaDB [Syslog]> grant all on Syslog.* to loguser@localhost identified by 'logpass';
Query OK, 0 rows affected (0.00 sec)

MariaDB [Syslog]> flush privileges;
Query OK, 0 rows affected (0.08 sec)

MariaDB [Syslog]> exit

[iyunv@node2 ~]# vim /etc/rsyslog.conf
...
#### MODULES ####
...
$ModLoad ommysql
...
#### RULES ####
...
*.info;mail.none;authpriv.none;cron.none :ommysql:127.0.0.1,Syslog,loguser,logpass
...
[iyunv@node2 ~]# service rsyslog restart
Shutting down system logger:                               [  OK  ]
Starting system logger:                                    [  OK  ]



1
2
[iyunv@node3 ~]# yum -y remove tree   #测试:执行一个卸载操作
...



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@node2 ~]# mysql
...
MariaDB [(none)]> use Syslog
Database changed

MariaDB [Syslog]> select * from SystemEvents\G   #日志信息已记录至mysql中
...
*************************** 3. row ***************************
                ID: 3
        CustomerID: NULL
        ReceivedAt: 2016-02-19 01:29:14
DeviceReportedTime: 2016-02-21 00:59:50
          Facility: 1
          Priority: 6
          FromHost: node3
           Message:  Erased: tree
        NTSeverity: NULL
...



   5、loganalyzer:一款通过webGUI展示日志信息的工具
       # yum -y install httpd php php-mysql php-gd
       # tar xf loganalyzer-3.6.5.tar.gz
       # mkdir /var/www/html/loganalyzer
       # cp -r loganalyzer-3.6.5/src/* /var/www/html/loganalyzer/
       # cp -r loganalyzer-3.6.5/contrib/* /var/www/html/loganalyzer/
       # cd /var/www/html/loganalyzer/
       # chmod +x configure.sh secure.sh
       # ./configure.sh
       # ./secure.sh
       # chmod 666 config.php
       # chown -R apache.apache ./*  #编译安装的httpd其服务进程是以daemon用户的身份运行的
       访问:http://SERVER_IP/loganalyzer/
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
[iyunv@node2 ~]# tar xf loganalyzer-3.6.5.tar.gz
[iyunv@node2 ~]# ls loganalyzer-3.6.5
ChangeLog  contrib  COPYING  doc  INSTALL  src
[iyunv@node2 ~]# less loganalyzer-3.6.5/INSTALL
...
Installation in Detail
----------------------

  1. Upload all files from the loganalyzer/src/ folder to you webserver.
     The other files are not needed on the webserver.

  2. If your webserver has write access to the LogAnalyzer folder,
     you can skip the following step:

         Upload the scripts configure.sh and secure.sh from the
         contrib folder to your webserver, into the same folder
         where you uploaded the other LogAnalyzer files into. Then set
         the execution flag to them (chmod +x configure.sh secure.sh).

         Now run ./configure.sh, this will create a blank config.php,
         and will also set write access to everyone to it.

         You can of course do this manually if you want.
...
[iyunv@node2 ~]# mkdir /web/htdocs/loganalyzer   #本例中web服务器的站点根目录为/web/htdocs
[iyunv@node2 ~]# cp -r loganalyzer-3.6.5/src/* /web/htdocs/loganalyzer/
[iyunv@node2 ~]# cp -r loganalyzer-3.6.5/contrib/* /web/htdocs/loganalyzer/
[iyunv@node2 ~]# cd /web/htdocs/loganalyzer/
[iyunv@node2 htdocs]# ls
admin               chartgenerator.php  convert.php  details.php  favicon.ico  index.php    lang                 reports.php  statistics.php  userchange.php
asktheoracle.php    classes             cron         doc          images       install.php  login.php            search.php   templates
BitstreamVeraFonts  configure.sh        css          export.php   include      js           reportgenerator.php  secure.sh    themes
[iyunv@node2 htdocs]# chmod +x configure.sh secure.sh
[iyunv@node2 htdocs]# ./configure.sh
[iyunv@node2 htdocs]# ./secure.sh
[iyunv@node2 htdocs]## ls   #执行以上两个脚本后会生成文件config.php
admin               chartgenerator.php  configure.sh  css          export.php   include      js         reportgenerator.php  secure.sh       themes
asktheoracle.php    classes             convert.php   details.php  favicon.ico  index.php    lang       reports.php          statistics.php  userchange.php
BitstreamVeraFonts  config.php          cron          doc          images       install.php  login.php  search.php           templates
[iyunv@node2 htdocs]# chmod 666 config.php
[iyunv@node2 htdocs]# chown -R daemon.daemon ./*   #本例中的httpd是编译安装的,其服务进程以daemon用户身份运行



wKioL1bJ0-rxovB2AADKYLLwsEU312.jpg
wKiom1bJ04HSRW5rAAJD9UwtzDc156.jpg
wKiom1bLKkLyHXM5AAUXdsMP5sI131.jpg


运维网声明 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-181628-1-1.html 上篇帖子: rsyslog+loganalyzer+mysql部署日志服务器 下篇帖子: rhel 5.11 搭建 rsyslog + loganalyzer 日志服务器 服务器 mysql
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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