一、前言
今天分享一个 zabbix 自动安装脚本。
安装环境是 Centos 7.6,依赖采用 yum 方式安装,服务全部源码安装。
期望效果:脚本成功运行完毕后,服务全部启动并正常监听,且已经加入开机启动。
另外因为编译PHP时需要用到高版本的 libzip,而 yum 源版本不满足,所以还有:
zabbix agent 选取的 agent2,所以需要 golang:
zabbix 6.2 和 6.0 编译时 gcc 有点问题,暂时解决不了,采用了 5.0 的版本。
php 8 显示 zabbix web 界面有点问题,使用了 7.4 的版本。
有些包(比如 boost 和 libzip)可能需要一些手段才能下载,可以另外下载好放到安装脚本同级目录下,会自动检测到。
编译时间最长的为 mysql 和 php,安装过程输出到安装脚本同级目录的 install.log
,可以另起一个终端,以 tail -f install.log
来实时查看安装。
完整版脚本请查看:完整版
二、说明
第一部分是提示信息函数,这里分了成功,失败,警告和普通四个提示等级,减少 echo 命令代码。
# 根据信息提示类型定义打印格式
SETCOLOR_SUCCESS='\E[1;32m' #green
SETCOLOR_FAILURE='\E[1;31m' #red
SETCOLOR_WARNING='\E[1;33m' #yellow
SETCOLOR_NORMAL='\E[1;34m' #blue
RES='\E[0m' #清除颜色
success()
{
echo -e "${SETCOLOR_SUCCESS}[SUCCESS]${RES} $1"
}
failure()
{
echo -e "${SETCOLOR_FAILURE}[FAILED]${RES} $1"
}
warning()
{
echo -e "${SETCOLOR_WARNING}[WARN]${RES} $1"
}
normal()
{
echo -e "${SETCOLOR_NORMAL}[NORMAL]${RES} $1"
}
实际效果大概这样:
接着是检查网络情况和替换 yum 国内源以及设置脚本所在路径的变量:
normal "检查网络"
if ! ping -c2 www.baidu.com &>/dev/null
then
failure "网络不通,中止安装"
exit
fi
normal "替换国内源..."
sed -e 's|^mirrorlist=|#mirrorlist=|g' -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g' -i.bak /etc/yum.repos.d/CentOS-*.repo
yum makecache &> /dev/null
work_dir=$(pwd)
1. 安装 mysql
检查 mysql 和 boost 包是否在,如果没有检测到自动下载。
version="mysql-5.7.9.tar.gz"
b_version="boost_1_59_0.tar.gz"
user="mysql"
cd ${work_dir}/mysql57
if [ ! -e $version ];then
warning "找不到\"${work_dir}/${version}\", 开始下载"
wget -c https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.9.tar.gz
fi
if [ ! -e $b_version ];then
warning "找不到\"${work_dir}/${b_version}\", 开始下载"
wget -c https://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz --no-check-certificate
fi
# 解压
tar -zxf mysql-5.7.9.tar.gz
if [ ! -d /usr/local/boost ];then
mkdir -p /usr/local/boost
fi
# 将boost包放到指定目录
cp ./boost_1_59_0.tar.gz /usr/local/boost
这里是安装 mysql 编译依赖和创建 mysql 安装前需要的用户或目录
normal "安装 mysql 编译依赖..."
yum -y install gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgcrypt libtool bison openssl-devel &> /dev/null
normal "创建 mysql 用户..."
if ! id $user &>/dev/null
then
useradd $user -M -s /sbin/nologin
fi
normal "创建 mysql 相关文件夹..."
if [ ! -d /usr/local/mysql57 ];then
mkdir -p /usr/local/mysql57
fi
chown mysql:mysql /usr/local/mysql57
if [ ! -d /data/mysql57 ];then
mkdir -p /data/mysql57
fi
chown -R mysql:mysql /data/mysql57
这里是执行 mysql 的编译安装
cd ./mysql-5.7.9
normal "安装过程日志生成在 \"autoInstall.sh\" 同级目录下, 如有需要可以进行查看"
normal "安装 mysql, 时间较久, 不要关闭本窗口, 请耐心等待..."
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql57 \
-DMYSQL_UNIX_ADDR=/data/mysql57/mysql.sock \
-DMYSQL_DATADIR=/data/mysql57 \
-DSYSCONFDIR=/usr/local/mysql57 \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DWITH_XTRADB_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EXTRA_CHARSETS=1 \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DWITH_BIG_TABLES=1 \
-DWITH_DEBUG=0 \
-DENABLE_DTRACE=0 \
-DDOWNLOAD_BOOST=1 \
-DWITH_BOOST=/usr/local/boost &> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "mysql 安装失败"
exit
fi
make &>> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "mysql 安装失败"
exit
fi
make install &>> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "mysql 安装失败"
exit
fi
这里是修改 mysql 配置、初始化 mysql、启动并将 mysql 加入 init.d 并设置开机自启。
normal "创建 mysql 配置文件..."
echo -e "[mysqld]\n\
basedir=/usr/local/mysql57/\n\
datadir=/data/mysql57/\n\
port=3306\n\
pid-file=/data/mysql57/mysql.pid\n\
socket=/data/mysql57/mysql.sock\n\
[mysqld_safe]\n\
log-error=/data/mysql57/mysql.log" > /usr/local/mysql57/my.cnf
chown mysql:mysql /usr/local/mysql57/my.cnf
normal "初始化 mysql..."
/usr/local/mysql57/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql57 --datadir=/data/mysql57 >& /usr/local/mysql57/mysql_passwd
normal "将 mysql 启动..."
cp ./support-files/mysql.server /etc/init.d/mysqld57
chmod +x /etc/init.d/mysqld57
/etc/init.d/mysqld57 start
if [ $? -ne 0 ];then
failure "mysql 启动失败"
exit
fi
normal "检查3306端口监听:"
netstat -ntlp | grep 3306
normal "将 mysql 加入开机启动..."
chkconfig --add mysqld57
chkconfig mysqld57 on
这里是修改 mysql 初始密码(mysql 要求修改初始密码才让执行mysql语句),如果想换密码,修改所有 “123456” 项。
normal "修改 mysql root 初始密码..."
mysql_password=$(cat /usr/local/mysql57/mysql_passwd | grep 'password' | awk -F':' '{print $4}' | sed "s/ //g")
/usr/local/mysql57/bin/mysql -u root -p${mysql_password} --connect-expired-password -e "alter user 'root'@'localhost' identified by '123456';" &>> ${work_dir}/install.log
echo "mysql root's password is \"123456\"" > mysql_passwd.txt
mysql_password="123456"
echo -e "mysql new password is in \"${work_dir}/mysql57/mysql_passwd.txt\""
success "mysql 安装成功!"
2. 安装 zabbix
这里是创建 zabbix 的数据库和相关 mysql 用户,数据库名和用户名可自行修改。
normal "安装 zabbix..."
normal "创建 zabbix 数据库和用户..."
/usr/local/mysql57/bin/mysql -u root -p${mysql_password} -e "drop database if exists zabbix;" &>> ${work_dir}/install.log
/usr/local/mysql57/bin/mysql -u root -p${mysql_password} -e "create database zabbix character set utf8 collate utf8_bin;" &>> ${work_dir}/install.log
/usr/local/mysql57/bin/mysql -u root -p${mysql_password} -e "create user 'zabbix'@'localhost' identified by 'zabbix';" &>> ${work_dir}/install.log
/usr/local/mysql57/bin/mysql -u root -p${mysql_password} -e "grant all privileges on zabbix.* to 'zabbix'@'localhost';" &>> ${work_dir}/install.log
/usr/local/mysql57/bin/mysql -u root -p${mysql_password} -e "SET GLOBAL log_bin_trust_function_creators = 1;" &>> ${work_dir}/install.log
这里是检测 zabbix 安装包、创建 zabbix server 相关用户
cd ${work_dir}
zab_package="zabbix-5.0.31.tar.gz"
zab_server_user="zabbix"
zab_agent_user="zabAgent"
base_path="/usr/local"
if [ ! -e ${zab_package} ]; then
warning "'${work_dir}/${zab_package}'安装包不存在,下载中..."
wget -c https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.31.tar.gz
fi
tar -zxf zabbix-5.0.31.tar.gz
cd ${work_dir}/zabbix-5.0.31
normal "创建 zabbix_server 用户..."
if ! id ${zab_server_user} &>/dev/null
then
groupadd --system ${zab_server_user}
useradd --system -g ${zab_server_user} -d /usr/lib/${zab_server_user} -s /sbin/nologin -c "Zabbix Monitoring System" ${zab_server_user}
fi
这里是导入 zabbix 表和数据,注意数据库名和用户与创建的保持一致
normal "导入 zabbix表和数据..."
/usr/local/mysql57/bin/mysql -uzabbix zabbix < ./database/mysql/schema.sql -pzabbix &>> ${work_dir}/install.log
/usr/local/mysql57/bin/mysql -uzabbix zabbix < ./database/mysql/images.sql -pzabbix &>> ${work_dir}/install.log
/usr/local/mysql57/bin/mysql -uzabbix zabbix < ./database/mysql/data.sql -pzabbix &>> ${work_dir}/install.log
编译安装 zabbix server 并进行配置
normal "安装 zabbix 编译依赖"
yum install gcc mysql-devel libevent-devel libcurl-devel libxml2-devel libssh2-devel net-snmp-devel -y &> /dev/null
normal "安装 zabbix_server..."
./configure --prefix=/usr/local/zabbix \
--enable-server \
--with-mysql \
--enable-ipv6 \
--with-net-snmp \
--with-libcurl \
--with-libxml2 &>> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "zabbix_server 安装失败"
exit
fi
make install &>> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "zabbix_server 安装失败"
exit
fi
normal "修改 zabbix_server 配置..."
sed -i 's/^LogFile=\/tmp\/zabbix_server.log/LogFile=\/usr\/local\/zabbix\/zabbix_server.log/' /usr/local/zabbix/etc/zabbix_server.conf
sed -i 's/^# PidFile=\/tmp\/zabbix_server.pid/PidFile=\/usr\/local\/zabbix\/tmp\/zabbix_server.pid/' /usr/local/zabbix/etc/zabbix_server.conf
sed -i 's/^# DBPassword=/DBPassword=zabbix/;s/^# DBPassword=/DBPassword=zabbix/;s/^# DBSocket=/DBSocket=\/data\/mysql57\/mysql.sock/;s/^# DBPort=/DBPort=3306/' /usr/local/zabbix/etc/zabbix_server.conf
normal "创建 zabbix_server 相关文件夹..."
mkdir /usr/local/zabbix/tmp
chown -R zabbix:zabbix /usr/local/zabbix
normal "zabbix server 安装完毕..."
zabbix agent 选取的是 agent2,所以编译安装需要 golang
normal "安装 golang..."
if ! $(go version &> /dev/null 2>&1); then
warning "找不到 golang, 开始安装 golang..."
go_package="go1.20.1.linux-amd64.tar.gz"
cd ${work_dir}
if [ ! -e ${go_package} ]; then
warning "'${work_dir}/${go_package}'安装包不存在,下载中..."
wget -c https://dl.google.com/go/go1.20.1.linux-amd64.tar.gz
fi
tar xzf go1.20.1.linux-amd64.tar.gz
if [ -d /usr/local/go ]; then
failure "文件夹 /usr/local/go 已经存在, 请处理"
exit
fi
mv go /usr/local/
export GOROOT="/usr/local/go"
export GOPATH="$HOME/go"
export PATH=$PATH:$GOROOT/bin
fi
if [ $(go version | wc -l) = "1" ]
then
success "golang 安装完毕..."
else
failure "golang 安装失败"
exit
fi
go env -w "GOPROXY=https://goproxy.cn,direct"
安装 agent2
normal "创建 zabbix_agent 用户..."
cd ${work_dir}/zabbix-5.0.31
if ! id ${zab_agent_user} &>/dev/null
then
groupadd --system ${zab_agent_user}
useradd --system -g ${zab_agent_user} -d /usr/lib/${zab_agent_user} -s /sbin/nologin -c "Zabbix Monitoring System" ${zab_agent_user}
fi
normal "安装 zabbix agent2..."
./configure --prefix=/usr/local/zabbixAgent \
--enable-agent2 &>> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "zabbix agent2 安装失败"
exit
fi
make install &>> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "zabbix agent2 安装失败"
exit
fi
normal "修改 zabbix agent2 配置..."
sed -i 's/^LogFile=\/tmp\/zabbix_agent2.log/LogFile=\/usr\/local\/zabbixAgent\/zabbix_agent2.log/' /usr/local/zabbixAgent/etc/zabbix_agent2.conf
sed -i 's/^# PidFile=\/tmp\/zabbix_agent2.pid/PidFile=\/usr\/local\/zabbixAgent\/tmp\/zabbix_agent2.pid/' /usr/local/zabbixAgent/etc/zabbix_agent2.conf
normal "创建 zabbix agent2 相关文件夹..."
mkdir /usr/local/zabbixAgent/tmp
chown -R zabAgent:zabAgent /usr/local/zabbixAgent
success "zabbix agent2 安装成功, 移除 golang..."
rm -rf /usr/local/go
安装nginx
nginx_package="nginx-1.16.1.tar.gz"
cd ${work_dir}
if [ ! -e ${nginx_package} ]; then
warning "'${work_dir}/${nginx_package}' 安装包不存在,下载中..."
wget -c http://nginx.org/download/nginx-1.16.1.tar.gz
fi
tar -xzf nginx-1.16.1.tar.gz
cd nginx-1.16.1
normal "安装 nginx 编译依赖..."
yum -y install gcc gcc-c++ openssl-devel pcre-devel make zlib-devel psmisc &> /dev/null
mkdir -p /var/tmp/nginx/
useradd -s /sbin/nologin -M nginx
normal "安装 nginx..."
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_sub_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_realip_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --with-file-aio --with-http_secure_link_module &>> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "nginx 安装失败"
exit
fi
make &>> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "nginx 安装失败"
exit
fi
make install &>> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "nginx 安装失败"
exit
fi
normal "修改 nginx 配置..."
echo -e "user nginx;\n\
worker_processes 2;\n\
events {\n\
use epoll;\n\
worker_connections 2048;\n\
}\n\
http {\n\
include mime.types;\n\
default_type application/octet-stream;\n\
log_format main '\$remote_addr - \$remote_user [\$time_local] "\$request" '\n\
'\$status \$body_bytes_sent "\$http_referer" '\n\
'"\$http_user_agent" "\$http_x_forwarded_for"';\n\
access_log logs/access.log main;\n\
sendfile on;\n\
keepalive_timeout 65;\n\
server_names_hash_bucket_size 128;\n\
client_header_buffer_size 32k;\n\
large_client_header_buffers 4 64k;\n\
client_max_body_size 8m;\n\
server_tokens off;\n\
server {\n\
listen 80;\n\
server_name localhost;\n\
location / {\n\
root html/ui;\n\
index index.html index.htm index.php;\n\
}\n\
error_page 500 502 503 504 /50x.html;\n\
location = /50x.html {\n\
root html;\n\
}\n\
location ~ \.php$ {\n\
root html/ui;\n\
fastcgi_pass 127.0.0.1:9000;\n\
fastcgi_index index.php;\n\
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n\
include fastcgi_params;\n\
}\n\
}\n\
}" > /usr/local/nginx/conf/nginx.conf
安装 zabbix web 界面
cp -R ${work_dir}/zabbix-5.0.31/ui /usr/local/nginx/html
chown -R nginx:nginx /usr/local/nginx/html
success "nginx 安装完毕..."
安装编译 PHP 需要的 libzip
cmake_package="cmake-3.25.0-linux-x86_64.tar.gz"
cd ${work_dir}
if [ ! -e ${cmake_package} ]; then
warning "'${work_dir}/${cmake_package}' 安装包不存在,下载中..."
wget -c https://cmake.org/files/LatestRelease/cmake-3.25.0-linux-x86_64.tar.gz
fi
tar xzf cmake-3.25.0-linux-x86_64.tar.gz
libzip_package="libzip-1.7.3.tar.gz"
cd ${work_dir}
if [ ! -e ${libzip_package} ]; then
warning "'${work_dir}/${libzip_package}' 安装包不存在,下载中..."
wget -c https://libzip.org/download/libzip-1.7.3.tar.gz
fi
tar xzf libzip-1.7.3.tar.gz
mkdir libzip-1.7.3/build
cd libzip-1.7.3/build
yum -y install libxml2 libxml2-devel openssl openssl-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel pcre pcre-devel libxslt libxslt-devel bzip2 bzip2-devel sqlite-devel &> /dev/null
${work_dir}/cmake-3.25.0-linux-x86_64/bin/cmake -DCMAKE_INSTALL_PREFIX=/data/apps/libs .. &>> ${work_dir}/install.log
make &>> ${work_dir}/install.log
make install &>> ${work_dir}/install.log
export PKG_CONFIG_PATH=/data/apps/libs/lib64/pkgconfig:$PKG_CONFIG_PATH
安装 php
php_package="php-7.4.33.tar.gz"
cd ${work_dir}
if [ ! -e ${php_package} ]; then
warning "'${work_dir}/${php_package}' 安装包不存在,下载中..."
wget -c https://www.php.net/distributions/php-7.4.33.tar.gz
fi
tar xzf php-7.4.33.tar.gz
cd php-7.4.33
normal "安装 PHP..."
./configure --prefix=/usr/local/php --with-curl --with-freetype --enable-gd --with-gettext --with-kerberos --with-libdir=lib64 --with-mysqli --with-openssl --with-pdo-mysql --with-pdo-sqlite --with-pear --with-jpeg --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --with-zip --enable-mbstring --disable-mbregex &>> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "php 安装失败"
exit
fi
make &>> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "php 安装失败"
exit
fi
make install &>> ${work_dir}/install.log
if [ $? -ne 0 ];then
failure "php 安装失败"
exit
fi
修改 php 配置
normal "修改 PHP 配置..."
cp php.ini-development /usr/local/php/lib/php.ini && cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
sed -i 's/^user = nobody/user = nginx/;s/^group = nobody/group = nginx/' /usr/local/php/etc/php-fpm.d/www.conf
sed -i 's/^post_max_size = 8M/post_max_size = 16M/;s/^max_execution_time = 30/max_execution_time = 300/;s/^max_input_time = 60/max_input_time = 300/;s/^;date.timezone =/date.timezone = Asia\/Shanghai/;s/^mysqli.default_socket =/mysqli.default_socket = \/data\/mysql57\/mysql.sock/;s/^pdo_mysql.default_socket=/pdo_mysql.default_socket=\/data\/mysql57\/mysql.sock/' /usr/local/php/lib/php.ini
success "PHP 安装完毕..."
zabbix web 资源没有中文字体,所以换成中文就会乱码。只需要把 Windows 的宋体或者其他中文字体文件复制一份并更名就可以了。
normal "替换 zabbix web 字体..."
cd ${work_dir}
if [ ! -e ${work_dir}/simsun.ttc ]; then
warning "找不到中文字体文件 \"simsun.ttc\",后续可自行替换: 将中文字体文件 \"simsun.ttc\" 复制到 \"/usr/local/nginx/html/ui/assets/fonts\" 目录下, 并更名为 \"DejaVuSans.ttf\""
else
mv /usr/local/nginx/html/ui/assets/fonts/DejaVuSans.ttf /usr/local/nginx/html/ui/assets/fonts/DejaVuSans.ttf.bak
cp simsun.ttc /usr/local/nginx/html/ui/assets/fonts/DejaVuSans.ttf
fi
启动 zabbix server、zabbix agent2、nginx 和 php
normal "启动 zabbix server..."
sudo -u zabbix /usr/local/zabbix/sbin/zabbix_server
if [ $? -ne 0 ];then
failure "zabbix server 启动失败"
exit
fi
success "zabbix server 启动完成..."
normal "启动 zabbix agent2..."
sudo -u zabAgent /usr/local/zabbixAgent/sbin/zabbix_agent2 2>&1 &
if [ $? -ne 0 ];then
failure "zabbix agent2 启动失败"
exit
fi
success "zabbix agent2 启动完成..."
normal "启动 nginx..."
/usr/local/nginx/sbin/nginx
if [ $? -ne 0 ];then
failure "nginx 启动失败"
exit
fi
success "nginx 启动完成..."
normal "启动 php-fpm..."
/usr/local/php/sbin/php-fpm
if [ $? -ne 0 ];then
failure "php-fpm 启动失败"
exit
fi
success "php-fpm 启动完成..."
将 zabbix server、zabbix agent2、nginx 和 php 加入开机启动
#加入开机启动
normal "添加各服务到开机启动..."
echo '#!/bin/sh' > /usr/local/zabbix/start.sh
echo -e "sudo -u zabbix /usr/local/zabbix/sbin/zabbix_server\n\
sudo -u zabAgent /usr/local/zabbixAgent/sbin/zabbix_agent2 2>&1 &\n\
/usr/local/nginx/sbin/nginx\n\
/usr/local/php/sbin/php-fpm" >> /usr/local/zabbix/start.sh
chmod +x /usr/local/zabbix/start.sh
chmod +x /etc/rc.d/rc.local
echo "/usr/local/zabbix/start.sh" >> /etc/rc.d/rc.local
success "添加完毕..."
success "zabbix 整体服务安装完成"
各端口监听信息:
netstat -nltp | grep -E "80|9000|3306|10051|10050"
结束
访问服务器 ip 来配置 zabbix web。
初始用户名和密码为:Admin/zabbix
因为 zabbix 数据库用户设置的是 localhost,所以数据库地址那里要填 localhost,zabbix server 那里填写 127.0.0.1。