模拟web服务器产生的日志占满磁盘故障解决方案
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模拟web服务器产生的日志占满磁盘故障解决方案相关的知识,希望对你有一定的参考价值。
生产环境可能你会遇到web服务器产生的log日志文件占满磁盘的现象,下面给出具体的解决方案
安装httpd web服务
[[email protected] ~]# yum install httpd -y
启动
[[email protected] ~]# /etc/init.d/httpd start
测试
[[email protected] ~]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
httpd 1542 root 4u IPv6 12105 0t0 TCP *:http (LISTEN)
httpd 1544 apache 4u IPv6 12105 0t0 TCP *:http (LISTEN)
httpd 1545 apache 4u IPv6 12105 0t0 TCP *:http (LISTEN)
httpd 1546 apache 4u IPv6 12105 0t0 TCP *:http (LISTEN)
安装目录介绍
Apache默认将网站的根目录指向/var/www/html 目录
默认的主配置文件是/etc/httpd/conf/httpd.conf
配置存储在的/etc/httpd/conf.d/目录
[[email protected] html]#cd /var/www/html
设置访问页面
[[email protected] html]# echo "hello web" >/var/www/html/index.html
访问测试
[[email protected] html]# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Date: Wed, 31 Aug 2016 19:31:54 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Wed, 31 Aug 2016 19:30:35 GMT
创建日志文件
[[email protected] html]# mkdir -p /app/logs
定义日志到指定目录
[[email protected] html]# sed -i "[email protected]#CustomLog logs/access_log [email protected] /app/logs/access_log [email protected]" /etc/httpd/conf/httpd.conf
[[email protected]r html]# grep "access_log common" /etc/httpd/conf/httpd.conf
CustomLog /app/logs/access_log common
重启使日志生效
[[email protected] html]# /etc/init.d/httpd restart
创建一个小的文件系统,用于存放上述 access_log 日志
[[email protected] html]# dd if=/dev/zero of=/dev/sdb bs=10k count=100
100+0 records in
100+0 records out
1024000 bytes (1.0 MB) copied, 0.00173305 s, 591 MB/s
格式和挂载
[[email protected] html]# mkfs -t ext4 /dev/sdb
[[email protected] html]# mount -o loop /dev/sdb /app/logs
[[email protected] html]#/etc/init.d/httpd restart
[[email protected] html]# curl -s 127.0.0.1
hello world!!!
[[email protected] html]# cat /app/logs/access_log
127.0.0.1 - - [01/Sep/2016:04:02:23 +0800] "GET / HTTP/1.1" 200 15
写个循环脚本访问 httpd,使得 httpd 日志充满/app/logs 整个空间
for n in `seq 1000000`;do curl -s 127.0.0.1 >/dev/null;done
[[email protected] ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.9G 15G 12% /
tmpfs 429M 0 429M 0% /dev/shm
/dev/sda1 194M 29M 155M 16% /boot
/dev/sda2 2.0G 67M 1.9G 4% /swap
/dev/sdb 979K 909K 20K 98% /app/logs
错误的删除方案
[[email protected] ~]# rm -f /app/logs/access_log
[[email protected] ~]# lsof|grep del #===>还有进程在调用
httpd 3306 root 7w REG 7,0 912339 12 /app/logs/access_log (deleted)
httpd 35409 apache 7w REG 7,0 912339 12 /app/logs/access_log (deleted)
httpd 35828 apache 7w REG 7,0 912339 12 /app/logs/access_log (deleted)
httpd 35829 apache 7w REG 7,0 912339 12 /app/logs/access_log (deleted)
解决方法
1、 请先停掉模拟访问测试脚本
for n in `seq 1000000`;do curl -s 127.0.0.1>/dev/null;done
2、重启 Http 服务
[[email protected] html]# /etc/init.d/httpd restart
查看处理结果
[[email protected] html]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 18G 1.9G 15G 12% /
tmpfs 429M 0 429M 0% /dev/shm
/dev/sda1 194M 29M 155M 16% /boot
/dev/sda2 2.0G 67M 1.9G 4% /swap
/dev/sdb 979K 17K 912K 2% /app/logs
理想的处理方案
清空日志而不删除日志
>/app/logs/access_log
本文出自 “比尔运维笔记” 博客,请务必保留此出处http://chenshoubiao.blog.51cto.com/6159058/1845310
以上是关于模拟web服务器产生的日志占满磁盘故障解决方案的主要内容,如果未能解决你的问题,请参考以下文章