1 #!/bin/bash 2 3 # 检查操作系统版本,该脚本只能运行在 Centos 7.x 系统上 4 5 cat /etc/redhat-release |grep -i centos |grep '7.[[:digit:]]' >/dev/null 6 7 if [[ $? != 0 ]] 8 then 9 echo -e "不支持的操作系统,该脚本只适用于CentOS 7.x x86_64 操作系统" 10 exit 1 11 fi 12 13 # 配置无人值守的安装,定义安装过程中需要用到的一些信息 14 15 DBPassword=zabbix_db_password 16 CacheSize=256M 17 ZBX_SERVER_NAME=XXX-Zabbix-Server 18 http_port=59980 19 20 # 配置 Zabbix 4.2 YUM 源 21 22 rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/4.2/rhel/7/x86_64/zabbix-release-4.2-1.el7.noarch.rpm 23 sed -i 's/repo.zabbix.com/mirrors.aliyun.com/zabbix/g' /etc/yum.repos.d/zabbix.repo 24 yum clean all 25 26 # 安装 Zabbix 4.2 Server、Web、Agent、DB 27 yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-agent mysql mariadb-server 28 29 # 配置 Zabbix 数据库(创建账号,并导入zabbix库结构) 30 31 systemctl enable mariadb.service 32 systemctl start mariadb.service 33 mysql -e "create database zabbix character set utf8 collate utf8_bin;" 34 mysql -e "grant all privileges on zabbix.* to zabbix@localhost identified by '$DBPassword';" 35 mysql -e "flush privileges;" 36 zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql zabbix 37 38 # 配置 Zabbix Web Server 39 40 sed -i 's/# php_value date.timezone Europe/Riga/php_value date.timezone Asia/Shanghai/' /etc/httpd/conf.d/zabbix.conf 41 sed -i 's/Listen 80/Listen '$http_port'/' /etc/httpd/conf/httpd.conf 42 43 cp /usr/share/zabbix/conf/zabbix.conf.php.example /etc/zabbix/web/zabbix.conf.php 44 sed -i "10c $DB["PASSWORD"] = '$DBPassword';" /etc/zabbix/web/zabbix.conf.php 45 sed -i "16c $ZBX_SERVER_NAME = '$ZBX_SERVER_NAME';" /etc/zabbix/web/zabbix.conf.php 46 47 # 配置 Zabbix Server 48 49 sed -i "/^# DBPassword=/a DBPassword=$DBPassword" /etc/zabbix/zabbix_server.conf 50 sed -i "/^# CacheSize=8M/a CacheSize=$CacheSize" /etc/zabbix/zabbix_server.conf 51 sed -i "/^# StartPingers=1/a StartPingers=10" /etc/zabbix/zabbix_server.conf 52 53 # 启动 Zabbix 相关服务,并设置开机自启动 54 55 systemctl enable zabbix-server zabbix-agent httpd 56 systemctl start zabbix-server zabbix-agent httpd 57 58 # 配置 Zabbix 防火墙 59 60 firewall-cmd --permanent --zone=public --add-port=10051/tcp 61 firewall-cmd --permanent --zone=public --add-port=162/udp 62 firewall-cmd --permanent --zone=public --add-port=$http_port/tcp 63 firewall-cmd --reload 64 65 # 禁用服务器 SELinux 66 67 etenforce 0 68 sed -i 's/SELINUX=enforcing/SELINUX=permissive/' /etc/sysconfig/selinux 69 70 # 安装成功,提示访问方式 71 72 echo -e " 您可以通过以下地址来访问 Zabbix Web Page " 73 for i in `ip a | grep 'inet ' | awk '{print $2}' | awk -F '/' '{print $1}'` ; do echo -e " http://$i:$http_port/zabbix" ; done