-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-linux-disk-usage.py
More file actions
71 lines (57 loc) · 2.81 KB
/
get-linux-disk-usage.py
File metadata and controls
71 lines (57 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Linux磁盘使用率监控脚本
功能描述:
该脚本用于监控Linux系统的磁盘使用情况,包括以下功能:
1. 检查磁盘分区使用率,当超过设定阈值时发出警告
2. 分析指定目录下的文件和子目录占用空间
3. 显示占用空间最大的前N个文件或目录
主要特性:
- 使用df命令获取磁盘分区使用情况
- 使用du命令分析目录空间占用
- 支持自定义磁盘使用率阈值
- 支持自定义显示Top N个最大文件/目录
- 友好的输出格式,便于运维人员快速定位问题
"""
import subprocess
def check_disk_usage(threshold, top_n=5):
try:
result = subprocess.run(['df', '-h'], capture_output=True, text=True, check=True)
output = result.stdout
print(f"disk usage:\n {output}")
# 1.先检查磁盘使用率
# splitlines按行分割,获取到每行的filesystem统计,存到列表里面
lines = output.splitlines()
# 从列表第二个元素开始遍历(第一行元素是输出的字段说明,不要)
for line in lines[1:]:
# 把每行再分割。split不写参数默认是按照空格分割
columns = line.split()
# 使用率去掉右边百分号,必须转成int才能比较
percent_used = int(columns[4].rstrip('%'))
if percent_used > threshold:
print(f"Warning: Disk usage has exceeded threshold: {percent_used}")
# 2. du获取目录或者文件大小。默认du只显示目录,-a显示文件,-h人类可读,-x忽略外部挂载的文件系统
# du -ahx 获取某个目录下所有的目录和文件的磁盘占用情况
result = subprocess.run(['du', '-ahx','/etc/docker/'], capture_output=True, text=True, check=True)
output = result.stdout
lines = output.splitlines()
directory_usage = {}
# 字典赋值
for line in lines:
size, path = line.split(maxsplit=1)
directory_usage[path]=size
# 找出使用磁盘最多的文件,字典排序
# sorted把字典键值对变成元组[('dir1','1'),('dir2',2)]
# lamda x:x[1]指定按照字典value的值来排序
# reverse=True从大到小排序
sorted_dirs = sorted(directory_usage.items(), key=lambda x:x[1], reverse=True)
print(f"Top {top_n} usage directory or file in: \n")
#只取到列表前top_n个元素
for path, size in sorted_dirs[:top_n]:
print(f"Path: {path}, size: {size}")
except subprocess.CalledProcessError as e:
print(f"Command execution failed: {e}")
except Exception as e:
print(f"Exception occurred: {e}")
if __name__ == "__main__":
# 设置磁盘使用率阈值为80%
check_disk_usage(threshold=80, top_n=5)