一、日志基礎(chǔ)
日志服務(wù)功能是內(nèi)核提供的
rsyslog是用戶管理日志的工具
安裝軟件
sudo yum -y install rsyslog-5.8.10-6.el6.x86_64
啟動服務(wù)
/etc/init.d/rsyslog start //rhel5/6
systemctl start rsyslog //rhel7
查看日志:兩個位置
1) /var/log
2)軟件本身指定的目錄
常用日志
(以下都位于/var/log)
message ?? //系統(tǒng)日志
maillog ???//郵件日志
cron ????//計劃任務(wù)
xferlog ????//vsftpd日志,下載日志
httpd ????apache日志
secure ????安全日志 ssh ftp telent pop3 pam等
lastlog ????//記錄每一個賬戶最后一次登陸的時間,使用lastlog 命令查看日志,(安全相關(guān))
wtmp????//查看的是一個月所有賬戶的登陸情況,使用last命令查看日志 因為>wtmp的日志輪轉(zhuǎn)是一月一次,且只輪轉(zhuǎn)一次,關(guān)于輪轉(zhuǎn),后文會講到
utmp???? //查看當前登陸賬戶,用w who 命令查看日志,兩個命令看到的結(jié)果略有不同
btmp ???? // 查看錯誤登陸嘗試,使用lastb命令查看日志
samba ???? //samba 共享日志
yum.log ???? //yum程序相關(guān)的日志,記錄安裝和卸載
dmesg ???? //開機是核心檢查過程中所產(chǎn)生的信息
boot.log ???? //系統(tǒng)啟動過程中日志記錄存放
libvirt ???? // kvm虛擬化的日志
sa ???? //(是一個目錄,記錄一個月的cpu的使用率,cpu負載,磁盤I/O)用sar命令來查看, -f 參數(shù)后跟某天的文件名
tail -f 動態(tài)查看日志
也可以用cat 、vim 等來查看日志。
更加詳細的可以我的另外一篇 日志服務(wù)
二 、自定義日志
vim /etc/rsyslog.conf
*.* ??????? /var/log/mylog
kern.err????? /var/log/kernel.log
*.info;mail.none ??/var/log/big.log
mail.info ?????/var/log/mail.log
cron.info;cron.!err?? /var/log/newcron
/etc/rsyslog.conf中都是以下的模式來配置的
日志對象.日志級別???日志文件
查看都有哪些日志對象和日志級別
man 5 rsyslog.conf??(自己翻譯的,可能有些地方不一定準確)
日志對像(也叫日志設(shè)備)
The facility is one of the following keywords: auth(認證messsage), authpriv(privileges)認證權(quán)限、安全權(quán)限, cron計劃任務(wù), daemon后臺守護進程, kern內(nèi)核, lpr打印機, mail, mark, news新聞服務(wù)器, security (same as auth)安全, syslog(系統(tǒng)日志), user(用戶), uucp(unix to unix cp) and local0 through local7(用戶自定義日志用的).
日志級別
The priority(優(yōu)先級) is one of the following keywords, in ascending order: debug, info, notice, warning, warn (same as warning), err, error (same as err), crit, alert, emerg, panic (same as emerg). The keywords error, warn and panic are deprecated and should not be used anymore. The priority defines the severity of the message.
優(yōu)先級從低到高,級別低的包含級別高的,級別高的不包含級別低的。
level syslogd 遇到何種情況(正常、錯誤)才會記錄日志
LOG_EMERG 緊急,致命,服務(wù)無法繼續(xù)運行,如配置文件丟失
LOG_ALERT 報警,需要立即處理,如磁盤空使用95%
LOG_CRIT 致命行為
LOG_ERR 錯誤行為
LOG_WARNING 警告信息
LOG_NOTICE 普通
LOG_INFO 標準信息
LOG_DEBUG 調(diào)試信息,排錯所需,一般不建議使用
日志文件
日志存放的位置
/var/log/ ?? 一般的日志都存放在這里
還有一些是服務(wù)類的,這些都是服務(wù)定義的日志位置,比如/etc/ssh/sshd_conf 文件是ssh的配置文件,他就指定了ssh日志的存放位置。
自定義日志
local0-local7的使用
定義ssh日志為例
1.修改ssh的主配置文件/etc/ssh/sshd_config
SyslogFacility local5 //設(shè)置ssh的日志定義由local5設(shè)備來記錄
2.修改rsyslog的主配置文件/etc/rsyrlog.conf
local5.info ??/var/log/ssh
3.重啟日志服務(wù),重新加載服務(wù)配置文件。
自定義日志就完成了,新產(chǎn)生的日志就會到你指定的位置
logger小工具:用于shell腳本
使用命令行寫日志到指定的設(shè)備及級別
>logger "run......."
>logger -p emerg "run......."
>logger -p authpriv.info "run......."
三、日志輪轉(zhuǎn)
日志輪轉(zhuǎn)也叫日志切割,日志管理的重中之重
日志輪轉(zhuǎn)的配置文件是/etc/logrotate.conf
全局配置
`vim /etc/logrotate.conf`
see "man logrotate" for details
rotate log files weekly
weekly ? ? #輪轉(zhuǎn)周期
keep 4 weeks worth of backlogs
rotate 4 ? ? #輪轉(zhuǎn)次數(shù)
create new (empty) log files after rotating old ones
create ? ?#創(chuàng)建新的文件
use date as a suffix of the rotated file
dateext ? ? #后綴 ,以日期為后綴
uncomment this if you want your log files compressed
compress
RPM packages drop log rotation information into this directory
include /etc/logrotate.d ? ? 全局變量
no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly ? ? #輪轉(zhuǎn)周期
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok ? ? ? ? #丟失也不報錯
monthly
create 0600 root utmp
rotate 1
}
system-specific logs may be also be configured here.
服務(wù)類日志輪轉(zhuǎn)
服務(wù)類日志輪轉(zhuǎn)的配置文件在/etc/logrotate.d/下
以apache日志輪轉(zhuǎn)為例
/etc/logrotate.d/httpd
/var/log/httpd/*log {
?? missingok
?? notifempty ?? //空文件不輪轉(zhuǎn) ?? ??
?? sharedscripts //指下邊的不管有多少的文件被輪轉(zhuǎn),只執(zhí)行一次scripts
?? delaycompress ?? //壓縮相關(guān)的
?? postrotate ?? //開始
/bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true ?? //重新加載配置文件,把產(chǎn)生的信息扔了。
endscript ?? // 結(jié)束
}
還可以加上
prerotate
endscript
輪轉(zhuǎn)前
輪轉(zhuǎn)驗證
logrotate -f /etc/logrotate.conf 強制輪轉(zhuǎn),所有的都被輪轉(zhuǎn)了
強制輪轉(zhuǎn):
logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
-s 指定最后的日志輪轉(zhuǎn)記錄文件位/var/lib/logrotate/logrotate.status
#### 日志輪轉(zhuǎn)小提示
日志輪轉(zhuǎn)中重啟服務(wù)的重要性:
日志輪轉(zhuǎn)配置文件中重啟服務(wù)的腳本 是為了把新的日志內(nèi)容寫入到新的日志文件里
因為舊的日志文件被輪轉(zhuǎn)只是改了個名字,INODE并沒有變,但是日志程序是按日志文件的inode號識別文件的,所以需要重啟日志以改變?nèi)罩疚募樾碌奈募?
四 、日志使用案例
1: 統(tǒng)計登錄失敗top 5
grep 'Fail' /var/log/secure |awk '{print $11}' |sort |uniq -c|sort -k1 -n -r |head -5
7 172.16.130.14
6 172.16.130.70
5 172.16.130.56
3 172.16.130.80
2 172.16.130.76
2: 統(tǒng)計登錄成功
grep 'Accepted' /var/log/secure |awk '{print $(NF-3)}' |sort |uniq -c
4 111.201.131.215
1 116.243.0.213
1 123.120.14.32
3 123.120.38.233
2 221.222.199.175
1 221.222.202.102
3: 查看網(wǎng)卡是否已被驅(qū)動
grep -i eth /var/log/dmesg
[ 0.809104] r8169 Gigabit Ethernet driver 2.3LK-NAPI loaded
[ 0.814193] r8169 0000:02:00.0 eth0: RTL8168g/8111g at 0xffffc9000183e000, 40:8d:5c:9b:3c:17, XID 0c000800 IRQ 25
[ 0.814195] r8169 0000:02:00.0 eth0: jumbo features [frames: 9200 bytes, tx checksumming: ko]
[ 1.724991] bnx2 0000:01:00.0 eth0: Broadcom NetXtreme II BCM5709 1000Base-T (C0
) PCI Express found at mem d6000000, IRQ 32,1.725693] bnx2 0000:01:00.1 eth1: Broadcom NetXtreme II BCM5709 1000Base-T (C0
) PCI Express found at mem d8000000, IRQ 33,1.726387] bnx2 0000:02:00.0 eth2: Broadcom NetXtreme II BCM5709 1000Base-T (C0
) PCI Express found at mem da000000, IRQ 35, 1.727432] bnx2 0000:02:00.1 eth3: Broadcom NetXtreme II BCM5709 10