#!/bin/bash

###### 檔案資訊 ######
#
# 檔名: disk_space_monitor_notice
#
# 功能: 檢測所有 Game Servers / 測試機與控制機的硬碟空間,
#       將結果輸出至各台機器, 並將所有結果輸出至測試機
#
# 版更:
# v1: 基本款
#
###### 檔案資訊 ######    

# 如果 gamerc 存在, 則載入它
[ -f ~/.gamerc ] && source ~/.gamerc

# 如果是非 Ctrl 架構請自行設定遊戲名稱跟國家名稱
[ -z "${GAME_TYPE}" ] && GAME_TYPE="非Ctrl架構, 自行設定"
[ -z "${COUNTRY_CODE}" ] && COUNTRY_CODE="非Ctrl架構, 自行設定"

# 參數設定, 如果舊遊戲的 gamerc 沒有, 或是代理版本的遊戲, 則自動設定對應參數
# Warning disk space size too low for TEST when under this setting.(in GB)
# 測試機與控制機, 硬碟剩餘量的監控設定
[ -z "${LOW_DISK_SPACE_WARNING_SIZE_TEST}" ] && LOW_DISK_SPACE_WARNING_SIZE_TEST="15"

# Warning disk space percentage too low for TEST when under this setting.(in %)
# 測試機與控制機, 硬碟使用百分比的監控設定
[ -z "${LOW_DISK_SPACE_WARNING_PERCENTAGE_TEST}" ] && LOW_DISK_SPACE_WARNING_PERCENTAGE_TEST="90"

# Warning disk space size too low for LIVE when under this setting.(in GB)
# 正式機, 硬碟剩餘量的監控設定
[ -z "${LOW_DISK_SPACE_WARNING_SIZE_LIVE}" ] && LOW_DISK_SPACE_WARNING_SIZE_LIVE="30"

# Warning disk space percentage too low for LIVE when under this setting.(in %)
# 正式機, 硬碟使用百分比的監控設定
[ -z "${LOW_DISK_SPACE_WARNING_PERCENTAGE_LIVE}" ] && LOW_DISK_SPACE_WARNING_PERCENTAGE_LIVE="80"

# Check the mounted directory
# 需被監控的分割區設定
[ -z "${LOW_DISK_SPACE_WARNING_DIR}" ] && LOW_DISK_SPACE_WARNING_DIR="/ /storage"

# 指定輸出的 LOG 檔
LOG_OUTPUT_TEST="/tmp/disk_space_monitor_test.log"
LOG_OUTPUT_LIVE="/tmp/disk_space_monitor_live.log"

# 舊的 LOG 檔案如果存在, 則進行刪除
[ -f "${LOG_OUTPUT_TEST}" ] && rm -rf ${LOG_OUTPUT_TEST}
[ -f "${LOG_OUTPUT_LIVE}" ] && rm -rf ${LOG_OUTPUT_LIVE}

# 如果有需要自訂或是修改顯示的遊戲與國家名稱, 請自行修正
[ "${GAME_TYPE}" == "AK" ] && GAME_TYPE_DISPLAY="M7" || GAME_TYPE_DISPLAY=${GAME_TYPE}
[ ! -z "${COUNTRY_CODE_DISPLAY}" ] && COUNTRY_CODE_DISPLAY=$(echo ${COUNTRY_CODE_DISPLAY}) || COUNTRY_CODE_DISPLAY=$(echo ${COUNTRY_CODE})

# DISK PERCENTAGE CHECK
# 檢查硬碟使用的百分比
METHOD="PERCENTAGE"

# 迴圈搜尋欲監控的分割區
for directory in ${LOW_DISK_SPACE_WARNING_DIR}; do
	# 撈出監控分割區的資訊
	DATA=$(/bin/df -hl | awk -F" " '{print $1","$2","$3","$4","$5","$6}' | grep "${directory}$")

	# 如果有撈到分割區的資訊
	if [ ! -z "${DATA}" ]; then
		# 撈出該分割區的百分比資訊
		PERCENT=$(echo ${DATA} | awk -F"," '{print $5}' | /bin/sed 's/%//')

		# 測試機/控制機 => 測試機的標準 vs. 其餘則為正式機標準
		[[ "${HOST_NAME}" == "TEST" ]] && CRITERIA=${LOW_DISK_SPACE_WARNING_PERCENTAGE_TEST} || CRITERIA=${LOW_DISK_SPACE_WARNING_PERCENTAGE_LIVE}
		[[ "${HOST_NAME}" == "TEST" ]] && OUTPUT_FILE=${LOG_OUTPUT_TEST} || OUTPUT_FILE=${LOG_OUTPUT_LIVE}

		# 如果使用百分比超過設定上限, 則輸出資訊到 LOG 檔
		if [ `echo "${PERCENT} > ${CRITERIA}" | bc` -eq 1 ]; then
			echo ${GAME_TYPE_DISPLAY},${COUNTRY_CODE_DISPLAY},${HOST_NAME},${METHOD},${DATA}
			echo ${GAME_TYPE_DISPLAY},${COUNTRY_CODE_DISPLAY},${HOST_NAME},${METHOD},${DATA} >> ${OUTPUT_FILE}
		fi
	fi
done

# DISK AVAILABLE SIZE CHECK
# 檢查硬碟剩餘量
METHOD="SIZE"

# 迴圈搜尋欲監控的目錄
for directory in ${LOW_DISK_SPACE_WARNING_DIR}; do
	# 撈出監控分割區的資訊
	DATA=$(/bin/df -hl | awk -F" " '{print $1","$2","$3","$4","$5","$6}' | grep "${directory}$")

	# 如果有撈到分割區的資訊
	if [ ! -z "${DATA}" ]; then
		# 撈出該分割區的剩餘空間資訊
		SIZE=$(echo ${DATA} | awk -F"," '{print $4}' | /bin/sed 's/G//')

		# 測試機/控制機 => 測試機的標準 vs. 其餘則為正式機標準
		[[ "${HOST_NAME}" == "TEST" ]] && CRITERIA=${LOW_DISK_SPACE_WARNING_SIZE_TEST} || CRITERIA=${LOW_DISK_SPACE_WARNING_SIZE_LIVE}
		[[ "${HOST_NAME}" == "TEST" ]] && OUTPUT_FILE=${LOG_OUTPUT_TEST} || OUTPUT_FILE=${LOG_OUTPUT_LIVE}

		# 如果剩餘空間大小小於設定下限, 則輸出資訊到 LOG 檔
		if [ `echo "${SIZE} <= ${CRITERIA}" | bc` -eq 1 ]; then
			echo ${GAME_TYPE_DISPLAY},${COUNTRY_CODE_DISPLAY},${HOST_NAME},${METHOD},${DATA}
			echo ${GAME_TYPE_DISPLAY},${COUNTRY_CODE_DISPLAY},${HOST_NAME},${METHOD},${DATA} >> ${OUTPUT_FILE}
		fi
	fi
done
