diff --git a/CHANGELOG.md b/CHANGELOG.md index 5368627..d411e35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Which is based on [Keep A Changelog](http://keepachangelog.com/) ### Added - intergration test for container check + - check-container.rb: -x (--allow-exited) to avoid raising alerts when container exited without error, useful for task oriented containers monitoring. ## [3.0.0] - 2018-02-17 ### Breaking Changes diff --git a/bin/check-container.rb b/bin/check-container.rb index 9acaa57..a121300 100755 --- a/bin/check-container.rb +++ b/bin/check-container.rb @@ -56,6 +56,12 @@ class CheckDockerContainer < Sensu::Plugin::Check::CLI short: '-t TAG', long: '--tag TAG' + option :allowexited, + short: '-x', + long: '--allow-exited', + boolean: true, + description: 'Do not raise alert if container has exited without error' + def run @client = DockerApi.new(config[:docker_host]) path = "/containers/#{config[:container]}/json" @@ -74,6 +80,12 @@ def run end end ok "#{config[:container]} is running on #{@client.uri}." + elsif config[:allowexited] && body['State']['Status'] == 'exited' + if (body['State']['ExitCode']).zero? + ok "#{config[:container]} has exited without error on #{@client.uri}." + else + critical "#{config[:container]} has exited with status code #{body['State']['ExitCode']} on #{@client.uri}." + end else critical "#{config[:container]} is #{body['State']['Status']} on #{@client.uri}." end diff --git a/test/fixtures/bootstrap.sh b/test/fixtures/bootstrap.sh index 941b49b..2f51cc1 100644 --- a/test/fixtures/bootstrap.sh +++ b/test/fixtures/bootstrap.sh @@ -15,4 +15,9 @@ gem install sensu-plugins-docker-*.gem # start container for testing -docker run --name test -d --rm alpine sh -c 'while true; do sleep 1; done' +docker run --name test_running -d --rm alpine sh -c 'while true; do sleep 1; done' +docker run --name test_exited_ok -d alpine sh -c 'exit 0' +docker run --name test_exited_fail -d alpine sh -c 'exit 1' + +# for debugging +docker ps -a diff --git a/test/integration/helpers/serverspec/check-container_spec.rb b/test/integration/helpers/serverspec/check-container_spec.rb index 313f871..25d60c8 100644 --- a/test/integration/helpers/serverspec/check-container_spec.rb +++ b/test/integration/helpers/serverspec/check-container_spec.rb @@ -11,7 +11,27 @@ it_behaves_like 'ruby checks', check end -describe command("#{check} -N test") do +describe command("#{check} -N test_running") do its(:exit_status) { should eq 0 } - its(:stdout) { should match(/CheckDockerContainer OK: test is running on unix:\/\/\/var\/run\/docker.sock./) } + its(:stdout) { should match(/CheckDockerContainer OK: test_running is running on unix:\/\/\/var\/run\/docker.sock./) } +end + +describe command("#{check} -x -N test_running") do + its(:exit_status) { should eq 0 } + its(:stdout) { should match(/CheckDockerContainer OK: test_running is running on unix:\/\/\/var\/run\/docker.sock./) } +end + +describe command("#{check} -N test_exited_ok") do + its(:exit_status) { should eq 2 } + its(:stdout) { should match(/CheckDockerContainer CRITICAL: test_exited_ok is exited on unix:\/\/\/var\/run\/docker.sock./) } +end + +describe command("#{check} -x -N test_exited_ok") do + its(:exit_status) { should eq 0 } + its(:stdout) { should match(/CheckDockerContainer OK: test_exited_ok has exited without error on unix:\/\/\/var\/run\/docker.sock./) } +end + +describe command("#{check} -x -N test_exited_fail") do + its(:exit_status) { should eq 2 } + its(:stdout) { should match(/CheckDockerContainer CRITICAL: test_exited_fail has exited with status code 1 on unix:\/\/\/var\/run\/docker.sock./) } end