Skip to content

Instantly share code, notes, and snippets.

@kchristensen
Created December 17, 2015 14:00
Show Gist options
  • Select an option

  • Save kchristensen/ac513cef572b33826652 to your computer and use it in GitHub Desktop.

Select an option

Save kchristensen/ac513cef572b33826652 to your computer and use it in GitHub Desktop.

Revisions

  1. kchristensen created this gist Dec 17, 2015.
    23 changes: 23 additions & 0 deletions docker-watch.sh
    Original 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