#/bin/bash

#请勿修改删除相关配置，不当操作会使得无法准确预测可用大小导致系统盘崩溃预警无法发现！
if [ ! -f  /etc/storage/file/base-storage ] ;then
	 BASE_ROOT_USED_BYTES=`du  -s --bytes  --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/run --exclude=/home/suanba --exclude=/datasets  / |awk '{print $1}'`
	 mkdir -p  /etc/storage/file
	 echo $BASE_ROOT_USED_BYTES > /etc/storage/file/base-storage
fi 

if [ -f  /etc/storage/file/base-storage ] ;then
	grep -E '[0-9]'  /etc/storage/file/base-storage  || echo `du  -s --bytes  --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/run --exclude=/home/suanba --exclude=/datasets  / |awk '{print $1}'` > /etc/storage/file/base-storage
	FINALL_ROOT_USED_BYTES=`du  -s --bytes    --exclude=/proc --exclude=/sys --exclude=/dev --exclude=/run --exclude=/home/suanba --exclude=/datasets  / |awk '{print $1}'`
	#GPU_NUM=`nvidia-smi --query-gpu=uuid --format=csv,noheader|wc -l`
	#ROOT_LIMITED_BYTES=$[${GPU_NUM}*32212254720]
	BASE_ROOT_USED_BYTES_FINAL=`cat /etc/storage/file/base-storage`
	if [ ${BASE_ROOT_USED_BYTES_FINAL} -le $[${FINALL_ROOT_USED_BYTES}+10240000] ] ;then
		ROOT_PER=$[(${FINALL_ROOT_USED_BYTES}-${BASE_ROOT_USED_BYTES_FINAL})*100/${ALLOC_ROOT_STORAGE}]
		USED=$[${FINALL_ROOT_USED_BYTES}-${BASE_ROOT_USED_BYTES_FINAL}]
		AVAIABLED=$[${ALLOC_ROOT_STORAGE}-(${FINALL_ROOT_USED_BYTES}-${BASE_ROOT_USED_BYTES_FINAL})]
	elif [ ${BASE_ROOT_USED_BYTES_FINAL} -gt $[${FINALL_ROOT_USED_BYTES}+51200000]  ];then
		echo "storge_error" > /var/log/storage_error.log
	fi
fi

if printenv |grep COMMAND_BASH   &> /dev/null ;then
    bash -c "$COMMAND_BASH"
fi

echo $USED > /etc/storage/file/used.file
echo $AVAIABLED > /etc/storage/file/ava.file
echo $ROOT_PER > /etc/storage/file/rootper.file
