日志分析及管理

日志分析及管理
知识要点
日志的作用
rsyslog服务介绍
日志文件的存放位置
常见日志文件内容介绍
用户验证相关日志
常用日志操作命令
日志管理策略
日志转储功能(logrotate)
日志分析及管理
日志的作用
用于记录系统、程序运行中发生的各种事件
通过阅读日志,有助于诊断和解决系统故障
[root@localhost ~]# yum install -y httpd
//日志的位置 /var/log/messages
[root@localhost ~]# tail -f /var/log/messages 
Jan  7 06:45:36 localhost acpid: 1 client rule loaded
Jan  7 06:45:37 localhost automount[1550]: lookup_read_master: lookup(nisplus): couldn't locate nis+ table auto.master
Jan  7 06:45:38 localhost kdump: kexec: loaded kdump kernel
Jan  7 06:45:38 localhost kdump: started up
Jan  7 06:45:43 localhost abrtd: Init complete, entering main loop
Jan  7 09:50:42 localhost abrtd: Got signal 15, exiting
Jan  7 09:50:44 localhost init: rc main process (3505) killed by TERM signal
Jan  7 09:50:44 localhost kernel: ip_tables: (C) 2000-2006 Netfilter Core Team
Jan  7 09:50:44 localhost abrtd: Init complete, entering main loop
Jan  7 09:50:45 localhost init: start-ttys main process (3990) terminated with status 1
日志文件的分类
内核及系统日志
由系统服务rsyslog统一进行管理,日志格式基本相似
用户日志
记录系统用户登录及退出系统的相关信息
程序日志
由各种应用程序独立管理的日志文件,记录格式不统一
日志格式
日志记录的一般格式
[root@localhost ~]# tail  -5  /var/log/messages
Sep 14 11:22:44 localhost kernel: sdb: cache data unavailable
Sep 14 11:22:44 localhost kernel: sdb: assuming drive cache: write through
Sep 14 11:22:44 localhost kernel:  sdb: sdb1
Sep 14 11:23:37 localhost kernel: VFS: Can't find ext3 filesystem on dev sdb1.
Sep 14 16:54:48 localhost NetworkManager: <information> starting...
//     时间标签  主机名    子系统名         消息字段
rsyslog系统日志
由系统服务 rsyslog统一管理
软件包:rsyslog-5.8.10-8
[root@localhost ~]# rpm -q rsyslog
rsyslog-5.8.10-10.el6_6.x86_64
主要程序:/sbin/rsyslogd
配置文件:/etc/rsyslog.conf
[root@localhost ~]# grep  -v  "^#"  /etc/rsyslog.conf |  grep  -v  ^$
*.info;mail.none;authpriv.none;cron.none           /var/log/messages
authpriv.* /var/log/secure
mail.* -/var/log/maillog
cron.* /var/log/cron
//设备类别,日志级别                               // 消息 发送位置
配置文件:/etc/rsyslog.conf语法
日志设备(类型).(连接符号)日志级别 日志处理方式(action)
日志设备(可以理解为日志类型):
日志的级别
日志级别
NONE: 什么都不记录
EMERG(紧急):会导致主机系统不可用的情况
ALERT(警告):必须马上采取措施解决的问题
CRIT(严重):比较严重的情况
ERR(错误):运行出现错误
WARNING(提醒):可能会影响系统功能的事件
NOTICE(注意):不会影响系统但值得注意
INFO(信息):一般信息
DEBUG(调试):程序或系统调试信息等
从上到下,级别从高到低,记录的信息越来越多
连接符号
. :记录大于等于后面的级别日志
.=:只记录等于后面的级别日志
.!=:只记录不等于后面的级别日志
日志处理方式
本地文件:通常就是文件的绝对路径
打印机:例如 /dev/lp0 这个打印机装置
用户名称:显示给用户
远程主机:例如 @202.100.100.1
*:所有在线的用户
规则:
#### 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
系统日志文件
系统日志保存位置
默认位于:/var/log 目录下
主要日志文件介绍
  • 内核及公共消息日志:/var/log/messages
  • 计划任务日志:/var/log/cron
  • 系统引导日志:/var/log/dmesg
  • 邮件系统日志:/var/log/maillog
