Web服务器磁盘满故障深入解析

转载自:  http://oldboy.blog.51cto.com/2561410/612351

#########################################################
# 硬盘显示被写满但是用du -sh /*查看时占用硬盘空间之和还远
#小于硬盘大小问的解决
#date:2010-06-09
#作者:老男孩---《老男孩linux就业培训中心 》
#QQ:31333741 MAIL:[email protected]
#QQ交流群:45039636    
#blog: http://oldboy.blog.51cto.com
#psite: http://oldboy.cc(即将开放)
#pblog:http://blog.etiantian.org
##########################################################


问题:硬盘显示被写满,但是用du -sh /*查看时占用硬盘空间之和还远小于硬盘大小
即找不到硬盘分区是怎么被写满的。

今天下午接到一学生紧急求助,说生产线服务器硬盘满了。该删的日志都删掉了。可空间还是满的,情况危急啊。这个问题,在多年以前直接和间接的遇到过3-4次。以前太懒惰了,这次记录下来和大家分享。


相关日志如下:

█ 查看硬盘分区大小
[root@www ~]# cat /etc/redhat-release 
CentOS release 5.3 (Final)
[root@www ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             117G  111G     0 100% /
/dev/sda1             145M   12M  126M   9% /boot
tmpfs                 3.0G     0  3.0G   0% /dev/shm
这是一台web服务器(apache+tomcat+mysql)
通过dh -h命令查看 总大小117G,使用了111G
[root@www /]# fdisk -l

Disk /dev/sda: 146.8 GB, 146815733760 bytes
255 heads, 63 sectors/track, 17849 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          19      152586   83  Linux
/dev/sda2              20        2108    16779892+  82  Linux swap / Solaris
/dev/sda3            2109       17849   126439582+  83  Linux
[root@www /]# fdisk -l /dev/sda3

Disk /dev/sda3: 129.4 GB, 129474132480 bytes
255 heads, 63 sectors/track, 15741 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sda3 doesn't contain a valid partition table
==========================================================================================
█ 使用du命令查看到底哪些目录占用了空间
[root@www /]# du -sh /*
8.6M    /bin
6.1M    /boot
2.8G    /data ===》这里是DB目录
124K    /dev
60M     /etc
1.6G    /home ===》这里是日志目录
120M    /lib
24M     /lib64
16K     /lost+found
8.0K    /media
0       /misc
12K     /mnt
0       /net
8.0K    /opt
0       /proc
614M    /root
35M     /sbin
8.0K    /selinux
24K     /server
8.0K    /srv
0       /sys
24K     /tmp
2.5G    /usr
29G     /var  ==》这里是www目录

可以看出,上面所有目录之和 远小于总的空间占用111G,是什么占用了硬盘,还查不到呢?
---------------
解答:
出现上面问题原因:
在apache/tomcat服务在运行状态下,清空了运行服务的日志,这里是清理了当天或正在写入的apache及tomcat的日志文件,从而导致了上面问题。(有关原理细节见下文)

引申下:
一般情况下,大多数服务(包括脚本)在运行时,是不能删除当前正在写入的日志文件的。这点请大家要记牢。

本文的解决办法:
查找机器自身的服务,然后重起apache和tomcat。
重起apache后:
[root@www 07]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             117G  109G  2.3G  98% /
/dev/sda1             145M   12M  126M   9% /boot
tmpfs                 3.0G     0  3.0G   0% /dev/shm

重起tomcat后:
[root@www ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3             117G   38G   74G  34% /
/dev/sda1             145M   12M  126M   9% /boot
tmpfs                 3.0G     0  3.0G   0% /dev/shm
 

 

删除文件原理图:

1
老男孩运维班28期上述案例实战模拟:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
(1)安装httpd web服务
   yum  install  httpd -y
   /etc/init .d /httpd  start
   lsof  -i :80
   /etc/init .d /iptables  stop
   cd /etc/httpd/conf  编辑配置文件,让日志记录到 /app/logs 下面。
sed  -i 's@#CustomLog logs/access_log common@CustomLog /app/logs/access_logcommon@g'  httpd.conf 
  (2)创建一个小的文件系统,用于存放上述access_log日志。
   dd  if = /dev/zero  of= /dev/sdc  bs=8K  count=10
   ls  -l  /dev/sdc
   mkfs -t ext4  /dev/sdc
   tune2fs -c -1  /dev/sdc
   mount  -o loop  /dev/sdc  /app/logs
   echo  oldboy > /var/www/html/index .html
  (3)重启httpd服务,确保日志记录到了上述文件系统挂载的 /app/log 下面
   /etc/init .d /httpd  restart
  (4)写个循环脚本访问httpd,使得httpd日志充满 /app/logs 整个空间。
  for  in  ` seq  100000`; do  curl -s 127.0.0.1> /dev/null ; done
[root@C64log] # df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              7.2G  2.0G 4.9G  30% /
tmpfs                 244M     0 244M   0%  /dev/shm
/dev/sda1              194M   54M 131M  30%  /boot
/dev/sdc                73K   73K    0 100%  /app/log
      (5)错误的删除方案
[root@oldboylogs] # rm -f /app/logs/access_log
[root@oldboylogs] # df -h
Filesystem      Size Used Avail Use% Mounted on
/dev/sda3        8.8G 1.6G  6.9G  19% /
tmpfs           491M     0 491M   0%  /dev/shm
/dev/sda1        190M  36M  145M  20%  /boot
/dev/sdc          73K  68K  1.0K  99%  /app/logs
提示:此时空间并未被释放,你可知道原因?
     查看被删除的但仍由进程占用的文件名。
[root@oldboylogs] # lsof|grep del
httpd      6148     root    7w      REG   7,0    55260         12  /app/logs/access_log  (deleted)
httpd     38178   apache    7w      REG   7,0    55260         12  /app/logs/access_log  (deleted)
httpd     38483   apache    7w      REG   7,0    55260         12  /app/logs/access_log  (deleted)
httpd     38484   apache    7w      REG   7,0    55260         12  /app/logs/access_log  (deleted)
httpd     38752   apache    7w      REG   7,0    55260         12  /app/logs/access_log  (deleted)
  (5)解决问题
1、请先停掉模拟访问测试脚本
forn  in  ` seq  100000`; do  curl -s 127.0.0.1 > /dev/null ; done
2、重启Http服务
[root@C64log] # /etc/init.d/httpd restart
Stoppinghttpd:                                           [  OK  ]
Startinghttpd:                                          [ OK  ]
  (6)查看处理结果
[root@C64log] # df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda3              7.2G  2.0G 4.9G  30% /
tmpfs                 244M     0 244M   0%  /dev/shm
/dev/sda1              194M   54M 131M  30%  /boot
/dev/sdc                73K   14K  55K  21% /app/logs
(7)较好的处理方案
清空日志而不删除日志。
> /app/logs/access_log

 

猜你喜欢

转载自hongtoushizi.iteye.com/blog/2314299