温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

十、syslog日志与loganalyzer日志管理

发布时间:2020-08-10 18:06:34 来源:网络 阅读:7168 作者:少年不在了 栏目:数据库

10.1、rsyslog简介

  syslog是一个历史悠久的日志系统。几乎所有的UNIX和Linux操作系统都采用syslog进行系统日志的管理和配置。Linux系统内核和许多程序会产生各种错误信息、警告信息和其他的提示信息。syslog可以根据信息的来源以及信息的重要程度将信息保存到不同的日志文件中。在默认的syslog配置下,日志文件通常都保存在/var/log目录下,在Centos6中,syslog的守护进程为rsyslog,系统启动时,默认会自动运行rsyslog守护进程。

  在syslog中定义了一些日志文件,这些日志文件的位置以及说明:

/var/log/dmesg   记录init进程启动之前的信息

/sbin/init       进程启动后日志需要滚动(日志切割)

/var/log/maillog 邮件系统产生的日志信息

/var/log/secure  安全系统产生的日志信息

/var/log/messages系统标准错误日志信息;非内核产生引导信息;各子系统产生的信息

10.2、rsyslog配置

 syslog的配置文件为/etc/rsyslog.conf,在该文件中指定了rsyslog记录日志的信息来源、信息类型以及保存位置。

[root@mylinux log]# cat /etc/rsyslog.conf 
# rsyslog v5 configuration file
# For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html
# If you experience problems, see http://www.rsyslog.com/doc/troubleshoot.html
#### 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
#$UDPServerRun 514
# Provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514
#### GLOBAL DIRECTIVES ####
# Use default timestamp format
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
# File syncing capability is disabled by default. This feature is usually not required,
# not useful and an extreme performance hit
#$ActionFileEnableSync on
# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf
#### 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.log
# ### begin forwarding rule ###
# The statement between the begin ... end define a SINGLE forwarding
# rule. They belong together, do NOT split them. If you create multiple
# forwarding rules, duplicate the whole block!
# Remote Logging (we use TCP for reliable delivery)
#
# An on-disk queue is created for this action. If the remote host is
# down, messages are spooled to disk and sent when it is up again.
#$WorkDirectory /var/lib/rsyslog # where to place spool files
#$ActionQueueFileName fwdRule1 # unique name prefix for spool files
#$ActionQueueMaxDiskSpace 1g   # 1gb space limit (use as much as possible)
#$ActionQueueSaveOnShutdown on # save messages to disk on shutdown
#$ActionQueueType LinkedList   # run asynchronously
#$ActionResumeRetryCount -1    # infinite retries if host is down
# remote host is: name/ip:port, e.g. 192.168.0.1:514, port optional
#*.* @@remote-host:514
# ### end of the forwarding rule ###

   该文件以'#'为注释符,其中每一行的语法格式为:

[消息来源.消息级别]    [动作]

  其中,[消息来源.消息级别]和[动作]之间以Tab键进行分隔,同一行rsyslog配置中允许出现多个[消息来源.消息级别],必须以';'进行分隔。

*.info;mail.none;authpriv.none;cron.none                /var/log/messages

rsyslog(facility)消息来源及说明:

auth:  认证相关的         authpriv:  权限,授权相关的

cron:  任务计划相关的     daemon:    守护进程相关的

kern:  内核相关的         lpr:       打印相关的

mail:  邮件相关的         mark:      标记相关的

news:  新闻相关的         security:  安全相关的,与auth 类似  

syslog:syslog自己的       user:      用户相关的

*       表示所有的facility uucp:      unix to unix cp 相关的

local0-local7:本地用户 

rsyslog(log level)消息级别及说明:

debug:       程序或系统的调试信息           info:      一般信息

notice:      不影响正常功能,需要注意的消息  err/error: 错误信息

crit:        比较严重的                     alert:     必须马上处理的

emerg/panic: 会导致系统不可用的             *:         表示所有的日志级别         

none:        跟*相反,表示啥也没有

warning/warn:可能影响系统功能,需要提醒用户的重要事件

  rsyslog消息级别是向上匹配的,即如果指定了一个消息级别,那么指定级别及比该指定级别更高级的消息都会包含进去。例如,warning表示大于或等于warning级别的消息都会被处理,包括emerg、alert、crit、err和warning。消息级别越低,消息的数量就会越大,如果只想匹配某个确定等级的消息,而不希望更高级的消息,可以使用等号进行指定。

