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

[经验分享] Linux syslog 系统日志管理

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-10-21 08:39:32 | 显示全部楼层 |阅读模式
rsyslog记录日志于mysql:
操作如下:
  实验环境:CentOS 7 操作系统
前提:
    准备好msql server或mariadb server(一定要编辑配置文件/etc/my.cnf,添加跳过反向解析等)并启动服务;

实验步骤:
  1.安装rsyslog连接至mysql server的驱动模块;
    # yum install rsyslog-mysql
  2.在mysql server准备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
[iyunv@centos7 ~]# mysql -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

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

# 授权数据库Syslog的所有表允许rsyslog用户在本地主机访问,访问密码为134296
MariaDB [(none)]>  GRANT ALL ON Syslog.* TO 'rsyslog'@'127.0.0.1' IDENTIFIED BY '134296';
Query OK, 0 rows affected (0.00 sec)

# 为了安全不被反解,再授权一个使用local的本地主机
MariaDB [(none)]> GRANT ALL ON Syslog.* TO 'rsyslog'@'local' IDENTIFIED BY '134296';
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

MariaDB [(none)]> use mysql;
MariaDB [mysql]> SELECT user,host,password FROM user;
+------------+-----------+-------------------------------------------+
| user       | host      | password                                  |
+------------+-----------+-------------------------------------------+
| root       | localhost | *41EE0F8759D5340036B009143E1727DB5787A448 |
| root       | centos7   | *41EE0F8759D5340036B009143E1727DB5787A448 |
| root       | 127.0.0.1 | *41EE0F8759D5340036B009143E1727DB5787A448 |
| root       | ::1       | *41EE0F8759D5340036B009143E1727DB5787A448 |
| ultraxuser | 127.0.0.1 | *41EE0F8759D5340036B009143E1727DB5787A448 |
| ultraxuser | localhost | *41EE0F8759D5340036B009143E1727DB5787A448 |
| rsyslog    | 127.0.0.1 | *41EE0F8759D5340036B009143E1727DB5787A448 | # 授权的用户
| rsyslog    | local     | *41EE0F8759D5340036B009143E1727DB5787A448 |
+------------+-----------+-------------------------------------------+
8 rows in set (0.00 sec)

MariaDB [mysql]> \q
Bye




  3.生成所需要的数据库和表;
    前面已经安装了rsyslog连接至mysql server的驱动模块rsyslog-mysql,查看其配置文件如下:
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
[iyunv@centos7 ~]#  rpm -ql rsyslog-mysql
/usr/lib64/rsyslog/ommysql.so
/usr/share/doc/rsyslog-7.4.7/mysql-createDB.sql  # 是生成数据库和表的一个脚本文件

[iyunv@centos7 ~]# cat /usr/share/doc/rsyslog-7.4.7/mysql-createDB.sql
CREATE DATABASE Syslog;
USE Syslog;
CREATE TABLE SystemEvents
(
        ID int unsigned not null auto_increment primary key,
        CustomerID bigint,
        ReceivedAt datetime NULL,
        DeviceReportedTime datetime NULL,
        Facility smallint NULL,
        Priority smallint NULL,
        FromHost varchar(60) NULL,
        Message text,
        NTSeverity int NULL,
        Importance int NULL,
        EventSource varchar(60),
        EventUser varchar(60) NULL,
        EventCategory int NULL,
        EventID int NULL,
        EventBinaryData text NULL,
        MaxAvailable int NULL,
        CurrUsage int NULL,
        MinUsage int NULL,
        MaxUsage int NULL,
        InfoUnitID int NULL ,
        SysLogTag varchar(60),
        EventLogType varchar(60),
        GenericFileName VarChar(60),
        SystemID int NULL
);

CREATE TABLE SystemEventsProperties
(
        ID int unsigned not null auto_increment primary key,
        SystemEventID int NULL ,
        ParamName varchar(255) NULL ,
        ParamValue text NULL
);



   直接把脚本导入数据库中
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
66
67
68
69
[iyunv@centos7 ~]# mysql -ursyslog -h127.0.0.1 -p134296 < /usr/share/doc/rsyslog-7.4.7/mysql-createDB.sql

# 登录数据库查看如下:
[iyunv@centos7 ~]# mysql -ursyslog -h127.0.0.1 -p134296
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation 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             |
| test               |
+--------------------+
3 rows in set (0.00 sec)

