agatan / isucon7-final

ISUCON7 本戦
2 stars 0 forks source link

最初に追加するスクリプト #2

Open agatan opened 6 years ago

agatan commented 6 years ago

link_etc.sh

#! /bin/bash

set -eu

MYSQL_CONF_DIR=/etc/mysql
NGINX_CONF_DIR=/etc/nginx

mkdir -p $HOME/$MYSQL_CONF_DIR
mkdir -p $HOME/$NGINX_CONF_DIR

TARGETS=(        
        /etc/sysctl.conf
        /etc/mysql/my.cnf
        /etc/mysql/conf.d
        /etc/nginx/nginx.conf
        /etc/nginx/conf.d
        /etc/nginx/sites-enabled
        /etc/nginx/sites-available
)

for target in ${TARGETS[@]}
do
  if [[ ! -e $target.backup ]]; then
    sudo cp -r $target $target.backup
    sudo mv $target $HOME/$target
    # sudo chmod -R a+rw $HOME/$target
    sudo ln -s $HOME/$target $target
  fi
done

rotate.sh


#! /bin/bash

set -eu

SCRIPTS_DIR=$(dirname "$0")

echo '>>> Rotating alp log... <<<'
$SCRIPTS_DIR/rotate_alplog.sh
echo '>>> Rotating alp log... Success! <<<'
echo ''
echo '>>> Rotating slow log... <<<'
$SCRIPTS_DIR/rotate_slowlog.sh
echo '>>> Rotating slow log... Success!<<<'

rotate_alplog.sh

#! /bin/bash

set -eu

ALP_LOG_FILE=/var/log/nginx/access.alp.log
ROTATE_ALP_LOG_DIR="$HOME/alp"
ROTATE_ALP_LOG_FILE="$ROTATE_ALP_LOG_DIR/alp-$(date '+%Y%m%d%H%M%S').log"

rotate() {
  echo 'rotate'

  mkdir -p "$ROTATE_ALP_LOG_DIR"
  sudo cp "$ALP_LOG_FILE" "$ROTATE_ALP_LOG_FILE"
  sudo truncate --size 0 $ALP_LOG_FILE
}

rotate

rotate_slowlog.sh

#! /bin/bash

set -eu

MYSQL_USER='root'
MYSQL_PASSWD=''

SLOW_LOG_FILE='/tmp/slowlog.log'

ROTATE_SLOW_LOG_DIR="$HOME/slowlog"
ROTATE_SLOW_LOG_FILE="$ROTATE_SLOW_LOG_DIR/slowlog-$(date '+%Y%m%d%H%M%S').log"

LONG_QUERY_TIME=0

if [ ! -z "${MYSQL_PASSWD-}" ]; then
  MYSQL="mysql -u $MYSQL_USER -p $MYSQL_PASSWD"
else
  MYSQL="mysql -u $MYSQL_USER"
fi

rotate() {
  echo 'rotate'

  mkdir -p "$ROTATE_SLOW_LOG_DIR"
  if [ -f $SLOW_LOG_FILE ]; then
    sudo mv "$SLOW_LOG_FILE" "$ROTATE_SLOW_LOG_FILE"
    sudo chown "$(whoami)" "$ROTATE_SLOW_LOG_FILE"
  fi
}

describe_current_conf(){
  echo ''
  echo 'Describe current conf'
  echo '====================='

  eval "$MYSQL --skip-column-names" << EOS
show variables like 'slow_query_log%';
show variables like 'long_query_time';
EOS

  echo '====================='
  echo ''
}

disable_slow_log() {
  echo 'disable slow log'

  eval "$MYSQL" << EOS
set global slow_query_log=0;
EOS
}

enable_slow_log() {
  echo 'enable slow log'

  eval "$MYSQL" << EOS
set global slow_query_log=1;
set global slow_query_log_file='$SLOW_LOG_FILE';
set global long_query_time=$LONG_QUERY_TIME;
EOS
}

describe_current_conf
disable_slow_log
rotate
enable_slow_log
describe_current_conf
0gajun commented 6 years ago
wget https://gist.githubusercontent.com/0gajun/fb7151f36d120130ab3db8698ec2f0aa/raw/dd2124ed5287d2c612096098d3e89d182a04e780/rotate.sh

wget https://gist.githubusercontent.com/0gajun/6ce50e0295f9e21534eee817800fc91d/raw/e961db272679e981e8a0ce7a791a5a99b8fc1da0/rotate_slowlog.sh

wget https://gist.githubusercontent.com/0gajun/60ec393edc0c89f729976f9da71ee4c8/raw/2f6d253fa90efdac887915e50e0873d42dc9350f/rotate_alplog.sh

wget https://gist.githubusercontent.com/0gajun/4f137ef43aad3a62f191c3421f740af4/raw/f898769f6ffdfbe47045b89b441a430cea4e6e8d/link_etc.sh
agatan commented 6 years ago
GOOS=linux go build -o app .....
HOSTS="isu1 isu2 isu3"
for target in ${HOSTS[@]}
do
    ssh $target sudo systemctl stop isu
    scp ./app isucon@$target:/home/isucon/...
    ssh $target sudo systemctl start isu
done