程序日志文件
由相应的应用程序独立进行管理
Web服务:/var/log/httpd/
access_log、error_log
代理服务:/var/log/squid/
access.log、cache.log、squid.out、store.log
FTP服务:/var/log/xferlog
……
分析工具
文本查看、grep过滤检索、Webmin管理套件中查看
awk、sed等文本过滤、格式化编辑工具
Webalizer、Awstats等专用日志分析工具
用户日志文件
保存了用户登录、退出系统等相关信息
数据文件,cat无法查看
/var/log/lastlog:最近的用户登录事件
/var/log/wtmp:用户登录、注销及系统开、关机事件
/var/run/utmp:当前登录的每个用户的详细信息
/var/log/secure:与用户验证相关的安全性事件
用户登录分析
who、w、users、last、ac、lastlog
日志命令
主动记录日志工具
logger:从命令行直接向系统日志文件写入一行信息
[root@localhost ~]# ping -c2 10.10.10.20 | logger -it "this is a test" -p authpriv.info
[root@localhost ~]# tail -5 /var/log/secure 
Aug  4 09:40:44 localhost this is a test[10351]: 64 bytes from 10.10.10.20: icmp_seq=2 ttl=64 time=0.276 ms
Aug  4 09:40:44 localhost this is a test[10351]: 
Aug  4 09:40:44 localhost this is a test[10351]: --- 10.10.10.20 ping statistics ---
日志管理策略
及时作好备份和归档
控制日志访问权限
日志中可能会包含各类敏感信息,如账户、口令等
集中管理日志
使用日志服务器便于日志的统一收集、整理和分析
杜绝日志信息的意外丢失、恶意篡改或删除
日志服务器
应用示例:
调整rsyslog服务设置,建立集中管理的日志服务器
将客户机B中所有日志消息,自动发送到服务器A的日志文件中
在服务器上配置:
1、修改/etc/rsyslog.conf文件,把以下2项的注释取消
[root@localhost ~]#  vim /etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
2、重启rsyslog服务
[root@localhost ~]# service rsyslog restart
service rsyslog restart
在客户机上配置:
1、修改/etc/rsyslog.conf文件,在最后加一行,内容如下:、、
*.* @172.16.88.189 服务器IP
2、重启rsyslog服务
service rsyslog restart
3、发送
[root@localhost log]# echo “123445” | logger -it "from88.189" -p local5.info
报错:
[root@localhost ~]# tail -1 /var/log/messages
Jan 11 21:34:43 localhost rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="3462" x-info="http://www.rsyslog.com"] start
关闭防火墙
[root@localhost ~]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter          [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
测试
在客户机通过logger添加日志
注意:
关闭selinux,Linux的一种安全保护
[root@localhost ~]# vim /etc/selinux/config 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled  //将此处修改为disabled*****
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
在服务器上查看日志
[root@localhost ~]# tail -1 /var/log/messages
Jan  8 20:00:55 localhost from88.189[3696]: hello2 world
日志转储功能
介绍
系统时时刻刻都在产生日志,如果不及时清理,很快就会填满硬盘,但如果要手工清理,又很麻烦。这种情况下,logrotate 这个程序很好的完成这个任务。
logrotate 用来把旧的日志文件删除,并创建新的日志文件,我们把它叫做“转储”。我们可以根据日志文件的大小,也可以根据天数来转储,这个过程一般通过crond进程来执行,logrotate 还可以用于压缩日志文件
Logrotate的次要配置文件:
/etc/logrotate.d/*
每个文件代表一种日志的配置
[root@localhost ~]# cat /etc/logrotate.d/syslog
/var/log/messages  /var/log/secure  /var/log/maillog  /var/log/spooler /var/log/boot.log /var/log/cron {
    sharedscripts
    postrotate
 /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
 /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}
日志转储
logrotate -v /etc/logrotate.conf
-f:强制转储
[root@localhost ~]# logrotate -vf /etc/logrotate.conf  
rotating log /var/log/messages, log->rotateCount is 4
renaming /var/log/messages.4 to /var/log/messages.5 (rotatecount 4, logstart 1, i 4), 
renaming /var/log/messages.3 to /var/log/messages.4 (rotatecount 4, logstart 1, i 3), 
renaming /var/log/messages.2 to /var/log/messages.3 (rotatecount 4, logstart 1, i 2), 
[root@localhost ~]# logrotate -v /etc/logrotate.conf  
reading config file /etc/logrotate.conf
including /etc/logrotate.d
..
rotating pattern: /var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron  weekly (4 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/messages
  log does not need rotating
本章总结
理解syslog的运行机制
掌握syslog.conf文件的语法
掌握基本的日志查询命令
理解日志轮转(logrotate)的运行机制
掌握logrotate.conf文件的语法
练习
将主机2上与计划任务相关的日志都发到主机1上去;修改主机1配置文件,将计划任务相关的日志记录到日志文件/var/log/newcron.log,注意该文件不能被随意删除和篡改;设定该日志文件的轮转,每周轮转一次,若该文件大于5M,则自动轮转,保留6个备份
配置:
主机1
1、vim /etc/rsyslog.conf
新建规则
cron.* /var/log/newcron.log
取消注释:
$ModLoad imudp
$UDPServerRun 514
2、重启服务
srevice rsyslog restart
主机2
1、vim /etc/rsyslog.conf
新建规则
cron.* @1
2、重启服务
srevice rsyslog restart
发送消息:
主机2:echo "123" | logger -it "from88.188" -p cron.info
查看消息:
主机1:cat /var/log/newcron.log
控制权限:
chmod 600 /var/log/newcron.log
配置次要文件
主机1: cd /etc/logrotate.d/
touch newcron
vim newcron
/var/log/newcron.log {
weeekly
size 5M
rotate 6
postrotate
srevice rsyslog restart
endscript
}
posted @ 2019-01-22 21:27  DBA_zzher  阅读(534)  评论(0编辑  收藏  举报