--no-count-empty does not count empty files (useful for /var/mail)

master
neingeist 3 years ago
parent abbc4ccb0d
commit eb79bb5893

@ -1,8 +1,23 @@
# check_dir_empty # check_dir_empty
nagios plugin: check that the directories given are empty nagios plugin: check that the directories given are empty
Example: ## Example
~~~ ~~~
$ ./check_dir_empty /var/mail $ ./check_dir_empty --no-count-empty /var/mail
OK: All given directories are empty OK: All given directories are empty
~~~ ~~~
## Usage
~~~
% ./check_dir_empty -h
usage: check_dir_empty [-h] [--no-count-empty] dir [dir ...]
Check that the directories given are empty
positional arguments:
dir directory to be checked
optional arguments:
-h, --help show this help message and exit
--no-count-empty do not count empty files (useful to check /var/mail)
~~~

@ -10,11 +10,19 @@ parser = argparse.ArgumentParser(
parser.add_argument( parser.add_argument(
'directories', metavar='dir', nargs='+', type=str, 'directories', metavar='dir', nargs='+', type=str,
help='directory to be checked') help='directory to be checked')
parser.add_argument(
'--no-count-empty', dest='count_empty', action='store_false',
help='do not count empty files (useful to check /var/mail)')
args = parser.parse_args() args = parser.parse_args()
for directory in args.directories: for directory in args.directories:
listdir = os.listdir(directory) listdir = os.listdir(directory)
if not args.count_empty:
listdir = [fn for fn in listdir
if os.path.getsize(os.path.join(directory, fn)) > 0]
if len(listdir) != 0: if len(listdir) != 0:
print('WARNING: Directory {} is not empty ({} entries)' print('WARNING: Directory {} is not empty ({} entries)'
.format(directory, len(listdir))) .format(directory, len(listdir)))

Loading…
Cancel
Save