install_zabbix.sh

#!/bin/bash
function memu(){
cat <<EOF
1.install zabbix_server
2.install zabbix_agent
EOF
}
memu
read -p "Do you need to install zabbix_server or zabbix-agent,please input 1 or 2: " NUM
if [ -z $NUM ];then
echo "请输入{1|2}"
exit 1
fi 


if [ $NUM -eq 1 ];then
echo "start install zabbix_server"

system_version=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
yum_dir=/installzabbix
#pack_deposit_dir=/usr/local/src
pack_name=install-zabbbix.tar.gz
FILE=`date +%F_%T`_install_zabbix.log
touch /tmp/${FILE}
INSTALLLOG=/tmp/${FILE}
if [ -e ${yum_dir} ];then
echo "${yum_dir} dir already existed" >> ${INSTALLLOG}
echo " please change yum_dir name "
exit 1; 
else
 mkdir -pv ${yum_dir} &>/dev/null
if [ -e ${yum_dir} ];then
echo "${yum_dir} dir create finish" >> ${INSTALLLOG}
fi
fi


for i in /etc/passwd /etc/shadow /etc/group /etc/gshadow
do
 if [ `lsattr ${i} | cut -c 5` = i ];then
/usr/bin/chattr -i $i 
/usr/bin/lsattr $i >> ${INSTALLLOG}
else
echo "$i不存在i安全属性" >>${INSTALLLOG} 
tar xvf c${system_version}packges.tar.gz -C ${ yum_dir}
tar xvf ${pack_name} 
tar xvf ${pack_deposit_dir}/${pack_name} -C ${yum_dir}
installation package#
done
fi



#Install createrepo package to generate repodata 
 
yum -y install createrepo yum-utils 
if [$? -eq 0 ];then 
echo "createrepo packages already install finish" >> ${INSTALLLOG} 
else 
echo "createrepo packeages install failed" >> ${INSTALLLOG} 
exit 1 
fi #Generate the 

repo file in the /etc/yum.repos.d/ directory 
yum-config-manager --add-repo=file://${yum_dir} 
if [-e /etc/yum .repos.d${yum_dir}.repo ];then 
echo "repo file has been generated" >> ${INSTALLLOG} 
else 
echo "repo file is not generated" >> ${INSTALLLOG} 
exit 1 
fi 

#generate 
repodata createrepo ${yum_dir } 
if [-e ${yum_dir}/repodata ]; then 
echo "repodata has been generated" >> ${INSTALLLOG}
else 
echo "repodata is not generated" >> ${INSTALLLOG} 
fi

yum repolist | grep ${yum_dir}
if [ $? -eq 0 ];then
echo "yum源配置完成" >> ${INSTALLLOG}
else
echo "yum源配置失败" >> ${INSTALLLOG}
exit 
fi
echo "----------------------------------------------" >> ${INSTALLLOG}
echo "gpgcheck=0" >> /etc/yum.repos.d/${yum_dir}.repo

#安装包
if [ ${system_version} -eq 6 ];then
yum -y install httpd php php-mysql php-ldap php-gd php-bcmath php-mbstring php-xml libaio libaio-devel 
yum -y install zabbix-server zabbix-agent zabbix-java-gateway zabbix-web-mysql zabbix-get
fi


if [ ${system_version} -eq 7 ];then
yum -y install httpd php php-pecl-mysql php-mysqlnd php-ldap php-gd php-bcmath php-mbstring php-xml libaio libaio-devel 
yum -y install zabbix-server zabbix-agent zabbix-java-gateway zabbix-web-mysql zabbix-get
fi


#安装mysql
mysql_version=5.7.24
mysql_user=mysql
mysql_install_dir=/app/database
mysql_root_passwd=centos
base_dir=${mysql_install_dir}/${mysql_user}
mysql_data_dir=/data/mysql
mysql_logs=$mysql_install_dir/logs
error_logs=${mysql_logs}/mysqld.log
mysql_pid=${mysql_install_dir}/${mysql_user}/mysqld.pid
socket_file=${base_dir}/mysql.sock
yum -y install libaio*
id $mysql_user &>/dev/null
if [ $? = 0 ];then
  echo " $mysql_user user already existed"
  exit 1
