Centos6.5部署Rsyslog-日志的存储方式及监测服务状态
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
生活的一大乐趣就是
完成别人认为你不能做到的事情