1、以IP地址命名
在/etc/rsyslog.conf中加入如下配置,並做好備注。添加這三行配置之后,遠程日志會被單獨輸出到一個以IP命名的日志文件中。
#IP format by zhz at xxxx-xx-xx $template IpTemplate,"/var/log/%FROMHOST-IP%.log" *.* ?IpTemplate & ~
2、設備名&日期
Rsyslog的日志輪詢默認為一周,保存4周,如果生產中日志產生量很大,或者設備數目過多的話,查詢時就會遇到障礙,此時我們選擇將日志以設備名和日期來命名日志,此處以天為單位,每天存儲一份。在/etc/rsyslog.conf中加入如下配置,並做好備注。
#Divided by prefecture and city,end by date.(以省和地市划分) #province(省公司) $EscapeControlCharactersOnReceive off $template PRO-SW-01,"/var/log/province/PRO-SW-01_log.%$year%-%$month%-%$day%" $template myFormat,"%msg%\n" :rawmsg,contains,"PRO-SW-01" -?PRO-SW-01;myFormat $EscapeControlCharactersOnReceive off $template PRO-QR-01,"/var/log/province/PRO-QR-01_log.%$year%-%$month%-%$day%" $template myFormat,"%msg%\n" :rawmsg,contains,"PRO-QR-01" -?PRO-QR-01;myFormat #huawei-route(地市公司的華為路由) $EscapeControlCharactersOnReceive off $template City--QR-01,"/var/log/NE40X3/City-QR-01_log.%$year%-%$month%-%$day%" $template myFormat,"%msg%\n" :rawmsg,contains,"City--QR-01" -?City--QR-01;myFormat #H3c-Quitway-route---1#經測試這個模式日志會不顯示時間,建議用-2方式 $EscapeControlCharactersOnReceive off $template City-SR-01,"/var/log/SR/City-SR-01_log.%$year%-%$month%-%$day%" $template myFormat,"%msg%\n" :rawmsg,contains,"City-SR-01" -?City-SR-01;myFormat
#H3c-Quitway-route-2(地市公司的華三路由)
$template logformat,"%TIMESTAMP% %FROMHOST-IP%%msg%\n"
$template DynFile_HXJF-NE20-01,"/var/log/SR6604_log.%$year%-%$month%-%$day%"
$template DynFile_HXJF-SR6604-01,"/var/log/SR6604_log.%$year%-%$month%-%$day%"
#devide to dir(以文件夾划分)
:rawmsg, contains, "HXJF-NE20-01" ?DynFile_HXJF-NE20-01;
:rawmsg, contains, "HXJF-SR6604-01" ?DynFile_HXJF-SR6604-01; #huawei-sw(地市華為交換機) $EscapeControlCharactersOnReceive off $template City-SW-01,"/var/log/S5756/City-SW-01_log.%$year%-%$month%-%$day%" $template myFormat,"%msg%\n" :rawmsg,contains,"City-SW-01" -?City-SW-01;myFormat
3、采用默認的存儲方式
如果采用默認的方式的話,即以周為輪詢單位,存儲4周。
4、日志備份
不管采取哪種方式,顯然只有一台日志主機的話,會存在單點故障,為了解決這個問題,我們將收集的日志采用定期備份的方式推送到Backup服務器上,我這里使用的是rsync。
思路如下:
通過本地打包備份,rsync應用把日志數據統一備份到一個固定的存儲服務器上,通過腳本檢測並通知管理員備份的結果。
rysnc的有關知識請查閱本博客:https://www.cnblogs.com/daynote/p/9006571.html
5、服務監測
#!/bin/bash #copy by Mr.zhang at 2018-06-08 #This script is used to detect the status of the log server #parameter defined
##定義多個郵箱 MAIL="1888888888@163.com,mangdou@baidu.com" BAKDATE=`date +%c` LOG=`ps -ef | grep rsyslog | wc -l` #check backup result if [ $LOG -ne 4 ] then echo -e "Log site can't be accessed\n ${BAKDATE}" | mail -s "Log Site Down" $MAIL else echo "${BAKDATE} All Log Site is good" &>>/var/log/zhang.log fi
6、定時任務
#每天早上8:30份檢測一遍。
30 08 * * * /bin/sh /server/scripte/check_rsyslog_site.sh &>/dev/null