cron.=notice                                                  /var/log/cron

rsyslog(action)处理动作:

系统上的绝对路径 :将消息保存到普通文件           如: /var/log/xxx

|                :通过管道送给其他的命令处理 

终端             :把消息发送到本地主机终端       如:/dev/console

@HOST            :把消息转发到另一台syslog服务器上进行处理     如: @10.0.0.1      

用户             :把消息发送到指定用户,用户名以','进行分隔    如: root

*                :登录到系统上的所有用户,一般emerg级别日志这样定义 

配置实例:

 通过logger命令可以模拟产生各类的syslog消息,从而测试配置是否正确。

 用法:logger [-isd] [-f file] [-p pri] [-t tag] [-u socket][message ...]

[root@mylinux log]# vim /etc/rsyslog.conf 
#syslog测试                                       #修改配置文件,添加这两行
kern.info   /var/log/kern_test.log
[root@mylinux log]# /etc/init.d/rsyslog restart   #重启进程
关闭系统日志记录器:                                       [确定]
启动系统日志记录器:                                       [确定]
[root@mylinux log]# logger kern.info 'test info'  #模拟内核信息
[root@mylinux log]# cat /var/log/kern_test.log    #产生的日至消息
May  3 19:11:30 localhost kernel: imklog 5.8.10, log source = /proc/kmsg started.

10.3、其它日志

  除了rsyslog以外,Linux系统还提供了大量其他的日志文件,这些日志文件中记录了非常重要的消息 。常用的主要有dmesg、wtmp、btmp和.bash_history等。

  • dmesg日志:记录内核日志信息

 日志文件/var/log/dmesg中记录了系统启动过程中内核日志信息,包括系统的设备信息,以及在启动和操作过程中系统记录的任何错误和问题的信息。

[root@mylinux log]# less /var/log/dmesg
Initializing cgroup subsys cpuset
Initializing cgroup subsys cpu
Linux version 2.6.32-642.11.1.el6.x86_64 (mockbuild@c1bm.rdu2.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) ) #1 SMP Fri Nov 18 19:25:05 UTC 2016
Command line: ro root=/dev/vda1 console=ttyS0 console=tty0 printk.time=1 panic=5 rd_NO_LUKS  KEYBOARDTYPE=pc KEYTABLE=us LANG=zh_CN.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 rd_NO_LVM crashkernel=auto   rd_NO_DM
KERNEL supported cpus:
  Intel GenuineIntel
  AMD AuthenticAMD
  Centaur CentaurHauls
BIOS-provided physical RAM map:
 BIOS-e820: 0000000000000000 - 000000000009fc00 (usable)
  • 用户登录日志

 /var/log/wtmp和/var/log/btmp是Linux系统上用户保存用户登录信息的日志文件。其中,wtmp用于保存用户成功登录的记录,btmp用于保存用户登录失败的日志记录。这两个文件都是二进制的,无法直接使用文本编辑工具打开,必须通过last和lastb命令进行查看。

[root@mylinux log]# last | less   #查看登录成功的信息
root     pts/0        192.168.1.120    Wed May  3 18:30   still logged in   
root     pts/0        192.168.1.120    Tue May  2 14:47 - 15:40  (00:53)    
root     pts/0        192.168.1.120    Fri Apr 28 12:00 - 22:01  (10:00)    
root     pts/0        192.168.1.120    Mon Apr 24 08:54 - 17:18  (08:23)    
root     pts/0        192.168.1.120    Sun Apr 23 18:48 - 22:00  (03:11)    
root     pts/0        192.168.1.120    Sun Apr 23 16:28 - 16:32  (00:03)    
root     pts/0        192.168.1.120    Sat Apr 22 12:51 - 13:39  (00:47)    
root     pts/0        192.168.1.120    Sat Apr 22 12:03 - 12:51  (00:47)    
root     pts/0        192.168.1.120    Fri Apr 21 12:51 - 13:16  (00:24)
...
[root@mylinux log]# lastb         #查看登录失败的信息
support  ssh:notty    171.212.140.107  Wed May  3 18:27 - 18:27  (00:00)    
admin    ssh:notty    78.106.24.79     Wed May  3 13:18 - 13:18  (00:00)    
liuyr    ssh:notty    61.147.166.76    Wed May  3 12:53 - 12:53  (00:00)    
admin    ssh:notty    124.131.79.205   Wed May  3 12:41 - 12:41  (00:00)    
admin    ssh:notty    112.120.73.220   Wed May  3 11:37 - 11:37  (00:00)    
support  ssh:notty    2.177.238.135    Wed May  3 10:59 - 10:59  (00:00)    
ubnt     ssh:notty    109.161.75.102   Wed May  3 09:33 - 09:33  (00:00)    
admin    ssh:notty    181.25.207.227   Wed May  3 09:14 - 09:14  (00:00)    
admin    ssh:notty    181.26.181.184   Wed May  3 06:54 - 06:54  (00:00)    
admin    ssh:notty    181.26.181.184   Wed May  3 06:54 - 06:54  (00:00)    
admin    ssh:notty    167.160.149.47   Wed May  3 05:27 - 05:27  (00:00)    
admin    ssh:notty    179.63.255.251   Wed May  3 05:07 - 05:07  (00:00)  
...
  • 用户操作记录

 默认情况下,在每个用户的主目录下都会有一个.bash_history文件,该文件中保存了该用户输入的所有命令记录,管理员可以通过该文件查看某个用户做过什么操作。