else
  useradd -r $mysql_user -s /sbin/nologin 
fi

if [ -e ${mysql_install_dir} ];then
echo " ${mysql_install_dir} dir already existed "
exit 1
else
mkdir -p $mysql_data_dir 
mkdir -p $mysql_logs
touch $mysql_logs/mysqld.log
fi

if [ -e mysql-${mysql_version}-linux-glibc2.12-x86_64.tar ];then
 tar xvf mysql-${mysql_version}-linux-glibc2.12-x86_64.tar 
 tar xvf mysql-${mysql_version}-linux-glibc2.12-x86_64.tar.gz -C ${mysql_install_dir}
else
 echo " packages inexistence"
 exit 1
fi

cd ${mysql_install_dir}
mv mysql-5.7.24-linux-glibc2.12-x86_64 ${mysql_user}
chown -R ${mysql_user}.${mysql_user} ${mysql_install_dir}
chmod -R 755 ${mysql_install_dir}






cat > /etc/my.cnf <<EOF
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
default_password_lifetime=360
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=${base_dir}
datadir=${mysql_data_dir}
socket=${socket_file}
log-error=${error_logs}
pid-file=${mysql_pid}
#不区分大小写
lower_case_table_names = 1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_connections=5000
default-time_zone = '+8:00'
EOF

${base_dir}/bin/mysqld --initialize --user=$mysql_user --basedir=${base_dir} --datadir=${mysql_data_dir}

echo "PATH=${base_dir}/bin:$PATH" > /etc/profile.d/mysql.sh 
source /etc/profile.d/mysql.sh

cp ${base_dir}/support-files/mysql.server /etc/init.d/mysqld
ln -sv ${socket_file} /tmp/mysql.sock 
chkconfig --add mysqld
chkconfig mysqld on 


service mysqld start

sleep 5


#cat /var/log/mysqld.log | awk '/root@localhost/{print $NF}'
#root_init_passwd=`cat ${mysql_logs}/mysqld.log | awk '/root@localhost/{print $NF}'`
root_init_passwd=`cat ${mysql_logs}/mysqld.log |grep 'A temporary password'|awk  '{print $NF}'`

#/usr/local/mysql/bin/mysql -uroot -p${root_init_passwd} -e "set password for root@localhost=password('centos');"
${base_dir}/bin/mysql -uroot -p${root_init_passwd}  --connect-expired-password -e "set  password=password('${mysql_root_passwd}');"

${base_dir}/bin/mysql -uroot -p${mysql_root_passwd} --connect-expired-password  -e "flush privileges;"

#创建zabbix用户
${base_dir}/bin/mysql -uroot -p${mysql_root_passwd} -e "create database zabbix;grant all on zabbix.* to zabbix@'localhost' identified by 'zabbix';" 
${base_dir}/bin/mysql -uroot -p${mysql_root_passwd} -e "flush privileges;"

#导入zabbix数据
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | ${base_dir}/bin/mysql -uzabbix -hlocalhost -pzabbix zabbix #Modify 

zabbix_server configuration file
if [ ${system_version} -eq 6 ];then
sed -i 's/# DBHost=localhost/DBHost=localhost/g' /etc/zabbix/zabbix_server.conf
sed -i 's/# DBname=zabbix/DBname=zabbix/g' /etc/zabbix/zabbix_server.conf
sed -i 's/# DBPassword=/DBPassword=zabbix/g' /etc/zabbix/zabbix_server.conf
sed -i 's/# ListenPort=10051/ListenPort=10051/g' /etc/zabbix/zabbix_server.conf
sed -i 's/# DBSocket=\/tmp\/mysql.sock/DBSocket=\/tmp\/mysql.sock/g' /etc/zabbix/zabbix_server.conf

