#!/bin/bash
############################################################
# $Name: install_sevice.lib
# $Version: v1.0
# $Functions:
#
# 0 print_log "$RETVAL" "description"
# 1 env_check
# 2 install_repo
# 3 function_install_nginx "nginx packet"
# 4 function_install_httpd 'httpd_packet'
# 5 MySQL Functions
# 5.1 function_create_app_DBConf "DBName" "DBHost" "DBPort" "DBPassword" "ZBXserver"
# 5.2 function_create_app_webCode_NginxWebConf "DBName" "Source_WebCode_path"
# 5.3 function_AppSQL_import "DBName" "DBPort" "SQL_Path1" "SQL_Path2" "SQL_Path3"
# 5.4 function_create_mysql_user_database "DBName" "DBPassword"
# 5.5 function_install_mysql_db 'DBName' 'DBPort' 'root_DBPassword' 'slave' 'backup_slave'
# 5.6 function_install_mysql "mysql_packet"
# 6 function_install_php 'libiconv packet' 'php packet' 'php_redis_plugin_packet' 'redis_host'
# 7 function_install_zabbix 'zabbix packet' '--enable-agent' '--enable-server' '--enable-proxy'
# 8 function_install_wordpress 'ServerName' 'wordpress_packet'
# 9 function_install_phpMyAdmin 'ServerName' 'phpMyAdmin_packet'
# 10 function_install_mysql_proxy 'proxy_packet' 'proxy_address' 'backend_addresses' 'read_backend_01,read_backend_02,read_backend_03'
# 11 function_install_drbd
# 12 function_install_heartbeat
# 13 function_create_java_jpress 'java_war_name'
# 14 function_install_tomcat 'tomcat_packet' '8081 8082 8083' 'jpress' 'java_war_name'
# 15 function_create_Springboard
# 16 function_rsyslog 'log_name' 'facility_number'
# 17 function_install_nfs 'nfs_data'
# 18 function_install_keepalived '[ MASTER | BACKUP ]' 'router_id' 'vrrp_instance' 'interface' 'virtual_router_id' 'virtual_ipaddress' 'REMOTE_HOSTNAME' 'redis_port'
# 19 function_install_redis "6381 6382 ..."
# 20 function_install_proxy 'backend_static' 'backend_dynamic'
# 21 function_net_ipv4_ip_forward_eth1
# 22 function_ifdown_eth
#
# $Author: Jeson Li
# $organization: www.lzbbg.cn
# $Create Date: 2017-03-10
# $Mail: libobin@tom.com
# $Description: Linux Service Installing Lib
############################################################
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
source /etc/profile
source /etc/rc.d/init.d/functions
Tik="\e[5m";Red="\e[31m";Grn="\e[32m";Ylw="\e[33m";Blu="\e[34m";Pup="\e[35m";Rst="\e[0m"
tools_scripts='/server/tools_scripts'
IP="$(grep "$(hostname)" /etc/hosts|awk 'NR==1{print $1}'|head -1)"
URL_download='http://download.lzbbg.cn:81/CentOS7/packets_tar'
DIR_versions='/apps/versions'
DIR_links='/apps/links'
download_to='/tmp'
# MySQL function_install_mysql_db variables
server_id="${DIR_links}/mysql/server_id"
DBPort="330$(cat $server_id)"
mysql_data_path='/mysql_data'
root_DBPassword='jeson123456'
# MySQL function_create_mysql_user_database variables
DBPassword='123456'
# function_create_app_webCode_NginxWebConf
webCode_path='/webCodes'
# function_install_wordpress
nfs_VIP='nfs_vip.lzbbg.cn'
nfs_data='/nfs_data'
mkdir -p ${tools_scripts} ${DIR_versions} ${DIR_links}
#######################################################################################################
# Print log USGE: print_log "$RETVAL" "description"
print_log(){
status_number=$1
description="$2"
if [ ${status_number} -eq 0 ];then
action "[$(date +%F_%T)] ${description}" /bin/true|tee -a ${download_to}/install_service.log
else
action "[$(date +%F_%T)] ${description}" /bin/false|tee -a ${download_to}/install_service.log
rm -f ${download_to}/install_service.lock
exit 1
fi
}
# Env check USGE: env_check
env_check(){
CPUS="$(lscpu |awk '$1~ "CPU.s.:"{print $NF*100}')"
CPULoad="$(uptime |awk -F"[ ,]+" '{print $9*100}')"
[ $CPUS -gt $CPULoad ]
RETVAL=$?
print_log ${RETVAL} "CPU Load"
DidskUse="$(df|grep -w "/"|awk -F "[ %]+" '{print $5}')"
[ $DidskUse -lt 85 ]
RETVAL=$?
print_log ${RETVAL} "No space left on device"
MemFree=$(free|awk '$1~"^Mem:"{print $NF/$2*100}')
[ ${MemFree%.*} -gt 15 ]
RETVAL=$?
print_log ${RETVAL} "No space left on memory"
Version="$(awk -F "[ .]+" '{print $4}' /etc/redhat-release)"
[ $Version -eq 7 ]
RETVAL=$?
print_log ${RETVAL} "Only use on CentOS release 7.X"
}
# Install repo in /etc/yum.d/ USGE: install_repo
install_repo(){
# CentOS6 yum repo
# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/3.5/rhel/6/x86_64/zabbix-release-3.5-1.el6.noarch.rpm
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
# CentOS7 yum repo
rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/3.5/rhel/7/x86_64/zabbix-release-3.5-1.el7.noarch.rpm
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/epel-7.repo
mv /etc/yum.repos.d/CentOS-Base.repo{,.$(date +%F)}
wget -O /etc/yum.repos.d/CentOS-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
RETVAL=$?
print_log ${RETVAL} "install_repo"
}
# function_install_nginx "Nginx packet"
function_install_nginx(){
nginx_packet="${1:-nginx-1.12.2.tar.gz}"
id www &>/dev/null || useradd -u 888 -s /sbin/nologin -M www
yum -y install openssl-devel bzip2-devel pcre-devel zlib-devel gd-devel nfs-utils
wget -O ${download_to}/${nginx_packet} ${URL_download}/${nginx_packet}
tar xzf ${download_to}/${nginx_packet} -C ${download_to}/
cd ${download_to}/${nginx_packet%.tar.gz}
nginx_parameter="
./configure
--prefix=${DIR_versions}/${nginx_packet%.tar.gz}
--user=www
--group=www
--with-http_stub_status_module
--with-http_ssl_module
--with-http_v2_module
--with-http_gzip_static_module
--with-http_flv_module
--with-pcre
--with-threads
"
${nginx_parameter}
make -j 4 && make install
RETVAL=$?
print_log ${RETVAL} "function_install_nginx ${nginx_packet}"
ln -s ${DIR_versions}/${nginx_packet%.tar.gz} ${DIR_links}/nginx
[ $(grep "^DIR_links=" /etc/profile) ] || echo "DIR_links=$DIR_links" >>/etc/profile
echo 'export PATH=${DIR_links}/nginx/sbin:$PATH' >>/etc/profile
sed -ri "/#log_format/,/http_x_forwarded_for/s@#@@g;/^[ \t]*#|^$/d" ${DIR_links}/nginx/conf/nginx.conf
sed -i "/error_page/i\ location /nginx_status {\n stub_status on;\n access_log off;\n allow 127.0.0.1;\n deny all;\n }" ${DIR_links}/nginx/conf/nginx.conf
${DIR_links}/nginx/sbin/nginx
chmod +x /etc/rc.d/rc.local
echo -e "### Nginx boot from the startup date: $(date +%F) user: $(whoami)###\n${DIR_links}/nginx/sbin/nginx\n" >>/etc/rc.d/rc.local
mkdir -p ${DIR_links}/nginx/conf/extra
cat >${DIR_links}/nginx/conf/extra/proxy.conf<<-EOF
proxy_redirect off;
proxy_set_header Host \$host;
proxy_set_header X-Forwarded-For \$remote_addr;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
EOF
}
# function_install_httpd 'httpd_packet'
function_install_httpd(){
httpd_packet="${1:-httpd-2.4.33.tar.gz}"
id www &>/dev/null || useradd -u 888 -s /sbin/nologin -M www
yum -y install openssl-devel libxml2-devel pcre pcre-devel expat-devel nfs-utils # nss-devel
cd ${download_to}
wget ${URL_download}/apr-1.6.3.tar.gz
wget ${URL_download}/apr-util-1.6.1.tar.gz
wget ${URL_download}/${httpd_packet}
wget ${URL_download}/cronolog-1.6.2.tar.gz
tar zxf ${download_to}/apr-1.6.3.tar.gz -C ${download_to}
cd ${download_to}/apr-1.6.3
./configure --prefix=/usr/local/apr && make -j 4 && make install
tar zxf ${download_to}/apr-util-1.6.1.tar.gz -C ${download_to}
cd ${download_to}/apr-util-1.6.1
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr --with-crypto && make -j 4 && make install
# 日志轮询命令cronolog,要先启动这个命令。要装在/usr/local 下,并且要比Apache先启动
tar zxf ${download_to}/cronolog-1.6.2.tar.gz -C ${download_to}
cd ${download_to}/cronolog-1.6.2
./configure --prefix=/usr/local && make -j 4 && make install
# 安装apache主程序
tar zxf ${download_to}/${httpd_packet} -C ${download_to}
cd ${download_to}/${httpd_packet%.ta*}
httpd_parameter="
./configure
--prefix=${DIR_versions}/${httpd_packet%.ta*}
--enable-deflate
--enable-expires
--enable-headers
--enable-modules=most
--enable-so
--enable-rewrite
--enable-mpms-shared=all
--enable-modules=most
--enable-ssl
--enable-cgi
--with-pcre
--with-z
--with-mpm=worker
--with-apr=/usr/local/apr
--with-apr-util=/usr/local/apr-util
"
${httpd_parameter}
make -j 4 && make install
RETVAL=$?
print_log ${RETVAL} "function_install_httpd ${httpd_packet}"
ln -s ${DIR_versions}/${httpd_packet%.tar.gz} ${DIR_links}/httpd
[ $(grep "^DIR_links=" /etc/profile) ] || echo "DIR_links=$DIR_links" >>/etc/profile
echo 'export PATH=${DIR_links}/httpd/bin:$PATH' >>/etc/profile
sed -i".$(date +%F)" "
/^User /s@daemon@www@
/^Group /s@daemon@www@
/^#ServerName /i ServerName 127.0.0.1:80
s#^ServerAdmin .*#ServerAdmin 1270963692@qq.com#
/httpd-vhosts.conf/s@#@@
/httpd-mpm.conf/s@#@@
/s Indexes F/s@Indexes@@
" ${DIR_links}/httpd/conf/httpd.conf
\cp ${DIR_links}/httpd/conf/extra/httpd-vhosts.conf{,.$(date +%F)} && >${DIR_links}/httpd/conf/extra/httpd-vhosts.conf
#/usr/local/sbin/cronolog start &
${DIR_links}/httpd/bin/apachectl
chmod +x /etc/rc.d/rc.local
echo -e "### Httpd boot from the startup date: $(date +%F) user: $(whoami)###\n${DIR_links}/httpd/bin/apachectl\n" >>/etc/rc.d/rc.local
}
# function_create_app_DBConf "DBName" "DBHost" "DBPort" "DBPassword" "ZBXserver"
function_create_app_DBConf(){
DBName="${1:-$DBName}"
DBHost="${2:-dbzabbix.lzbbg.cn}"
DBPort="${3:-330$(cat $server_id)}"
DBPassword="${4:-$DBPassword}"
ZBXserver="${5:-zabbix.lzbbg.cn}"
mkdir -p ${webCode_path}
cat >${webCode_path}/zabbix/conf/zabbix.conf.php<<-EOF
<?php
// Zabbix GUI configuration file.
global \$DB;
\$DB['TYPE'] = 'MYSQL';
\$DB['SERVER'] = '${DBHost}';
\$DB['PORT'] = '${DBPort}';
\$DB['DATABASE'] = '${DBName}';
\$DB['USER'] = '${DBName}';
\$DB['PASSWORD'] = '${DBPassword}';
// Schema name. Used for IBM DB2 and PostgreSQL.
\$DB['SCHEMA'] = '';
\$ZBX_SERVER = '${ZBXserver}';
\$ZBX_SERVER_PORT = '10051';
\$ZBX_SERVER_NAME = '${ZBXserver}';
\$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
EOF
chown -R www.www ${webCode_path}/
RETVAL=$?
print_log ${RETVAL} "function_create_app_DBConf ${DBName}"
}
# function_create_app_webCode_NginxWebConf "ServerName" "Source_WebCode_path"
function_create_app_webCode_NginxWebConf(){
ServerName="${1:-$DBName}"
Source_WebCode_path="${2:-${download_to}/${zabbix_packet%.tar.gz}/frontends/php/*}"
mkdir -p ${webCode_path}/${ServerName}
\cp -a ${Source_WebCode_path} ${webCode_path}/${ServerName}/
chown -R www.www ${webCode_path}/
source /etc/profile
if [ $(which nginx) ];then
cat >${DIR_links}/nginx/conf/extra/${ServerName}.conf<<-EOF
server {
listen 80;
server_name ${ServerName}.lzbbg.cn ;
root ${webCode_path}/${ServerName}/;
location / {
index index.php index.html index.htm;
}
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log logs/access_${ServerName}.log main buffer=32k flush=5s;
}
EOF
sed -i "$ i\ \tinclude extra/${ServerName}.conf;" ${DIR_links}/nginx/conf/nginx.conf
${DIR_links}/nginx/sbin/nginx -s stop
${DIR_links}/nginx/sbin/nginx
RETVAL=$?
print_log ${RETVAL} "function_create_app_webCode_NginxWebConf nginx ${ServerName}"
fi
if [ -e ${DIR_links}/httpd/conf/extra/httpd-vhosts.conf ];then
cat >>${DIR_links}/httpd/conf/extra/httpd-vhosts.conf<<-EOF
<Directory "${webCode_path}/${ServerName}">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<VirtualHost *:80 >
ServerAdmin 1270963692@qq.org
DocumentRoot ${webCode_path}/${ServerName}
ServerName ${ServerName}.lzbbg.cn
ServerAlias ${ServerName}1.lzbbg.cn
ErrorLog "logs/${ServerName}-error_log"
CustomLog "logs/${ServerName}-access_log" combined
</VirtualHost>
EOF
${DIR_links}/httpd/bin/httpd -k graceful
RETVAL=$?
print_log ${RETVAL} "function_create_app_webCode_NginxWebConf httpd ${ServerName}"
fi
}
# function_AppSQL_import "DBName" "DBPort" "SQL_Path1" "SQL_Path2" "SQL_Path3"
function_AppSQL_import(){
# MySQL function_AppSQL_import
DBName="${1:-$DBName}"
DBPort="${2:-330$(cat $server_id)}"
SQL_Path1="${3:-${download_to}/${zabbix_packet%.tar.gz}/database/mysql/schema.sql}"
SQL_Path2="${4:-${download_to}/${zabbix_packet%.tar.gz}/database/mysql/images.sql}"
SQL_Path3="${5:-${download_to}/${zabbix_packet%.tar.gz}/database/mysql/data.sql}"
[ "${DBName}" == "zabbix_proxy" ] && { SQL_Path2='';SQL_Path3='' ;}
for SQL_Path_sum in $SQL_Path1 $SQL_Path2 $SQL_Path3
do
${DIR_links}/mysql/bin/mysql -u root -p${root_DBPassword} -S ${mysql_data_path}/${DBName}_${DBPort}/mysql.sock ${DBName}<$SQL_Path_sum
done
RETVAL=$?
print_log ${RETVAL} "function_AppSQL_import ${DBName} DBPort:${DBPort}"
}
# function_create_mysql_user_database "DBName" "DBPassword"
function_create_mysql_user_database(){
DBName="$1"
DBUsers="$2"
rep_name="$3"
DBPort="${4:-330$(cat $server_id)}"
DBPassword="${5:-$DBPassword}"
if [ -n "${DBUsers}" ];then
for DBUser in $(echo ${DBUsers})
do
${DIR_links}/mysql/bin/mysql -u root -p${root_DBPassword} -S ${mysql_data_path}/${DBName}_${DBPort}/mysql.sock -e "
create database ${DBUser} character set utf8 collate utf8_general_ci;
grant all on ${DBUser}.* to ${DBUser}@'%' identified by '${DBPassword}';
delete from mysql.user where host='$(hostname)' or user='';
flush privileges;"
RETVAL=$?
print_log ${RETVAL} "function_create_mysql_user_database ${DBUser} ${DBUser}@'%'"
done
fi
if [ -n "${rep_name}" ];then
${DIR_links}/mysql/bin/mysql -u root -p${root_DBPassword} -S ${mysql_data_path}/${DBName}_${DBPort}/mysql.sock -e "
grant replication slave on *.* to ${rep_name}@'%' identified by '${DBPassword}';
delete from mysql.user where host='$(hostname)' or user='';
flush privileges;"
RETVAL=$?
print_log ${RETVAL} "function_create_mysql_user_database ${rep_name}@'%'"
fi
}
# function_install_mysql_db 'DBName' 'DBPort' 'root_DBPassword' 'slave' 'backup_slave'
function_install_mysql_db(){
echo $(($(cat $server_id)+1))>$server_id
DBName="${1:-$DBName}"
DBPort="${2:-330$(cat $server_id)}"
root_DBPassword="${3:-$root_DBPassword}"
mysql_bin2nfs='mysql_bin2nfs'
log_bin="log_bin=${mysql_data_path}/${DBName}_${DBPort}/${mysql_bin2nfs}/mysql_bin"
if [ "$4" == "slave" ];then
log_bin=''
read_only="read_only"
relay_log="relay_log=${mysql_data_path}/${DBName}_${DBPort}/relay_log"
expire_logs_days='expire_logs_days=7'
slave_skip_errors='slave_skip_errors=1007,1008,1032,1062'
\cp ${download_to}/scripts/tools_scripts/auto_rep_sql.sh ${tools_scripts}
if [ "$5" == "backup_slave" ];then
log_bin="log_bin=${mysql_data_path}/${DBName}_${DBPort}/${mysql_bin2nfs}/mysql_bin"
log_slave_updates="log-slave-updates"
source ${download_to}/scripts/rsync_backup.lib
\mv ${download_to}/scripts/tools_scripts/mysql_bak.sh ${cron_scripts}
chmod +x ${cron_scripts}/*.sh
echo -e "# Backup ${mysql_data_path}/${DBName}_${DBPort} data to store-rsync-01 date:$(date +%F) user:$(whoami)\n* * * * * /bin/rsync -rz --password-file=${passwordfile} ${mysql_data_path}/${DBName}_${DBPort}/${mysql_bin2nfs} ${auth_users}@store-rsync-01::${module_backup}/${DBName}_${DBPort}/ &>/dev/null\n00 00 * * * ${cron_scripts}/mysql_bak.sh &>/dev/null" >>/var/spool/cron/$(whoami)
fi
fi
mkdir -p ${mysql_data_path}/${DBName}_${DBPort}/{data,${mysql_bin2nfs}}
${DIR_links}/mysql/scripts/mysql_install_db --basedir=${DIR_links}/mysql --datadir=${mysql_data_path}/${DBName}_${DBPort}/data
RETVAL=$?
print_log ${RETVAL} "function_install_mysql_db ${DBName}_${DBPort}"
cat >${mysql_data_path}/${DBName}_${DBPort}/my.cnf<<-EOF
[client]
port=${DBPort}
socket=${mysql_data_path}/${DBName}_${DBPort}/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
user=mysql
basedir=${DIR_links}/mysql
datadir=${mysql_data_path}/${DBName}_${DBPort}/data
port=${DBPort}
socket=${mysql_data_path}/${DBName}_${DBPort}/mysql.sock
server-id=$(cat $server_id)
${log_bin}
${log_slave_updates}
${read_only}
${relay_log}
${expire_logs_days}
${slave_skip_errors}
skip-character-set-client-handshake
character-set-server=utf8
slow_query_log=1
long_query_time=1
log_queries_not_using_indexes
slow_query_log_file=${mysql_data_path}/${DBName}_${DBPort}/slow.log
expire_logs_days = 7
[mysqldump]
quick
max_allowed_packet=2M
[mysqld_safe]
log-error=${mysql_data_path}/${DBName}_${DBPort}/mysql.err
pid-file=${mysql_data_path}/${DBName}_${DBPort}/mysqld.pid
EOF
chown -R mysql.mysql ${mysql_data_path}/
if [ -n "${log_bin}" ];then
echo -e "### MySQL boot from the startup date: $(date +%F) user: $(whoami)###\n${DIR_links}/mysql/bin/mysqld_safe --defaults-file=${mysql_data_path}/${DBName}_${DBPort}/my.cnf &\nsleep 5\n" >>/etc/rc.d/rc.local
# /bin/mount -t nfs rsync01.lzbbg.cn:/backup/$(hostname).mysql_bin ${mysql_data_path}/${DBName}_${DBPort}/${mysql_bin2nfs}
# echo -e "### MySQL boot from the startup date: $(date +%F) user: $(whoami)###\n/bin/mount -t nfs rsync01.lzbbg.cn:/backup/$(hostname).mysql_bin ${mysql_data_path}/${DBName}_${DBPort}/${mysql_bin2nfs} && \
# ${DIR_links}/mysql/bin/mysqld_safe --defaults-file=${mysql_data_path}/${DBName}_${DBPort}/my.cnf &\nsleep 5\n" >>/etc/rc.d/rc.local
else
echo -e "### MySQL boot from the startup date: $(date +%F) user: $(whoami)###\n${DIR_links}/mysql/bin/mysqld_safe --defaults-file=${mysql_data_path}/${DBName}_${DBPort}/my.cnf &\nsleep 5\n" >>/etc/rc.d/rc.local
fi
${DIR_links}/mysql/bin/mysqld_safe --defaults-file=${mysql_data_path}/${DBName}_${DBPort}/my.cnf &
sleep 5
${DIR_links}/mysql/bin/mysqladmin -uroot -S ${mysql_data_path}/${DBName}_${DBPort}/mysql.sock password ${root_DBPassword}
sleep 5
chmod +x /etc/rc.d/rc.local
}
# function_install_mysql "mysql_packet"
function_install_mysql(){
mysql_packet="${1:-mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz}"
server_id_num="${2:-1}"
id mysql &>/dev/null || useradd -u 3306 -s /sbin/nologin -M mysql
wget -O ${download_to}/${mysql_packet} ${URL_download}/${mysql_packet}
tar xzf ${download_to}/${mysql_packet} -C ${DIR_versions}/
RETVAL=$?
print_log ${RETVAL} "function_install_mysql ${mysql_packet}"
ln -s ${DIR_versions}/${mysql_packet%.tar.gz} ${DIR_links}/mysql
[ $(grep "^DIR_links=" /etc/profile) ] || echo "DIR_links=$DIR_links" >>/etc/profile
echo 'export PATH=${DIR_links}/mysql/bin:$PATH' >>/etc/profile
sed -i "s#/usr/local/#${DIR_links}/#g" ${DIR_links}/mysql/bin/mysqld_safe
echo $((${server_id_num}-1)) >$server_id
}
# function_install_php 'libiconv packet' 'php packet' 'php_redis_plugin_packet' 'redis_host'
function_install_php(){
libiconv="${1:-libiconv-1.14.tar.gz}"
php_packet="${2:-php-5.6.35.tar.gz}"
php_redis_plugin_packet="$3"
redis_host="${4:-cache_vip.lzbbg.cn:6380}"
httpd_apxs="$(find / -path "*/bin/apxs")"
[ -n "$httpd_apxs" ] && httpd_apxs2="--with-apxs2=${httpd_apxs}"
wget -O ${download_to}/${libiconv} ${URL_download}/${libiconv}
wget -O ${download_to}/${php_packet} ${URL_download}/${php_packet}
yum install -y zlib-devel libxml2-devel libjpeg-turbo-devel freetype-devel libpng-devel libjpeg-turbo-devel gd-devel libxslt-devel libmcrypt-devel mhash mhash-devel mcrypt libcurl-devel openssl-devel bzip2-devel expect openldap-devel
\cp -a /usr/lib64/libldap* /usr/lib
# install libiconv
tar xzf ${download_to}/${libiconv} -C ${download_to}/
cd ${download_to}/${libiconv%.tar.gz}
sed -i '698i#if defined(__GLIBC__) && !defined(__UCLIBC__) && !__GLIBC_PREREQ(2, 16)' ${download_to}/${libiconv%.tar.gz}/srclib/stdio.in.h
sed -i '700i#endif' ${download_to}/${libiconv%.tar.gz}/srclib/stdio.in.h
./configure --prefix=${DIR_versions}/${libiconv%.tar.gz}
make -j 4 && make install
RETVAL=$?
print_log ${RETVAL} "Install ${libiconv%.tar.gz}"
ln -s ${DIR_versions}/${libiconv%.tar.gz} ${DIR_links}/libiconv
#install ${php_packet}
id www &>/dev/null || useradd -u 888 -s /sbin/nologin -M www
tar xzf ${download_to}/${php_packet} -C ${download_to}/
cd ${download_to}/${php_packet%.tar.gz}
php_parameter="
./configure --prefix=${DIR_versions}/${php_packet%.tar.gz}
--enable-mbstring --with-iconv-dir=${DIR_links}/libiconv
--with-mysql=mysqlnd --enable-inline-optimization
--with-mysqli=mysqlnd --enable-gd-native-ttf
--with-pdo-mysql=mysqlnd --with-openssl
--with-mcrypt --with-mhash
--with-freetype-dir --enable-pcntl
--with-jpeg-dir --enable-sockets
--with-png-dir --with-xmlrpc
--with-zlib --enable-soap
--with-libxml-dir=/usr --enable-short-tags
--enable-xml --enable-static
--disable-rpath --with-xsl
--enable-bcmath --with-fpm-user=www
--enable-shmop --with-fpm-group=www
--enable-sysvsem --enable-zip
--with-gd --with-bz2
--with-curl --enable-ftp
--enable-mbregex --with-ldap
--enable-fpm --with-gettext ${httpd_apxs2}
"
${php_parameter}
sed -i '/^EXTRA_LIBS/s#.*#& -llber#g' ${download_to}/${php_packet%.tar.gz}/Makefile
make -j 4 && make install
RETVAL=$?
print_log ${RETVAL} "Install ${php_packet}"
ln -s ${DIR_versions}/${php_packet%.tar.gz} ${DIR_links}/php
[ $(grep "^DIR_links=" /etc/profile) ] || echo "DIR_links=$DIR_links" >>/etc/profile
echo 'export PATH=${DIR_links}/php/bin:${DIR_links}/php/sbin:$PATH' >>/etc/profile
\cp -a ${download_to}/${php_packet%.tar.gz}/php.ini-production ${DIR_links}/php/lib/php.ini
\cp ${DIR_versions}/${php_packet%.tar.gz}/etc/php-fpm.conf.default ${DIR_versions}/${php_packet%.tar.gz}/etc/php-fpm.conf
cat >>${DIR_links}/php/lib/php.ini<<-EOF
max_execution_time = 300
memory_limit = 128M
post_max_size = 16M
upload_max_filesize = 2M
max_input_time = 300
always_populate_raw_post_data = -1
date.timezone = Asia/Shanghai
EOF
# php + redis session cache_vip.lzbbg.cn:6380
if [ -n "${php_redis_plugin_packet}" ];then
wget -O ${download_to}/${php_redis_plugin_packet} ${URL_download}/${php_redis_plugin_packet}
tar xf ${download_to}/php_redis_plugin.tar.gz -C ${download_to}/
cd ${download_to}/phpredis
${DIR_links}/php/bin/phpize
./configure --with-php-config=${DIR_links}/php/bin/php-config
make && so_tmp="$(make install)"
so_path="$(echo $so_tmp |awk '{print $NF}')"
sed -ri "
s@session.save_handler = .*@session.save_handler = redis@
s@.*session.save_path = \"N;/path\"@session.save_path = \"tcp://${redis_host}\"@
$ a extension_dir = "${so_path}"
$ a extension = redis.so
" ${DIR_links}/php/lib/php.ini
fi
# php + httpd
if [ -n "${httpd_apxs2}" ];then
sed -ri".$(date +%F)" "
s@( DirectoryIndex index.html)@\1 index.php@
/AddType application\/x-gzip .gz .tgz/a \ AddType application/x-httpd-php .php .phtml\n AddType application/x-httpd-php-source .phps
" ${DIR_links}/httpd/conf/httpd.conf
else
${DIR_links}/php/sbin/php-fpm
chmod +x /etc/rc.d/rc.local
echo -e "### PHP-fpm boot from the startup date: $(date +%F) user: $(whoami)###\n${DIR_links}/php/sbin/php-fpm\n" >>/etc/rc.d/rc.local
fi
}
# function_install_zabbix 'zabbix packet' '--enable-agent' '--enable-server' '--enable-proxy'
function_install_zabbix(){
zabbix_packet="${1:-zabbix-3.4.10.tar.gz}"
zabbix_agent="$2"
zabbix_server="$3"
zabbix_proxy="$4"
DBName="${5:-${DBName}}"
wget -O ${download_to}/${zabbix_packet} ${URL_download}/${zabbix_packet}
yum -y install net-snmp-devel libssh2 libssh2-devel curl-devel libxml2-devel libevent-devel pcre-devel mysql-devel gcc unixODBC-devel zabbix-java-gateway OpenIPMI-devel openldap-devel
\cp -frp /usr/lib64/libldap* /usr/lib
tar xzf ${download_to}/${zabbix_packet} -C ${download_to}/
cd ${download_to}/${zabbix_packet%.tar.gz}
zabbix_parameter="
./configure
--prefix=${DIR_versions}/${zabbix_packet%.tar.gz}
--sysconfdir=${DIR_versions}/${zabbix_packet%.tar.gz}
--with-mysql
--with-net-snmp
--with-libcurl
--with-libxml2
--with-ssh2
--with-openssl
--with-unixodbc
--enable-ipv6
--with-ldap
--with-openipmi ${zabbix_agent} ${zabbix_proxy} ${zabbix_server}
"
${zabbix_parameter}
make -j 4 && make install
RETVAL=$?
print_log ${RETVAL} "function_install_zabbix ${zabbix_packet} ${zabbix_agent} ${zabbix_proxy} ${zabbix_server}"
ln -s ${DIR_versions}/${zabbix_packet%.tar.gz} ${DIR_links}/zabbix
[ $(grep "^DIR_links=" /etc/profile) ] || echo "DIR_links=$DIR_links" >>/etc/profile
echo 'export PATH=${DIR_links}/zabbix/bin:${DIR_links}/zabbix/sbin:$PATH' >>/etc/profile
if [ -n "${zabbix_server}" ];then
# create user and import mysql data
function_install_mysql_db 'zabbix'
function_create_mysql_user_database 'zabbix' 'zabbix'
function_AppSQL_import
function_create_app_webCode_NginxWebConf
function_create_app_DBConf
sed -i".$(date +%F)" "
/^# Include.*\*\.conf$/aInclude=$DIR_links\/zabbix\/zabbix_server.conf.d\/*.conf
/^# AlertScriptsPath=/aAlertScriptsPath=$DIR_links\/zabbix\/share\/zabbix\/alertscripts
/^# ExternalScripts=/aExternalScripts=$DIR_links\/zabbix\/share\/zabbix\/externalscripts
s@^# DBHost=.*@DBHost=${DBHost}@
s@^DBName=.*@DBName=${DBName}@
s@^DBUser=.*@DBUser=${DBName}@
s@^# DBPassword=.*@DBPassword=${DBPassword}@
s@^# DBPort=.*@DBPort=${DBPort}@
" ${DIR_links}/zabbix/zabbix_server.conf
${DIR_links}/zabbix/sbin/zabbix_server
chmod +x /etc/rc.d/rc.local
echo -e "### Zabbix_server boot from the startup date: $(date +%F) user: $(whoami)###\n${DIR_links}/zabbix/sbin/zabbix_server\n" >>/etc/rc.d/rc.local
fi
if [ -n "${zabbix_agent}" ];then
sed -i".$(date +%F)" "
/^# Include.*\*\.conf$/aInclude=$DIR_links\/zabbix\/zabbix_agentd.conf.d\/*.conf
s@^Server=.*@Server=zabbix.lzbbg.cn@
s@^Hostname=.*@Hostname=$(hostname)@
s@^ServerActive=.*@# ServerActive=zabbix.lzbbg.cn@
" ${DIR_links}/zabbix/zabbix_agentd.conf
# Create 'tcp_status nginx_status memcached_status redis_status' monitor item
echo "UserParameter=linux_status[*],${DIR_links}/zabbix/zabbix_agentd.conf.d/zabbix_linux_plugin.sh \"\$1\" \"\$2\" \"\$3\"" >${DIR_links}/zabbix/zabbix_agentd.conf.d/zabbix-linux-plugin.conf
\cp ${download_to}/scripts/tools_scripts/zabbix_linux_plugin.sh ${DIR_links}/zabbix/zabbix_agentd.conf.d/
# MySQL monitor
source /etc/profile
CMD_test="which mysql"
[ -n "$CMD_test" ] && {
php_cmd="$(which php)"
if [ -n "$php_cmd" ];then
ln -s $php_cmd /usr/bin/php
else
yum -y install php php-mysql
fi
sed "s#/var/lib/zabbix/percona/scripts/#${DIR_links}/zabbix/zabbix_agentd.conf.d/#g" ${download_to}/scripts/tools_scripts/userparameter_percona_mysql.conf >${DIR_links}/zabbix/zabbix_agentd.conf.d/userparameter_percona_mysql.conf
\cp ${download_to}/scripts/tools_scripts/{get_mysql_stats_wrapper.sh,ss_get_mysql_stats.php} ${DIR_links}/zabbix/zabbix_agentd.conf.d/
cat >${DIR_links}/zabbix/zabbix_agentd.conf.d/ss_get_mysql_stats.php.cnf <<-EOF
<?php
\$mysql_user = 'root';
\$mysql_pass = '${root_DBPassword}';
\$mysql_port = ${DBPort};
\$mysql_socket = '${mysql_data_path}/${DBName}_${DBPort}/mysql.sock';
EOF
}
chmod -R +x ${DIR_links}/zabbix/zabbix_agentd.conf.d/{*.sh,*.php}
${DIR_links}/zabbix/sbin/zabbix_agentd
chmod +x /etc/rc.d/rc.local
echo -e "### Zabbix_agentd boot from the startup date: $(date +%F) user: $(whoami)###\n${DIR_links}/zabbix/sbin/zabbix_agentd\n" >>/etc/rc.d/rc.local
fi
if [ -n "${zabbix_proxy}" ];then
# create user and import mysql data
function_install_mysql_db 'zabbix_proxy'
function_create_mysql_user_database 'zabbix_proxy' 'zabbix_proxy'
function_AppSQL_import
sed -i".$(date +%F)" "
/^# Include.*\*\.conf$/aInclude=$DIR_links\/zabbix\/zabbix_proxy.conf.d\/*.conf
s@^Server=.*@Server=zabbix.lzbbg.cn@
s@^Hostname=.*@Hostname=$(hostname)@
s@^# DBHost=.*@DBHost=${DBHost}@
s@^DBName=.*@DBName=${DBName}@
s@^DBUser=.*@DBUser=${DBName}@
s@^# DBPassword=.*@DBPassword=${DBPassword}@
s@^# DBPort=.*@DBPort=${DBPort}@
" ${DIR_links}/zabbix/zabbix_proxy.conf
${DIR_links}/zabbix/sbin/zabbix_proxy
chmod +x /etc/rc.d/rc.local
echo -e "### Zabbix_proxy boot from the startup date: $(date +%F) user: $(whoami)###\n${DIR_links}/zabbix/sbin/zabbix_proxy\n" >>/etc/rc.d/rc.local
fi
}
# function_install_wordpress 'ServerName' 'wordpress_packet'
function_install_wordpress(){
ServerName="${1:-blog}"
wordpress_packet="${2:-wordpress-4.9.4-zh_CN.tar.gz}"
nfs_mount_uploads="${webCode_path}/${ServerName}/wp-content/uploads"
wget -O ${download_to}/${wordpress_packet} ${URL_download}/${wordpress_packet}
tar xzf ${download_to}/${wordpress_packet} -C ${download_to}/
function_create_app_webCode_NginxWebConf "${ServerName}" "${download_to}/wordpress/*"
mkdir -p ${nfs_mount_uploads}
[[ $(ls ${nfs_mount_uploads}) ]] &>/dev/null && { umount -lf ${nfs_mount_uploads} &>/dev/null;mount -t nfs ${nfs_VIP}:${nfs_data}/uploads ${nfs_mount_uploads};}
chmod +x /etc/rc.d/rc.local
cat >>/etc/rc.d/rc.local<<-EOF
### mount jpress from the startup date: $(date +%F) user: $(whoami)###
[[ \$(ls ${nfs_mount_uploads}) ]] &>/dev/null && { umount -lf ${nfs_mount_uploads} &>/dev/null;mount -t nfs ${nfs_VIP}:${nfs_data}/uploads ${nfs_mount_uploads};}
EOF
cat >>/var/spool/cron/$(whoami)<<-EOF
# web_nfs_remount date:$(date +%F) user:$(whoami)
* * * * * [[ \$(/bin/ls ${nfs_mount_uploads}) ]] &>/dev/null||{ /bin/umount -lf ${nfs_mount_uploads} &>/dev/null;/bin/mount -t nfs ${nfs_VIP}:${nfs_data}/uploads ${nfs_mount_uploads};} &>/dev/null
EOF
}
# function_install_tomcat 'tomcat_packet' '8081 8082 8083' 'jpress' 'java_war_name'
function_install_tomcat(){
tomcat_packet="${1:-apache-tomcat-8.5.34.tar.gz}"
tomcat_http_ports="${2:-8080}"
ServerName="${3:-tomcat}"
java_war_name="${4:-jpress-web-newest.war}"
tomcat_webCode_path=${webCode_path}/"${java_war_name%.war}"
mkdir -p ${tomcat_webCode_path}/ROOT
yum -y install java
wget -O ${download_to}/${tomcat_packet} ${URL_download}/${tomcat_packet}
tar xf ${download_to}/${tomcat_packet} -C ${download_to}/
sed -i".$(date +%F)" '/ echo "Using CATALINA_BASE: $CATALINA_BASE"/i JAVA_OPTS="$JAVA_OPTS -server -XX:PermSize=128M -XX:MaxPermSize=1024m"' ${download_to}/${tomcat_packet%.ta*}/bin/catalina.sh
for tomcat_port in $(echo ${tomcat_http_ports})
do
tomcat_shutdown_port="$((${tomcat_port}-70))"
AJP_port="$((${tomcat_port}+30))"
upstream_servers="${upstream_servers}\tserver 127.0.0.1:${tomcat_port};\n"
BalancerMember="${BalancerMember}\tBalancerMember ajp://localhost:${AJP_port} loadfactor=1 route=tomcat_${tomcat_port} smax=5 max=20 ttl=120 retry=300 timeout=15\n"
\cp -a ${download_to}/${tomcat_packet%.ta*} ${DIR_versions}/${tomcat_packet%.ta*}_${tomcat_port}
#sed -re '22s#8005#8011#' -e '69s#8080#8081#' -e '148s#appBase=".*"# appBase="/data/www/www"#' /application/tomcat8_1/conf/server.xml
sed -ri".$(date +%F)" "
/port=\"8005\"/s@8005@${tomcat_shutdown_port}@
/port=\"8080\"/s@8080@${tomcat_port}@
/appBase=\"webapps\"/s@webapps@${tomcat_webCode_path}@
/AJP\/1.3/s@8009@${AJP_port}@
s@(.* defaultHost=\"localhost\")@\1 jvmRoute=\"tomcat_${tomcat_port}\"@
" ${DIR_versions}/${tomcat_packet%.ta*}_${tomcat_port}/conf/server.xml
ln -s ${DIR_versions}/${tomcat_packet%.ta*}_${tomcat_port} ${DIR_links}/tomcat_${tomcat_port}
${DIR_links}/tomcat_${tomcat_port}/bin/startup.sh
chmod +x /etc/rc.d/rc.local
echo -e "### tomcat_${tomcat_port} boot from the startup for $(date +%F) from $(whoami)###\n${DIR_links}/tomcat_${tomcat_port}/bin/startup.sh\n" >>/etc/rc.local
done
source /etc/profile
if [ $(which nginx) ];then
cat >${DIR_links}/nginx/conf/extra/${ServerName}.conf<<-EOF
upstream web_pools {
$(echo -e ${upstream_servers})
}
server {
listen 80;
server_name ${ServerName}.lzbbg.cn ;
root ${tomcat_webCode_path};
location / {
index index.jsp index.html index.htm;
proxy_pass http://web_pools;
include extra/proxy.conf;
}
access_log logs/access_${ServerName}.log main buffer=32k flush=5s;
}
EOF
sed -i "$ i\ \tinclude extra/${ServerName}.conf;" ${DIR_links}/nginx/conf/nginx.conf
${DIR_links}/nginx/sbin/nginx -s stop
${DIR_links}/nginx/sbin/nginx
RETVAL=$?
print_log ${RETVAL} "function_create_app_webCode_NginxWebConf ${ServerName}"
fi
if [ -e ${DIR_links}/httpd/conf/extra/httpd-vhosts.conf ];then
sed -i".$(date +%F)" "
/mod_lbmethod_byrequests.so/s@#@@
/mod_proxy.so/s@#@@
/mod_proxy_ajp.so/s@#@@
/mod_proxy_balancer.so/s@#@@
/mod_proxy_connect.so/s@#@@
/mod_proxy_ftp.so/s@#@@
/mod_proxy_http.so/s@#@@
/mod_slotmem_shm.so/s@#@@
" ${DIR_links}/httpd/conf/httpd.conf
cat >>${DIR_links}/httpd/conf/extra/httpd-vhosts.conf<<-EOF
<VirtualHost *:80>
ServerAdmin 1270963692@qq.com
ServerName ${ServerName}.lzbbg.cn
ServerAlias ${ServerName}1.lzbbg.cn
ProxyPass / balancer://cluster/ stickysession=JSESSIONID|jsessionid nofailover=On
ProxyPassReverse / balancer://cluster/
ErrorLog "logs/${ServerName}-error.log"
CustomLog "logs/${ServerName}-access.log" combined
</VirtualHost>
#The ProxyRequests directive should usually be set off when using ProxyPass.
ProxyRequests Off
<proxy balancer://cluster>
$(echo -e ${BalancerMember})
</proxy>
EOF
${DIR_links}/httpd/bin/apachectl -k restart
RETVAL=$?
print_log ${RETVAL} "function_create_app_webCode_NginxWebConf ${ServerName}"
fi
}
# function_create_java_jpress 'java_war_name'
function_create_java_jpress(){
java_war_name="${1:-jpress-web-newest.war}"
nfs_mount_attachment="${webCode_path}/${java_war_name%.war}/ROOT/attachment"
mkdir -p ${nfs_mount_attachment}
wget -O ${download_to}/${java_war_name} ${URL_download}/${java_war_name}
cd ${webCode_path}/${java_war_name%.war}/ROOT
unzip ${download_to}/${java_war_name}
chown -R www.www ${webCode_path}/
[[ $(ls ${nfs_mount_attachment}) ]] &>/dev/null && { umount -lf ${nfs_mount_attachment} &>/dev/null;mount -t nfs ${nfs_VIP}:${nfs_data}/attachment ${nfs_mount_attachment};}
chmod +x /etc/rc.d/rc.local
echo -e "### mount jpress from the startup date: $(date +%F) user: $(whoami)###\n[[ \$(ls ${nfs_mount_attachment}) ]] &>/dev/null && { umount -lf ${nfs_mount_attachment} &>/dev/null;mount -t nfs ${nfs_VIP}:${nfs_data}/attachment ${nfs_mount_attachment};}" >>/etc/rc.d/rc.local
echo -e "# web_nfs_remount date:$(date +%F) user:$(whoami)\n* * * * * [[ \$(/bin/ls ${nfs_mount_attachment}) ]] &>/dev/null||{ /bin/umount -lf ${nfs_mount_attachment} &>/dev/null;/bin/mount -t nfs ${nfs_VIP}:${nfs_data}/attachment ${nfs_mount_attachment};} &>/dev/null\n" >>/var/spool/cron/$(whoami)
}
# function_install_phpMyAdmin 'ServerName' 'phpMyAdmin_packet'
function_install_phpMyAdmin(){
ServerName="${1:-phpMyAdmin}"
phpMyAdmin_packet="${2:-phpMyAdmin-4.8.3-all-languages.tar.gz}"
wget -O ${download_to}/${phpMyAdmin_packet} ${URL_download}/${phpMyAdmin_packet}
tar xzf ${download_to}/${phpMyAdmin_packet} -C ${download_to}/
function_create_app_webCode_NginxWebConf "${ServerName}" "${download_to}/${phpMyAdmin_packet%.tar*}/*"
sed -i "s#^[$]cfg\['AllowArbitraryServer.*#\$cfg['AllowArbitraryServer'] = true;#g" ${webCode_path}/${ServerName}/libraries/config.default.php
}
# function_install_mysql_proxy 'proxy_packet' 'proxy_address' 'backend_addresses' 'read_backend_01,read_backend_02,read_backend_03'
function_install_mysql_proxy(){
mysql_proxy_packet="${1:-mysql-proxy-0.8.5-linux-glibc2.3-x86-64bit.tar.gz}"
proxy_ports="${2:-3306}"
backend_addresses="${3:-dbmaster_vip.lzbbg.cn:3301}"
read_only_backend_addresses="${4:-dbslave01.lzbbg.cn:3302,dbslave01.lzbbg.cn:3303}"
id mysql-proxy &>/dev/null || useradd -u 4040 -s /sbin/nologin -M mysql-proxy
wget -O ${download_to}/${mysql_proxy_packet} ${URL_download}/${mysql_proxy_packet}
tar xzf ${download_to}/${mysql_proxy_packet} -C ${DIR_versions}/
RETVAL=$?
print_log ${RETVAL} "function_install_mysql_proxy ${mysql_proxy_packet}"
ln -s ${DIR_versions}/${mysql_proxy_packet%.tar.gz} ${DIR_links}/mysql-proxy
[ $(grep "^DIR_links=" /etc/profile) ] || echo "DIR_links=$DIR_links" >>/etc/profile
echo 'export PATH=${DIR_links}/mysql-proxy/bin:$PATH' >>/etc/profile
sed -ri".$(date +%F)" "
s@^[\t]+min_idle_connections = [0-9],@\tmin_idle_connections = 1,@g
s@^[\t]+max_idle_connections = [0-9]+,@\tmax_idle_connections = 1,@g
" ${DIR_links}/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
for proxy_port in $(echo ${proxy_ports})
do
cat >${DIR_links}/mysql-proxy/mysql-proxy_${proxy_port}.cnf<<-EOF
[mysql-proxy]
daemon=true
keepalive=true
plugins=proxy,admin
user=mysql-proxy
max-open-files=2048
event-threads=50
admin-username=admin
admin-password=admin
admin-address=0.0.0.0:$((735+proxy_port))
proxy-address=0.0.0.0:${proxy_port}
proxy-backend-addresses=${backend_addresses}
proxy-read-only-backend-addresses=${read_only_backend_addresses}
admin-lua-script=${DIR_links}/mysql-proxy/lib/mysql-proxy/lua/admin.lua
proxy-lua-script=${DIR_links}/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
log-level=debug
log-file=${DIR_links}/mysql-proxy/mysql-proxy_${proxy_port}.log
pid-file=${DIR_links}/mysql-proxy/mysql-proxy_${proxy_port}.pid
EOF
echo -e "### Mysql-proxy_${proxy_port} boot from the startup date: $(date +%F) user: $(whoami)###\nsleep 10\n${DIR_links}/mysql-proxy/bin/mysql-proxy --defaults-file=${DIR_links}/mysql-proxy/mysql-proxy_${proxy_port}.cnf\n" >>/etc/rc.d/rc.local
chmod 660 ${DIR_links}/mysql-proxy/mysql-proxy_${proxy_port}.cnf
${DIR_links}/mysql-proxy/bin/mysql-proxy --defaults-file=${DIR_links}/mysql-proxy/mysql-proxy_${proxy_port}.cnf
done
chmod +x /etc/rc.d/rc.local
/bin/chown -R mysql-proxy.mysql-proxy ${DIR_links}/mysql-proxy/
}
function_install_drbd(){
echo -e "n\np\n\n+1G\n"|fdisk /dev/sda
yum -y install drbd84-utils kmod-drbd84
\cp /etc/drbd.d/global_common.conf{,.$(date +%F)}
cat >/etc/drbd.d/global_common.conf <<-EOF
global {
usage-count no;
}
common {
net {
protocol C;
}
}
EOF
cat >/etc/drbd.d/mysql_data.res <<-EOF
resource mysql_data {
disk /dev/sda3;
device /dev/drbd0;
meta-disk internal;
on mysql-master-01 {
address 172.16.1.50:7788;
}
on mysql-master-02 {
address 172.16.1.51:7788;
}
EOF
}
function_install_drbd(){
yum -y install drbd84-utils kmod-drbd84
cp /etc/drbd.d/global_common.conf{,.$(date +%F)}
cat >/etc/drbd.d/global_common.conf<<-EOF
global {
usage-count no;
}
common {
net {
protocol C;
}
}
EOF
cat >/etc/drbd.d/mysql_data.res <<-EOF
resource mysql_data {
disk /dev/sda3;
device /dev/drbd0;
meta-disk internal;
on mysql-master-01 {
address 172.16.1.50:7788;
}
on mysql-master-02 {
address 172.16.1.51:7788;
}
EOF
echo -e "n\n p\n \n \n +1G\n w"|fdisk /dev/sda
partprobe /dev/sda
modprobe drbd
echo -e "modprobe drbd\ndrbdadm up all\n">>/etc/rc.local
chmod +x /etc/rc.d/rc.local
drbdadm create-md all
drbdadm up all
sleep 5
[ "$(hostname)" == "mysql-master-01" ] && { drbdadm primary all --force;mkfs.xfs /dev/drbd0 ;}
}
# function_install_heartbeat
function_install_heartbeat(){
# Configuring the basic environment
rpm -e OpenIPMI-devel-2.0.23-2.el7.x86_64
yum install -y bzip2 autoconf automake libtool glib2-devel libxml2-devel bzip2-devel libtool-ltdl-devel asciidoc libuuid-devel # psmisc
wget -O ${download_to}/Reusable-Cluster-Components-glue--0a7add1d9996.tar.bz2 ${URL_download}/Reusable-Cluster-Components-glue--0a7add1d9996.tar.bz2
wget -O ${download_to}/resource-agents-3.9.6.tar.gz ${URL_download}/resource-agents-3.9.6.tar.gz
wget -O ${download_to}/Heartbeat-3-0-958e11be8686.tar.bz2 ${URL_download}/Heartbeat-3-0-958e11be8686.tar.bz2
# Install glue
tar xf ${download_to}/Reusable-Cluster-Components-glue--0a7add1d9996.tar.bz2 -C ${download_to}
cd ${download_to}/Reusable-Cluster-Components-glue--0a7add1d9996/
id hacluster &>/dev/null || { groupadd haclient;useradd -g haclient hacluster -s /sbin/nologin ;}
./autogen.sh
./configure --prefix=${DIR_versions}/heartbeat-3.0.6/ --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
make && make install
RETVAL=$?
print_log ${RETVAL} "function_install_heartbeat Install glue"
ln -s ${DIR_versions}/heartbeat-3.0.6 ${DIR_links}/heartbeat
[ $(grep "^DIR_links=" /etc/profile) ] || echo "DIR_links=$DIR_links" >>/etc/profile
echo 'export PATH=${DIR_links}/heartbeat/sbin:$PATH' >>/etc/profile
# Install Resource Agents
tar xf ${download_to}/resource-agents-3.9.6.tar.gz -C ${download_to}
cd ${download_to}/resource-agents-3.9.6/
./autogen.sh
export CFLAGS="$CFLAGS -I${DIR_links}/heartbeat/include -L${DIR_links}/heartbeat/lib"
./configure --prefix=${DIR_versions}/heartbeat-3.0.6/ --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
echo "${DIR_links}/heartbeat/lib" >>/etc/ld.so.conf.d/heartbeat.conf
ldconfig
make && make install
RETVAL=$?
print_log ${RETVAL} "function_install_heartbeat Install Resource Agents"
# Install HeartBeat-3.0.6
tar xf ${download_to}/Heartbeat-3-0-958e11be8686.tar.bz2 -C ${download_to}
cd ${download_to}/Heartbeat-3-0-958e11be8686
./bootstrap
export CFLAGS="$CFLAGS -I${DIR_links}/heartbeat/include -L${DIR_links}/heartbeat/lib"
./configure --prefix=${DIR_versions}/heartbeat-3.0.6/ --with-daemon-user=hacluster --with-daemon-group=haclient --enable-fatal-warnings=no LIBS='/lib64/libuuid.so.1'
sed -ri 's#(.*define HA_HBCONF_DIR.*)#/*\1*/#g' ${DIR_links}/heartbeat/include/heartbeat/glue_config.h
make && make install
RETVAL=$?
print_log ${RETVAL} "function_install_heartbeat Install HeartBeat-3.0.6"
# Copy configuration file
\cp -a /etc/ha.d/resource.d/* ${DIR_links}/heartbeat/etc/ha.d/resource.d/ &>/dev/null
ln -svf ${DIR_links}/heartbeat/lib64/heartbeat/plugins/RAExec/* ${DIR_links}/heartbeat/lib/heartbeat/plugins/RAExec/
ln -svf ${DIR_links}/heartbeat/lib64/heartbeat/plugins/* ${DIR_links}/heartbeat/lib/heartbeat/plugins/
cat >${DIR_links}/heartbeat/etc/ha.d/authkeys<<-EOF
auth 1
1 sha1 0145a867ac4d266620cef1e5ed5a7680ec93339c
EOF
chmod 600 ${DIR_links}/heartbeat/etc/ha.d/authkeys
mkdir -p /mysql_data
cat >${DIR_links}/heartbeat/etc/ha.d/haresources<<-EOF
$(hostname) drbddisk::all filesystem::/dev/drbd0::/mysql_data mysqld IPaddr::172.16.1.52/24/eth0
EOF
cat >${DIR_links}/heartbeat/etc/ha.d/ha.cf<<-EOF
debugfile /var/log/ha-debug
logfile /var/log/ha-log
logfacility local1
keepalive 2
deadtime 30
warntime 10
initdead 60
#ucast eth0 172.16.1.50 #设定心跳方式使用单播方式,并且是在eth0接口上进行单播,ip地址为对方的IP(网卡名称要一致性的IP)从机要改成主机的IP
mcast eth0 225.0.0.31 694 1 0
#bcast eth0
auto_failback off
node mysql-master-01 mysql-master-02
crm no
EOF
cat >${DIR_links}/heartbeat/etc/ha.d/resource.d/filesystem<<-EOF
#!/bin/sh
if [ \$# -gt 1 ];then
dev="\${1:-/dev/drbd0}"
dir="\${2:-/mnt/}"
action=\$3
else
dev="/dev/drbd0"
dir="/mnt/"
action=\$1
fi
case \${action} in
start)
mount \${dev} \${dir}
;;
stop)
umount -lf \${dir}
;;
*)
echo "\$0 [dev] [dir] [start|stop]"
esac
exit 0
EOF
chmod +x ${DIR_links}/heartbeat/etc/ha.d/resource.d/filesystem
cat >${DIR_links}/heartbeat/etc/ha.d/resource.d/mysqld <<-EOF
#!/bin/sh
source /etc/profile
case \$1 in
start)
${DIR_links}/mysql/bin/mysqld_safe --defaults-file=/mysql_data/ShareDB_3301/my.cnf &
;;
stop)
${DIR_links}/mysql/bin/mysqladmin -uroot -pjeson123456 -S /mysql_data/ShareDB_3301/mysql.sock shutdown
;;
*)
echo "\$0 [start|stop]"
esac
EOF
chmod +x ${DIR_links}/heartbeat/etc/ha.d/resource.d/mysqld
systemctl enable heartbeat
systemctl start heartbeat
}
function_create_Springboard(){
echo "[ \$UID -ne 0 ] && [ \$USER != 'jeson' ] && source ${tools_scripts}/Springboard.sh" >/etc/profile.d/tb.sh
\mv ${download_to}/scripts/tools_scripts/{Springboard.sh,send_sshkey.exp} ${tools_scripts}
}
# function_rsyslog 'log_name' 'facility_number'
function_rsyslog(){
log_name="$1"
facility_number="$2"
sed -ri".$(date +%F)" "
s@^#(\$ModLoad imudp)@\1@
s@^#(\$UDPServerRun 514)@\1@
s@^#(\$ModLoad imtcp)@\1@
s@^#(\$InputTCPServerRun 514)@\1@
$ a \ \n# Save ${log_name} messages also to ${log_name}.log date:$(date +%F) user: $(whoami)
$ a local${facility_number}.* \t /var/log/${log_name}.log
" /etc/rsyslog.conf
systemctl restart rsyslog
}
# function_install_nfs 'nfs_data'
function_install_nfs(){
nfs_data="${1:-/nfs_data/}"
nfs_user='nobody'
nfs_uid='99'
allow_IP="${IP%.*}.0/24"
yum -y install nfs-utils
eval mkdir -p ${nfs_data}
id ${nfs_user} &>/dev/null || { groupadd -g ${nfs_uid} ${nfs_user};useradd -u ${nfs_uid} -g ${nfs_uid} -s /sbin/nologin -M ${nfs_user};}
eval chown -R ${nfs_user}.${nfs_user} ${nfs_data}
\cp /etc/exports{,.$(date +%F).$(whoami)}
for nfs_dir in $(eval echo ${nfs_data})
do
echo "${nfs_dir} ${allow_IP}(rw,sync,all_squash,anonuid=${nfs_uid},anongid=${nfs_uid})" >>/etc/exports
done
systemctl start rpcbind
systemctl start nfs
systemctl enable rpcbind
systemctl enable nfs
}
# function_install_keepalived '[ MASTER | BACKUP ]' 'router_id' 'vrrp_instance' 'interface' 'virtual_router_id' 'virtual_ipaddress' 'REMOTE_HOSTNAME' 'redis_port'
function_install_keepalived(){
role="$1"
router_id="${2:-nfs_service}"
vrrp_instances="${3:-nfs}"
interface="${4:-eth0}"
virtual_router_id="${5:-51}"
virtual_ipaddress="${6:-${IP%.*}.32}"
REMOTE_HOSTNAME=$7
redis_port="${8:-6380}"
if [ "${role}" == "MASTER" -o "${role}" == "BACKUP" ];then
RETVAL=$?
print_log ${RETVAL} 'function_install_keepalived'
else
RETVAL=$?
print_log ${RETVAL} 'USAGE: function_install_keepalived [ MASTER | BACKUP ],function_install_keepalived '
fi
if [[ $(echo ${vrrp_instances} | grep " ") ]];then
[ "${role}" == "MASTER" ] && priority=100 || priority=50
nopreempt=''
else
[ "${role}" == "MASTER" ] && { priority=100 ;nopreempt=nopreempt ;} || { priority=50 ;nopreempt='' ;}
role=BACKUP
fi
# use redis keepalived
if [ -n "${REMOTE_HOSTNAME}" ];then
vrrp_script="
vrrp_script chk_redis {\n
\tscript /etc/keepalived/scripts/redis_check.sh\n
\tinterval 2\n}"
track_script="track_script {\n\tchk_redis\n\t}"
notify="\tnotify_master /etc/keepalived/scripts/redis_master.sh\n
\tnotify_backup \"/etc/keepalived/scripts/redis_backup.sh ${REMOTE_HOSTNAME}\"\n
\tnotify_fault /etc/keepalived/scripts/redis_fault.sh\n
\tnotify_stop /etc/keepalived/scripts/redis_stop.sh"
mkdir -p /etc/keepalived/scripts
cat >/etc/keepalived/scripts/redis_master.sh<<-EOF
#!/bin/bash
redis_port="\${1:-${redis_port}}"
REDISCLI="/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
sleep 10 #延迟10秒待数据被对方同步完成之后再切换主从角色
echo -e "[keepalived master]\t\$(date +%F_%T)\nRun SLAVEOF NO ONE cmd ..." >>\${LOGFILE}
\$REDISCLI -h \$(hostname) -p \${redis_port} SLAVEOF NO ONE &>>\${LOGFILE}
EOF
cat >/etc/keepalived/scripts/redis_backup.sh<<-EOF
#!/bin/bash
REMOTE_HOSTNAME="\${1:-${REMOTE_HOSTNAME}}"
redis_port="\${2:-${redis_port}}"
REDISCLI="/bin/redis-cli"
LOGFILE="/var/log/keepalived-redis-state.log"
sleep 15 #延迟15秒待数据被对方同步完成之后再切换主从角色
echo -e "[keepalived backup]\t\$(date +%F_%T)\nRun SLAVEOF \${REMOTE_HOSTNAME} \${redis_port} ..." >>\${LOGFILE}
\$REDISCLI -h \$(hostname) -p \${redis_port} SLAVEOF \${REMOTE_HOSTNAME} \${redis_port} &>>\${LOGFILE}
EOF
cat >/etc/keepalived/scripts/redis_check.sh<<-EOF
#!/bin/bash
redis_port="\${1:-${redis_port}}"
LOGFILE=/var/log/keepalived-redis-state.log
ALIVE=\$(/bin/redis-cli -h \$(hostname) -p \${redis_port} PING)
if [ "\$ALIVE" == "PONG" ]; then
exit 0
else
echo -e "[redis_check fault]\t\$(date +%F_%T)" >> \$LOGFILE
exit 1
fi
EOF
cat >/etc/keepalived/scripts/redis_fault.sh<<-EOF
#!/bin/bash
systemctl stop keepalived.service
EOF
cat >/etc/keepalived/scripts/redis_stop.sh<<-EOF
#!/bin/bash
LOGFILE=/var/log/keepalived-redis-state.log
echo -e "[keepalived stop]\t\$(date +%F_%T)" >> \$LOGFILE
EOF
chmod +x /etc/keepalived/scripts/*.sh
fi
yum -y install keepalived
\cp /etc/keepalived/keepalived.conf{,.$(date +%F).$(whoami)}
cat >/etc/keepalived/keepalived.conf <<-EOF
! Configuration File for keepalived
global_defs {
notification_email {
1270963592@qq.com
}
notification_email_from 18978038790@189.cn
smtp_server smtp.189.cn
smtp_connect_timeout 30
router_id ${router_id}
vrrp_skip_check_adv_addr
# vrrp_strict # can not use ping
vrrp_garp_interval 0
vrrp_gna_interval 0
}
$(echo -e ${vrrp_script})
EOF
for vrrp_instance in $(echo ${vrrp_instances})
do
cat >>/etc/keepalived/keepalived.conf <<-EOF
vrrp_instance ${vrrp_instance} {
state ${role}
interface ${interface}
virtual_router_id ${virtual_router_id}
priority ${priority}
${nopreempt}
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
$(echo -e ${track_script})
virtual_ipaddress {
${virtual_ipaddress}/24 dev ${interface} label ${interface}:${virtual_ipaddress#*.*.*.}
}
$(echo -e ${notify})
}
EOF
[ "${role}" == "MASTER" ] && { role=BACKUP;((priority-=50));} || { role=MASTER;((priority+=50));}
((virtual_router_id+=5))
virtual_ipaddress="${virtual_ipaddress%.*}.$(echo $((${virtual_ipaddress#*.*.*.}+1)))"
done
systemctl start keepalived
systemctl enable keepalived
}
# function_install_redis "6381 6382 ..."
function_install_redis(){
redis_ports="${1:-6379}"
yum -y install redis
echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
sysctl vm.overcommit_memory=1
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >>/etc/rc.local
for redis_port in $(echo ${redis_ports})
do
sed "
s@^daemonize .*@daemonize yes@
s@^bind .*@bind 0.0.0.0@
s@^port .*@port ${redis_port}@
s@^pidfile .*@pidfile /var/run/redis_${redis_port}.pid@
s@^logfile .*@logfile /var/log/redis/redis_${redis_port}.log@
" /etc/redis.conf >/etc/redis_${redis_port}.conf
redis-server /etc/redis_${redis_port}.conf
chmod +x /etc/rc.d/rc.local
echo -e "### Redis_${redis_port} boot from the startup date: $(date +%F) user: $(whoami)###\nredis-server /etc/redis_${redis_port}.conf\n" >>/etc/rc.d/rc.local
done
}
# function_install_proxy 'backend_static' 'backend_dynamic'
function_install_proxy(){
backend_static="${1:-monitor.lzbbg.cn}"
backend_dynamics="${2:-web01.lzbbg.cn:80 web02.lzbbg.cn:80}"
seq=0
for backend_dynamic_name in $(echo ${backend_dynamics})
do
((++seq))
backend_dynamic="${backend_dynamic}\tserver\tnode_dynamic_${seq} ${backend_dynamic_name} check maxconn 1000\n"
done
yum -y install haproxy
\cp /etc/haproxy/haproxy.cfg{,.$(date +%F)}
cat >/etc/haproxy/haproxy.cfg<<-EOF
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
log-send-hostname
stats socket /var/lib/haproxy/stats
defaults
mode http
log global
option httplog
option dontlognull
option httpclose
option logasap
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
listen stats
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth jeson:jeson
stats admin if TRUE
frontend main *:80
capture request header Host len 20
capture request header Referer len 60
acl host_agent hdr_reg(user-agent) -i .*firefox.*
acl host_static hdr_beg(host) -i download.
acl host_zabbix hdr_beg(host) -i zabbix.
acl host_dynamic hdr_beg(host) -i bldog. phpmyadmin. jpress.
use_backend agent if ! host_agent
use_backend static if host_static
use_backend zabbix if host_zabbix
use_backend dynamic if host_dynamic
default_backend dynamic
backend agent
balance roundrobin
server node_agent_1 ${backend_static}:81 check maxconn 3000
backend static
balance roundrobin
server node_static_1 ${backend_static}:81 check maxconn 3000
backend zabbix
balance roundrobin
server node_zabbix_1 ${backend_static}:80 check maxconn 3000
backend dynamic
balance roundrobin
$(echo -e ${backend_dynamic})
EOF
systemctl start haproxy
systemctl enable haproxy
function_rsyslog 'haproxy' '2'
}
function_net_ipv4_ip_forward_eth1(){
sed -i '/net.ipv4.ip_forward = 0/d;$ a net.ipv4.ip_forward = 1' /etc/sysctl.conf
iptables -t nat -A POSTROUTING -s ${IP%.*}.0/24 -o eth1 -j MASQUERADE
}
function_ifdown_eth1(){
route add default gw monitor.lzbbg.cn
echo -e "route add default gw monitor.lzbbg.cn\n" >>/etc/rc.local
sed -i "s@ONBOOT=yes@ONBOOT=no@" /etc/sysconfig/network-scripts/ifcfg-eth1
ifdown eth1
}