-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathslack_storage_notifier.sh
60 lines (59 loc) · 1.67 KB
/
slack_storage_notifier.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/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"