ls /etc/zabbix/zabbix_server.conf >> ${INSTALLLOG}
egrep -v "^$|^#" /etc/zabbix/zabbix_server.conf >> ${INSTALLLOG}
echo "----------------------------------------------" >> ${INSTALLLOG}
fi


if [ ${system_version} -eq 7 ];then
sed -i 's/# DBHost=localhost/DBHost=127.0.0.1/g' /etc/zabbix/zabbix_server.conf
sed -i 's/# DBname=zabbix/DBname=zabbix/g' /etc/zabbix/zabbix_server.conf
sed -i 's/# DBPassword=/DBPassword=zabbix/g' /etc/zabbix/zabbix_server.conf
sed -i 's/# ListenPort=10051/ListenPort=10051/g' /etc/zabbix/zabbix_server.conf
sed -i 's/# DBSocket=\/tmp\/mysql.sock/DBSocket=\/tmp\/mysql.sock/g' /etc/zabbix/zabbix_server.conf

ls /etc/zabbix/zabbix_server.conf >> ${INSTALLLOG}
egrep -v "^$|^#" /etc/zabbix/zabbix_server.conf >> ${INSTALLLOG}
echo "----------------------------------------------" >> ${INSTALLLOG}
fi
#修改zabbix_agent配置文件
if [ ${system_version} -eq 6 ];then
sed -i 's/Hostname=Zabbix server/#Hostname=Zabbix server/g' /etc/zabbix/zabbix_agentd.conf
sed -i 's/# HostnameItem=system.hostname/HostnameItem=system.hostname/g' /etc/zabbix/zabbix_agentd.conf
ls /etc/zabbix/zabbix_agentd.conf >> ${INSTALLLOG}
egrep -v "^$|^#" /etc/zabbix/zabbix_agentd.conf >> ${INSTALLLOG}
echo "----------------------------------------------" >> ${INSTALLLOG}
fi

if [ ${system_version} -eq 7 ];then
sed -i 's/Hostname=Zabbix server/#Hostname=Zabbix server/g' /etc/zabbix/zabbix_agentd.conf
sed -i 's/# HostnameItem=system.hostname/HostnameItem=system.hostname/g' /etc/zabbix/zabbix_agentd.conf
ls /etc/zabbix/zabbix_agentd.conf >> ${INSTALLLOG}
egrep -v "^$|^#" /etc/zabbix/zabbix_agentd.conf >> ${INSTALLLOG}
echo "----------------------------------------------" >> ${INSTALLLOG}
fi

#修改php.ini
if [ ${system_version} -eq 6 ];then
cp /usr/share/doc/zabbix-web-3.4.15/httpd22-example.conf /etc/httpd/conf.d/zabbix.conf
sed -i 's/# php_value date.timezone Europe\/Riga/php_value date.timezone Asia\/Shanghai/g' /etc/httpd/conf.d/zabbix.conf
sed -i 's/mysqli.default_socket =/mysqli.default_socket = \/tmp\/mysql.sock/g' /etc/php.ini
sed -i "/post_max_size/s/8/16/g" /etc/php.ini
sed -i "/max_execution_time/s/30/300/g" /etc/php.ini
sed -i "/max_input_time = 60/s/60/300/g" /etc/php.ini
sed -i 's/;date.timezone =/date.timezone = \"Asia\/Shanghai\"/g' /etc/php.ini
chown -R apache.apache /etc/zabbix/web
fi
if [ ${system_version} -eq 7 ];then
sed -i 's/mysqli.default_socket =/mysqli.default_socket = \/tmp\/mysql.sock/g' /etc/php.ini
sed -i '/<IfModule mod_php5.c>/c\    <IfModule mod_php7.c>' /etc/httpd/conf.d/zabbix.conf
sed -i 's/# php_value date.timezone Europe\/Riga/php_value date.timezone Asia\/Shanghai/g' /etc/httpd/conf.d/zabbix.conf
fi
#修改http默认访问路径为zabbix的页面
sed -i -r '/^DocumentRoot/c\ DocumentRoot "/usr/share/zabbix"' /etc/httpd/conf/httpd.conf

