« Back to Index

[Bash Watchtower]

View original Gist on GitHub

Tags: #bash #shell #watchtower

Bash Watchtower.sh

function cleanup() {
  local file=$1
  if [ -f $file ]; then
    rm $file
  fi
}

function pull() {
  local base=$1
  local urls=("${!2}")

  for resource in "${urls[@]}"
  do
    curl $base$resource --head \
                        --location \
                        --silent \
                        --output /dev/null \
                        --connect-timeout 2 \
                        --write-out "%{url_effective} %{http_code}\n" &
  done

  wait
}

function parse() {
  local results=$1
  local remote=https://hooks.slack.com/services/foo/bar/baz

  cat $results | awk '!/200/ { print $2 ": " $1 }' > temp.txt

  while read line; do
    curl --header "Content-Type: application/json" \
         --silent \
         --output /dev/null \
         --request POST \
         --data "{\"text\": \"$line\"}" $remote &
  done < temp.txt

  wait

  display temp.txt
  cleanup temp.txt
}

function display() {
  printf "\n\n"
  cat $1
  printf "\n\n"
}

endpoints=(
  /newsbeat
  /newsbeat/popular
  /newsbeat/topics
  /newsbeat/topics/entertainment
  /newsbeat/topics/surgery
  /newsbeat/article/32792353/im-engaged-but-will-i-ever-be-able-to-marry-my-boyfriend
)

cleanup results.txt

pull http://bbc.co.uk endpoints[@] >> results.txt
pull http://composition.newsbeat.news.cloud.bbc.co.uk endpoints[@] >> results.txt

display results.txt
parse results.txt

Output.md

Depending on if there are failures, Slack should get a notification like:

500: http://www.bbc.co.uk/newsbeat/popular
500: http://www.bbc.co.uk/newsbeat/topics