#!/bin/bash
	ROOT_USED=`cat /etc/storage/file/used.file`
        ROOT_AVAIL=`cat /etc/storage/file/ava.file`
        ROOT_PER=`cat /etc/storage/file/rootper.file`%
	DATA_USED=`df -Th /home/suanba  |awk 'NR==2{print  $4}'`
	DATA_AVAIL=`df -Th /home/suanba  |awk 'NR==2{print  $5}'`
	DATA_PER=`df -Th /home/suanba  |awk 'NR==2{print  $6}'`
	DATA_SET_USED=`df -Th /datasets  |awk 'NR==2{print  $4}'`
	DATA_SET_AVAIL=`df -Th /datasets  |awk 'NR==2{print  $5}'`
	DATA_SET_PER=`df -Th /datasets  |awk 'NR==2{print  $6}'`
if [ ${ROOT_USED} -gt ${ALLOC_ROOT_STORAGE}  ] &> /dev/null ;then
	ROOT_AVAIL=0
fi
if [ ${ROOT_AVAIL} -lt 0  ]  &> /dev/null ;then
	ROOT_AVAIL=0
fi

if [ !  -f /var/log/storage_error.log ]  &> /dev/null ;then
  if [ ${ROOT_AVAIL} -lt 0  ]  &> /dev/null ;then
        HUMAN_ROOT_AVAIL=0
  elif [ ${ROOT_AVAIL} -lt 1048576  ]  &> /dev/null ;then
	HUMAN_ROOT_AVAIL=$[${ROOT_AVAIL}/1024]K
  elif [ ${ROOT_AVAIL} -lt 1073741824  ]  &> /dev/null ;then
        HUMAN_ROOT_AVAIL=$[${ROOT_AVAIL}/1048576]M
  else
	HUMAN_ROOT_AVAIL=$[${ROOT_AVAIL}/1073741824]G
  fi
  if   [ ${ROOT_USED} -lt 0  ]  &> /dev/null;then
	HUMAN_ROOT_USED=0
  elif [ ${ROOT_USED} -lt 1048576  ] &> /dev/null;then
	HUMAN_ROOT_USED=$[${ROOT_USED}/1024]K
  elif [ ${ROOT_USED} -lt 1073741824  ]  &> /dev/null;then
        HUMAN_ROOT_USED=$[${ROOT_USED}/1048576]M
  else
	HUMAN_ROOT_USED=$[${ROOT_USED}/1073741824]G
  fi
fi
printf "++----------------------------------------------------------------[1;32m欢迎使用[0m----------------------------------------------------------------++\n"

printf "[32m系统目录:[0m\n"

printf "╔════════════╦══════════╦═══════════════╦═════════╦════════╦═══════╦══════════════════════════════════════════════════════╗\n"

printf "║系统目录    ║名称      ║内容介绍       ║已用大小 ║可用大小║使用率 ║操作建议                                              ║\n"

printf "╠════════════╬══════════╬═══════════════╬═════════╬════════╬═══════╬══════════════════════════════════════════════════════╣\n"


printf "║/           ║系统盘    ║操作系统目录   ║%-4s     ║%-4s    ║%-4s   ║镜像系统初始环境，[1;31m注意*[0m [31m请勿将重要数据保留在系统盘中！[0m║\n"  $HUMAN_ROOT_USED $HUMAN_ROOT_AVAIL $ROOT_PER

printf "╠════════════╬══════════╬═══════════════╬═════════╬════════╬═══════╬══════════════════════════════════════════════════════╣\n"

printf "║/home/suanba║数据盘    ║数据目录存储   ║%-4s     ║%-4s    ║%-4s   ║支持数据持久化保存，[31m强烈建议将重要数据保留在数据盘内！[0m║\n"  $DATA_USED $DATA_AVAIL $DATA_PER

printf "╠════════════╬══════════╬═══════════════╬═════════╬════════╬═══════╬══════════════════════════════════════════════════════╣\n"

printf "║/datasets   ║数据集与大║suanba平台自带 ║         ║        ║       ║只读文件存储，可以使用该共享目录下的模型或数据集模型  ║\n"
printf "║            ║模型目录  ║共享模型数据   ║         ║        ║       ║                                                      ║\n"

printf "╚════════════╩══════════╩═══════════════╩═════════╩════════╩═══════╩══════════════════════════════════════════════════════╝\n"

if [ -f /sys/fs/cgroup/cpu.max  ];then
	CORE_NUM=`awk '{print $1/$2}' /sys/fs/cgroup/cpu.max`
	printf "[32mCPU:[0m         %s 核心\n" $CORE_NUM
fi
if [ -f /sys/fs/cgroup/cpu.max  ];then
	TOTAL_MEM=`awk '{print $1/1024/1024/1024}' /sys/fs/cgroup/memory.max`
	#SHM_MEM=`df -H  /dev/shm | awk  'NR==2{print $2}'`
	printf "[32m内存大小:  [0m  %sG\n" $TOTAL_MEM
	#printf "[32m共享内存:  [0m  %s \n" $SHM_MEM

fi
if printenv |grep CUDA_VISIBLE_DEVICES &> /dev/null;then
  NVIDIA_CARD_NUM=0
  printf "[32mGPU个数:[0m     %s 个\n" $NVIDIA_CARD_NUM
  GPU_MODEL=""
  printf "[32mGPU型号:[0m     %s\n" "$GPU_MODEL"
else
   if nvidia-smi >/dev/null 2>&1 ;then
 	NVIDIA_CARD_NUM=`nvidia-smi -L |wc -l`
 	printf "[32mGPU个数:[0m     %s 个\n" $NVIDIA_CARD_NUM
 	if [ $NVIDIA_CARD_NUM -eq 1 ];then
 		GPU_MODEL=`nvidia-smi -i 0 --query-gpu=name,index --format=csv,noheader`
 		printf "[32mGPU型号:[0m     %s\n" "$GPU_MODEL"
 	else
 		printf "[32mGPU型号:[0m \n"
 		 RESULT=$(expr $NVIDIA_CARD_NUM - 1)
 		 i=0
 		 while [ $i -le $RESULT ]; do
 		  GPU_MODEL=`nvidia-smi -i ${i} --query-gpu=name,index --format=csv,noheader`
            	  printf " %38s\n" "$GPU_MODEL"
            	  i=$((i + 1))
         	 done
 	fi
 
   fi
fi
printf "++----------------------------------------------------------------------------------------------------------------------------------------++\n"

printf "[31m注意事项:[0m\n"

printf "[31m1.请不要写满系统磁盘，或者使用超过最大内存大小，建议在执行高内存作业情况前保存镜像！[0m\n"
printf "[31m2.请勿删除或修改系统自带的配置文件或目录，以免导致系统崩溃系统数据丢失！[0m\n"


if [ -f /var/log/storage_error.log ];then
	printf "[1;31m警告!!检测到您删除了系统文件，此行为可能导致系统盘使用统计失效，无法准确预测您的使用情况与阈值!!数据盘使用率信息清空。请备份系统盘的重要数据![0m\n"
fi
if [ !  -f /var/log/storage_error.log ];then
  TEMP_ROOT_PER=`cat /etc/storage/file/rootper.file`
  if [ ${TEMP_ROOT_PER} -gt 90  ];then
	printf "[1;31m警告!!检测到您系统盘使用即将越限!!请及时备份系统盘的重要数据!避免数据丢失！[0m \n"
  fi
fi