if [ ${system_version} -eq 6 ];then
#启动httpd
service httpd start
chkconfig --add httpd
chkconfig httpd on
#启动本地zabbix_server和zabbix_agent
/etc/init.d/zabbix-server start
/etc/init.d/zabbix-agent start
chkconfig --add zabbix-server
chkconfig zabbix-server on
chkconfig --add zabbix-agent 
chkconfig zabbix-agent on
fi
if [ ${system_version} -eq 7 ];then
systemctl start httpd
systemctl enable httpd
systemctl start zabbix-server zabbix-agent
systemctl enable zabbix-server zabbix-agent
fi

for i in /etc/passwd /etc/shadow /etc/group /etc/gshadow
do
 if [ `lsattr ${i} | cut -c 5` = i ];then
   echo " ${i} 存在i安全属性" >> ${INSTALLLOG}
else
   chattr +i $i
   lsattr $i >> ${INSTALLLOG}
fi
done
echo "----------------------------------------------" >> ${INSTALLLOG}
echo "访问地址为zabbix-server ip地址"  >> ${INSTALLLOG}
echo "The default login account on the zabbix web page is admin and the password is zabbix" >> ${INSTALLLOG} 
echo "mysql user name is ${mysql_user}" >> ${INSTALLLOG} 
echo "mysql_root_passwd is changed to ${mysql_root_passwd}" >> $ {INSTALLLOG} 
echo "mysql_install_dir is ${mysql_install_dir}" >> ${INSTALLLOG} 
echo -e "\033[1;34m zabbix-server installation is complete, the output result is in the ${INSTALLLOG} file in the /tmp directory\033[ 0m" 
fi 


if [$NUM -eq 2 ]; then 
echo "start install zabbix_agent" 

read -p "please input zabbix_server IP: "zabbixserverIP 


system_version=`cat /etc/redhat-release|sed -r's/.* ( [0-9]+)\..*/\1/'` 
yum_dir=/installzabbix 
#pack_deposit_dir=/usr/local/src 
pack_name=install-zabbbix.tar.gz
FILE=`date +%F_%T`_install_zabbix.log
touch /tmp/${FILE}
INSTALLLOG=/tmp/${FILE}
if [ -e ${yum_dir} ];then
echo "${yum_dir} dir already existed" >> ${INSTALLLOG}
#exit 1; 
else
 mkdir -pv ${yum_dir} &>/dev/null
if [ -e ${yum_dir} ];then
echo "${yum_dir} dir create finish" >> ${INSTALLLOG}
fi
fi


for i in /etc/passwd /etc/shadow /etc/group /etc/gshadow
do
 if [ `lsattr ${i} | cut -c 5` = i ];then
/usr/bin/chattr -i $i 
/usr/bin/lsattr $i >> ${INSTALLLOG}
else
echo "$i不存在i安全属性" >> ${INSTALLLOG}
fi
done

#解压安装包
tar xvf ${pack_name} 
tar xvf c${system_version}packges.tar.gz -C ${yum_dir} 

#Install the createrepo package to generate repodata
cd ${yum_dir}  
yum -y install createrepo yum-utils 
if [$? -eq 0 ];then 
echo "createrepo packages already install finish" >> ${INSTALLLOG} 
else 
echo "createrepo packeages install failed" >> ${INSTALLLOG } 
fi 

#Generate the repo file in the /etc/yum.repos.d/ directory 
yum-config-manager --add-repo=file://${yum_dir} 
if [-e /etc/yum.repos.d$ {yum_dir}.repo ];then 
echo "repo file has been generated" >> ${INSTALLLOG} 
else 
echo "repo file is not generated" >> ${INSTALLLOG} 
fi 

