诡异的磁盘空间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);
}
诡异的磁盘空间100%报警分析得出df -h与du -sh的根本性差别的更多相关文章
- linux shell脚本检测硬盘磁盘空间 邮件报警
使用 http://www.weiruoyu.cn/?p=368 shell脚本监控硬盘空间剩余空间 邮件报警 1.先观察一下磁盘,和如何使用脚本 [root@localhost ~]# df -h ...
- (转)Linux服务器磁盘空间占满问题
转自:https://www.cnblogs.com/cindy-cindy/p/6796684.html 下面我们一起来看一篇关于Linux服务器磁盘占满问题解决(/dev/sda3 满了),希望碰 ...
- 关于Linux服务器磁盘空间占满问题的解决方法
下面给大家分享一篇关于Linux服务器磁盘占满问题解决方法(/dev/sda3 满了),需要的的朋友参考下吧 下面我们一起来看一篇关于Linux服务器磁盘占满问题解决(/dev/sda3 满了), ...
- Linux服务器磁盘空间占满问题
下面我们一起来看一篇关于Linux服务器磁盘占满问题解决(/dev/sda3 满了),希望碰到此类问题的人能带来帮助. 今天下班某电商技术部leader发现个问题,说他们服务器硬盘满了.把日志文件 ...
- Linux磁盘空间爆满,MySQL无法启动
OS: Cent OS 6.3 DB: 5.5.14 看到一个帖子,在服务器上安装了oracle和mysql数据库,mysql数据库忘记开启innodb_file_per_table,导致插入测试数据 ...
- du和df显示磁盘空间使用差异大的几种情况
接触客户多了,发现经常有客户遇到du看到系统只使用了一部分磁盘空间,但df看时磁盘空间已经满了,或者差异较大,我遇到过三次不一样的原因,下面做一下总结,恶补一下!也希望对大家有点帮助! 现象:有客户 ...
- linux 小技巧(磁盘空间搜索)
这里记录一些linux 管理中可能会用到的又容易忘的一些小技巧. linux磁盘写入失败,提示磁盘空间不足.一般都会用df -h 或者df -i看是不是磁盘空间不足或者是inode空间不足.发生这种情 ...
- 【Linux命令】删除大文件后磁盘空间未释放问题
前言 工作中经常遇到Linux系统磁盘空间不足,但是删除后较大的日志文件后,发现磁盘空间仍没有被释放,有点摸不着头脑,今天博主带大家解决这个问题. 思路 1.工作发现磁盘空间不足: 2.找到占用磁盘空 ...
- Linux磁盘空间不足处理方法
维护Linux服务器正常使用需要经常删除Linux系统运行产生的系统日志和业务环境产生的debug日志文件.安装包等.本文主要描述如何通过脚本实现清理业务环境产生的 debug日志文件和上传或备份的打 ...
随机推荐
- RabbitMQ 均衡调度(公平分发机制)
均衡调度是针对Consumer来说的.现在有两个Consumer请求同一个队列的消息.RabbitMQ会将序号为奇数的消息发给第一个Consumer,会将序号为偶数的消息发送给第二个Consumer. ...
- list的四种遍历方式
1.手先增强for循环和iterator遍历的效果是一样的,也就说 增强for循环的内部也就是调用iteratoer实现的,但是增强for循环 有些缺点,例如不能在增强循环里动态的删除集合内容.不能获 ...
- Java小程序分析
public class Helloworld { public static void main(String[] args) { System.out.println("hello wo ...
- Linux系统管理常用命令用法总结(1)
1.usermod可用来修改用户帐号的各项设定. usermod [-LU][-c <备注>][-d <登入目录>][-e <有效期限>][-f <缓冲天数& ...
- UI控件Telerik UI for WinForms发布R1 2019|附下载
Telerik UI for WinForms拥有适用Windows Forms的110多个令人惊叹的UI控件.所有的UI for WinForms控件都具有完整的主题支持,可以轻松地帮助开发人员在桌 ...
- android 自定义命名空间 http://schemas.android.com/apk/res-auto
XML中用 xmlns="http://schemas.android.com/apk/res-auto" 获取自定义属性值: public static String NAMES ...
- js基础 三种弹出框 数据类型
总结:js三个组成部分ES:语法DOM:对象模型 => 通过js代码与页面文档(出现在body中的所有可视化标签)进行交互BOM:对象模型 => 通过js代码与浏览器自带功能进行交互 引入 ...
- [转]Introduction to Learning to Trade with Reinforcement Learning
Introduction to Learning to Trade with Reinforcement Learning http://www.wildml.com/2018/02/introduc ...
- Python 笔试 —— 效率与优雅
1. 效率 字符串拼接: 加号拼接字符串将造成对象的创建和垃圾的回收: 使用字符串的 join 方法对尤其是循环中的字符串进行拼接(先将不断出现的字符串 append 到 一个 list 中,再进行 ...
- pprof进行golang程序性能分析
一.导入包 import _ "net/http/pprof" 二.启动监控routine go func() { http.ListenAndServe("0.0.0. ...