サーバー完成図書を自動で作りたいんだ 第4話 Nginxの基本設定まわり

#!/bin/sh
IFS=$'\n'
SCRIPT_PATH=`echo $(cd $(dirname $0);pwd)`
DOCUMENT_ROOTS=""
ACCESS_LOGS=""
ERROR_LOGS=""

function search_conf() {
  _SERVER_ROOT=$1
  _CONF_FILE=$2

  if [[ ! "${_CONF_FILE}"  =~ ^/ ]]; then
    _CONF_FILE="${_SERVER_ROOT}/${_CONF_FILE}"
  fi

  echo "[INFO] Nginxの設定ファイル ${_CONF_FILE} をチェックします" >&2
  if [ `egrep -r "^[ \t]*root " ${CONF_FILE} | wc -l` -gt 0 ]; then
    for LINE in `egrep -r "^[ \t]*root " ${CONF_FILE} | sed -e "s/^[ \t]*root[ \t]*//g" | sed -e "s/[\'\"]//g" | sed -e "s/#.*//g"`
    do
      LINE=`echo ${LINE} | sed -e "s/;$//"`
      if [ `echo ${DOCUMENT_ROOTS} | grep "${LINE}," | wc -l` -eq 0 ]; then
        DOCUMENT_ROOTS="${DOCUMENT_ROOTS} ${LINE},"
      fi
    done
  fi

  if [ `egrep -r "^[ \t]*access_log " ${CONF_FILE} | wc -l` -gt 0 ]; then
    for LINE in `egrep -r "^[ \t]*access_log " ${CONF_FILE} | sed -e "s/^[ \t]*access_log[ \t]*//g" | sed -e "s/[\'\"]//g" | sed -e "s/[# ].*$//g"`
    do
      if [[ ! "${LINE}"  =~ ^/ ]]; then
        LINE="${_SERVER_ROOT}/${LINE}"
      fi
      LINE=`echo ${LINE} | sed -e "s/;$//"`
      if [ `echo ${ACCESS_LOGS} | grep "${LINE}," | wc -l` -eq 0 ]; then
        ACCESS_LOGS="${ACCESS_LOGS} ${LINE},"
      fi
    done
  fi

  if [ `egrep -r "^[ \t]*error_log " ${CONF_FILE} | wc -l` -gt 0 ]; then
    for LINE in `egrep -r "^[ \t]*error_log " ${CONF_FILE} | sed -e "s/^[ \t]*error_log[ \t]*//g" | sed -e "s/[\'\"]//g" | sed -e "s/[# ].*$//g"`
    do
      if [[ ! "${LINE}"  =~ ^/ ]]; then
        LINE="${_SERVER_ROOT}/${LINE}"
      fi
      LINE=`echo ${LINE} | sed -e "s/;$//"`
      if [ `echo ${ACCESS_LOGS} | grep "${LINE}," | wc -l` -eq 0 ]; then
        ERROR_LOGS="${ACCESS_LOGS} ${LINE},"
      fi
    done
  fi

  if [ `egrep -r "^[ \t]*include " ${_CONF_FILE} | wc -l` -gt 0 ]; then
    for INCLUDE in `egrep -r "^[ \t]*include " ${_CONF_FILE} | sed -e "s/^[ \t]*include[ \t]*//g" | sed -e "s/[\'\"]//g" | sed -e "s/#.*//g"`
    do
      if [[ ! "${INCLUDE}"  =~ ^/ ]]; then
        INCLUDE="${_SERVER_ROOT}/${INCLUDE}"
      fi
      INCLUDE=`echo ${INCLUDE} | sed -e "s/;$//"`
      if [ `ls -1 ${INCLUDE} | wc -l` -gt 0 ]; then
        for CHILE_CONF in `ls -1 ${INCLUDE}`
        do
          search_conf "${_SERVER_ROOT}" "${CHILE_CONF}"
        done
      fi
    done
  fi
}

if [ -f "/etc/init.d/nginx" ]; then
  CONF_FILE=`cat /etc/init.d/nginx | grep config: | grep -v chkconfig | grep -v sysconfig | cut -f2 -d: | sed -e "s/[ \t]\+//g"`
  if [ -f "${CONF_FILE}" ]; then
    echo "|_. 項目|_. 値|"
    search_conf "${SERVER_ROOT}" "${CONF_FILE}"

    echo "|設定ファイル|${CONF_FILE}|"
    DOCUMENT_ROOTS=`echo ${DOCUMENT_ROOTS} | sed -e "s/,$//"`
    echo "|ドキュメントルート|${DOCUMENT_ROOTS}|"

    ACCESS_LOGS=`echo ${ACCESS_LOGS} | sed -e "s/,$//"`
    echo "|アクセスログ|${ACCESS_LOGS}|"

    ERROR_LOGS=`echo ${ERROR_LOGS} | sed -e "s/,$//"`
    echo "|エラーログ|${ERROR_LOGS}|"

    if [ -f "/etc/logrotate.d/nginx" ]; then
      sh ${SCRIPT_PATH}/textile_logrotate.sh /etc/logrotate.d/nginx
    fi
  fi
fi