Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions check_docker/check_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
6 changes: 5 additions & 1 deletion tests/test_check_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down