#generate 
repodata createrepo ${yum_dir} 
if [-e ${ yum_dir}/repodata ];then 
echo "repodata has been generated" >> ${INSTALLLOG} 
else 
echo "repodata is not generated" >> ${INSTALLLOG} 
fi

yum repolist | grep ${yum_dir}
if [ $? -eq 0 ];then
echo "yum源配置完成" >> ${INSTALLLOG}
else
echo "yum源配置失败" >> ${INSTALLLOG}
exit 
fi
echo "----------------------------------------------" >> ${INSTALLLOG}
echo "gpgcheck=0" >> /etc/yum.repos.d/${yum_dir}.repo

#安装包
yum -y install zabbix-agent
if [ $? -eq 0 ];then
echo " zabbix-agent install finish " >> ${INSTALLLOG}
else
echo "zabbix-agent install faild" >> ${INSTALLLOG}
exit 1
fi
echo "----------------------------------------------" >> ${INSTALLLOG}
#配置zabbix-agentd.conf
if [ ${system_version} -eq 6 ];then
sed -i "s/Server=127.0.0.1/Server=$zabbixserverIP/g" /etc/zabbix/zabbix_agentd.conf
sed -i 's/# ListenPort=10050/ListenPort=10050/g' /etc/zabbix/zabbix_agentd.conf
sed -i "s/ServerActive=127.0.0.1/ServerActive=$zabbixserverIP/g" /etc/zabbix/zabbix_agentd.conf
sed -i 's/Hostname=Zabbix server/#Hostname=Zabbix server/g' /etc/zabbix/zabbix_agentd.conf
sed -i 's/# HostnameItem=system.hostname/HostnameItem=system.hostname/g' /etc/zabbix/zabbix_agentd.conf

ls /etc/zabbix/zabbix_agentd.conf >> ${INSTALLLOG}
egrep -v "^$|^#" /etc/zabbix/zabbix_agentd.conf >> ${INSTALLLOG}
echo "----------------------------------------------" >> ${INSTALLLOG}
fi
if [ ${system_version} -eq 7 ];then
sed -i "s/Server=127.0.0.1/Server=$zabbixserverIP/g" /etc/zabbix/zabbix_agentd.conf
sed -i 's/# ListenPort=10050/ListenPort=10050/g' /etc/zabbix/zabbix_agentd.conf
sed -i "s/ServerActive=127.0.0.1/ServerActive=$zabbixserverIP/g" /etc/zabbix/zabbix_agentd.conf
sed -i 's/Hostname=Zabbix server/#Hostname=Zabbix server/g' /etc/zabbix/zabbix_agentd.conf
sed -i 's/# HostnameItem=system.hostname/HostnameItem=system.hostname/g' /etc/zabbix/zabbix_agentd.conf

ls /etc/zabbix/zabbix_agentd.conf >> ${INSTALLLOG}
egrep -v "^$|^#" /etc/zabbix/zabbix_agentd.conf >> ${INSTALLLOG}
echo "----------------------------------------------" >> ${INSTALLLOG}
fi
#启动zabbix-agent
if [ ${system_version} -eq 6 ];then
/etc/init.d/zabbix-agent start
chkconfig --add zabbix-agent 
chkconfig zabbix-agent on
fi
if [ ${system_version} -eq 7 ];then
systemctl start zabbix-agent
systemctl enable zabbix-agent
fi


for i in /etc/passwd /etc/shadow /etc/group /etc/gshadow
do
 if [ `lsattr ${i} | cut -c 5` = i ];then
   echo " ${i} 存在i安全属性" >> ${INSTALLLOG}
else
   chattr +i $i
   lsattr $i >> ${INSTALLLOG}
fi
done
echo -e "\033[1;34m zabbix-agent安装完成,输出结果在/tmp目录下${INSTALLLOG}文件中 \033[0m"
fi



Guess you like

Origin blog.51cto.com/13768323/2678483