当前位置: 移动技术网 > 网络运营>服务器>Linux > Linux 查看空间使用情况的实例详解

Linux 查看空间使用情况的实例详解

2019年04月22日  | 移动技术网网络运营  | 我要评论

linux 查看空间使用情况的实例详解

在日常的linux巡检中,我们会遇到文件系统目录使用空间很高的情况,例如如下利用”df -h “查看到根目录空间使用超过80%。而我们仅仅知道是根目录空间使用过高,这样是不够的。还需要知道是目录还是文件让根目录空间使用过高。通常我们使用的命令是”du -sh *”。

第一步:查看linux系统的文件系统使用情况,如下可以看到根目录”/”已经使用81%。


[root@hostname ~]# df -h
filesystem      size used avail use% mounted on
/dev/sda6       67g  51g  13g 81% /
tmpfs         16g 152k  16g  1% /dev/shm
/dev/sda3       99g 188m  94g  1% /arch
/dev/sda1       197g 188m 187g  1% /u01
/dev/sda2       197g  50g 138g 27% /u02


第二步:进入根目录,利用du -sh 命令查询各个目录或者文件占用空间的情况,因为/u02是独立的文件系统,所以可以忽略。我们关注的是root这个目录,占用了空间47g。

[root@hostname ~]# cd /

[root@hostname /]# du -sh *
20k   arch
7.6m  bin
27m   boot
324k  dev
36m   etc
36k   home
136m  lib
26m   lib64
16k   lost+found
4.0k  media
0    misc
4.0k  mnt
0    net
8.0k  opt
4.0k  orbit-root
--省略/proc目录统计的报错
0    proc
47g   root  ##根目录文件系统使用最大的目录
15m   sbin
0    selinux
4.0k  srv
0    sys
80k   tmp
20k   u01
49g   u02

第三步:进入root目录,其实也是root用户的主目录。我们从ls命令可以猜到可能是【apache-tomcat-6.0.36-linux】这个tomcat的程序目录占用比较多。

[root@hostname /]# cd root
[root@hostname ~]# ls
anaconda-ks.cfg apache-tomcat-6.0.36-linux desktop documents downloads install.log install.log.syslog music pictures public templates videos

第四步:通过统计发现,确实是【apache-tomcat-6.0.36-linux】这个目录占用了47g。但是到此问题还没有结束,因为既然是应用程序的目录,根据我的经验很可能是tomcat的日志文件占用空间比较多。

[root@hostname ~]# du -sh *
4.0k  anaconda-ks.cfg
47g   apache-tomcat-6.0.36-linux
4.0k  desktop
4.0k  documents
4.0k  downloads
56k   install.log
12k   install.log.syslog
4.0k  music
4.0k  pictures
4.0k  public
4.0k  templates
4.0k  videos

第五步:既然看到有tomcat的目录,很可能这个tomcat运行着,利用ps命令查看是否有tomcat的进程,一般可以过滤java这个关键词。如下,果然有tomcat在运行着。那么很可能占用空间最多就是tomcat的日志文件。

[root@hostname ~]# ps -ef|grep java
root   4879 4836 0 08:26 pts/0  00:00:00 grep java
root   11864   1 47 aug16 ?    26-03:11:29 /usr/bin/java -djava.util.logging.config.file=/root/apache-tomcat-6.0.36-linux/conf/logging.properties -xms512m -xmx1024m -xx:permsize=128m -xx:maxpermsize=256m -djava.util.logging.manager=org.apache.juli.classloaderlogmanager -djava.endorsed.dirs=/root/apache-tomcat-6.0.36-linux/endorsed -classpath /root/apache-tomcat-6.0.36-linux/bin/bootstrap.jar -dcatalina.base=/root/apache-tomcat-6.0.36-linux -dcatalina.home=/root/apache-tomcat-6.0.36-linux -djava.io.tmpdir=/root/apache-tomcat-6.0.36-linux/temp org.apache.catalina.startup.bootstrap start

第六步:进入tomcat的程序目录,查看哪个目录占用的空间最大。如下,发现是一个【fare】的目录占用了46g,而【logs】是日志目录却占用了23m。

[root@hostname ~]# cd apache-tomcat-6.0.36-linux
[root@hostname apache-tomcat-6.0.36-linux]# ls
backup bin conf fare lib license logs notice release-notes running.txt temp webapps work
[root@hostname apache-tomcat-6.0.36-linux]# du -sh *
92k   backup
882m  bin
108k  conf
46g   fare
6.8m  lib
40k   license
23m   logs
4.0k  notice
12k   release-notes
16k   running.txt
7.9m  temp
188m  webapps
7.1m  work

第七步:继续一探究竟【fare】目录里面到底存放的是什么类型的目录或者文件。如下看到有很多类似日志文件,如common.log和pricing.log。通过和开发人员沟通之后,确定确实日志文件。

