diff --git a/README.rst b/README.rst index 89d6873..08be18a 100644 --- a/README.rst +++ b/README.rst @@ -93,6 +93,9 @@ check_docker Usage One or more RegEx that match the names of the container(s) to check. If omitted all containers are checked. (default: ['all']) + --exclude-containers EXCLUDECONTAINERS [EXCLUDECONTAINERS ...] + One or more containers to exclude. + --present Modifies --containers so that each RegEx must match at least one container. --threads THREADS This + 1 is the maximum number of concurent diff --git a/check_docker/check_docker.py b/check_docker/check_docker.py index 7e5069e..b6f0878 100755 --- a/check_docker/check_docker.py +++ b/check_docker/check_docker.py @@ -332,11 +332,14 @@ def get_ps_name(name_list): raise NameError("Error when trying to identify 'ps' name in {}".format(name_list)) -def get_containers(names, require_present): +def get_containers(names, require_present, exclude_containers=[]): containers_list, _ = get_url(daemon + '/containers/json?all=1') all_container_names = set(get_ps_name(x['Names']) for x in containers_list) + if exclude_containers: + all_container_names = all_container_names.difference(exclude_containers) + if 'all' in names: return all_container_names @@ -785,7 +788,16 @@ def process_args(args): default=['all'], help='One or more RegEx that match the names of the container(s) to check. If omitted all containers are checked. (default: %(default)s)') - # Container name + # Exclude container name + parser.add_argument('--exclude-containers', + dest='excludecontainers', + action='store', + nargs='+', + type=str, + default=[], + help='One or more containers to exclude.') + + # Presence parser.add_argument('--present', dest='present', default=False, @@ -983,7 +995,7 @@ def perform_checks(raw_args): return # Here is where all the work happens - containers = get_containers(args.containers, args.present) + containers = get_containers(args.containers, args.present, args.excludecontainers) if len(containers) == 0 and not args.present: unknown("No containers names found matching criteria") diff --git a/tests/test_check_docker.py b/tests/test_check_docker.py index 1225bde..7d98334 100644 --- a/tests/test_check_docker.py +++ b/tests/test_check_docker.py @@ -593,6 +593,11 @@ def test_get_containers_2(check_docker, sample_containers_json, mock_get_contain container_list = check_docker.get_containers(['name.*'], False) assert container_list == {'name1', 'name2'} +def test_get_containers_2_withexclude(check_docker, sample_containers_json, mock_get_container_info): + with patch('check_docker.check_docker.get_url', return_value=(sample_containers_json, 200)): + with patch('check_docker.check_docker.get_container_info', side_effect=mock_get_container_info): + container_list = check_docker.get_containers(['name.*'], False, ['name2']) + assert container_list == {'name1'} def test_get_containers_3(check_docker, sample_containers_json, mock_get_container_info): check_docker.rc = -1 @@ -613,7 +618,6 @@ def test_get_containers_4(check_docker, sample_containers_json, mock_get_contain assert container_list == set() assert patched.call_count == 1 - def test_socketfile_failure_false(check_docker, fs): fs.create_file('/tmp/socket', contents='', st_mode=(stat.S_IFSOCK | 0o666)) args = ('--status', 'running', '--connection', '/tmp/socket')