-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux_check_disk_space.py
More file actions
executable file
·122 lines (99 loc) · 2.59 KB
/
linux_check_disk_space.py
File metadata and controls
executable file
·122 lines (99 loc) · 2.59 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
"""
Check Disk Space [Linux]
Check disk space and report an error if usage is above threshold.
Supports traditional partitions and ZFS pools.
TRMM ARGUMENTS:
--threshold 20
Syntax:
--threshold: <int> - The percentage of free space that is considered a warning
Supports:
Linux-All
Category:
Disks
License:
AGPLv3
Author:
Charlie Powell <cdp1337@veraciousnetwork.com>
Changelog:
20250204 - Add mount point to output
20250130 - Initial version
"""
import argparse
import subprocess
import json
class Cmd:
"""
Simple subprocess wrapper to provide convenience methods for common interactions.
"""
def __init__(self, cmd: list):
self.cmd = cmd
self.result = None
def exists(self) -> bool:
"""
Check if this binary exists
:return:
"""
return subprocess.run(['which', self.cmd[0]], check=False, stdout=subprocess.PIPE).returncode == 0
def text(self) -> str:
"""
Get the output of the command as raw text
:return:
"""
return self._exec().stdout.strip()
def lines(self) -> list:
"""
Get the output of the command as lines of text (as a list)
:return:
"""
return self.text().split('\n')
def json(self):
"""
Get the output of the command decoded as JSON
:return:
"""
return json.loads(self.text())
def _exec(self):
if self.result is None:
self.result = subprocess.run(
self.cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
encoding='utf-8'
)
return self.result
parser = argparse.ArgumentParser(description="Check Disk Space [Linux]")
parser.add_argument(
"--threshold",
type=int,
default=20,
help="The percentage of free space that is considered a warning"
)
options = parser.parse_args()
ret = 0
for line in Cmd(['df', '--output=source,pcent,target']).lines():
if not line.startswith('/dev/'):
# Only check physical partitions
continue
if '/loop' in line:
# Skip loop devices
continue
line_parts = line.split()
free = 100 - int(line_parts[1][:-1])
if free < options.threshold:
print('WARNING: Partition %s (%s) has %s%% free space remaining!' % (line_parts[0], line_parts[2], free))
ret = 1
else:
print('Partition %s (%s) has %s%% free space remaining.' % (line_parts[0], line_parts[2], free))
# Check ZFS pools
zfs = Cmd(['zpool', 'list', '-H', '-o', 'name,capacity'])
if zfs.exists():
for line in zfs.lines():
free = 100 - int(line.split()[1][:-1])
if free < options.threshold:
print('WARNING: ZFS pool %s has %s%% free space remaining!' % (line.split()[0], free))
ret = 1
else:
print('ZFS pool %s has %s%% free space remaining.' % (line.split()[0], free))
exit(ret)