--
logrotate - rotates, compresses, and
R_597_11845@ails system logs
日志轮转 rotate 日志切割
轮转 切割备份归档
/var/log/cron
/var/log/boot.log
/var/log/maillog
/var/log/secure
vim /etc/logrotate.conf --配置文件
# see "man logrotate" for details
# rotate log files weekly
# keep 4 weeks worth of BACklogs
# use date as a suffix of the rotated file
dateext --rHel6里的新参数,表示轮转后,名字后不再以.1,.2这样的结尾,而以时间来结尾,时间格式也可以由dataformat参数来设定
# create new (empty) log files after rotaTing old ones
create --rotate后,创建一个新的代替
# uncomment this if you want your log files compressed
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d --这表示/etc/logrotate.d目录下的所有配置文件都生效
# no packages own wtmp -- we‘ll rotate them here
/var/log/wtmp { --针对wtmp单独的配置,这里是绝对路径
monthly --一个月rotate一次,取代上面的全局配置里的一个星期一次
minsize 1M --指定最小大小为1M,才rotate,就算大小到了,但时间没到,不轮转;如果时间到了,就轮转; 这样看设定这个大小就没意义了;但如果只设定大小,不设定时间就有意义了
create 0664 root utmp --定义创建的为属性为0664,属主为root,属组utmp
}
missingok
monthly
create 0600 root utmp
做原理测试:
把主配置文件里的dateext参数给注释掉,再来做下面的测试;如果不注释,时间也不变的话,就不会轮转出4个文件出来,因为名字一样
# logrotate -f /etc/logrotate.conf --强制轮转,然后可以看到/var/log/下很多时间都开始轮转了
例5,配置/var/log/ssh日志每星期轮转一次,保留2份备份日志,并且最小要2M才轮转,忽略文件不存在的情况
# vim /etc/logrotate.conf --加上下面一段
或者上面那段不写到主配置里,建一个子配置文件,把上面那段加进去,也是一样的
# logrotate -f /etc/logrotate.conf --使用此命令去验证
================================
vim /etc/logrotate.d/syslog
/var/log/messages /var/log/secure /var/log/maillog /var/log/spooler /var/log/boot.log /var/log/cron { --对哪些日志文件进行定义
sharedscripts --脚本开始
postrotate --表示rotate结束
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
/bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true
endscript --脚本结束
--上面的/bin/kill -HUP的这两句其实就是做了/etc/init.d/syslog reload
--为什么轮转之后要reload,因为如果aaa.log轮转成aaa.log.1,又新create一个aaa.log,如果不reload的话,则新的日志还是会记录到aaa.log.1里去,只有reload后,才会记录到新的aaa.log里
结构如下:
sharedscripts
prerotate
endscript
postrotate
@H_705_19
@may 8 15:33:02 li aa: bb --可以看到刚才加的
标记
例七:
/bin/logger -t ‘rotate‘ ‘end‘
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
--在上面例六的基础上,把/bin/logger -t ‘rotate‘ ‘end‘这句放到kill -HUP两句的前面
再logrotate -f /etc/logrotate.conf强制轮转一下
发现/var/log/messages里没有轮转前后的信息
==============================
进程知识点补充:
-9是参数信号量,以后学到的比较大型的软件的文档都会有关于软件本身的各种信号量的说明
比如:
kill -HUP (pid) --一般指刷新进程,reload
kill -USR1 (pid) --类似HUP
kill -TERM (pid) --强制杀掉进程
实验通过对httpd软件的进程给不同的信号来理解信号的作用
# /etc/init.d/httpd start
# ps -ef |grep httpd |grep -v grep --找到它的父PID
再次启动
# kill -HUP 父PID --发现所有的子PID都会变化,也就是刷新(相当于就是/etc/init.d/httpd reload)
--可以使用下面的两条命令都可以查出httpd有关的进程
# kill -9 4205 4206 .......
# ps -ef |grep httpd |grep -v grep|awk ‘{print $2}‘|xargs kill -9
# kill -TERM 父PID --对比kill -9 父PID
--区别:以httpd服务为例,kill -9 父PID后,子进程还在,web仍然可以被访问,并且它们的父进程变为1了。你再使用service httpd start|stop 都会报错
--kill -TERM 父PID,所有的子进程都没了,web不能被访问了
================================================================
结合crontab实现按时间(每天)的日志切割
#!/bin/bash
year=`date -d ‘-1 day‘ +%Y`
@H_705_19
@month=`date -d ‘-1 day‘ +%m`
day=`date -d ‘-1 day‘ +%d`
@H_705_19
@mkdir /var/log/
httpd/$year/$month -p
@H_705_19
@mv /var/log/
httpd/access_log /var/log/
httpd/$year/$month/access_log-$day
touch /var/log/httpd/access_log
kill -HUP `cat /var/run/httpd/httpd.pid`
--然后把执行这个脚本的语句,放到crontab里每天晚上凌晨01分做
==================================
# /etc/init.d/postfix restart
# netstat -ntlup |grep :25
tcp 0 0 0.0.0.0:25 0.0.0.0:* LISTEN 4368/master
tcp 0 0 :::25 :::* LISTEN 4368/master
i am root
haha --正文
. -- 点表示结束
EOT
# cat /var/mail/user1 --邮件在这里
关于@H_705_19@mail,mailfirst,maillast这几个logrotate自带的发邮件的参数的讨论
先把/etc/logrotate.d/syslog里的配置还原
# echo > /var/mail/root --清空邮件,方便查找(这一步可选,非必要)
我以前面做的/var/log/ssh这个来配置做示例
# vim /etc/logrotate.conf
# logrotate -f /etc/logrotate.conf
# cat /var/mail/root
Subject: /var/log/ssh.3 --主题为轮转不存在的那个日志
--------
@H_705_19
@mailfirst
--再加@L_724_4
@mailfirst
Subject: /var/log/ssh.1 --主题为just-rotate的日志
---------------
@H_705_19
@maillast
--换成maillast
Subject: /var/log/ssh.3 --主题为轮转不存在的那个日志,结果和什么都不加是一样的,是默认值
# vim /etc/logrotate.d/ssh --对ssh这个子配置文件进行邮件通知
sharedscripts
postrotate
kill -HUP `cat /var/run/sshd.pid`
echo "ssh.2 is gone!,ssh.1 变成了ssh.2,ssh 变成 了ssh.1 " |mail -s ‘ssh日志轮转了‘ user1
endscript
=============================================================
rsync 同步
rsync remote sync 远程同步 比wget要好
rsync - faster,flexible replacement for rcp
rsync — a fast,versatile,remote (and local)
Wget - The non-interactive network down-loader.
wget http://127.0.0.1/ule07.txt --直接接url uniform resource locate(统一资源定位)
# wget -m -k -np http://10.1.1.35/ule
rsync 特点:
2,容易做到保存原有的权限,时间(修改时间,modify timE),软硬链接
rsync 参数介绍
-v verbo
-R 保留相对路径
-p,-o,-g,-A 分别保留权限,属主,属组,acl等,但如果加了-a,这些就都包括了
-D 等于--devices --specials 表示支持b,c,s,p类型的文件
-e ‘ssh –p 22’ 通过ssh协议安全同步
普通应用:
rsync -av /home/ 10.1.1.218:/BACkup --把本地的/home目录内容,同步到远端218的/BACkup目录下
rsync -av 10.1.1.218:/BACkup/ /BACkup/ --把远端218的/BACkup目录下的内容同步到本地的/BACkup目录
1。 同步时保留原路径,也就是把/home同步到/BACkup会变成/BACkup/home/*再内容
下面这三个有区别:
rsync -av /home/ /BACkup/
rsync -aRv /home/ /BACkup/
--这两上是/home被同步成/BACkup/home/*
# rsync -av /share/120214 /BACkup/
# rsync -aRv /share/120214/ /BACkup/
--被同步成/BACkup/share/120214/*
2。如果源目录有文件被@L_197_88@了,那同步的目录是@L_197_88@还是不@L_197_88@,是否可以用参数来控制?
思考:能否把上面的rsync做成服务的形式,并加上一些控制功能或日志记录功能呢?
@H_705_19
@man rsyncd.conf
服务器端的配置:
1,第一步:
vim /etc/rsyncd.conf
@H_705_19
@motd file=/etc/rsyncd.welcome
[abc]
path=/test
vim /etc/rsyncd.welcome --手动建立自己定义的motd文件,并写上欢迎信息
2,第二步:
vim /etc/xinetd.d/rsync --修改服务启动脚本
disable = no --yes改为no 打开rsync服务功能
第三步:
/etc/init.d/xinetd restart --重启服务
netstat -ntlup |grep 873 --服务端就会有873的端口在监听了
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 4080/xinetd
客户端的操作:
# rsync -v 10.1.1.35:: --直接这样可以查看35这个服务器共享了哪些
# rsync -avlR 10.1.1.35::abc /root/Desktop/
--这样同步,是不需要密码的;注意abc为共享名,前面有两个::
--(这里要求使用man rsyncd.conf的参数来实现,而不是man xinetd.conf使用super daemon的参数来实现)
要求:
1。把日志记录到/var/log/rsyncd.log
2。共享模块要求隐藏(也就是说客户端查看不到这个模块名) 提示:参数list
3。并且同时只能1个客户端连接进行同步这个module 提示:参数max connections
4。只能允许192.168.20.123同步这个module :参数hosts allow
[@L_874_102@ /]# vim /etc/rsyncd.conf
log file=/var/log/rsyncd.log
path=/share/weekend4
@H_705_19
@max connections=1
hosts allow=192.168.20.123
rsync的补充:
实现实时同步(也就是源一变化,它就会触发同步)
inotify+rsync
sersync
软件路径
/share/soft/inotify-tools-3.13.tar.gz
# tar xf /share/soft/inotify-tools-3.13.tar.gz -C /usr/src/
# cd /usr/src/inotify-tools-3.13/
# ./configure ;make ;make install
--安装完后,就会产生下面两个命令
/usr/local/bin/inotifywait
/usr/local/bin/inotifywatch
做测试:
测试本机/test目录里一有(增,删,改,属性改变),就会同步到/test3目录
#vim /tmp/1.sh
/usr/local/bin/inotifywait -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f%e‘ -e modify,delete,create,attrib /test |while read files
rsync -a --delete /test/ /test3/
echo "$files 被同步" >> /var/log/rsync.log 2>&1
done
#sh /tmp/1.sh &
再然后对/test进行各种操作,验证可以实时同步到/test3
--实现双向的实时同步
但我们就可以用inotify反向再做一次也一样可以简单去实现
==================================================
一:要求
1,使用两台虚拟机和真实机一起做ssh等效性,要求互相ssh不需要密码
2,真实机做日志管理服务器,在/var/log/clientssh.log里记录两台虚拟机的ssh信息
3,真实机对/var/log/clientssh.log配置日志轮转,
要求:(1.)配置到/etc/logrotate.conf文件里或者配置到/etc/logrotate.d/目录里的子配置文件
(2.)每天轮转一次
(3.)最多保留4个副本
4,真实机在对/var/log/clientssh.log的日志轮转之前自动把/var/log/clientssh.log
的最后一个副本(也就是/var/log/clientssh.log.4)文件给cp到/BACkup/4天前的年/4天前的月/4天前的日期.clientssh.log的格式
(比如:今天为2011-04-28,cp的文件名就是/BACkup/2011/04/24.clientssh.log)
prerotate
cp /var/log/clientssh.log /var/log/`date -d ‘-4 day‘ %F`.clientssh.log
5,真实机配置每月1号的凌晨3点打包/BACkup目录(格式自定),并自动scp到两台虚拟机的/BACkup目录下
--提示,配置了ssh等效性的两台机器,scp文件东西不需要输入密码
6,真实机在每月四号(时间应该在当前日志轮转之后,一般logrotate默认
被定义在凌晨4点02分,所以这个时间定义在4点02分之后)对上个月的所有日志进行结合分析,只需要分析下面两句结果,并把邮件发给root就可以)
linux下有两种比较流行的开源日志分析工具:
awstats
webalizer
--上面两个工具就可以直接帮你做日志合并
如果不使用工具的话,要做日志合并,也可以使用下面的方法:
2,sort -m -o all.log 1.log 2.log 3.log
--关于日志切割和合并的总结:
logrotate
写脚本
使用cronolog工具 --在讲apache时做
sort
使用日志分析工具awstats,webalizer
7,搭建rsyncd服务,使用它自带的日志,因为日志里的时间比我们现在北京时间要晚8个小时,请写一个脚本对它处理成北京时间
# cat rsycd.log
2012/11/19 00:41:12 [7653] rsync on weekend/ from li.cluster.com (172.16.2.35)
2012/11/19 01:41:12 [7653] building file list
2012/11/19 02:41:27 [7653] sent 466382582 bytes received 27709 bytes @R_385_10586@l size 466234513
2012/11/19 05:43:05 [7686] rsync on weekend/ from li.cluster.com (172.16.2.35)
2012/11/19 08:43:05 [7686] building file list
2012/11/19 18:43:11 [7686] sent 466382582 bytes received 27709 bytes @R_385_10586@l size 466234513
# cat rsycd.log |sed -r ‘s/:/ /g‘|awk ‘$2<=1 {print $1" 0"$2+8":"$3":"$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14} $2>1 && $2<16 {print $1" "$2+8":"$3":"$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14} $2>=16 {print $1" 0"$2+8-24":"$2":"$3":"$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14}‘
2012/11/19 08:41:12 [7653] rsync on weekend/ from li.cluster.com (172.16.2.35)
2012/11/19 09:41:12 [7653] building file list
2012/11/19 10:41:27 [7653] sent 466382582 bytes received 27709 bytes @R_385_10586@l size 466234513
2012/11/19 13:43:05 [7686] rsync on weekend/ from li.cluster.com (172.16.2.35)
2012/11/19 16:43:05 [7686] building file list
2012/11/19 02:18:43:11 [7686] sent 466382582 bytes received 27709 bytes @R_385_10586@l size 466234513
=========================================================
touch /var/log/newrsyncd.log
sed ‘‘ /var/log/rsyncd.log | while read message
do
time=`echo $message | awk ‘{print $1" "$2}‘`
newtime=`date +%Y-%m-%d" "%k:%M:%s -d "$time 8 hour"`
if [ `echo $newtime | awk ‘{print $2}‘ | awk -F: ‘{print $1}‘` -le 9 ]
then
newtime=`echo $newtime | awk ‘{print $1" 0"$2}‘`
fi
echo $newtime" ["$newmessage >> /var/log/newrsyncd.log
@H_705_19
@mv -f /var/log/newrsyncd.log /var/log/rsyncd.log