zabbix简易一键安装脚本

脚本执行时候要输入的参数:

  • Enter db_ip (172.16.%.%)> #输入数据库访问授权ip范围,例如172.16.%.%、192.168.%.%、192.168.1.%
  • Enter db_root_password > #输入root用户数据库密码
  • Enter zabbix_dbuser > 输入zabbix用户名
  • Enter zabbix_dbpassword >输入zabbix用户密码
  • Enter zabbix_version (3.x).* > 输入zabbix主版本号,例如:3.0、3.2
  • Enter zabbix_subversion ..(x) > 输入zabbix子版本号,例如:3.0.28中的28
~]# vi zabbix_autoinstall.sh
#!/bin/bash
# zabbix 3.x一键安装脚本 单机
# 环境 centos 7.4.1708 x86_64
 read  -p "Enter db_ip (172.16.%.%)> " dbip  #输入数据库访问授权ip范围
 read  -p "Enter db_root_password > " dbrootpw  #输入数据库密码
 read  -p "Enter zabbix_dbuser > " dbuser  #输入zabbix数据库名
 read -p "Enter zabbix_dbpassword >" dbpassword #输入zabbix数据密码
 read -p "Enter zabbix_version (3.x).*  > " zabbix_version #输入zabbix版本号
 read -p "Enter zabbix_subversion *.*.(x) > " zabbix_subversion #输入zabbix子版本号

 IP=`ip add|grep global|awk -F'[ /]+' '{ print $3 }'|head -n 1` 

#1、设置
echo  '调整selinux、关闭防火墙'
systemctl stop firewalld.service
systemctl disable firewalld.service
firewall-cmd --state
sed -i '/^SELINUX=.*/c SELINUX=disable' /etc/selinux/config
grep --color=auto '^SELINUX' /etc/selinux/config
setenforce 0
#安装必要的软件
echo  '正在安装必要的软件,请稍后................'

yum install -y epel-release &> /dev/null
yum install -y ntp wget vim net-tools openssh tree &> /dev/null

#修改阿里云源
mkdir -p /etc/yum.repos.d/remark && mv  /etc/yum.repos.d/* /etc/yum.repos.d/remark/
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

sed -i '/aliyuncs.com/d' /etc/yum.repos.d/*.repo
#清理缓存
yum clean all && yum makecache

echo  '修改成阿里源!'

#同步时间
[[ -f /usr/sbin/ntpdate ]] || { echo "install ntp";yum install ntp ntpdate -y &> /dev/null; }
#若没NTP则安装
/usr/sbin/ntpdate ntp6.aliyun.com
echo "*/3 * * * * /usr/sbin/ntpdate ntp6.aliyun.com  &> /dev/null" > /tmp/crontab
crontab /tmp/crontab

echo  '时间同步完成,正在升级软件,请稍后................'

#升级所有包,不改变软件设置和系统设置,系统版本升级,内核不改变
yum -y upgrade &> /dev/null
        
 echo '------------执行完毕---------' 

##########################################
#2 安装
yum  install mariadb-server -y

echo 'skip_name_resolve = ON
innodb_file_per_table = ON
' >> /etc/my.cnf
echo '启动数据库服务'
systemctl start mariadb && systemctl enable mariadb

sleep 5
netstat -antp|grep mysqld
#mysql_secure_installation #初始化设置密码,自动交互
[[ -f /usr/bin/expect ]] || { yum install expect -y; } #若没expect则安装
/usr/bin/expect << EOF
set timeout 30
spawn mysql_secure_installation
expect {
    "enter for none" { send "\r"; exp_continue}
    "Y/n" { send "Y\r" ; exp_continue}
    "password:" { send "$dbrootpw\r"; exp_continue}
    "new password:" { send "$dbrootpw\r"; exp_continue}
    "Y/n" { send "Y\r" ; exp_continue}
    eof { exit }
}
EOF
#测试
mysql -u root -p$dbrootpw -e "show databases;"
[ $? = 0 ] || { echo "mariadb初始化失败";exit; }

#创建zabbix库并授权
echo '创建数据库、用户授权'
mysql -u root -p$dbrootpw -e "
CREATE DATABASE zabbix CHARSET 'utf8';
GRANT ALL ON zabbix.* TO '$dbuser'@'$dbip' IDENTIFIED BY '$dbpassword';
GRANT ALL ON zabbix.* TO '$dbuser'@'127.0.0.1' IDENTIFIED BY '$dbpassword';
GRANT ALL ON zabbix.* TO '$dbuser'@'localhost' IDENTIFIED BY '$dbpassword';
FLUSH PRIVILEGES;
"