MariaDB [(none)]> use Syslog;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

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

MariaDB [Syslog]> desc SystemEvents;
+--------------------+------------------+------+-----+---------+----------------+
| Field              | Type             | Null | Key | Default | Extra          |
+--------------------+------------------+------+-----+---------+----------------+
| ID                 | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| CustomerID         | bigint(20)       | YES  |     | NULL    |                |
| ReceivedAt         | datetime         | YES  |     | NULL    |                |
| DeviceReportedTime | datetime         | YES  |     | NULL    |                |
| Facility           | smallint(6)      | YES  |     | NULL    |                |
| Priority           | smallint(6)      | YES  |     | NULL    |                |
| FromHost           | varchar(60)      | YES  |     | NULL    |                |
| Message            | text             | YES  |     | NULL    |                |
| NTSeverity         | int(11)          | YES  |     | NULL    |                |
| Importance         | int(11)          | YES  |     | NULL    |                |
| EventSource        | varchar(60)      | YES  |     | NULL    |                |
| EventUser          | varchar(60)      | YES  |     | NULL    |                |
| EventCategory      | int(11)          | YES  |     | NULL    |                |
| EventID            | int(11)          | YES  |     | NULL    |                |
| EventBinaryData    | text             | YES  |     | NULL    |                |
| MaxAvailable       | int(11)          | YES  |     | NULL    |                |
| CurrUsage          | int(11)          | YES  |     | NULL    |                |
| MinUsage           | int(11)          | YES  |     | NULL    |                |
| MaxUsage           | int(11)          | YES  |     | NULL    |                |
| InfoUnitID         | int(11)          | YES  |     | NULL    |                |
| SysLogTag          | varchar(60)      | YES  |     | NULL    |                |
| EventLogType       | varchar(60)      | YES  |     | NULL    |                |
| GenericFileName    | varchar(60)      | YES  |     | NULL    |                |
| SystemID           | int(11)          | YES  |     | NULL    |                |
+--------------------+------------------+------+-----+---------+----------------+
24 rows in set (0.01 sec)

MariaDB [Syslog]> Ctrl-C -- exit!
Aborted




  4.配置rsyslog使用ommysql模块
1
2
3
4
[iyunv@centos7 ~]# vim /etc/rsyslog.conf
#### MODULES ####
......
$ModLoad ommysql



wKiom1gIdi3iXMjgAAAcU594PO8091.jpg


5.配置RULES,将所期望的日志信息记录于mysql中;
      facility.priority   :ommysql:DBHOST,DB,DBUSER,DBUSERPASS
wKiom1gId8Ow_kVDAAAVeLPO5SA913.jpg

6.重启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
  # systemctl start rsyslog # 重启服务

  [iyunv@centos7 ~]# mysql -ursyslog -h127.0.0.1 -p134296
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.44-MariaDB MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> use Syslog;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [Syslog]> select * from SystemEvents\G;
*************************** 11. row ***************************
                ID: 11
        CustomerID: NULL
        ReceivedAt: 2016-10-20 16:00:01
DeviceReportedTime: 2016-10-20 16:00:01
          Facility: 9
          Priority: 6
          FromHost: centos7
           Message: (root) CMD (/usr/lib64/sa/sa1 1 1)
        NTSeverity: NULL
        Importance: NULL
       EventSource: NULL
         EventUser: NULL
     EventCategory: NULL
           EventID: NULL
   EventBinaryData: NULL
      MaxAvailable: NULL
         CurrUsage: NULL
          MinUsage: NULL
          MaxUsage: NULL
        InfoUnitID: 1
         SysLogTag: CROND[3521]:
      EventLogType: NULL
   GenericFileName: NULL
          SystemID: NULL
11 rows in set (0.00 sec)




7.loganalyzer图形化工具

        WebGUI, 运行amp环境中;
  1)获取loganalyzer-3.6.5.tar.gz
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
lftp 10.1.0.1:/pub/Sources/sources/loganalyzer> ls
-rwxr--r--    1 500      500       1046600 Aug 24  2013 loganalyzer-3.6.4.tar.gz
-rwxr--r--    1 500      500       1046957 Aug 19  2014 loganalyzer-3.6.5.tar.gz
lftp 10.1.0.1:/pub/Sources/sources/loganalyzer> mget loganalyzer-3.6.5.tar.gz
1046957 bytes transferred                              
lftp 10.1.0.1:/pub/Sources/sources/loganalyzer> bye
[iyunv@centos7 ~]# ls
anaconda-ks.cfg    bin    Templates    Desktop   Documents        
loganalyzer-3.6.5.tar.gz  Pictures     Videos
Downloads         Music    Public

