每日一个shell脚本之一键部署Zabbix

每日一个shell脚本之一键部署Zabbix

上一期我们展示了一键部署Agent端zabbix提升业务效率,本期带来一键部署zabbix的server端的服务脚本!

源码参上

 #!/usr/bin/bash                          
 # **************************************
 #   CSDN:         M乔木 
 #   qq邮箱:        [email protected] 
 #   解释器:        这是一个shell脚本        
 # **************************************

	#声明函数
	function zabbix_S(){
	#环境检测
        echo "--------------------------------------------------------"
	echo "[1]开始检测防火墙Selinux..."
	f_w=`systemctl status firewalld | awk '/Active:/{print $2}'`
        s_l=`getenforce`
        #判断是否关闭 没关闭就结束运行
        if [ ${f_w} = "inactive" -a ${s_l} = "Disabled" ];then
        echo "[2]防火墙SeLinux已关闭..."
        sleep 1
        echo "[3]正在飞速安装zabbix中..."
        sleep 1
        else
        echo "[2]防火墙SeLinux未关闭..."
        systemctl disable firewalld &>/dev/null
        sed -i 's/SELINUX=enforcing/SELINUX=disabled/'  /etc/sysconfig/selinux
        sed -i 's/SELINUX=enforcing/SELINUX=disabled/'  /etc/selinux/config  
        sleep 1
        echo "[3]正在关闭..."
        sleep 1
        echo "[4]防火墙SeLinux已关闭 请重启机器生效..."
        echo "--------------------------------------------------------"
        exit 0
        fi
	#安装zabbix源
	rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm &>/dev/null
	#安装数据库并清理缓存
	echo '[4]准备安装数据库,全程20秒左右,请耐心等待...'
        sleep 1
        echo '[5]开始清理环境...'
        yum erase mariadb mariadb-server mariadb-libs mariadb-devel -y &>/dev/null
        userdel -r mysql  &>/dev/null
        rm -rf /etc/my*    &>/dev/null
        rm -rf /var/lib/mysql   &>/dev/null
        rm -rf /usr/bin/mysql  &>/dev/null
        yum -y erase `rpm -qa | egrep "mysql|mariadb"` &>/dev/null

        echo '[6]正在下载mysqlyum源...'
        yum install -y https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm &>/dev/null

        echo '[7]正在安装mysql...'
        yum install -y mysql-community-server --enablerepo mysql57-community --disablerepo mysql80-community &>/dev/null
        yum -y groupinstall "Development Tools" &>/dev/null

        echo '[8]正在配置mysql...'
        yum -y install yum-utils &>/dev/null
        yum-config-manager --disable mysql80-community &>/dev/null
        yum-config-manager --enable mysql57-community &>/dev/null

        echo '[9]正在启动mysql...'
        systemctl start  mysqld &>/dev/null

        echo '[10]正在设置密码,请稍后...'
        num_az=`awk '/temporary password/{p=$NF}END{print p}' /var/log/mysqld.log`
        echo 'validate-password=OFF' >> /etc/my.cnf

        systemctl restart  mysqld &>/dev/null
        read -p '请输入新密码:' passwdx
        mysqladmin -uroot -p"$num_az" password "$passwdx" &>/dev/null 

        echo "[11]安装完成,密码已设置完成为:$passwdx"
        echo "[12]密码已保存到/mysql_passwd.txt目录下"
        echo $passwdx > /mysql_passwd.txt 
    
	yum clean all &>/dev/null  && yum makecache fast &>/dev/null
	#安装zabbix模块
        echo "[13]开始安装zabbix..."
        yum -y install zabbix-server-mysql zabbix-agent zabbix-get zabbix-sender centos-release-scl &>/dev/null
	yum -y install yum-utils &>/dev/null
	rpm -qa | grep yum-utils &>/dev/null
	if [ $? -eq 1 ];then
	sleep 1
	yum -y install yum-utils &>/dev/null
	fi
	yum-config-manager --enable zabbix-frontend &>/dev/null
	yum -y install zabbix-web-mysql-scl  zabbix-nginx-conf-scl centos-release-scl &>/dev/null
	#建立运行数据库
        echo "[14]正在建立运行数据库..."
	mysql -p"${passwdx}" -e 'create database zabbix character set utf8 collate utf8_bin;' &>/dev/null 
	mysql -p"${passwdx}" -e "create user zabbix@localhost identified by 'admin';"  &>/dev/null
	mysql -p"${passwdx}" -e 'grant all privileges on zabbix.* to zabbix@localhost;'  &>/dev/null
	mysql -p"${passwdx}" -e 'set global log_bin_trust_function_creators = 1;'   &>/dev/null
	mysql -p"${passwdx}" -e 'flush privileges;'  &>/dev/null
	echo "[15]zabbix登录数据库密码为:admin" 
	echo "zabbix登录数据库密码为:admin" >> /zabbix-mysql.txt 
	zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p"admin" zabbix 2>/dev/null	
	#关联数据库
        echo "[16]正在改写相关配置文件..."
	mysql -p"${passwdx}" -e 'set global log_bin_trust_function_creators = 0;' &>/dev/null
	echo "DBHost=localhost" >> /etc/zabbix/zabbix_server.conf
	echo "DBPassword=admin" >> /etc/zabbix/zabbix_server.conf
	echo "DBPort=3306" >> /etc/zabbix/zabbix_server.conf	
	#关联php
	sed  -i '2s/^#//;3s/^#//' /etc/opt/rh/rh-nginx116/nginx/conf.d/zabbix.conf
	sed -i '38,118d' /etc/opt/rh/rh-nginx116/nginx/nginx.conf	
	sed -i 's/listen.acl_users = apache/listen.acl_users = apache,nginx/' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
	sed -i '25d' /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf	
	echo 'php_value[date.timezone] = Asia/Shanghai' >> /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf  
	#重启服务
        echo "[17]正在重启相关服务..."
	systemctl restart zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm &>/dev/null
	systemctl enable zabbix-server zabbix-agent rh-nginx116-nginx rh-php72-php-fpm &>/dev/null
	echo "[18]zabbix已部署 后续请登录浏览器安装..."
        echo "--------------------------------------------------------"
	}

	#主界面
	zabbix_S

使用方法

touch zabbix-az.sh
#新建一个.sh结尾的文件复制上面的源码粘贴进去

sh zabbix-az.sh
#运行刚才新建的脚本,根据提示完成部署安装!

点赞关注评论转发感谢!

你可能感兴趣的:(Shell,zabbix,学习方法,开发语言,安全,bash)