新手怎麼學習雲計算?小白也能玩轉日誌管理

在雲計算出現以前,有客戶端/服務器計算,集中存儲,其中全部數據,軟件應用程序和控件都駐留在服務器端。若是用戶想要運行程序或訪問特定數據,那麼他將鏈接到服務器並得到適當的訪問權限並能夠開展業務。在此以後出現了分佈式計算概念,其中全部計算機都聯網在一塊兒,而且在須要時共享資源。那麼新手怎麼學習雲計算?下面給你們講解小白也能玩轉日誌管理:node

1、日誌基礎shell

日誌服務功能是內核提供的
rsyslog是用戶管理日誌的工具apache

安裝軟件vim

#sudo yum -y install rsyslog-5.8.10-6.el6.x86_64安全

啓動服務服務器

#/etc/init.d/rsyslog start //rhel5/6
#systemctl start rsyslog //rhel7ssh

查看日誌:兩個位置分佈式

1) /var/log
2)軟件自己指定的目錄工具

經常使用日誌post

(如下都位於/var/log)
message    //系統日誌
maillog    //郵件日誌
cron     //計劃任務
xferlog     //vsftpd日誌,下載日誌
httpd     apache日誌
secure     安全日誌 ssh ftp telent pop3 pam等
lastlog     //記錄每個帳戶最後一次登錄的時間,使用lastlog 命令查看日誌,(安全相關)
wtmp    //查看的是一個月全部帳戶的登錄狀況,使用last命令查看日誌 由於wtmp的日誌輪轉是一月一次,且只輪轉一次,關於輪轉,後文會講到
utmp     //查看當前登錄帳戶,用w who 命令查看日誌,兩個命令看到的結果略有不一樣
btmp      // 查看錯誤登錄嘗試,使用lastb命令查看日誌
samba      //samba 共享日誌
yum.log      //yum程序相關的日誌,記錄安裝和卸載
dmesg      //開機是核心檢查過程當中所產生的信息
boot.log      //系統啓動過程當中日誌記錄存放
libvirt      // kvm虛擬化的日誌
sa      //(是一個目錄,記錄一個月的cpu的使用率,cpu負載,磁盤I/O)用sar命令來查看, -f 參數後跟某天的文件名
tail -f 動態查看日誌
也能夠用cat 、vim 等來查看日誌。
更加詳細的能夠個人另一篇 日誌服務

二 、自定義日誌

# 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  (本身翻譯的,可能有些地方不必定準確)

日誌對像(也叫日誌設備)

The facility is one of the following keywords: auth(認證messsage), authpriv(privileges)認證權限、安全權限, cron計劃任務, daemon後臺守護進程, kern內核, lpr打印機, mail, mark, news新聞服務器, security (same as auth)安全, syslog(系統日誌), user(用戶), uucp(unix to unix cp) and local0 through local7(用戶自定義日誌用的).

日誌級別

The priority(優先級) 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.
優先級從低到高,級別低的包含級別高的,級別高的不包含級別低的。

level syslogd 遇到何種狀況(正常、錯誤)纔會記錄日誌
LOG_EMERG 緊急,致命,服務沒法繼續運行,如配置文件丟失
LOG_ALERT 報警,須要當即處理,如磁盤空使用95%
LOG_CRIT 致命行爲
LOG_ERR 錯誤行爲
LOG_WARNING 警告信息
LOG_NOTICE 普通
LOG_INFO 標準信息
LOG_DEBUG 調試信息,排錯所需,通常不建議使用

日誌文件

日誌存放的位置
/var/log/    通常的日誌都存放在這裏
還有一些是服務類的,這些都是服務定義的日誌位置,好比/etc/ssh/sshd_conf 文件是ssh的配置文件,他就指定了ssh日誌的存放位置。

自定義日誌

local0-local7的使用
定義ssh日誌爲例
1.修改ssh的主配置文件/etc/ssh/sshd_config
SyslogFacility local5 //設置ssh的日誌定義由local5設備來記錄
2.修改rsyslog的主配置文件/etc/rsyrlog.conf
local5.info   /var/log/ssh
3.重啓日誌服務,從新加載服務配置文件。
自定義日誌就完成了,新產生的日誌就會到你指定的位置

logger小工具:用於shell腳本
使用命令行寫日誌到指定的設備及級別
# logger "run......."
# logger -p emerg "run......."
# logger -p authpriv.info "run......."

3、日誌輪轉

日誌輪轉也叫日誌切割,日誌管理的重中之重
日誌輪轉的配置文件是/etc/logrotate.conf

全局配置

# vim /etc/logrotate.conf
#see "man logrotate" for details
#rotate log files weekly
weekly     #輪轉週期

# keep 4 weeks worth of backlogs
rotate 4     #輪轉次數

# create new (empty) log files after rotating old ones
create    #建立新的文件

# 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     #輪轉週期
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.

服務類日誌輪轉

服務類日誌輪轉的配置文件在/etc/logrotate.d/下
以apache日誌輪轉爲例

/etc/logrotate.d/httpd

/var/log/httpd/*log {
   missingok
   notifempty    //空文件不輪轉       
   sharedscripts //指下邊的無論有多少的文件被輪轉,只執行一次scripts
   delaycompress    //壓縮相關的
   postrotate    //開始
/bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true    //從新加載配置文件,把產生的信息扔了。
endscript    // 結束
}

還能夠加上
prerotate
endscript
輪轉前

輪轉驗證

logrotate -f /etc/logrotate.conf 強制輪轉,全部的都被輪轉了
強制輪轉:
#logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
-s 指定最後的日誌輪轉記錄文件位/var/lib/logrotate/logrotate.status

日誌輪轉小提示

日誌輪轉中重啓服務的重要性: 
日誌輪轉配置文件中重啓服務的腳本 是爲了把新的日誌內容寫入到新的日誌文件裏
由於舊的日誌文件被輪轉只是改了個名字,INODE並無變,可是日誌程序是按日誌文件的inode號識別文件的,因此須要重啓日誌以改變日誌文件爲新的文件

四 、日誌使用案例

1: 統計登陸失敗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: 統計登陸成功
# 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: 查看網卡是否已被驅動 
# 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

相關文章
相關標籤/搜索