#!/bin/bash
###################################
#fuction  zabbix-server envirorment 
#created by AL  2018/05/01
####################################

install_path=`pwd`

###lib install
yum install -y gcc gcc-c++  ncurses-devel  curl-devel  libxpm-devel cmake libxml2 libxml2-devel net-snmp-devel libxPm wget

## create user and group

groupadd mysql -g 502
useradd -s /sbin/nologin -M mysql -u 502 -g mysql


groupadd www -g 504
useradd -s /sbin/nologin -M www -u 504 -g 504

groupadd zabbix
useradd -g zabbix zabbix

###initalize
cp -f  ${install_path}/conf/ld.so.conf  /etc/ld.so.conf

ldconfig


##install zlib1.2.8
cd ${install_path}/files
tar zxf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make && make install


###install mysql
cd ${install_path}/files
tar zxf  mysql-5.6.36.tar.gz
cd mysql-5.6.36

cmake  -DCMAKE_INSTALL_PREFIX=/opt/mysql/ \
-DMYSQL_UNIX_ADDR=/opt/mysql/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_INNODB_MEMCACHED=1 \
-DWITH_DEBUG=OFF \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DENABLED_PROFILING=ON \
-DMYSQL_MAINTAINER_MODE=OFF \
-DMYSQL_DATADIR=/opt/mysql/var \
-DMYSQL_TCP_PORT=3306  


make
make install

cp support-files/mysql.server /etc/init.d/mysqld
chmod +x  /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --level 345 mysqld on

cd /opt/mysql/

/opt/mysql/scripts/mysql_install_db --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/var
mkdir /opt/mysql/tmp
chown -R mysql.mysql tmp
chmod 755 tmp

rm -f /etc/my.cnf
cp -f ${install_path}/conf/my.cnf  ./

echo "PATH=/opt/mysql/bin:$PATH" >> /etc/profile
source /etc/profile
service mysqld start

###configure modules
tar zxf ${install_path}/files/modules.tar.gz -C /opt/

###configure php

cp  -f ${install_path}/bin/php-fpm  /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
/sbin/chkconfig --add php-fpm
/sbin/chkconfig --level 345 php-fpm on
tar zxf ${install_path}/files/php.tar.gz -C /opt/
service php-fpm start

###configure nginx
mkdir -p /opt/web/php
cp  -f ${install_path}/bin/nginx  /etc/init.d/nginx

chmod +x /etc/init.d/nginx
/sbin/chkconfig --add nginx
/sbin/chkconfig --level 345 nginx on
tar zxf ${install_path}/files/nginx.tar.gz -C /opt/

service nginx start
###install zabbix-server


cd ${install_path}/files

tar zxf  zabbix-3.2.6.tar.gz
cd zabbix-3.2.6

./configure --prefix=/opt/zabbix/ --enable-server \
--enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2

make && make install


cp -f ${install_path}/conf/zabbix_server.conf /opt/zabbix/etc/zabbix_server.conf

##copy web files
cp -rf ${install_path}/files/zabbix-3.2.6/frontends/php  /opt/web/
cp -f ${install_path}/conf/zabbix.conf.php  /opt/web/php/conf/zabbix.conf.php
cp -f ${install_path}/simkai.ttf  /opt/web/php/fonts/DejaVuSans.ttf

###导入数据
${install_path}/import.sh  ${install_path}

##节点监控部署
cp  -rf ${install_path}/check_ip_linux  /opt/zabbix/
chmod +x /opt/zabbix/check_ip_linux/check_ip
chmod +x /opt/zabbix/check_ip_linux/start.sh

if [ $? == 0 ];then
echo "##################"  
echo -e "\033[40;32m install success \033[0m"
echo "###################"
echo "user: admin     password: zabbix "
echo "节点监控修改配置 /opt/zabbix/check_ip_linux/check_ip.cfg"
echo "#############################"

  
 else 
  echo "##############"
 echo -e "\033[40;31m error \033[0m"
 echo "############" 
 fi




















