Skip to content

Commit a7d9be8

Browse files
authored
Merge pull request #103 from timdaman/feature/exclude-container
2 parents 537a96d + 1268880 commit a7d9be8

3 files changed

Lines changed: 23 additions & 4 deletions

File tree

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ check_docker Usage
9393
One or more RegEx that match the names of the
9494
container(s) to check. If omitted all containers are
9595
checked. (default: ['all'])
96+
--exclude-containers EXCLUDECONTAINERS [EXCLUDECONTAINERS ...]
97+
One or more containers to exclude.
98+
9699
--present Modifies --containers so that each RegEx must match at
97100
least one container.
98101
--threads THREADS This + 1 is the maximum number of concurent

check_docker/check_docker.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,11 +332,14 @@ def get_ps_name(name_list):
332332
raise NameError("Error when trying to identify 'ps' name in {}".format(name_list))
333333

334334

335-
def get_containers(names, require_present):
335+
def get_containers(names, require_present, exclude_containers=[]):
336336
containers_list, _ = get_url(daemon + '/containers/json?all=1')
337337

338338
all_container_names = set(get_ps_name(x['Names']) for x in containers_list)
339339

340+
if exclude_containers:
341+
all_container_names = all_container_names.difference(exclude_containers)
342+
340343
if 'all' in names:
341344
return all_container_names
342345

@@ -785,7 +788,16 @@ def process_args(args):
785788
default=['all'],
786789
help='One or more RegEx that match the names of the container(s) to check. If omitted all containers are checked. (default: %(default)s)')
787790

788-
# Container name
791+
# Exclude container name
792+
parser.add_argument('--exclude-containers',
793+
dest='excludecontainers',
794+
action='store',
795+
nargs='+',
796+
type=str,
797+
default=[],
798+
help='One or more containers to exclude.')
799+
800+
# Presence
789801
parser.add_argument('--present',
790802
dest='present',
791803
default=False,
@@ -983,7 +995,7 @@ def perform_checks(raw_args):
983995
return
984996

985997
# Here is where all the work happens
986-
containers = get_containers(args.containers, args.present)
998+
containers = get_containers(args.containers, args.present, args.excludecontainers)
987999

9881000
if len(containers) == 0 and not args.present:
9891001
unknown("No containers names found matching criteria")

tests/test_check_docker.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,11 @@ def test_get_containers_2(check_docker, sample_containers_json, mock_get_contain
593593
container_list = check_docker.get_containers(['name.*'], False)
594594
assert container_list == {'name1', 'name2'}
595595

596+
def test_get_containers_2_withexclude(check_docker, sample_containers_json, mock_get_container_info):
597+
with patch('check_docker.check_docker.get_url', return_value=(sample_containers_json, 200)):
598+
with patch('check_docker.check_docker.get_container_info', side_effect=mock_get_container_info):
599+
container_list = check_docker.get_containers(['name.*'], False, ['name2'])
600+
assert container_list == {'name1'}
596601

597602
def test_get_containers_3(check_docker, sample_containers_json, mock_get_container_info):
598603
check_docker.rc = -1
@@ -613,7 +618,6 @@ def test_get_containers_4(check_docker, sample_containers_json, mock_get_contain
613618
assert container_list == set()
614619
assert patched.call_count == 1
615620

616-
617621
def test_socketfile_failure_false(check_docker, fs):
618622
fs.create_file('/tmp/socket', contents='', st_mode=(stat.S_IFSOCK | 0o666))
619623
args = ('--status', 'running', '--connection', '/tmp/socket')

0 commit comments

Comments
 (0)