Skip to content

Commit

Permalink
Add option for task oriented containers
Browse files Browse the repository at this point in the history
In some cases docker containers would be used on demand or started
periodically to execute certain tasks. Option -x would allow to monitor
them and only raise alerts if they have exited with error (status code
different from zero).
  • Loading branch information
antonidabek authored and majormoses committed May 8, 2018
1 parent 921468e commit 1351647
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions bin/check-container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion test/fixtures/bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 22 additions & 2 deletions test/integration/helpers/serverspec/check-container_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1351647

Please sign in to comment.