Linux日志分析工具
那么我们有取代的方案吗?有,那就是日志分析工具。这些日志分析工具会详细地查看日志,同时分析这些日志,并且把分析的结果通过邮件的方式发送给 root 用户。这样,我们每天只要查看日志分析工具的邮件,就可以知道服务器的基本情况,而不用挨个检查日志了。这样系统管理员就可以从繁重的日常工作中解脱出来,去处理更加重要的工作。
在 CentOS 中自带了一个日志分析工具,就是 logwatch。不过这个工具默认没有安装(因为我们选择的是“Basic Server”),所以需要手工安装。安装命令如下:
[root@localhost Packages]# yum -y install logwatch
安装完成之后,需要手工生成 logwatch 的配置文件。默认配置文件是 /etc/logwatch/conf/logwatch.conf,不过这个配置文件是空的,需要把模板配置文件复制过来。命令如下: [root@localhost ~]# cp /usr/share/logwatch/default.conf/logwatch.conf /etc/logwatch/conf/logwatch.conf
#复制配置文件
[root@localhost ~]# vi /etc/logwatch/conf/logwatch.conf
#查看配置文件
LogDir = /var/log
#logwatch会分析和统计/var/log/中的日志
TmpDir = /var/cache/logwatch
#指定logwatch的临时目录
MailTo = root
#日志的分析结果,给root用户发送邮件
MailFrom = Logwatch
#邮件的发送者是Logwatch,在接收邮件时显示
Print =
#是否打印。如果选择“yes”,那么日志分析会被打印到标准输出,而且不会发送邮件。我们在这里不打印,#而是给root用户发送邮件
#Save = /tmp/logwatch
#如果开启这一项,日志分析就不会发送邮件,而是保存在/tmp/logwatch文件中
#如果开启这一项,日志分析就不会发送邮件,而是保存在/tmp/logwatch文件中
Range = yesterday
#分析哪天的日志。可以识别“All”“Today”“Yesterday”,用来分析“所有日志”“今天日志”“昨天日志”
Detail = Low
#日志的详细程度。可以识别“Low”“Med”“High”。也可以用数字表示,范围为0~10,“0”代表最不详细,“10”代表最详细
Service = All
#分析和监控所有日志
Service = "-zz-network"
#但是不监控“-zz-network”服务的日志。“-服务名”表示不分析和监控此服务的日志
Service = "-zz-sys"
Service = "-eximstats"
如果想要让这个日志分析马上执行,则只需执行 logrotate 命令即可。命令如下:
[root@localhost ~]# logwatch
#马上执行logwatch日志分析工具
[root01ocalhost ~]# mail
#查看邮件
Heirloom Mail version 12.4 7/29/08. Type ? for help, "/var/spool/mail/root": 5 messages 1 new 2 unread
1 logwatch@localhost.1 Fri Jun 7 11:17 42/1482 "Logwatch for localhost.localdomain (Linux)"
U 2 logwatch@localhost.1 Fri Jun 7 11:19 42/1481 "Logwatch for localhost.localdomain (Linux)"
3 logwatch@localhost.1 Fri Jun 7 11:23 1234/70928 "Logwatch for localhost.localdomain (Linux)"
4 logwatch@localhost.1 Fri Jun 7 11:24 190/5070 "Logwatch for localhost.localdomain (Linux)"
5 logwatch@localhost.1 Fri Jun 7 11:55 41/1471 "Logwatch for localhost.localdomain (Linux)"
>N 6 logwatch@localhost.1 Fri Jun 7 11:57 189/5059 "Logwatch for localhost.localdomain (Linux)"
#第6封邮件就是刚刚生成的曰志分析邮件,"N"代表没有查看
& 6
Message 6:
From root@localhost.localdomain Fri Jun 7 11:57:35 2013 Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
To: root@localhost.localdomain
From: logwatch@localhost.localdomain
Subject: Logwatch for localhost.localdomain (Linux)
Content-Type: text/plain; charset="iso-8859-1"
Date: Fri, 7 Jun 2013 11:57:33 +0800 (CST)
Status: R
######## Logwatch 7.3.6 (05/19/07) ################
Processing Initiated: Fri Jun 7 11:57:33 2013
Date Range Processed: all
Detail Level of Output: 0
Type of Output: unformatted
Logfiles for Host: localhost.localdomain
###################################################
#上面是曰志分析的时间和日期
...省略部分输出...
--------- Connections (secure-log) Begin-----------
#分析secure.log日志的内容。统计新建立了哪些用户和组,以及错误登录信息 New Users:
bb (501)
def (503)
hjk (504)
zhangsan (505)
dovecot (97)
dovenull (498)
aa (500)
New Groups:
bb (501)
def (503)
hjk (504)
zhangsan (505)
dovecot (97)
dovenull (498)
aa (500)
Failed logins:
User root:
(null): 3 Time(s)
Root logins on tty's: 7 Time(s).
**Unmatched Entries**
groupadd: group added to /etc/group: name=dovecot, GID=97: 1 Time(s)
groupadd: group added to /etc/group: name=dovenul1, GID=498: 1 Time(s)
groupadd: group added to /etc/gshadow: name=dovecot: 1 Time(s)groupadd: group added to /etc/gshadow: name=dovenull: 1 Time(s)
--------Connections (secure-log)End-------
-------------SSHD Begin-------------------
#分析SSHD的日志。可以知道哪些IP地址连接过服务器
SSHD Killed: 7 Time(s)
SSHD Started: 24 Time(s)
Users logging in through sshd:
192.168.0.104: 10 times
192.168.0.108: 8 times
192.168.0.101: 6 times
192.168.0.126: 4 times
192.168.0.100: 3 times
192.168.0.105: 3 times
192.168.0.106: 2 times
192.168.0.102: 1 time
192.168.0.103: 1 time
SFTP subsystem requests: 3. Time(s)
**Unmatched Entries**
Exiting on signal 15 : 6 time(s)
----------------SSHD End-----------
--------------- yum Begin ---------
#统计yum安装的软件。可以知道我们安装了哪些软件
Packages Installed:
perl-YAML-Syck-1.07-4.el6.i686
perl-Date-Manip-6.24-1.el6.noarch
logwatch-7.3.6-49.el6.noarch
-----------yum End-------------
--------Disk Space Begin-------
#统计磁盘空间情况
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 20G 1.9G 17G 11% /
/dev/sda1 194M 26M 158M 15% /boot
/dev/sr0 3.5G 3.5G 0 100% /mnt/cdrom
---------Disk Space End-----------------
#########Logwatch End ##################