[iyunv@centos7 ~]# tar xf loganalyzer-3.6.5.tar.gz # 解压缩
[iyunv@centos7 ~]# cd loganalyzer-3.6.5/
[iyunv@centos7 loganalyzer-3.6.5]#
admin  bbs  drupal-7.28  lastlog.txt  messages.txt  phpinfo.php  phpMyAdmin-4.0.5-all-languages  php-mysql.php  pma  src  text.html

# 只复制src目录到/var/www/html/中,并命名为loganalyzer-3.6.5
[iyunv@centos7 loganalyzer-3.6.5]# cp -a src/ /var/www/html/loganalyzer-3.6.5

[iyunv@centos7 loganalyzer-3.6.5]# cd /var/www/html/
[iyunv@centos7 html]# ln -sv loganalyzer-3.6.5/  log # 创建软链接
‘log’ -> ‘loganalyzer-3.6.5/’
[iyunv@centos7 html]# ll
total 432
drwxr-xr-x  2 root root     38 Oct 14 12:51 admin
drwxr-xr-x 12 root root   4096 Jun  9  2015 bbs
drwxr-xr-x  9 6226 6226   4096 May  8  2014 drupal-7.28
-rw-r--r--  1 root root 585460 Oct 10 22:08 lastlog.txt
lrwxrwxrwx  1 root root     18 Oct 20 16:27 log -> loganalyzer-3.6.5/  # 链接文件
drwxrwxr-x 14 root root   4096 Oct  9  2013 loganalyzer-3.6.5
-rw-r--r--  1 root root 329712 Oct 11 09:19 messages.txt
-rw-r--r--  1 root root     25 Oct 11 22:03 phpinfo.php
drwxr-xr-x  9 root root   4096 Oct 13 11:05 phpMyAdmin-4.0.5-all-languages
-rw-r--r--  1 root root    125 Oct 12 22:27 php-mysql.php
lrwxrwxrwx  1 root root     31 Oct 13 10:54 pma -> phpMyAdmin-4.0.5-all-languages/
-rw-r--r--  1 root root    139 Oct  8 17:00 text.html

# 到原来的目录中找到contrib目录,这里面有两个脚本,可以帮助我们配置源码。
[iyunv@centos7 loganalyzer-3.6.5]# ls contrib/
configure.sh  secure.sh
[iyunv@centos7 loganalyzer-3.6.5]# cp  contrib/* /var/www/html/log/   # 复制过去

[iyunv@centos7 log]# cat configure.sh  # 从安装步骤中得知要先执行此脚本
#!/bin/sh

touch config.php
chmod 666 config.php
[iyunv@centos7 log]# cat secure.sh
#!/bin/sh
chmod 644 config.php  # 安装完成之后执行此脚本

[iyunv@centos7 log]# touch config.php  # 因为太简单所以,自己手动修改即可
[iyunv@centos7 log]# chmod 666 config.php



2)准备amp环境,这里以httpd+php为模块的方式
3)安装配置如下图
wKioL1gIijfAh73VAABcGLwcldw141.jpg

wKiom1gIil2wLyWRAACYfd3FeVc865.jpg

wKioL1gIim2Bx9nJAAB1RNzmRiA225.jpg

wKiom1gIioHAkwQmAAClOLUvfzU086.jpg

wKioL1gIip3zXTBoAAC7pbhfYBk555.jpg

wKiom1gIip6x9l_FAABNj2rpwx4464.jpg
  注意:这里localhost应该改为127.0.0.1 编辑配置文件config.php即可
wKioL1gIiqPSGs8fAACk3Ip7ixA108.jpg

wKioL1gIiqiTMwTgAAMPvVcI3VE455.jpg
  安装php-gd可以显示柱状图
wKiom1gIjJfDkxZlAADStSGTvHY826.jpg


步骤总结如下:
wKioL1gIjZyjwkaWAADbp0_l7JU129.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-289136-1-1.html 上篇帖子: centos6.7 route命令使用以及 添加永久路由的方法 下篇帖子: 关于rsync报错的问题 Linux
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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