echo '安装zabbix----------------------------------------------------------------------------------------------------------'
#安装zabbix
yum -y install httpd php php-mysql php-mbsting php-gd php-bcmath php-ldap php-xml
sleep 5

if ! yum -y install https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-release-"$zabbix_version"-1.el7.noarch.rpm ; then yum -y install  https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-release-"$zabbix_version"-2.el7.noarch.rpm ; fi
yum clean all && yum makecache
sleep 5

wget https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-agent-"$zabbix_version"."$zabbix_subversion"-1.el7.x86_64.rpm
wget https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-java-gateway-"$zabbix_version"."$zabbix_subversion"-1.el7.x86_64.rpm
wget https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-server-mysql-"$zabbix_version"."$zabbix_subversion"-1.el7.x86_64.rpm
wget https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-web-mysql-"$zabbix_version"."$zabbix_subversion"-1.el7.noarch.rpm
wget https://mirrors.aliyun.com/zabbix/zabbix/"$zabbix_version"/rhel/7/x86_64/zabbix-web-"$zabbix_version"."$zabbix_subversion"-1.el7.noarch.rpm
yum -y install *.rpm

#导入数据库
gzip -d /usr/share/doc/zabbix-server-mysql-"$zabbix_version"."$zabbix_subversion"/create.sql.gz
mysql -u$dbuser -h127.0.0.1 -p$dbpassword zabbix  < /usr/share/doc/zabbix-server-mysql-"$zabbix_version"."$zabbix_subversion"/create.sql
 
#配置zabbix
sed -i '/^# DBHost=/c DBHost='$IP'' /etc/zabbix/zabbix_server.conf
sed -i '/^DBUser=/c DBUser='$dbuser'' /etc/zabbix/zabbix_server.conf
sed -i '/^# DBPassword=/c DBPassword='$dbpassword'' /etc/zabbix/zabbix_server.conf
sed -i '/^# JavaGateway=/c JavaGateway='$IP'' /etc/zabbix/zabbix_server.conf
sed -i '/^# JavaGatewayPort=10052/c JavaGatewayPort=10052' /etc/zabbix/zabbix_server.conf
sed -i '/^# StartJavaPollers=0/c StartJavaPollers=5' /etc/zabbix/zabbix_server.conf

sed -i '/php_value date.timezone*/c php_value date.timezone Asia/shanghai' /etc/httpd/conf.d/zabbix.conf

#配置zabbix-java-geteway
sed -i '/^# LISTEN_IP="0.0.0.0"/c LISTEN_IP="0.0.0.0"' /etc/zabbix/zabbix_java_gateway.conf
sed -i '/^# LISTEN_PORT=10052/c LISTEN_PORT=10052' /etc/zabbix/zabbix_java_gateway.conf
sed -i '/^TIMEOUT=3/c TIMEOUT=30' /etc/zabbix/zabbix_java_gateway.conf

#启动服务并添加为开机启动项
if  systemctl start zabbix-server.service && systemctl start httpd && systemctl start zabbix-agent && systemctl start zabbix-java-gateway ; then systemctl enable zabbix-server.service && systemctl enable httpd && systemctl enable zabbix-agent && systemctl enable zabbix-java-gateway
        ss -tnl
        echo ‘=================================安装完成========================================’
        echo 'Please visit http://'$IP'/zabbix  to further configuration!!!!!'
else
        echo 'The install is fail!!!!!!!!'
fi

执行安装脚本
~]# . zabbix_autoinstall.sh
Enter db_ip (172.16.%.%)> 172.16.%.%    #授权访问的ip地址范围
Enter db_root_password > 123456           #数据库root密码
Enter zabbix_dbuser > zbxuser
Enter zabbix_dbpassword >123456
Enter zabbix_version (3.x).*  > 3.4
Enter zabbix_subversion *.*.(x) > 1         #这里输入的是zabbix版本号为3.4.1
........
  • 整个过程大概需要10-20分钟,最后出现安装完成
‘=================================安装完成========================================’
Please visit http://172.16.16.250/zabbix  to further configuration!!!!!

  • 用浏览器访问提示URL进行下一步配置


    浏览器访问url

    数据库设置

    服务节点设置

    前端配置完成

    登录

你可能感兴趣的:(zabbix简易一键安装脚本)