zabbix自动安装配置脚本

作者: admin 分类: Linux,Shell脚本 发布时间: 2018-04-12 23:53 浏览:2,196 次    
[root@localhost ~]# vim zabbix.sh
#############################################################
# File Name: zabbix.sh
# Author: 寻音
# E-mail: lnhxzwb@126.com
# Created Time: Fri 13 Apr 2018 07:47:23 AM CST
#==================================================================
#!/bin/bash

#提示信息
display()
{
echo "※※※※※※※※※※※※※※※※ <提示信息> ※※※※※※※※※※※※※※※※※※※ "
echo "※ 本环境基于Centos7系统 ※ "
echo "※ 可选zabbix3.2/zabbix3.4版本 ※ "
echo "※ 如果安装环境不同,请更改相关参数或手动安装 ※ "
echo "※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※ "
}
zabbix_repo ()
{
cd /etc/yum.repos.d
cat <<EOF >zabbix.repo
[zabbix]
name=zabbix-repo
baseurl=http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/
gpgcheck=0
enabled=1
EOF
}


#安装zabbix
install_zabbix()
{
yum -y install httpd &> /dev/null
yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent &> /dev/null
ls /usr/sbin/zabbix_server&> /dev/null
}
#安装mysql
install_mysql ()
{
 echo "正在安装mysql服务,请稍等....."
 read -p "请选择安装数据库的类型(mariadb|mysql): " sql
 if [ $sql == "mariadb" ];then
 cd /etc/yum.repos.d
 rm -rf zabbix.repo
 yum -y install mariadb mariadb-server &> /dev/nul
 
 if [ $? = 0 ];then
 echo "mariadb server安装已经完成!"
 #启动mysqld服务并设置开机自启
 systemctl start mariadb
 systemctl enable mariadb &> /dev/nul
 else
 echo "安装出现问题;请手动安装mariadb";exit
 fi 
 elif [ ${sql} == "mysql" ];then
 
 rpm -Uvh http://repo.mysql.com/mysql-community-release-el7.rpm &> /dev/null
 cd /etc/yum.repos.d
 rm -rf zabbix.repo
 yum -y install mysql-community-server &> /dev/null
 
 if [ $? = 0 ];then
 echo "mysql server安装已经完成!"
 rm -rf mysql-community*.repo
 #启动mysqld服务并设置开机自启
 systemctl start mysqld
 systemctl enable mysqld &> /dev/nul
 echo 
 else
 echo "安装出现问题;请手动安装mysql";exit
 fi 
 else
 echo "输入错误,请更改相关参数或手动安装"
 fi
 }

zabbix_mysql()
{
 echo "初始化数据库,输入或修改root密码后,直接enter"
 mysql_secure_installation
 #创建zabbix库
 read -p "请输入数据库root密码:" passwd
 read -p "请输入zabbix的授权用户名:" user
 read -p "请输入授权ip地址:" ip
 read -p "请输入授权用户密码:" zpasswd
 read -p "请输入创建zabbix库名:" zname
 mysql -uroot -p${passwd} -e "create database ${zname} default character set utf8 collate utf8_bin;" &> /dev/null
 mysql -uroot -p${passwd} -e "grant all on ${zname}.* to '${user}'@'${ip}' identified by '${zpasswd}';" &> /dev/null
 #zabbix库测试连接
 mysql -uroot -p${passwd} -e "show databases;" &> /dev/null
 if [ $? = 0 ];then
 echo "zabbix数据库创建完成"
 else
 echo "zabbix数据库创建失败,请手动检查!";exit
 fi
 #导入zabbix数据库
 path=`ls /usr/share/doc/* | grep server-mysql|awk -F ":" '{print $1}'`
 zcat ${path}/create.sql.gz | mysql -uroot -p${passwd} ${zname} &> /dev/null
 #如果有相关表格说明导入成功
 mysql -u${user} -p${zpasswd} -e "use ${zname};show tables" &> /dev/null
 if [ $? = 0 ];then
 echo "数据库导入成功"
 else
 echo "数据导入失败,请检查或手动设置";exit
 fi
 #修改zabbix_server.conf
 path_ser=/etc/zabbix/zabbix_server.conf
 egrep -v "^#|^$" ${path_ser} &> /dev/null
 read -p "请输入server-IP": sIP
 sed -i "s/#\ \DBHost=localhost/DBHost=${sIP}/" ${path_ser}
 sed -i "s/DBName=zabbix/DBName=${zname}/" ${path_ser}
 sed -i "s/DBUser=zabbix/DBUser=${user}/" ${path_ser}
 sed -i "s/#\ \DBPassword=/DBPassword=${zpasswd}/" ${path_ser}
 #修改zabbix.conf
 path_zabbix=/etc/httpd/conf.d/zabbix.conf

sed -i "s/#\ \php_value date.timezone.*/php_value date.timezone Asia\/Shanghai/" ${path_zabbix} 
 systemctl restart httpd
 systemctl enable httpd &> /dev/null
 systemctl start zabbix-server
 systemctl enable zabbix-server &> /dev/null
 systemctl restart zabbix-server
 if [ $? = 0 ];then
 echo "HTTP/ZABBIX服务启动成功"
 else
 echo "HTTP/ZABBIX服务启动失败,请检查或手动设置";exit
 fi
 echo "zabbix配置完成,请打开http://ip/zabbix/setup.php进行安装"
 echo "默认用户名Admin,密码zabbix"
 echo "登陆首页http://ip/zabbix/index.php"
}

