Linux 查看空間使用情況的實例詳解
在日常的Linux巡檢中,我們會遇到文件系統目錄使用空間很高的情況,例如如下利用”df -h “查看到根目錄空間使用超過80%。而我們僅僅知道是根目錄空間使用過高,這樣是不夠的。還需要知道是目錄還是文件讓根目錄空間使用過高。通常我們使用的命令是”du -sh *”。
第一步:查看Linux系統的文件系統使用情況,如下可以看到根目錄”/”已經使用81%。
[root@hostname ~]# df -hFilesystem 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 arch7.6M bin27M boot324K dev36M etc36K home136M lib26M lib6416K lost+found4.0K media0 misc4.0K mnt0 net8.0K opt4.0K orbit-root--省略/proc目錄統計的報錯0 proc47G root ##根目錄文件系統使用最大的目錄15M sbin0 selinux4.0K srv0 sys80K tmp20K u0149G u02
第三步:進入root目錄,其實也是root用戶的主目錄。我們從ls命令可以猜到可能是【apache-tomcat-6.0.36-linux】這個tomcat的程序目錄占用比較多。
[root@hostname /]# cd root[root@hostname ~]# lsanaconda-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.cfg47G apache-tomcat-6.0.36-linux4.0K Desktop4.0K Documents4.0K Downloads56K install.log12K install.log.syslog4.0K Music4.0K Pictures4.0K Public4.0K Templates4.0K Videos
第五步:既然看到有tomcat的目錄,很可能這個tomcat運行著,利用ps命令查看是否有tomcat的進程,一般可以過濾java這個關鍵詞。如下,果然有tomcat在運行著。那么很可能占用空間最多就是tomcat的日志文件。
[root@hostname ~]# ps -ef|grep javaroot 4879 4836 0 08:26 pts/0 00:00:00 grep javaroot 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]# lsbackup 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 backup882M bin108K conf46G fare6.8M lib40K LICENSE23M logs4.0K NOTICE12K RELEASE-NOTES16K RUNNING.txt7.9M temp188M webapps7.1M work
第七步:繼續一探究竟【fare】目錄里面到底存放的是什么類型的目錄或者文件。如下看到有很多類似日志文件,如common.log和pricing.log。通過和開發人員溝通之后,確定確實日志文件。
[root@hostname apache-tomcat-6.0.36-linux]# cd fare[root@hostname fare]# lscommon.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-04common.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-05common.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-06common.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-07common.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-08common.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-09common.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.logcommon.log.2017-09-16 common.log.2017-09-28 common.zip pricing.log.2017-09-17 pricing.log.2017-09-29 struts2.logcommon.log.2017-09-17 common.log.2017-09-29 framework.log pricing.log.2017-09-18 pricing.log.2017-09-30 xwork2.logcommon.log.2017-09-18 common.log.2017-09-30 hibernate.log pricing.log.2017-09-19 pricing.log.2017-10-01common.log.2017-09-19 common.log.2017-10-01 hibernate.log.2017-01-03 pricing.log.2017-09-20 pricing.log.2017-10-02common.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 -h0 framework.log0 hibernate.log.2017-01-030 spring.log0 struts2.log0 xwork2.log4.0K hibernate.log17M common.zip215M pricing.log216M common.log667M pricing.log.2017-10-08668M common.log.2017-10-08674M common.log.2017-10-05674M pricing.log.2017-10-05678M common.log.2017-10-07678M pricing.log.2017-10-07679M common.log.2017-10-06679M pricing.log.2017-10-06683M common.log.2017-10-04683M pricing.log.2017-10-04690M pricing.log.2017-09-10691M common.log.2017-09-10711M common.log.2017-09-12711M pricing.log.2017-09-12719M pricing.log.2017-09-11720M common.log.2017-09-11737M common.log.2017-09-30737M pricing.log.2017-09-28737M pricing.log.2017-09-30738M common.log.2017-09-28742M common.log.2017-09-29742M pricing.log.2017-09-29744M pricing.log.2017-10-09745M common.log.2017-10-09751M common.log.2017-10-01751M pricing.log.2017-10-01754M common.log.2017-09-13754M pricing.log.2017-09-13760M common.log.2017-10-03760M pricing.log.2017-10-03783M pricing.log.2017-10-02784M common.log.2017-10-02791M pricing.log.2017-09-18792M common.log.2017-09-18797M common.log.2017-09-27797M pricing.log.2017-09-27804M common.log.2017-09-17804M common.log.2017-09-19804M pricing.log.2017-09-17804M pricing.log.2017-09-19808M common.log.2017-09-26808M pricing.log.2017-09-26815M pricing.log.2017-09-24816M common.log.2017-09-24821M common.log.2017-09-14821M pricing.log.2017-09-14821M pricing.log.2017-09-23822M common.log.2017-09-23826M common.log.2017-09-25826M pricing.log.2017-09-25827M common.log.2017-09-16827M pricing.log.2017-09-15827M pricing.log.2017-09-16828M common.log.2017-09-15831M common.log.2017-09-22831M pricing.log.2017-09-22851M common.log.2017-09-21851M pricing.log.2017-09-21860M common.log.2017-09-20860M pricing.log.2017-09-20
第九步:從文件的命令規律,可以看出日志文件日期保留一個月,檢查root用戶是否有定時刪除日志文件計劃。如下命令查看所示,確實有每天凌晨03:15進行日志文件刪除的計劃。但是由于日志文件太多和根目錄空間大小原因,導致根目錄空間使用比例過高。在有部署自動監控工具,如zabbix等,會自動發送告警。
[root@hostname ~]# crontab -l15 3 * * * /u02/shell/rm_log_pricing.sh[root@hostname ~]# cat /u02/shell/rm_log_pricing.shfind /root/apache-tomcat-6.0.36-linux/bin/fare/ -name "common.log.2*" -mtime +30 | xargs rm -ffind /root/apache-tomcat-6.0.36-linux/bin/fare/ -name "pricing.log.2*" -mtime +30 | xargs rm -f
總結:
從運維的角度,不建議tomcat等應用程序部署在和Linux操作系統相關的文件系統中,尤其是直接部署在根目錄下。一般都建議使用獨立的文件系統來部署應用程序,無論從IO性能監控,文件系統空間使用監控都是有利的。
如有疑問請留言或者到本站社區交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答