Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

61 lines (59 sloc) 1.67 KB
#!/bin/bash
# ------------
hostname=${HOSTNAME}
# ------------
# Read webhook URL param
webhook_url=$1
if [[ $webhook_url == "" ]]; then
webhook_url=${SLACK_WEBHOOK_URL}
if [[ $webhook_url == "" ]]; then
echo "No webhook_url specified"
exit 1
fi
fi
# ------------
shift
channel=$1
if [[ $channel == "" ]]; then
channel=${SLACK_CHANNEL}
if [[ $channel == "" ]]; then
echo "No channel specified, posting to default channel."
fi
fi
# ------------
# Execute df-h
text="$(df -h)"
pretext="Summary of available disk storage space on *$hostname*."
# ------------
# Generate the JSON payload to POST to slack
json="{"
if [[ $channel != "" ]]; then
json+="\"channel\": \"$channel\","
fi
json+="\"attachments\":["
IFS=$'\n'
for textLine in $text
do
IFS=$' '
words=($textLine)
if [[ ${words[0]} == "Filesystem" ]]; then
# This is the header line of df- h command
json+="{\"text\": \"\`\`\`\n$textLine\n\`\`\`\", \"pretext\":\"$pretext\", \"color\":\"#0080ff\"},"
else
# Check the returned 'used' column to determine color
if [[ ${words[4]} > 89 ]]; then
color="danger"
elif [[ ${words[4]} > 60 ]]; then
color="warning"
else
color="good"
fi
json+="{\"text\": \"\`\`\`\n$textLine\n\`\`\`\", \"color\":\"$color\"},"
fi
done
# trim trailing comma
json="${json::-1}"
# -----------
# Complete JSON payload and make API request
json+="]}"
curl -s -d "payload=$json" "$webhook_url"
You can’t perform that action at this time.