#!/bin/bash

SCORE=0
DAY_NUM=5

function print_PASS() {
  echo -ne ' \033[1;32mPASS\033[0;39m '
}
function print_FAIL() {
  echo -ne ' \033[1;31mFAIL\033[0;39m '
}
function LINE {
  STTY_SIZE=$(stty size)
  STTY_COLUMNS=$(echo $STTY_SIZE | cut -f2 -d" ")
  yes = 2>/dev/null | sed $STTY_COLUMNS'q' | tr -d '\n'
  printf "\n"
}

function LCOURSE() {
  echo -n " The results of your Day${DAY_NUM}: "
  if [ ${SCORE} -ge 70 ]; then
    print_PASS ; echo -e " Your score: \033[1;32m${SCORE}\033[0;39m"
  else
    print_FAIL ; echo -e " Your score: \033[1;31m${SCORE}\033[0;39m"
  fi
}

function MQ1 {
  if [ "$MQ" = "1" ]; then MQ=1; else MQ=0; fi
}

#### GRADE ####
function g1 {
  MQ=1
  PSUM=$(ps o pid,comm,psr $(pgrep httpd) 2>/dev/null | grep -v PID | sort -nk3 | awk '{sum+=$3} END {print sum}')
	if [ "$PSUM" = "0" ]; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task1. 设置 apache 服务只能运行在第一个 CPU 上'
	fi
}

function g2 {
  MQ=1

	if grep -r \\[cfq] /sys/block/sd*/q* &>/dev/null || grep -r \\[none] /sys/block/vd*/q* &>/dev/null ; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task2. 修改磁盘的调度策略为 cfg'
	fi
}

function g3 {
  MQ=1

	if swapon | grep 512M | grep -wq 5; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task3. 使用 512MB 的文件增加交换分区的容量,优先级为 5'
	fi
}

function g4 {
  MQ=1
  IO_MAX=$(LANG=en sar 2>/dev/null | sort -nk6 | egrep -v '^A|L' | awk 'END {print $6}' 2>/dev/null )
  IO_TIME=$(LANG=en sar 2>/dev/null | awk "\$6 == $IO_MAX" 2>/dev/null | awk 'NR==1 {print $1}' 2>/dev/null )
	if /root/io.wait 2>/dev/null | grep -wq $IO_TIME 2>/dev/null ; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task4. 写个脚本 /root/io.wait'
	fi
}

function g5 {
  MQ=1
  WS=$(LANG=en sar -dp 2>/dev/null | sort -nk5 2>/dev/null | egrep -v '^A|^L|DEV' 2>/dev/null | awk 'END {print $5}' 2>/dev/null)
  [ -n $WS ] || 	if grep -q $(echo $WS*512 | bc 2>/dev/null ) /root/write.second 2>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task5. 写个脚本 /root/write.second'
	fi
}

function g6 {
  MQ=1

	if grep -wq ioctl /root/sys.call 2>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task6. 识别 /bin/dumpkeys 命令最频繁的系统调用名字'
	fi
}

function g7 {
  MQ=1
  SD=$(sysctl -a 2>/dev/null | awk '/vm.dirty_expire_centisecs/ {print $3}')
	if [ "$SD" = "4500" ]; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task7. 已经被修改的数据能够在被考虑写入磁盘前,在内存保留45秒'
	fi
}

function g8 {
  MQ=1
  SN=$(ps axo pid,nice,command | awk '/[/]sshd/ {print $2}')
	if [ "$SN" = "-5" ]; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task8. 调整 ssh 的守护进程的优先级为 -5'
	fi
}

function g9 {
  MQ=1

	if grep $(tuned-adm active | awk '{print $NF}') /root/tuned.name &>/dev/null; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task9. 推荐的调优方案'
	fi
}

function g10 {
  MQ=1

	if grep -q '@tom.*hard.*as.*131072' /etc/security/limits.conf; then
    MQ1
  else
    MQ=0
  fi
  if [ "$MQ" = "1" ]; then
		SCORE=$(expr $SCORE + 10 )
		print_PASS; 
		echo 'Task10. 限制用户最多可以合适内存 128MB'
	fi
}

# Main area
echo
echo -e " Spend Time: \e[1;32m$(uptime -p | sed -e 's/ hours, /h/' -e 's/ minutes/m/')\e[0;39m, $(date '+%Y-%m-%d %a %H:%M')"
LINE

g1
g2
g3
g4
g5
g6
g7
g8
g9
g10

LINE
LCOURSE
echo