[root@hostname apache-tomcat-6.0.36-linux]# cd fare
[root@hostname fare]# ls
common.log       common.log.2017-09-21 common.log.2017-10-03   pricing.log.2017-09-10 pricing.log.2017-09-22 pricing.log.2017-10-04
common.log.2017-09-10 common.log.2017-09-22 common.log.2017-10-04   pricing.log.2017-09-11 pricing.log.2017-09-23 pricing.log.2017-10-05
common.log.2017-09-11 common.log.2017-09-23 common.log.2017-10-05   pricing.log.2017-09-12 pricing.log.2017-09-24 pricing.log.2017-10-06
common.log.2017-09-12 common.log.2017-09-24 common.log.2017-10-06   pricing.log.2017-09-13 pricing.log.2017-09-25 pricing.log.2017-10-07
common.log.2017-09-13 common.log.2017-09-25 common.log.2017-10-07   pricing.log.2017-09-14 pricing.log.2017-09-26 pricing.log.2017-10-08
common.log.2017-09-14 common.log.2017-09-26 common.log.2017-10-08   pricing.log.2017-09-15 pricing.log.2017-09-27 pricing.log.2017-10-09
common.log.2017-09-15 common.log.2017-09-27 common.log.2017-10-09   pricing.log.2017-09-16 pricing.log.2017-09-28 spring.log
common.log.2017-09-16 common.log.2017-09-28 common.zip        pricing.log.2017-09-17 pricing.log.2017-09-29 struts2.log
common.log.2017-09-17 common.log.2017-09-29 framework.log       pricing.log.2017-09-18 pricing.log.2017-09-30 xwork2.log
common.log.2017-09-18 common.log.2017-09-30 hibernate.log       pricing.log.2017-09-19 pricing.log.2017-10-01
common.log.2017-09-19 common.log.2017-10-01 hibernate.log.2017-01-03 pricing.log.2017-09-20 pricing.log.2017-10-02
common.log.2017-09-20 common.log.2017-10-02 pricing.log        pricing.log.2017-09-21 pricing.log.2017-10-03

第八步:继续查看哪个目录或者文件占用的空间最大,发现都是common.log和pricing.log相关的文件占用的空间最大,都是700m以上。至此,已经查明占用根目录”/”空间最多的原因是tomcat的日志文件太多,而且每一个日志文件都很大。

[root@hostname fare]# du -sh *|sort -h
0    framework.log
0    hibernate.log.2017-01-03
0    spring.log
0    struts2.log
0    xwork2.log
4.0k  hibernate.log
17m   common.zip
215m  pricing.log
216m  common.log
667m  pricing.log.2017-10-08
668m  common.log.2017-10-08
674m  common.log.2017-10-05
674m  pricing.log.2017-10-05
678m  common.log.2017-10-07
678m  pricing.log.2017-10-07
679m  common.log.2017-10-06
679m  pricing.log.2017-10-06
683m  common.log.2017-10-04
683m  pricing.log.2017-10-04
690m  pricing.log.2017-09-10
691m  common.log.2017-09-10
711m  common.log.2017-09-12
711m  pricing.log.2017-09-12
719m  pricing.log.2017-09-11
720m  common.log.2017-09-11
737m  common.log.2017-09-30
737m  pricing.log.2017-09-28
737m  pricing.log.2017-09-30
738m  common.log.2017-09-28
742m  common.log.2017-09-29
742m  pricing.log.2017-09-29
744m  pricing.log.2017-10-09
745m  common.log.2017-10-09
751m  common.log.2017-10-01
751m  pricing.log.2017-10-01
754m  common.log.2017-09-13
754m  pricing.log.2017-09-13
760m  common.log.2017-10-03
760m  pricing.log.2017-10-03
783m  pricing.log.2017-10-02
784m  common.log.2017-10-02
791m  pricing.log.2017-09-18
792m  common.log.2017-09-18
797m  common.log.2017-09-27
797m  pricing.log.2017-09-27
804m  common.log.2017-09-17
804m  common.log.2017-09-19
804m  pricing.log.2017-09-17
804m  pricing.log.2017-09-19
808m  common.log.2017-09-26
808m  pricing.log.2017-09-26
815m  pricing.log.2017-09-24
816m  common.log.2017-09-24
821m  common.log.2017-09-14
821m  pricing.log.2017-09-14
821m  pricing.log.2017-09-23
822m  common.log.2017-09-23
826m  common.log.2017-09-25
826m  pricing.log.2017-09-25
827m  common.log.2017-09-16
827m  pricing.log.2017-09-15
827m  pricing.log.2017-09-16
828m  common.log.2017-09-15
831m  common.log.2017-09-22
831m  pricing.log.2017-09-22
851m  common.log.2017-09-21
851m  pricing.log.2017-09-21
860m  common.log.2017-09-20
860m  pricing.log.2017-09-20

 第九步:从文件的命令规律,可以看出日志文件日期保留一个月,检查root用户是否有定时删除日志文件计划。如下命令查看所示,确实有每天凌晨03:15进行日志文件删除的计划。但是由于日志文件太多和根目录空间大小原因,导致根目录空间使用比例过高。在有部署自动监控工具,如zabbix等,会自动发送告警。


[root@hostname ~]# crontab -l
15 3 * * * /u02/shell/rm_log_pricing.sh
[root@hostname ~]# cat /u02/shell/rm_log_pricing.sh
find /root/apache-tomcat-6.0.36-linux/bin/fare/ -name "common.log.2*" -mtime +30 | xargs rm -f
find /root/apache-tomcat-6.0.36-linux/bin/fare/ -name "pricing.log.2*" -mtime +30 | xargs rm -f

总结:

从运维的角度,不建议tomcat等应用程序部署在和linux操作系统相关的文件系统中,尤其是直接部署在根目录下。一般都建议使用独立的文件系统来部署应用程序,无论从io性能监控,文件系统空间使用监控都是有利的。

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网