[root@mylinux log]# cat /root/.bash_history 
#1493001194
htop
#1493352081
pip3 install Numpy
#1493707660
LS
#1493707661
ls
#1493707664
ls
#1493707668
cd /selinux/
#1493707669
ls
#1493707869
cat /etc/selinux/config 
#1493708491
ls
#1493708497
...

10.4、loganalyzer日志分析工具

  LogAnalyzer 是一款syslog日志和其他网络事件数据的Web前端。它提供了对日志的简单浏览、搜索、基本分析和一些图表报告的功能。数据可以从数据库或一般的syslog文本文件中获取,所以LogAnalyzer不需要改变现有的记录架构。基于当前的日志数据,它可以处理syslog日志消息,Windows 事件日志记录,支持故障排除,使用户能够快速查找日志数据中看出问题的解决方案。

  LogAnalyzer 获取客户端日志会有两种保存模式,一种是直接读取客户端/var/log/目录下的日志并保存到服务端该目录下,一种是读取后保存到日志服务器数据库中,推荐使用后者。LogAnalyzer采用php开发,所以日志服务器需要php的运行环境,这里采用LAMP。

1)安装配置好mysql数据库服务

[root@mylinux home]# yum install mysql  mysql-server -y
[root@mylinux home]#/etc/init.d/mysqld start

2)安装rsyslog-mysql包

[root@mylinux log]# yum install rsyslog-mysql -y

3)创建rsyslog依赖的数据库:

[root@mylinux doc]# mysql < /usr/share/doc/rsyslog-mysql-5.8.10/createDB.sql 
[root@localhost home]# mysql  
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> show databases;    #查看数据是否导入
+--------------------+
| Database           |
+--------------------+
| information_schema |
| Syslog             |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)

4)配置rsyslog启用模块,/etc/rsyslog.conf

 在#### Modules #####启用模块:

  $ModLoad ommysql

 在####rules####段中定义记录日志信息于数据库中

  facility.priority :ommysql:SERVER_IP,DATABASE,USERNAME,PASSWORD

[root@mylinux rsyslog.d]# vim /etc/rsyslog.conf 
#### MODULES ####
$ModLoad ommysql
...
#### RULES ####
facility.priority :ommysql:SERVER_IP,DATABASE,USERNAME,PASSWORD
[root@localhost home]# /etc/init.d/rsyslog restart
Shutting down system logger: [  OK  ]
Starting system logger: [  OK  ]

5)搭建lamp环境,安装loganalyzer

# yum -y install httpd php php-mysql php-gd
[root@localhost home]# httpd
httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[root@mylinux home]# wget    
[root@mylinux home]# tar xf loganalyzer-3.6.6.tar.gz
[root@mylinux home]# mkdir /var/www/html/loganalyzer
[root@mylinux home]# cp -R loganalyzer-3.6.6/src/* /var/www/html/loganalyzer/
[root@mylinux home]# cp -R loganalyzer-3.6.6/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 ./*

6)在浏览器输入网站地址,按照提示就可以完成配置。

十、syslog日志与loganalyzer日志管理

十、syslog日志与loganalyzer日志管理

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI