#!/bin/bash
####networktest
function networkAndFtp()  
{
    timeout=5
 
        target=www.baidu.com
 
    ret_code=`curl ----connect-timeout $timeout $target -%{http_code} | tail -n1`
 
    if "x$ret_code" = "x200" ]; then
          echo -"\033[42;37m Network Check Complate!!! Continue \033[0m"
    else
          echo -"Network Error!!! Program has exit"
          exit 1
    fi
}
 
#添加计划任务每天0点1分同步一次时间
yum install ntpdate -y
#使用时间服务器同步
echo "01 0 * * * ntpdate time.pool.aliyun.com " >/tmp/addcrontab.txt
crontab /tmp/addcrontab.txt
rm -rf /tmp/addcrontab.txt
 
###停止防火墙
systemctl stop firewalld.service
systemctl disable firewalld.service
 
#使用NTP服务同步
function update_time()
{
 
shpath=/apps/sh
if [ ! -d $shpath ];then
mkdir -p $shpath
fi
echo "01 0 * * * ntpdate time.pool.aliyun.com" >>/tmp/addcrontab.txt
crontab /tmp/addcrontab.txt
rm -rf /tmp/addcrontab.txt
cat >/apps/sh/update_time.sh<<"EOF"
#!/bin/bash
#by jack
function networkcheck()  
{
    timeout=5
 
        target=www.baidu.com
 
    ret_code=`curl ----connect-timeout $timeout $target -%{http_code} | tail -n1`
 
    if "x$ret_code" = "x200" ]; then
          echo "Network check Pass"
    else
          echo "Network Error exit....."
          exit 1
    fi
}
 
rpm -qa|grep ntpd &>/dev/null
ntppk=`echo $?`
 
if [ $ntppk -eq 0 ];then
logger restarting ntpd server.
/bin/systemctl restart ntpd.service
 
else
networkcheck
yum install ntp -y &>/dev/null
stat=`echo $?`
                if [ ! $stat -eq 0 ];then
                echo "ntpd install error."
                exit 10
                fi             
 
/bin/systemctl start ntpd.service
fi
 
EOF
#chmod +x /apps/sh/update_time.sh
}
 
 
#################
 
networkAndFtp
 
echo "Please enter Server name (hostname):"
read servername
echo "Please enter servicename (localhost service code):"
read servicename
echo "Please check hostname: $servername"
echo "=================================="
echo "Please check hostname: $servicename"
echo "Countdown 5s!"
for in `seq -5 -1 1`
  do
    echo "The countdown $i to start";
    sleep 1;
  done
 
hostnamectl --static set-hostname $servername
groupadd apps
adduser -g apps apps
yum install -y vim wget lrzsz iptables-services ntp net-tools psmisc unzip
/usr/sbin/ntpdate time.pool.aliyun.com
#update_time
 
wget -/etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
wget -/etc/sysctl.d/youhua.conf http://121.201.24.30:8188/conf/youhua.conf
mv  /etc/security/limits.conf  /etc/security/limits.conf_`date +%s`
echo '* - nofile 65535' /etc/security/limits.conf
setenforce 0
wget -/etc/selinux/config http://121.201.24.30:8188/conf/selinux/config
mkdir -/apps/svr/
#设置JAVA环境变量
 
#判断路由确定机器所在网段
RouteIp=`route -n | awk '{print $2}' | awk 'NR ==3 '`
if [ $RouteIp == "10.0.20.27"]
    then
    FileServerIp="10.0.20.250"
    else
    FileServerIp="10.0.5.250"
fi
     
#连接服务器下载java环境包
wget -/tmp/java.tar  http://$FileServerIp/apps-source-for-java.tar.gz
cd /tmp/ ;tar zxvf java.tar
 
#配置java运行环境
chown -R apps.apps /tmp/apps
rm -rf /tmp/apps/svr/nginx
rm -rf /tmp/apps/svr/nginx-1.6.3
rm -rf /tmp/apps/svr/apache-tomcat-7.0.70
rm -rf /tmp/apps/sh/rotate-1.3.sh
rm -rf /tmp/apps/sh/nginx.sh
#根据传参设置java版本
if [ $1 == "jdk18" ]
    then
    echo -"\033[42;37m Let's Install Java Development Kit Version 1.8 \033[0m "
    cp -/tmp/apps/svr /apps
    cp -/tmp/apps/sh /apps
    sleep 2;
    else
    echo -"\033[42;37m I Think You Need Java Development Kit Version 1.7 \033[0m "
    rm -rf /tmp/apps/svr/jdk
    cp -/tmp/apps/svr /apps
    cp -/tmp/apps/sh /apps
    cd /apps/svr/;ln -/apps/svr/jdk1.7.0_80 jdk
    sleep 2;
fi
#写入系统环境变量
echo "source /apps/sh/java_env.sh" >> /home/apps/.bashrc
 
    function install_zabbix_agentd()
{
mkdir -/apps/logs/zabbix/
mkdir -/apps/zabbix/etc/zabbix_agent.d
wget -/apps/zabbix.tar.gz http://121.201.24.30:8188/zabbix_centos7_64.tar.gz
tar xvf /apps/zabbix.tar.gz -/apps/
cp /apps/zabbix/sh/zabbix_agentd /etc/init.d/
chmod +/etc/init.d/zabbix_agentd
chkconfig --add zabbix_agentd
chkconfig --level 35 zabbix_agentd on
echo 'LogFile=/apps/logs/zabbix/zabbix_agentd.log' /apps/zabbix/etc/zabbix_agentd.conf
echo 'Server=10.0.5.30' >> /apps/zabbix/etc/zabbix_agentd.conf
echo 'ServerActive=10.0.5.30' >> /apps/zabbix/etc/zabbix_agentd.conf
echo "Hostname=$servername" >> /apps/zabbix/etc/zabbix_agentd.conf
echo 'AllowRoot=1' >> /apps/zabbix/etc/zabbix_agentd.conf
echo 'Include=/apps/zabbix/etc/zabbix_agent.d/*.conf' >> /apps/zabbix/etc/zabbix_agentd.conf
 
echo 'UserParameter=bd,/usr/bin/bash /apps/zabbix/sh/killall-check.sh' >/apps/zabbix/etc/zabbix_agent.d/system.conf
 
service zabbix_agentd start
}
 
echo "starting install_zabbix_agentd."
install_zabbix_agentd
 
yum update -y
 
#cp /home/apps/.ssh/authorized_keys /home/apps/.ssh/authorized_keys_bak
##需要用户登录后才能执行
#echo 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAx3M0s+E7EaBTDm67DO+vz9oiXUL9gkbf284YRu1bo4u1y6vx2v6Cblo7xbkUh8Au0APHATGCgsvbemFxnpB2/dY5cZNJA0SRrJEms382B2hsUtRT1E/6G21QwDf3cubCf+gDhxtQ5hAyPLKDoi+I9R+ebTaXMQoQ9IRVouviL8zeLrKI9SSURJscIjOfyhTj5363ygpjcAR303Lt+mDd1DyA/RW6gjcuNOM73c86JIOtvGNMR2li3lcxZ4IRnP5bweWQ33f7gi8P3bFOI9ZV28TO1C/eReVWuy41Ha54ycdPanPTJanJoVkVyZ5sxEEnn2QwoUHUcSv+dK+IG92Zcw== apps@JKM-UNCTL-033'>>/home/apps/.ssh/authorized_keys
echo 'ulimit -u 4096' >> /home/apps/.bashrc
echo 'ulimit -u 4096' >> /root/.bashrc
mkdir -/apps/newcode
 
echo -"\033[42;37m done!!! Please reboot server! \033[0m"
sync
sync
sync
reboot