#!/bin/zsh

# Report to Slack Webhook disk status if one of two thresholds are exceeded
# Please go to the following Slack link for details of setting up an Incoming Slack Webhook
# https://api.slack.com/incoming-webhooks
# 2018-06 sean.holden@filewave.com

# Set threshold values in for both percentage and GB
space_warning=$(defaults read /usr/local/etc/Scripts/com.filewave.slack space_warning)
space_danger=$(defaults read /usr/local/etc/Scripts/com.filewave.slack space_danger)
space_danger_gb=$(defaults read /usr/local/etc/Scripts/com.filewave.slack space_danger_gb)
space_warning_gb=$(defaults read /usr/local/etc/Scripts/com.filewave.slack space_warning_gb)
slack_channel=$(defaults read /usr/local/etc/Scripts/com.filewave.slack slack_channel)

# Slack Webhook
slack_webhook=$(defaults read /usr/local/etc/Scripts/com.filewave.slack slack_webhook)

# Current FileWave client name
filewave_client_name=$(defaults read /usr/local/etc/fwcld.plist fwUser)

# Device serial number
serial_number=$(ioreg -c IOPlatformExpertDevice -d 2 | awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}')

# Get time to post into footer
now=$(date +%s)

# Danger threshold message configuration
# Report to slack if free disk space is less than $space_danger threshold 
function SlackDanger {

        json="{
                \"username\": \"Helpdesk\",
                \"channel\": \"#${slack_channel}\",
                \"icon_emoji\": \":computer:\",
                \"text\": \":exclamation: *Warning: ${filewave_client_name}*\",
                \"attachments\": [
                                {
                                \"color\": \"#FF4500\",
                                \"pretext\": \"*!!System drive space low!!*\",
                                \"text\": \"*Serial Number:* ${serial_number}\n*Drive Space Used:* $free_space\n*Available:* ${free_space_gb}GB\",
                                \"ts\": \"$now\",
                                \"footer\": \"Thresholds ${space_warning}% ${space_danger_gb}GB\",
                        }
                ]
        }"
}

# Warning threshold message configuration
# Report to slack if free disk space is less than $space_warning threshold
function SlackWarning {

        json="{
                \"username\": \"Helpdesk\",
                \"channel\": \"#${slack_channel}\",
                \"icon_emoji\": \":computer:\",
                \"text\": \":warning: *Warning: ${filewave_client_name}*\",
                \"attachments\": [
                                        {
                                        \"color\": \"#FFE100\",
                                        \"pretext\": \"*System drive is low on space*\",
                                        \"text\": \"*Serial Number:* ${serial_number}\n*Drive Space Used:* $free_space\n*Available:* ${free_space_gb}GB\",
                                        \"footer\": \"Thresholds ${space_warning}% ${space_danger_gb}GB\",
                                        \"ts\": \"$now\"
                                }
                        ]
        }"
}

# Current free disk space
free_space=$(df / | awk '/^\/dev\/disk/ {print $5}')
free_space_gb=$(df -g / | awk '/^\/dev\/disk/ {print $4}')

# Compare free space with thresholds and report danger or warning
if [ ${free_space%%%*} -gt $space_danger ] || [ $free_space_gb -lt $space_danger_gb ] 
then
	SlackDanger
elif [ ${free_space%%%*} -gt $space_warning ] || [ $free_space_gb -lt $space_warning_gb ]
then
	SlackWarning
fi

# Post to slack
curl -X POST --data-urlencode "payload=$json" "$slack_webhook"

exit 0