diff --git a/README.md b/README.md index 78a1dc6..386a02b 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,23 @@ # check_dir_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 ~~~ + +## 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) +~~~ diff --git a/check_dir_empty b/check_dir_empty index 0e7c189..c565654 100755 --- a/check_dir_empty +++ b/check_dir_empty @@ -10,11 +10,19 @@ parser = argparse.ArgumentParser( parser.add_argument( 'directories', metavar='dir', nargs='+', type=str, 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() for directory in args.directories: 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: print('WARNING: Directory {} is not empty ({} entries)' .format(directory, len(listdir)))