Linux下Shell脚本实战之监测磁盘空间

Posted jks212454

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下Shell脚本实战之监测磁盘空间相关的知识,希望对你有一定的参考价值。

Linux下Shell脚本实战之监测磁盘空间

一、脚本目的及要求

(1) 监控/home下每各个用户目录的占用磁盘大小
(2) 监控/var/log下前10名占用磁盘大小
(3) 输出的磁盘空间报告文件加上时间戳保存
(4) 该脚本名为disk_sh

二、脚本内容

#!/bin/bash
##########################################################
#File Name:disk.sh
#Version:V1.0
#Aurhor:
#Emali:
#Created Time:2021-05-16 23:51:38
#Description:
##########################################################




# Parameters for Script 
# 
CHECK_DIRECTORIES=" /var/log /home" #Directories to check 
# 
############## Main Script ################################# 
# 
DATE=$(date '+%m%d%y') #Date for report file 
# 
exec > disk_space_$DATE.rpt #Make report file STDOUT 
# 
echo "Top Ten Disk Space Usage" #Report header 
echo "for $CHECK_DIRECTORIES Directories" 
# 
for DIR_CHECK in $CHECK_DIRECTORIES #Loop to du directories 
do
 echo "" 
 echo "The $DIR_CHECK Directory:" #Directory header 
# 
# Create a listing of top ten disk space users in this dir 
 du -S $DIR_CHECK 2>/dev/null |
 sort -rn |
 sed '{11,$D; =}' |
 sed 'N; s/\\n/ /' |
 gawk '{printf $1 ":" "\\t" $2 "\\t" $3 "\\n"}'
# 
done #End of loop 
# 
exit

三、运行脚本

[root@control scripts]# cat disk_space_051721.rpt 
Top Ten Disk Space Usage
for  /var/log /home Directories

The /var/log Directory:
1:	5980	/var/log
2:	4324	/var/log/sa
3:	1084	/var/log/anaconda
4:	188	/var/log/rhsm
5:	176	/var/log/zabbix
6:	160	/var/log/tuned
7:	68	/var/log/httpd
8:	64	/var/log/mysql
9:	48	/var/log/sssd
10:	12	/var/log/samba/old

The /home Directory:
1:	68	/home/admin/ansible
2:	32	/home/admin
3:	20	/home/user2
4:	20	/home/user1
5:	16	/home/ituser2
6:	12	/home/user99
7:	12	/home/user6
8:	12	/home/user5
9:	12	/home/user4
10:	12	/home/user3

以上是关于Linux下Shell脚本实战之监测磁盘空间的主要内容,如果未能解决你的问题,请参考以下文章

Linux Shell之监测磁盘空间

自学Linux Shell4.2-监测磁盘空间mount umount df du

Shell脚本完成用户磁盘空间监测报告

Linux/Unix shell 脚本监控磁盘可用空间

Linux系统下shell脚本实战之mariadb创建数据库和表

Linux下每天自动执行查看磁盘剩余空间的shell文件