Created
December 17, 2015 14:00
-
-
Save kchristensen/ac513cef572b33826652 to your computer and use it in GitHub Desktop.
Revisions
-
kchristensen created this gist
Dec 17, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ #!/bin/bash INSTANCE=$(curl -s http://169.254.169.254/latest/meta-data/instance-id) SLACK_CHANNEL='#ops-notifications' SLACK_URL='<%= node['base']['slack']['hook'] %>' SLACK_USERNAME='DockerBot' for CONTAINER in $(docker ps -a|grep -v CONTAINER|awk '{print $1}') do STATE=$(docker inspect -f '{{.State.Running}}' ${CONTAINER}) # We're only concerned with containers that aren't running if [ "$STATE" != "true" ]; then EXIT_CODE=$(docker inspect -f '{{.State.ExitCode}}' ${CONTAINER}) # Don't notify Slack on normal container shutdown if [ ${EXIT_CODE} -gt 0 ]; then CONTAINER_NAME=$(docker inspect -f '{{.Config.Image}}' ${CONTAINER}|awk -F/ '{print $2}'|awk -F: '{print $1}') SLACK_TEXT="Container ${CONTAINER_NAME} running on ${INSTANCE} exited with code ${EXIT_CODE}" curl -X POST --data "payload={\"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"text\": \"${SLACK_TEXT}\"}" ${SLACK_URL} fi fi done