#安装zabbix-agent 
 install_agent ()
 {
 yum -y install zabbix-agent &>/dev/null
 read -p "请输入server的ip:" sIP
 read -p "请输入agent的ip:" aIP
 path_agent=/etc/zabbix/zabbix_agentd.conf
 sed -i "s/Server=127.0.0.1/Server=${sIP}/" ${path_agent}
 sed -i "s/ServerActive=127.0.0.1/ServerActive=${sIP}/" ${path_agent}
 sed -i "s/Hostname=127.0.0.1/Hostname=${aIP}/" ${path_agent}
 #启动服务
 systemctl enable zabbix-agent
 systemctl start zabbix-agent
 if [ $? = 0 ];then
 echo "zabbix-agent 配置完成!"
 else
 echo "zabbix-agent 配置失败!" 
 exit
 fi 
 }

display

yum repolist &> /dev/null
 if [ $? = 0 ];then
 echo "本环境yum源可用"
 else
 echo "本环境yum源不可用";exit
 fi
read -p "请选择安装zabbix-server|zabbix-agent(server|agent):" service
if [ $service == "server" ] || [ $service == "zabbix-server" ] ;then
read -p "请选择安装zabbix-server的版本(3.2|3.4): " zabbix
if [ ${zabbix} == "3.2" ];then
 echo "正在安装zabbix server,请稍等....."
 zabbix_repo
 sed -i 's/3.4/3.2/g' zabbix.repo
 install_zabbix
 
 if [ $? = 0 ];then
 echo "zabbix3.2安装已经完成!"
 else
 echo "安装出现问题;请手动安装zabbix";exit
 fi
 
 install_mysql
 zabbix_mysql
 
elif [ ${zabbix} == "3.4" ];then
#安装zabbix3.4的版本
 echo "正在安装zabbix server,请稍等....."
 zabbix_repo
 install_zabbix
 if [ $? = 0 ];then
 echo "zabbix3.4安装已经完成!"
 else
 echo "安装出现问题;请手动安装zabbix";exit
 fi
 install_mysql
 zabbix_mysql
 
else
 echo "输入格式不对,请重新运行或者手动安装!";exit
fi 
elif [ $service == "agent" ] || [ $service == "zabbix-agent" ];then
 read -p "请选择安装zabbix-agent的版本(3.2|3.4): " agent
 if [ $agent = 3.2 ];then
 zabbix_repo
 sed -i 's/3.4/3.2/g' zabbix.repo
 install_agent
 if [ $? = 0 ];then
 echo "zabbix-agent3.2安装已经完成!"
 else
 echo "安装出现问题;请手动安装zzabbix-agent";exit
 fi

elif [ $agent = 3.4 ];then
 zabbix_repo
 install_agent
 if [ $? = 0 ];then
 echo "zabbix-agent3.4安装已经完成!"
 else
 echo "安装出现问题;请手动安装zabbix-agent";exit
 fi
 else 
 echo "输入格式不对,请重新运行或者手动安装!";exit
 fi
else
 echo "输入格式不对,请重新运行或者手动安装!";exit
fi

 

 


温馨提示:如无特殊说明,本站文章均为作者原创,转载时请注明出处及相应链接!

发表评论