诡异的磁盘空间100%报警分析得出df -h与du -sh *的根本性差别

前言:
早晨磁盘报警刚清空完tomcat和nginx日志,使用的命令是类似echo "" > show_web-error.log或者> show_web-debug.log清空语句,然后rm -rf 掉一些tar.gz包,空出来30G空间。 而且也关闭了tomcat的debug信息。刚刚又接到报警,磁盘100%了。怎么回事?

1,进去df -h下,确实100%了,如下所示:
[root@localhost ~]# df -h
文件系统              容量  已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
                      113G  113G     0 100% /
/dev/sda1              99M   13M   82M  14% /boot
tmpfs                 8.8G     0  8.8G   0% /dev/shm
确实已经100%了,再去/去检查

2,去/根目录check,du -sh *
[root@localhost ~]# cd /
[root@localhost /]# du -sh *
7.8M bin
6.9M boot
131M data
196K dev
111M etc
178M home
131M lib
23M lib64
119M logs
16K lost+found
8.0K media
0 misc
8.0K mnt
0 net
0 nohup.out
3.8G opt
15M pcre-8.33
2.1M pcre-8.33.zip
du: 无法访问 “proc/11575/task/11575/fd/1565”: 没有那个文件或目录
du: 无法访问 “proc/15403/task/14464/fd/625”: 没有那个文件或目录
0 proc
1.4G product
153M repo
143M root
37M sbin
8.0K selinux
363M soft
8.0K srv
0 sys
20K temp
100K tftpboot
2.1G tmp
8.6G usr
184M var
30M varnish-3.0.3
56M zabbix-2.0.8
[root@localhost /]# 
看到,占据的磁盘空间所有的加起来也不到30G,可是df -h下来,确实100%呢?差异在哪里?

3,baidu,google资料,找到 http://www.chinaunix.net/old_jh/6/465673.html 里面有这么2段话:
(1):

When you open a file, you get a pointer.  Subsequent writes to this file 
references this file pointer.  The write call does not check to see if the file 
is there or not.  It just writes to the specified number of characters starting 
at a predetermined location.  Regardless of whether the file exist or not, disk 
blocks are used by the write operation. 

The df command reports the number of disk blocks used while du goes through the 
file structure and and reports the number of blocks used by each directory.  As 
far as du is concerned, the file used by the process does not exist, so it does 
not report blocks used by this phantom file.  But df keeps track of disk blocks 
used, and it reports the blocks used by this phantom file.
以及leolein朋友的回复:
谢谢,就是这个原因。 
我因为磁盘快满了就删除了一些过期的文件,可能应用程序还在使用这些文件句柄,所以导致了我说的问题。 
我把所有的应用程序都停止后,du和df的结果就大致相同了

(2):
This section gives the technical explanation of why du and df sometimes report
different totals of disk space usage.

When a program that is running in the background writes to a file while the
process is running, the file to which this process is writing is deleted.
Running df and du shows a discrepancy in the amount of disk space usage.  The
df command shows a higher value.

如果文件已经删除了,但是还有残留的进程引用它(具体不知道怎么表达好),则df看到的空间使用量并没有减去那些已经删除的文件。而创建并写入一个文件是,判断空间是否足够是依据df(本人认为),所以df 100%的时候就不能写入文件了。--但是创建文件是可以的,我做过测试。查看这些残留进程(姑且这么称呼,我也不知道那些进程叫什么)的方法是lsof
# lsof /home | grep /home/oracle/osinfo | sort +8 | grep '^.*070920.*$'
sadc    17821   root    3w   REG  253,1 326492112 926724 /home/oracle/osinfo/070920sar.data (deleted)
sadc    17861   root    3u   REG  253,1 326492112 926724 /home/oracle/osinfo/070920sar.data (deleted)
sadc    17981   root    3u   REG  253,1 326492112 926724 /home/oracle/osinfo/070920sar.data (deleted)
top     17858   root    1w   REG  253,1 169919916 927111 /home/oracle/osinfo/070920top.data (deleted)
top     17977   root    1w   REG  253,1 169919916 927111 /home/oracle/osinfo/070920top.data (deleted)
注意后面的deleted
然后把这些进程都kill掉就可以释放空间了。

我想起了,我早晨在执行echo "" >shop_web.log类似操作的时候,并没有停止tomcat应用, 所以应用是一直往log里面写数据的,那么我>的那一刻,是du -sh *可能看到磁盘空间有了,df -h也可以看到磁盘释放了,但是当tomcat应用继续往shop_web.log里面写日志的时候,加载的还是最初打开的那个执行>shop_web.log之前的占据很大磁盘空间的缓存文件。所以磁盘其实一直没有释放掉,而能坚持一天不报警,是由于我rm了一些tar.gz包所释放的空间。


4,重启tomcat和nginx应用
所以,我应该重启tomcat和nginx,应用不再加载旧的缓存文件,执行重启tomcat命令,由于tomcat应用比较多,所以写了一个脚本来执行
[root@localhost local]# cat /root/start_tomcat_port.sh 
#!/bin/bash
PID=`ps -eaf|grep apache-tomcat-6.0.37_$1 |grep -v grep |grep -v start_tomcat_port |awk '{print $2}'`
echo $1
echo $PID
kill -9 $PID
rm -rf /var/tomcat/$1.pid
/usr/local/apache-tomcat-6.0.37_$1/bin/startup.sh
[root@localhost local]#
执行重启tomcat:
sh /root/start_tomcat_port.sh 6100;
sh /root/start_tomcat_port.sh 6200;
sh /root/start_tomcat_port.sh 6300;
sh /root/start_tomcat_port.sh 6400;
sh /root/start_tomcat_port.sh 6500;
sh /root/start_tomcat_port.sh 6700;
sh /root/start_tomcat_port.sh 7100;
sh /root/start_tomcat_port.sh 7200;
sh /root/start_tomcat_port.sh 7300;
执行重启nginx:
service nginx restart

5,再去check下磁盘空间
[root@localhost local]# df -h
文件系统              容量  已用 可用 已用% 挂载点
/dev/mapper/VolGroup00-LogVol00
                      113G   18G   90G  17% /
/dev/sda1              99M   13M   82M  14% /boot
tmpfs                 8.8G     0  8.8G   0% /dev/shm
[root@localhost local]# 

看到df -h命令正常了,已经释放了90G的磁盘空间,现在磁盘使用率才17%,nagios报警解除了。


6,汇总一些原理分析
实现原理:
du -s命令通过将指定文件系统中所有的目录、符号链接和文件使用的块数累加得到该文件系统使用的总块数;
df命令通过查看文件系统磁盘块分配图得出总块数与剩余块数。
du是用户级程序,不考虑Meta Data(系统为自身分配的一些磁盘块)

ps:应用程序打开的文件句柄没有关闭的话,会造成df命令显示的剩余磁盘空间少。而du则不会。
例子:
#include <unistd.h>   
#include <stdlib.h>   
#include <stdio.h>   
#include <fcntl.h>   
  
int main(int argc,char **argv)  
{  
    if(open("tempfile",O_RDWR) < 0){  
        fprintf(stderr,"open error");  
        exit(-1);  
    }  
  
    if(unlink("tempfile") < 0){  
        fprintf(stderr,"unlink error");  
        exit(-1);  
    }  
  
    printf("file unlinked\n");  
    sleep(15);  
    printf("done\n");  
    exit(0);  
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值