日志:
历史事件:时间,地点,人物,事件日期时间
事件记录格式:
日期时间 主机 进程[pid]: 事件内容
C/S架构:通过TCP或UDP协议的服务完成日志记录传送,将分布在不同主机的日志实现集中管理
rsyslog
rsyslog特性:CentOS6和7 多线程
UDP, TCP, SSL, TLS, RELP
MySQL, PGSQL, Oracle实现日志存储
强大的过滤器,可实现过滤记录日志信息中任意部分
自定义输出格式
[root@node4~]#rpm -q rsyslog #查询rsyslog日志包
rsyslog-8.24.0-12.el7.x86_64
[root@node4~]#rpm -ql rsyslog
/etc/logrotate.d/syslog
/etc/pki/rsyslog
/etc/rsyslog.conf
/etc/rsyslog.d
/etc/sysconfig/rsyslog
/usr/bin/rsyslog-recover-qi.pl
/usr/lib/systemd/system/rsyslog.service
ELK:elasticsearch, logstash, kibana
非关系型分布式数据库
基于apache软件基金会jakarta项目组的项目lucene
Elasticsearch是个开源分布式搜索引擎
Logstash对日志进行收集、分析,并将其存储供以后使用
kibana 可以提供的日志分析友好的 Web 界面
rsyslog 介绍
术语,参见man logger
facility:设施,从功能或程序上对日志进行归类
auth, authpriv, cron, daemon,ftp,kern, lpr, mail, news, security(auth), user, uucp, local0-local7, syslog
Priority 优先级别,从低到高排序
debug, info, notice, warn(warning), err(error), crit(critical), alert, emerg(panic)
参看帮助: man 3 syslog
rsyslog
程序包:rsyslog
主程序:/usr/sbin/rsyslogd
CentOS 6:service rsyslog {start|stop|restart|status} CentOS 7:/usr/lib/systemd/system/rsyslog.service
配置文件:/etc/rsyslog.conf,/etc/rsyslog.d/*.conf
库文件: /lib64/rsyslog/*.so
配置文件格式:由三部分组成
MODULES:相关模块配置
GLOBAL DIRECTIVES:全局配置
RULES:日志记录相关的规则配置
rsyslog
RULES配置格式: facility.priority; facility.priority… target
facility:*: 所有的facility
facility1,facility2,facility3,...:指定的facility列表
priority: *: 所有级别
none:没有级别,即不记录
PRIORITY:指定级别(含)以上的所有级别
=PRIORITY:仅记录指定级别的日志信息
target:
文件路径:通常在/var/log/,文件路径前的-表示异步写入
用户:将日志事件通知给指定的用户,* 表示登录的所有用户
日志服务器:@host,把日志送往至指定的远程服务器记录
管道: | COMMAND,转发给其它命令处理
出发日志工具:logger
[root@node4~]#logger "this is a test log"
[root@node4~]#tail /var/log/messages
Jan 18 17:40:01 node4 systemd: Starting Session 27 of user root.
Jan 18 17:46:26 node4 dbus[626]: [system] Activating via systemd: service name='org.freedesktop.PackageKit' unit='packagekit.service'
Jan 18 17:46:26 node4 dbus-daemon: dbus[626]: [system] Activating via systemd: service name='org.freedesktop.PackageKit' unit='packagekit.service'
Jan 18 17:46:26 node4 systemd: Starting PackageKit Daemon...
Jan 18 17:46:26 node4 dbus[626]: [system] Successfully activated service 'org.freedesktop.PackageKit'
Jan 18 17:46:26 node4 dbus-daemon: dbus[626]: [system] Successfully activated service 'org.freedesktop.PackageKit'
Jan 18 17:46:26 node4 systemd: Started PackageKit Daemon.
Jan 18 17:50:01 node4 systemd: Started Session 28 of user root.
Jan 18 17:50:01 node4 systemd: Starting Session 28 of user root.
Jan 18 17:51:03 node4 root: this is a test log
[root@node4~]#egrep -v '^$|#' /etc/rsyslog.conf #查看配置文件相关的日志路径
$WorkDirectory /var/lib/rsyslog
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat
$IncludeConfig /etc/rsyslog.d/*.conf
$OmitLocalLogging on
$IMJournalStateFile imjournal.state
*.info;mail.none;authpriv.none;cron.none /var/log/messages
authpriv.* /var/log/secure
mail.* -/var/log/maillog
cron.* /var/log/cron
*.emerg :omusrmsg:*
uucp,news.crit /var/log/spooler
local7.* /var/log/boot.log
ssh 的相关日志记录在secure 日志里
[root@node4~]#ssh 192.168.137.47
root@192.168.137.47's password:
[root@node4~]#tail /var/log/secure
Jan 18 18:14:56 node4 sshd[4090]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.137.47 user=root
Jan 18 18:14:56 node4 sshd[4090]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Jan 18 18:14:58 node4 sshd[4090]: Failed password for root from 192.168.137.47 port 52894 ssh3
[root@node4~]#egrep -v "^$|^#" /etc/ssh/sshd_config |grep SyslogFacility
SyslogFacility AUTHPRIV
定义log日志路径;
[root@node4~]#vim +33 /etc/ssh/sshd_config
33 SyslogFacility local7
34 LogLevel INFO
[root@node4~]#vim /etc/rsyslog.d/sshd.conf
1 local7.* /var/log/sshd.log
[root@node4~]#systemctl restart rsyslog sshd
[root@node4~]#ps aux |grep rsyslogd (rpm -q rsyslog /rpm -ql rsyslog.$packet )
root 4665 0.0 0.2 275560 2668 ? Ssl 18:49 0:00 /usr/sbin/rsyslogd -n
root 4700 0.0 0.0 112660 968 pts/1 S+ 18:50 0:00 grep --color=auto rsyslogd
[root@node4~]# ssh 192.168.137.47
root@192.168.137.47's password:
Permission denied, please try again.
root@192.168.137.47's password:
Permission denied, please try again.
root@192.168.137.47's password:
Permission denied (publickey,password).
[root@node4~]#tail /var/log/sshd.log
Jan 18 18:49:08 node4 sshd[4664]: Server listening on 0.0.0.0 port 22.
Jan 18 18:49:08 node4 sshd[4664]: Server listening on :: port 22.
Jan 18 18:49:46 node4 sshd[4681]: Failed password for root from 192.168.137.47 port 52900 ssh3
Jan 18 18:49:46 node4 sshd[4681]: Failed password for root from 192.168.137.47 port 52900 ssh3
Jan 18 18:49:46 node4 sshd[4681]: Connection closed by 192.168.137.47 port 52900 [preauth]
事件记录格式:
日期时间 主机 进程[pid]: 事件内容
centos7:修改主机名:
[root@node3~]#hostnamectl set-hostname $hostname
[root@node3~]#/etc/host
日志服务器:@host,把日志送往至指定的远程服务器记录
启用网络日志服务
通常的日志格式:
事件产生的日期时间 主机 进程(pid):事件内容
如: /var/log/messages,cron,secure等 配置rsyslog成为日志服务器
#### MODULES ####
# Provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
# Provides TCP syslog reception
$ModLoad imtcp
$InputTCPServerRun 514
日志服务器:@host,把日志送往至指定的远程服务器记录
源主机:192.168.137.47 node4
目标主机:192.168.137.37 node3
[root@node4~]#cat /etc/rsyslog.d/sshd.conf
local7.* /var/log/sshd.log
#udp
local2.* @192.168.137.37
#tcp
#local7.* @@192.168.137.37
[root@node3~]#cat /etc/rsyslog.conf |grep -A1 $ModLoad imudp
$ModLoad imudp
$UDPServerRun 514
[root@node3~]#cat /etc/rsyslog.conf |grep local2.*
local2.* /var/log/udp.log
测试:
[root@node4~]#ssh 192.168.137.47
root@192.168.137.47's password:
[root@node3~]#tail /var/log/udp.log -f
Jan 18 22:00:59 node4 sshd[7903]: Accepted password for root from 192.168.137.47 port 52916 ssh3
Jan 18 22:01:24 node4 sshd[7903]: Received disconnect from 192.168.137.47 port 52916:11: disconnected by user
Jan 18 22:01:24 node4 sshd[7903]: Disconnected from 192.168.137.47 port 52916
Jan 18 22:19:09 node4 sshd[8172]: Failed password for root from 192.168.137.47 port 52920 ssh3
Jan 18 22:19:12 node4 sshd[8172]: Accepted password for root from 192.168.137.47 port 52920 ssh3
其它日志
其它的日志文件
#/var/log/secure:系统安装日志,文本格式,应周期性分析
#/var/log/btmp:当前系统上,用户的失败尝试登录相关的日志信息,二进制格
式,lastb命令进行查看
#/var/log/wtmp:当前系统上,用户正常登录系统的相关日志信息,二进制格
式,last命令可以查看
#/var/log/lastlog:每一个用户最近一次的登录信息,二进制格式,lastlog命令
可以查看
#/var/log/dmesg:系统引导过程中的日志信息,文本格式
文本查看工具查看
专用命令dmesg查看
#/var/log/messages :系统中大部分的信息
#/var/log/anaconda : anaconda的日志
[root@node4/var/log]#lastb |head |awk '{print $3}' |sort |uniq -c
10 192.168.137.47
[root@node4/var/log]#lastb |head |awk '{ip [$3]++}END {for (i in ip ) {print ip [i] ,i }}'
10 192.168.137.47