43.第35章 Zabbix

一.zabbix 主动与被动监控模式

被动模式:
无论是模式还是被动模式,都是站在zabbix agent角度来说的工作模式,比如被动模式,是说zabbix agent被动的接受zabbix server周期性发送过来的数据收集指令,在被动模式之下,zabbix server会根据主机关联的模板中的监控项和数据采集间隔时间,周期性的打开随机端口并向zabbix agent服务器的10050发起tcp连接,然后发送获取监控项数据的指令,即zabbix server发送什么指令那么zabbix agent就收集什么数据,zabbix server什么时候发送zabbix agent就什么时候采集,zabbix server不发送zabbix agent就一直不响应,所以zabbix agent也不用关心其监控项和数据采集周期间隔时间。
被动模式的优点就是配置简单,安装后即可使用,因此也成为zabbix 的默认工作模式,但是被动模式的最大问题就是会加大zabbix server的工作量,在数百甚至数千台服务器的环境下会导致zabbix server需要轮训向每个zabbix agent发送数据采集指令,如果zabbix server负载很高还会导致不能及时获取到最新数据,但由于无需其他复杂配置,被设置为了默认的工作方式。

主动模式:
主动模式是由zabbix agent主动向zabbix server的10051端口发起tcp连接请求,因此主动模式下必须在zabbixagent配置文件中指定zabbix server的IP或者主机名(必须可以被解析为IP地址),在连接到zabbix server之前zabbix agent是不知道自己要采集那些数据以及间隔多久采集一次数据的,然后在连接到zabbix server以后获取到自己的监控项和数据采集间隔周期时间,然后再根据监控项采集数据并返回给zabbix server,在主动模式下不再需要zabbix serve向zabbix agent发起连接请求,因此主动模式在一定程度上可减轻zabbix server打开的本地随机端口和进程数,在一定程度就减轻看zabbix server的压力。

二. 如何自定义监控项:

  1. 先通过脚本或命令获取监控项数据

  2. 将脚本或命令添加到zabbix agent配置文件中的自定义监控项并重启

    UserParameter=n50[*],/bin/bash /data/scripts/n50.sh $1

    UserParameter=n51,/usr/bin/python3 /data/scripts/n50.py

    UserParameter=tcp_status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/tcp_status.sh $1 $2

  3. 在zabbix server使用zabbix_get命令测试监控项能否获取到数据

    zabbix_get -s 10.0.0.107 -p 10050 -k "tcp_status["tcp_status","ESTAB"]"

  4. 在zabbix web添加监控项、触发器、图形或者创建模板

  5. 验证数据

三.zabbix安装

1.安装数据库

[root@centos7-2 src]# cat install_mysql5.7_v3.sh 
#!/bin/bash
#
#******************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-06-01
#FileName:      install_mysql5.7_v3.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_mysql5.7 for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#******************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'
MYSQL_URL=https://mirrors.cloud.tencent.com/mysql/downloads/MySQL-
MYSQL_VERSION='5.7/'
MYSQL_FILE='mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz'
MYSQL_ROOT_PASSWORD=123456
PASSWORD=123456

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
    OS_RELEASE_VERSION=`lsb_release -rs |awk -F'.' '{print $1}'`
}

check_file(){
    cd  ${SRC_DIR}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${MYSQL_FILE} ];then
        ${COLOR}"缺少${MYSQL_FILE}文件"${END}
        ${COLOR}'开始下载MYSQL二进制安装包'${END}
        wget ${MYSQL_URL}${MYSQL_VERSION}${MYSQL_FILE} || { ${COLOR}"MYSQL二进制安装包下载失败"${END}; exit; }
    else
        ${COLOR}"${MYSQL_FILE}文件已准备好"${END}
    fi
}

install_mysql(){
    [ -d /usr/local/mysql ] && { ${COLOR}"MySQL数据库已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装MySQL数据库..."${END}
    ${COLOR}'开始安装MYSQL依赖包'${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install libaio perl-Data-Dumper ncurses-compat-libs &> /dev/null
    elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then
        yum -y install libaio perl-Data-Dumper &> /dev/null
    else
        apt update &> /dev/null;apt -y install numactl libaio-dev libtinfo5 &> /dev/null
    fi
    cd  ${SRC_DIR}
    tar xf ${MYSQL_FILE} -C /usr/local/
    MYSQL_DIR=`echo ${MYSQL_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    ln -s  /usr/local/${MYSQL_DIR} /usr/local/mysql
    id mysql &> /dev/null || { useradd -s /sbin/nologin -r  mysql ; ${COLOR}"创建mysql用户"${END}; }
    chown -R  mysql.mysql /usr/local/mysql/
    echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
    .  /etc/profile.d/mysql.sh
    cat > /etc/my.cnf <<-EOF
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
    [ -d /data/mysql ] || mkdir -p /data/mysql &> /dev/null
    chown -R  mysql.mysql /data/mysql
    mysqld --initialize --user=mysql --datadir=/data/mysql 
    cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/mysqld
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        chkconfig --add mysqld
    else
        update-rc.d -f mysqld defaults
    fi
    cat > /lib/systemd/system/mysqld.service <<-EOF
[Unit]
Description=mysql database server
After=network.target

[Service]
Type=notify
PrivateNetwork=false
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
SuccessExitStatus=5 6
ExecStart=/etc/init.d/mysqld start
ExecStop=/etc/init.d//mysqld stop
ExecReload=/etc/init.d/mysqld reload

[Install]
WantedBy=multi-user.target
Alias=mysqld.service
EOF
    systemctl daemon-reload
    systemctl enable --now mysqld &> /dev/null
    [ $? -ne 0 ] && { ${COLOR}"数据库启动失败,退出!"${END};exit; }
    MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
    mysqladmin  -uroot -p${MYSQL_OLDPASSWORD} password ${MYSQL_ROOT_PASSWORD} &>/dev/null
    ${COLOR}"MySQL数据库安装完成"${END}
}

create_database(){
    mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "create database zabbix_server character set utf8 collate utf8_bin;grant all privileges on zabbix_server.* to zabbix@'10.0.0.%' identified by '${PASSWORD}';" &>/dev/null

    mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "create database zabbix_proxy_passive character set utf8 collate utf8_bin;grant all privileges on zabbix_proxy_passive.* to proxy@'10.0.0.%' identified by '${PASSWORD}';" &>/dev/null

    mysql -uroot -p${MYSQL_ROOT_PASSWORD} -e "create database zabbix_proxy_active character set utf8 collate utf8_bin;grant all privileges on zabbix_proxy_active.* to proxy@'10.0.0.%' identified by '${PASSWORD}';" &>/dev/null
}

main(){
    os
    check_file
    install_mysql
    create_database
}

main

2.安装zabbix server

[root@centos7 src]# grep "^[^#]" zabbix_server.conf 
ListenPort=10051
LogFile=/tmp/zabbix_server.log
LogFileSize=0
DBName=zabbix
DBUser=zabbix
DBPort=3306
StartPollers=6
StartPreprocessors=4
StartPollersUnreachable=1
StartTrappers=5
StartPingers=3
StartHTTPPollers=2
StartTimers=2
StartEscalators=2
StartAlerters=3
SNMPTrapperFile=/tmp/zabbix_traps.tmp
HousekeepingFrequency=8
MaxHousekeeperDelete=1000000
CacheSize=16M
CacheUpdateFrequency=180
StartDBSyncers=4
HistoryCacheSize=16M
HistoryIndexCacheSize=4M
TrendCacheSize=4M
ValueCacheSize=8M
Timeout=5
TrapperTimeout=300
UnreachablePeriod=10
UnavailableDelay=5
UnreachableDelay=5
LogSlowQueries=3000
StartProxyPollers=3
ProxyConfigFrequency=30
ProxyDataFrequency=30

[root@centos7 src]# cat install_zabbix_server.sh 
#!/bin/bash
#
#********************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-06-08
#FileName:      install_zabbix_server.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_zabbix_server for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#********************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

URL=https://cdn.zabbix.com/zabbix/sources/stable/
VERSION=4.0
ZABBIX_FILE=zabbix-4.0.30.tar.gz
ZABBIX_SERVER_CONF=zabbix_server.conf
FONT=simkai.ttf
ZABBIX_INSTALL_DIR=/apps/zabbix_server

PASSWORD=123456

#数据库IP地址,根据实际情况修改
DBHOST=10.0.0.17

DBNAME=zabbix_server
ZABBIX_SERVER=`ip addr| awk -F" +|/" '/global/{print $3}'`

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
    OS_RELEASE_VERSION=`lsb_release -rs |awk -F'.' '{print $1}'`
}

check_file(){
    cd ${SRC_DIR}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${ZABBIX_FILE} ];then
        ${COLOR}"缺少${ZABBIX_FILE}文件"${END}
        ${COLOR}'开始下载ZABBIX源码包'${END}
        wget ${URL}${VERSION}/${ZABBIX_FILE} || { ${COLOR}"Zabbix 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"${ZABBIX_FILE}文件已准备好"${END}
    fi
    if [ ! -e ${ZABBIX_SERVER_CONF} ];then
        ${COLOR}"缺少${ZABBIX_SERVER_CONF}文件"${END}
        exit
    elif [ ! -e ${FONT} ];then
        ${COLOR}"缺少${FONT}文件"${END}
        exit
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
}

install_zabbix_server(){
    [ -d ${ZABBIX_INSTALL_DIR} ] && { ${COLOR}"Zabbix server已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装Zabbix server"${END}
    id zabbix  &> /dev/null || { useradd -s /sbin/nologin -r  zabbix; $COLOR"创建zabbix用户"$END; }

    ${COLOR}"开始安装Zabbix server依赖包"${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install java-1.8.0-openjdk-devel httpd php php-mysqli php-common php-gd php-mbstring libtomcrypt-devel php-devel php-xml php-bcmath gcc gcc-c++  net-snmp-devel libssh2 libssh2-devel curl-devel unixODBC-devel mysql-devel libxml2-devel libcurl-devel libevent-devel &> /dev/null
    elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then
        yum -y install java-1.8.0-openjdk-devel httpd php php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml php-bcmath gcc gcc-c++  net-snmp-devel libssh2 libssh2-devel curl-devel unixODBC-devel mysql-devel libxml2-devel libcurl-devel libevent-devel &> /dev/null
    else
        apt update &> /dev/null;apt -y install make apache2 apache2-bin apache2-data apache2-utils fontconfig-config fonts-dejavu-core fping libapache2-mod-php libapache2-mod-php7.2 libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libfontconfig1 libgd3 libiksemel3 libjbig0 libjpeg-turbo8 libjpeg8 liblua5.2-0 libodbc1 libopenipmi0 libsensors4 libsnmp-base libsnmp30 libsodium23 libssh2-1 libtiff5 libwebp6 libxpm4 php-bcmath php-common php-gd php-ldap php-mbstring php-mysql php-xml php7.2-bcmath php7.2-cli php7.2-common php7.2-gd php7.2-json php7.2-ldap php7.2-mbstring php7.2-mysql php7.2-opcache php7.2-readline php7.2-xml snmpd ssl-cert ttf-dejavu-core libmysqlclient-dev libxml2-dev libxml2 snmp libsnmp-dev libevent-dev openjdk-8-jdk curl libcurl4-openssl-dev &> /dev/null
	fi
    tar xf ${ZABBIX_FILE}
    ZABBIX_DIR=`echo ${ZABBIX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${ZABBIX_DIR}
    ./configure --prefix=${ZABBIX_INSTALL_DIR} --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2 --enable-java
    make install
    [ $? -eq 0 ] && $COLOR"Zabbix编译安装成功"$END ||  { $COLOR"Zabbix编译安装失败,退出!"$END;exit; }
    echo "PATH=${ZABBIX_INSTALL_DIR}/sbin:${ZABBIX_INSTALL_DIR}/bin:${PATH}" > /etc/profile.d/zabbix.sh

    ${COLOR}"开始导入数据库"${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install mysql &> /dev/null
    elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then	
        yum -y install mariadb &> /dev/null
    else
        apt -y install mysql-client &> /dev/null
	fi
    cd ${SRC_DIR}/${ZABBIX_DIR}/database/mysql/
    mysql -uzabbix -p${PASSWORD} -h${DBHOST} ${DBNAME} < schema.sql &> /dev/null
    mysql -uzabbix -p${PASSWORD} -h${DBHOST} ${DBNAME} < images.sql &> /dev/null
    mysql -uzabbix -p${PASSWORD} -h${DBHOST} ${DBNAME} < data.sql &> /dev/null

    cd ${SRC_DIR}
    mv ${ZABBIX_SERVER_CONF} ${ZABBIX_INSTALL_DIR}/etc/
    sed -i -e 's/# DBHost=.*/DBHost='${DBHOST}'/' -e 's/^DBName=.*/DBName='${DBNAME}'/' -e 's/# DBPassword=/DBPassword='${PASSWORD}'/' -e 's@# AlertScriptsPath=.*@AlertScriptsPath='${ZABBIX_INSTALL_DIR}'/share/zabbix/alertscripts@' -e 's@# ExternalScripts=.*@ExternalScripts='${ZABBIX_INSTALL_DIR}'/share/zabbix/externalscripts@' ${ZABBIX_INSTALL_DIR}/etc/zabbix_server.conf
    chown -R zabbix.zabbix  ${ZABBIX_INSTALL_DIR}

    mkdir -p /var/www/html/zabbix
    cd ${SRC_DIR}/${ZABBIX_DIR}/frontends/php/
    cp -a . /var/www/html/zabbix/

    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        sed -i -e 's@post_max_size = .*@post_max_size = 16M@' -e 's@max_execution_time = .*@max_execution_time = 300@' -e 's@max_input_time = .*@max_input_time = 300@' -e 's@;date.timezone =@date.timezone = Asia/Shanghai@' /etc/php.ini
        systemctl restart httpd;systemctl enable httpd &> /dev/null
    else
        ${COLOR}"开始安装Zabbix WEB依赖包"${END}
        apt -y install php-gettext php-xml php-net-socket php-gd php-mysql &> /dev/null
        sed -i -e 's@post_max_size = .*@post_max_size = 16M@' -e 's@max_execution_time = .*@max_execution_time = 300@' -e 's@max_input_time = .*@max_input_time = 300@' -e 's@;date.timezone =@date.timezone = Asia/Shanghai@' /etc/php/7.2/apache2/php.ini
        systemctl restart apache2;systemctl enable apache2 &> /dev/null
    fi
	
    cat > /var/www/html/zabbix/conf/zabbix.conf.php <<-EOF
<?php
// Zabbix GUI configuration file.
global \$DB;

\$DB['TYPE']     = 'MYSQL';
\$DB['SERVER']   = '${DBHOST}';
\$DB['PORT']     = '3306';
\$DB['DATABASE'] = '${DBNAME}';
\$DB['USER']     = 'zabbix';
\$DB['PASSWORD'] = '${PASSWORD}';

// Schema name. Used for IBM DB2 and PostgreSQL.
\$DB['SCHEMA'] = '';

\$ZBX_SERVER      = '${ZABBIX_SERVER}';
\$ZBX_SERVER_PORT = '10051';
\$ZBX_SERVER_NAME = 'zabbix-server';

\$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
EOF

    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        TIMEOUTSEC=0
        ENVIRONMENTFILE_DIR=sysconfig
    else
        TIMEOUTSEC=infinity
        ENVIRONMENTFILE_DIR=default
    fi
    cat > /lib/systemd/system/zabbix-server.service <<-EOF
[Unit]
Description=Zabbix Server
After=syslog.target
After=network.target

[Service]
Environment="CONFFILE=${ZABBIX_INSTALL_DIR}/etc/zabbix_server.conf"
EnvironmentFile=-/etc/${ENVIRONMENTFILE_DIR}/zabbix-server
Type=forking
Restart=on-failure
PIDFile=/tmp/zabbix_server.pid
KillMode=control-group
ExecStart=${ZABBIX_INSTALL_DIR}/sbin/zabbix_server -c \$CONFFILE
ExecStop=/bin/kill -SIGTERM \$MAINPID
RestartSec=10s
TimeoutSec=${TIMEOUTSEC}

[Install]
WantedBy=multi-user.target
EOF

    cat > /lib/systemd/system/zabbix-agent.service <<-EOF
[Unit]
Description=Zabbix Agent
After=syslog.target
After=network.target

[Service]
Environment="CONFFILE=${ZABBIX_INSTALL_DIR}/etc/zabbix_agentd.conf"
EnvironmentFile=-/etc/${ENVIRONMENTFILE_DIR}/zabbix-agent
Type=forking
Restart=on-failure
PIDFile=/tmp/zabbix_agentd.pid
KillMode=control-group
ExecStart=${ZABBIX_INSTALL_DIR}/sbin/zabbix_agentd -c \$CONFFILE
ExecStop=/bin/kill -SIGTERM \$MAINPID
RestartSec=10s
TimeoutSec=${TIMEOUTSEC}
User=zabbix
Group=zabbix

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable --now zabbix-server zabbix-agent &> /dev/null
    systemctl is-active zabbix-server &> /dev/null ||  { ${COLOR}"Zabbix-server 启动失败,退出!"${END} ; exit; }
    systemctl is-active zabbix-agent &> /dev/null ||  { ${COLOR}"Zabbix-agent 启动失败,退出!"${END} ; exit; }
    ${COLOR}"Zabbix server安装完成"${END}
}

cn_zabbix(){
    ${COLOR}"开始安装Zabbix server中文环境"${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install langpacks-zh_CN &> /dev/null
        echo 'LANG="zh_CN.UTF-8""' >> /etc/locale.conf
        systemctl restart httpd
    fi
    if [ ${OS_ID} == "Ubuntu" ] &> /dev/null;then
        apt-get install language-pack-zh*
        echo 'LANG="zh_CN.UTF-8"' >> /etc/environment
        dpkg-reconfigure locales
        systemctl restart apache2
    fi

    cd ${SRC_DIR}
    mv simkai.ttf /var/www/html/zabbix/assets/fonts
    chown zabbix.zabbix /var/www/html/zabbix/assets/fonts -R
    sed -i.bak -e "72cdefine('ZBX_GRAPH_FONT_NAME',       'simkai');" -e "113cdefine('ZBX_FONT_NAME', 'simkai');" /var/www/html/zabbix/include/defines.inc.php 
    ${COLOR}"Zabbix server中文环境安装完成"${END}
}

main(){
    os
    check_file
    install_zabbix_server
    cn_zabbix
}

main

注意:ubuntu安装要手动选择语言


默认用户名:Admin 密码:zabbix

选择用户,更改语言为中文

3.安装被动模式zabbix agent

[root@centos7-3 src]# grep "^[^#]" zabbix_agentd_passive.conf 
LogFile=/tmp/zabbix_agentd.log
LogFileSize=0
DebugLevel=3
Server=127.0.0.1
ListenPort=10050
ListenIP=0.0.0.0
StartAgents=5
ServerActive=127.0.0.1
Hostname=Zabbix server

[root@centos7-3 src]# cat install_zabbix_agent_passive.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-05-29
#FileName:      install_zabbix_agent_passive.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_zabbix_agent_passive for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

URL=https://cdn.zabbix.com/zabbix/sources/stable/
VERSION=4.0
ZABBIX_FILE=zabbix-4.0.30.tar.gz
ZABBIX_AGENT_CONF=zabbix_agentd_passive.conf
ZABBIX_INSTALL_DIR=/apps/zabbix_agent

#zabbix server ip地址根据实际情况修改
SERVER=10.0.0.7

HOSTNAME=`ip addr| awk -F" +|/" '/global/{print $3}'`

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
}

check_file(){
    cd ${SRC_DIR}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${ZABBIX_FILE} ];then
        ${COLOR}"缺少${ZABBIX_FILE}文件"${END}
        ${COLOR}'开始下载ZABBIX源码包'${END}
        wget ${URL}${VERSION}/${ZABBIX_FILE} || { ${COLOR}"Zabbix 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"${ZABBIX_FILE}文件已准备好"${END}
    fi
    if [ ! -e ${ZABBIX_AGENT_CONF} ];then
        ${COLOR}"缺少${ZABBIX_AGENT_CONF}文件"${END}
        exit
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
}

install_zabbix_agent(){
    [ -d ${ZABBIX_INSTALL_DIR} ] && { ${COLOR}"Zabbix agent已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装Zabbix agent"${END}
    id zabbix  &> /dev/null || { useradd -s /sbin/nologin -r  zabbix; $COLOR"创建zabbix用户"$END; }
    
    ${COLOR}"开始安装Zabbix agent依赖包"${END}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc gcc-c++ glibc glibc-devel pcre pcre-devel zlib-devellibevent libevent-devel &> /dev/null
    else
        apt update &> /dev/null;apt -y install libssl-dev libpcre3 libpcre3-dev zlib1g-dev gcc make &> /dev/null
    fi
    tar xf ${ZABBIX_FILE}
    ZABBIX_DIR=`echo ${ZABBIX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${ZABBIX_DIR}
    ./configure --prefix=${ZABBIX_INSTALL_DIR} --enable-agent
    make install
    [ $? -eq 0 ] && $COLOR"Zabbix编译安装成功"$END ||  { $COLOR"Zabbix编译安装失败,退出!"$END;exit; }
    echo "PATH=${ZABBIX_INSTALL_DIR}/sbin:${ZABBIX_INSTALL_DIR}/bin:${PATH}" > /etc/profile.d/zabbix.sh

    cd ${SRC_DIR}
    mv ${ZABBIX_AGENT_CONF} ${ZABBIX_INSTALL_DIR}/etc/zabbix_proxy.conf
    sed -i.bak -e "s/^Server=.*/Server=${SERVER}/" -e "s/^Hostname=.*/Hostname=${HOSTNAME}/" ${ZABBIX_INSTALL_DIR}/etc/zabbix_agentd.conf
    chown -R zabbix.zabbix  ${ZABBIX_INSTALL_DIR}

     cat > /lib/systemd/system/zabbix-agent.service <<-EOF
[Unit]                                                                                                                 
Description=Zabbix Agent
Documentation=man:zabbix_agentd
After=network.target

[Service]
Type=simple
User=zabbix
Group=zabbix
ExecStart=${ZABBIX_INSTALL_DIR}/sbin/zabbix_agentd --foreground
Restart=on-abnormal

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable --now zabbix-agent &> /dev/null
    systemctl is-active zabbix-agent &> /dev/null ||  { ${COLOR}"Zabbix-agent 启动失败,退出!"${END} ; exit; }
    ${COLOR}"Zabbix agent安装完成"${END}
}

main(){
    os
    check_file
    install_zabbix_agent
}

main

添加监控主机

添加模板

4.添加tomcat监控和安装java_gateway

#安装java环境和tomcat
[root@centos7-3 src]# cat install_java_tomcat.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-06-04
#FileName:      install_jdk_tomcat.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_jdk_tomcat for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

JAVA_FILE=jdk-8u291-linux-x64.tar.gz
JAVA_DIR=jdk1.8.0_291

TOMCAT_FILE=apache-tomcat-9.0.46.tar.gz
TOMCAT_HOSTNAME=`ip addr| awk -F" +|/" '/global/{print $3}'`

check_file(){
    cd ${SRC_DIR}
    if [ ! -e ${JAVA_FILE} ];then
        ${COLOR}"缺少${JAVA_FILE}文件"${END}
        exit
    elif [ ! -e ${TOMCAT_FILE} ];then
        ${COLOR}"TOMCAT_FILE}文件"${END}
        exit
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
}

install_java(){
    ${COLOR}"开始安装JAVA"${END}
    cd ${SRC_DIR}
    tar xf ${JAVA_FILE}
    ln -sv /usr/local/src/${JAVA_DIR}/ /usr/local/jdk
    cat >> /etc/profile <<-EOF
export JAVA_HOME=/usr/local/jdk
export TOMCAT_HOME=/apps/tomcat
export PATH=\$JAVA_HOME/bin:\$JAVA_HOME/jre/bin:\$TOMCAT_HOME/bin:\$PATH
export CLASSPATH=.\$CLASSPATH:\$JAVA_HOME/lib:\$JAVA_HOME/jre/lib:\$JAVA_HOME/lib/tools.jar
EOF
    source /etc/profile
    ${COLOR}"JAVA安装完成"${END}
}

install_tomcat(){
    ${COLOR}"开始安装TOMCAT"${END}
    mkdir /apps
    mv ${TOMCAT_FILE} /apps/
    cd /apps/
    tar xf ${TOMCAT_FILE}
    TOMCAT_DIR=`echo ${TOMCAT_FILE} | sed -nr 's/^(.*[0-9]).*/\1/p'`
    sed -i '125cCATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=12345 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname='${TOMCAT_HOSTNAME}'"' /apps/${TOMCAT_DIR}/bin/catalina.sh
    /apps/${TOMCAT_DIR}/bin/catalina.sh start
    ${COLOR}"TOMCAT安装完成"${END}
}

main(){
    check_file
    install_java
    install_tomcat
}

main
#安装java_gateway
[root@centos7-4 src]# cat install_zabbix_java_gateway.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-06-04
#FileName:      install_zabbix_java_gateway.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_zabbix_java_gateway for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

URL=https://cdn.zabbix.com/zabbix/sources/stable/
VERSION=4.0
ZABBIX_FILE=zabbix-4.0.30.tar.gz
ZABBIX_INSTALL_DIR=/apps/zabbix_java_gateway

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
}

check_file(){
    cd ${SRC_DIR}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${ZABBIX_FILE} ];then
        ${COLOR}"缺少${ZABBIX_FILE}文件"${END}
        ${COLOR}'开始下载ZABBIX源码包'${END}
        wget ${URL}${VERSION}/${ZABBIX_FILE} || { ${COLOR}"Zabbix 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"${ZABBIX_FILE}文件已准备好"${END}
    fi
}

install_zabbix_java_gateway(){
    [ -d ${ZABBIX_INSTALL_DIR} ] && { ${COLOR}"Zabbix java-gateway已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装Zabbix agent"${END}
    id zabbix  &> /dev/null || { useradd -s /sbin/nologin -r  zabbix; $COLOR"创建zabbix用户"$END; }
    ${COLOR}"开始安装Zabbix java-gateway依赖包"${END}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc libxml2-devel net-snmp net-snmp-devel curl curl-devel php phpbcmath php-mbstring mariadb-devel java-1.8.0-openjdk-devel &> /dev/null
    else
        apt -y install libmysqld-dev libmysqlclient-dev libxml2-dev libxml2 snmp libsnmp-dev libevent-dev  libcurl4-openssl-dev openjdk-8-jdk make gcc &> /dev/null
    fi
    cd ${SRC_DIR}
    tar xf ${ZABBIX_FILE}
    ZABBIX_DIR=`echo ${ZABBIX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${ZABBIX_DIR}
    ./configure --prefix=${ZABBIX_INSTALL_DIR} --enable-java --with-net-snmp --with-mysql --with-ssh2
    make install
    [ $? -eq 0 ] && $COLOR"Zabbix编译安装成功"$END ||  { $COLOR"Zabbix编译安装失败,退出!"$END;exit; }

    chown -R zabbix.zabbix  ${ZABBIX_INSTALL_DIR}
    
    cat > /lib/systemd/system/zabbix-java-gateway.service <<-EOF
[Unit]                                                                                                                  
Description=Zabbix Java Gateway
After=syslog.target
After=network.target

[Service]
Type=forking
KillMode=process
PIDFile=/tmp/zabbix_java.pid
ExecStart=${ZABBIX_INSTALL_DIR}/sbin/zabbix_java/startup.sh
SuccessExitStatus=143
User=zabbix
Group=zabbix

[Install]
WantedBy=multi-user.target
EOF
    systemctl daemon-reload
    systemctl enable --now zabbix-java-gateway &> /dev/null
    systemctl is-active zabbix-java-gateway &> /dev/null ||  { ${COLOR}"Zabbix-java-gateway 启动失败,退出!"${END} ; exit; }
    ${COLOR}"Zabbix java-gateway安装完成"${END}
}

main(){
    os
    check_file
    install_zabbix_java_gateway
}

main


[root@centos7 src]# vim /apps/zabbix_server/etc/zabbix_server.conf
JavaGateway=10.0.0.37
JavaGatewayPort=10052
StartJavaPollers=10
:wq

[root@centos7 src]# systemctl restart zabbix-server

添加jmx监控

导入tomcat模板

添加tomcat模板

5.安装主动模式zabbix agent

[root@centos7-5 src]# grep "^[^#]" zabbix_agentd_active.conf 
LogFileSize=0
DebugLevel=3
EnableRemoteCommands=1
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
Timeout=30
UnsafeUserParameters=1

[root@centos7-5 src]# cat install_zabbix_agent_active.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-05-29
#FileName:      install_zabbix_agent_active.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_zabbix_agent_active for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

URL=https://cdn.zabbix.com/zabbix/sources/stable/
VERSION=4.0
ZABBIX_FILE=zabbix-4.0.30.tar.gz
ZABBIX_AGENT_CONF=zabbix_agentd_active.conf
ZABBIX_INSTALL_DIR=/apps/zabbix_agent

#zabbix server ip地址根据实际情况修改
SERVER=10.0.0.7
SERVERACTIVE=10.0.0.7

HOSTNAME=`ip addr | awk -F" +|/" '/global/{print $3}'`

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
}

check_file(){
    cd ${SRC_DIR}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${ZABBIX_FILE} ];then
        ${COLOR}"缺少${ZABBIX_FILE}文件"${END}
        ${COLOR}'开始下载ZABBIX源码包'${END}
        wget ${URL}${VERSION}/${ZABBIX_FILE} || { ${COLOR}"Zabbix 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"${ZABBIX_FILE}文件已准备好"${END}
    fi
    if [ ! -e ${ZABBIX_AGENT_CONF} ];then
        ${COLOR}"缺少${ZABBIX_AGENT_CONF}文件"${END}
        exit
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
}

install_zabbix_agent(){
    [ -d ${ZABBIX_INSTALL_DIR} ] && { ${COLOR}"Zabbix agent已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装Zabbix agent"${END}
    id zabbix  &> /dev/null || { useradd -s /sbin/nologin -r  zabbix; $COLOR"创建zabbix用户"$END; }
    
    ${COLOR}"开始安装Zabbix agent依赖包"${END}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc gcc-c++ glibc glibc-devel pcre pcre-devel zlib-devellibevent libevent-devel &> /dev/null
    else
        apt update &> /dev/null;apt -y install libssl-dev libpcre3 libpcre3-dev zlib1g-dev gcc make &> /dev/null
    fi
    tar xf ${ZABBIX_FILE}
    ZABBIX_DIR=`echo ${ZABBIX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${ZABBIX_DIR}
    ./configure --prefix=${ZABBIX_INSTALL_DIR} --enable-agent
    make install
    [ $? -eq 0 ] && $COLOR"Zabbix编译安装成功"$END ||  { $COLOR"Zabbix编译安装失败,退出!"$END;exit; }
    echo "PATH=${ZABBIX_INSTALL_DIR}/sbin:${ZABBIX_INSTALL_DIR}/bin:${PATH}" > /etc/profile.d/zabbix.sh

    cd ${SRC_DIR}
    mv ${ZABBIX_AGENT_CONF} ${ZABBIX_INSTALL_DIR}/etc/zabbix_proxy.conf
    sed -i.bak -e "s/^Server=.*/Server=${SERVER}/" -e "s/^ServerActive=.*/ServerActive=${SERVERACTIVE}/" -e "s/^Hostname=.*/Hostname=${HOSTNAME}/" ${ZABBIX_INSTALL_DIR}/etc/zabbix_agentd.conf
    chown -R zabbix.zabbix  ${ZABBIX_INSTALL_DIR}

     cat > /lib/systemd/system/zabbix-agent.service <<-EOF
[Unit]                                                                                                                 
Description=Zabbix Agent
Documentation=man:zabbix_agentd
After=network.target

[Service]
Type=simple
User=zabbix
Group=zabbix
ExecStart=${ZABBIX_INSTALL_DIR}/sbin/zabbix_agentd --foreground
Restart=on-abnormal

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable --now zabbix-agent &> /dev/null
    systemctl is-active zabbix-agent &> /dev/null ||  { ${COLOR}"Zabbix-agent 启动失败,退出!"${END} ; exit; }
    ${COLOR}"Zabbix agent安装完成"${END}
}

main(){
    os
    check_file
    install_zabbix_agent
}

main

添加主动模式监控项

把监控类型改为主动模式

6.安装被动模式zabbix proxy

[root@centos7-6 src]# grep "^[^#]" zabbix_proxy_passive.conf 
ProxyMode=1
Server=127.0.0.1
ServerPort=10051
Hostname=Zabbix proxy 
LogFile=/tmp/zabbix_proxy.log
LogFileSize=0
DebugLevel=3
DBName=zabbix_proxy
DBUser=zabbix
DBPort=3306
ProxyLocalBuffer=360
ProxyOfflineBuffer=720
StartPollers=5
StartPollersUnreachable=1
StartTrappers=5
StartPingers=1
StartDiscoverers=1
StartHTTPPollers=1
SNMPTrapperFile=/tmp/zabbix_traps.tmp
ListenIP=0.0.0.0
HousekeepingFrequency=1
CacheSize=8M
StartDBSyncers=4
HistoryCacheSize=16M
HistoryIndexCacheSize=4M
Timeout=30
TrapperTimeout=30
UnreachablePeriod=45
UnavailableDelay=60
UnreachableDelay=15
LogSlowQueries=3000

[root@centos7-6 src]# cat install_zabbix_proxy_passive.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-05-28
#FileName:      install_zabbix_proxy_passive.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_zabbix_proxy_passive for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

URL=https://cdn.zabbix.com/zabbix/sources/stable/
VERSION=4.0
ZABBIX_FILE=zabbix-4.0.30.tar.gz
ZABBIX_PROXY_CONF=zabbix_proxy_passive.conf
ZABBIX_INSTALL_DIR=/apps/zabbix_proxy

DBUSER=proxy
PASSWORD=123456
#数据库ip地址根据实际情况修改
DBHOST=10.0.0.17

DBNAME=zabbix_proxy_passive

#zabbix server ip地址根据实际情况修改
SERVER=10.0.0.7

HOSTNAME=neteagle-proxy-passive

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
    OS_RELEASE_VERSION=`lsb_release -rs |awk -F'.' '{print $1}'`
}

check_file(){
    cd ${SRC_DIR}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${ZABBIX_FILE} ];then
        ${COLOR}"缺少${ZABBIX_FILE}文件"${END}
        ${COLOR}'开始下载ZABBIX源码包'${END}
        wget ${URL}${VERSION}/${ZABBIX_FILE} || { ${COLOR}"Zabbix 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"${ZABBIX_FILE}文件已准备好"${END}
    fi
    if [ ! -e ${ZABBIX_PROXY_CONF} ];then
        ${COLOR}"缺少${ZABBIX_PROXY_CONF}文件"${END}
        exit
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
}

install_zabbix_proxy(){
    [ -d ${ZABBIX_INSTALL_DIR} ] && { ${COLOR}"Zabbix proxy已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装Zabbix proxy"${END}
    id zabbix  &> /dev/null || { useradd -s /sbin/nologin -r  zabbix; $COLOR"创建zabbix用户"$END; }
    
    ${COLOR}"开始安装Zabbix proxy依赖包"${END}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc gcc-c++ mysql-devel libmysqlclient-dev libxml2-devel net-snmp-devel libevent-devel libcurl-devel libssh2 libssh2-devel java-1.8.0-openjdk-devel
    else
        apt update &> /dev/null;apt -y install libmysqld-dev libmysqlclient-dev libxml2-dev libxml2 snmp libsnmp-dev libevent-dev curl libcurl4-openssl-dev openjdk-8-jdk make &> /dev/null
    fi
    tar xf ${ZABBIX_FILE}
    ZABBIX_DIR=`echo ${ZABBIX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${ZABBIX_DIR}
    ./configure --prefix=${ZABBIX_INSTALL_DIR} --enable-proxy --enable-agent --enable-java --with-mysql --with-net-snmp --with-libcurl --with-libxml2
    make install
    [ $? -eq 0 ] && $COLOR"Zabbix编译安装成功"$END ||  { $COLOR"Zabbix编译安装失败,退出!"$END;exit; }
    echo "PATH=${ZABBIX_INSTALL_DIR}/sbin:${ZABBIX_INSTALL_DIR}/bin:${PATH}" > /etc/profile.d/zabbix.sh

    ${COLOR}"开始导入数据库"${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install mysql &> /dev/null
    elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then 
        yum -y install mariadb &> /dev/null
    else
        apt -y install mysql-client &> /dev/null
    fi
    cd ${SRC_DIR}/${ZABBIX_DIR}/database/mysql/
    mysql -u${DBUSER} -p${PASSWORD} -h${DBHOST} ${DBNAME} < schema.sql

    cd ${SRC_DIR}
    mv ${ZABBIX_PROXY_CONF} ${ZABBIX_INSTALL_DIR}/etc/zabbix_proxy.conf
    sed -i.bak -e 's/^Server=.*/Server='${SERVER}'/' -e 's/^Hostname=.*/Hostname='${HOSTNAME}'/' -e 's/^# DBHost=.*/DBHost='${DBHOST}'/' -e 's/^DBName=.*/DBName='${DBNAME}'/' -e 's/^DBUser=.*/DBUser='${DBUSER}'/' -e 's/# DBPassword=/DBPassword='${PASSWORD}'/' ${ZABBIX_INSTALL_DIR}/etc/zabbix_proxy.conf
    chown -R zabbix.zabbix  ${ZABBIX_INSTALL_DIR}

    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        TIMEOUTSEC=0
        ENVIRONMENTFILE_DIR=sysconfig
    else
        TIMEOUTSEC=infinity
        ENVIRONMENTFILE_DIR=default
    fi
    cat > /lib/systemd/system/zabbix-proxy.service <<-EOF
[Unit]
Description=Zabbix Proxy
After=syslog.target
After=network.target

[Service]
Environment="CONFFILE=${ZABBIX_INSTALL_DIR}/etc/zabbix_proxy.conf"
EnvironmentFile=-/etc/${ENVIRONMENTFILE_DIR}/zabbix-proxy
Type=forking
Restart=on-failure
PIDFile=/tmp/zabbix_proxy.pid
KillMode=control-group
ExecStart=${ZABBIX_INSTALL_DIR}/sbin/zabbix_proxy -c \$CONFFILE
ExecStop=/bin/kill -SIGTERM \$MAINPID
RestartSec=10s
TimeoutSec=${TIMEOUTSEC}
User=zabbix
Group=zabbix

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable --now zabbix-proxy &> /dev/null
    systemctl is-active zabbix-proxy &> /dev/null ||  { ${COLOR}"Zabbix-proxy 启动失败,退出!"${END} ; exit; }
    ${COLOR}"Zabbix proxy安装完成"${END}
}

main(){
    os
    check_file
    install_zabbix_proxy
}

main

7.安装被动模式zabbix agent使用proxy

[root@eentos7-7 src]# cat install_zabbix_agent_proxy_passive.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-05-29
#FileName:      install_zabbix_agent_proxy_passive.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_zabbix_agent_proxy_passive for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

URL=https://cdn.zabbix.com/zabbix/sources/stable/
VERSION=4.0
ZABBIX_FILE=zabbix-4.0.30.tar.gz
ZABBIX_AGENT_CONF=zabbix_agentd_passive.conf
ZABBIX_INSTALL_DIR=/apps/zabbix_agent

#zabbix server ip地址和agent ip地址根据实际情况修改
SERVER=10.0.0.7,10.0.0.57

HOSTNAME=`ip addr| awk -F" +|/" '/global/{print $3}'`

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
}

check_file(){
    cd ${SRC_DIR}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${ZABBIX_FILE} ];then
        ${COLOR}"缺少${ZABBIX_FILE}文件"${END}
        ${COLOR}'开始下载ZABBIX源码包'${END}
        wget ${URL}${VERSION}/${ZABBIX_FILE} || { ${COLOR}"Zabbix 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"${ZABBIX_FILE}文件已准备好"${END}
    fi
    if [ ! -e ${ZABBIX_AGENT_CONF} ];then
        ${COLOR}"缺少${ZABBIX_AGENT_CONF}文件"${END}
        exit
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
}

install_zabbix_agent(){
    [ -d ${ZABBIX_INSTALL_DIR} ] && { ${COLOR}"Zabbix agent已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装Zabbix agent"${END}
    id zabbix  &> /dev/null || { useradd -s /sbin/nologin -r  zabbix; $COLOR"创建zabbix用户"$END; }
    
    ${COLOR}"开始安装Zabbix agent依赖包"${END}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc gcc-c++ glibc glibc-devel pcre pcre-devel zlib-devellibevent libevent-devel &> /dev/null
    else
        apt update &> /dev/null;apt -y install libssl-dev libpcre3 libpcre3-dev zlib1g-dev gcc make &> /dev/null
    fi
    tar xf ${ZABBIX_FILE}
    ZABBIX_DIR=`echo ${ZABBIX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${ZABBIX_DIR}
    ./configure --prefix=${ZABBIX_INSTALL_DIR} --enable-agent
    make install
    [ $? -eq 0 ] && $COLOR"Zabbix编译安装成功"$END ||  { $COLOR"Zabbix编译安装失败,退出!"$END;exit; }
    echo "PATH=${ZABBIX_INSTALL_DIR}/sbin:${ZABBIX_INSTALL_DIR}/bin:${PATH}" > /etc/profile.d/zabbix.sh

    cd ${SRC_DIR}
    mv ${ZABBIX_AGENT_CONF} ${ZABBIX_INSTALL_DIR}/etc/zabbix_proxy.conf
    sed -i.bak -e "s/^Server=.*/Server=${SERVER}/" -e "s/^Hostname=.*/Hostname=${HOSTNAME}/" ${ZABBIX_INSTALL_DIR}/etc/zabbix_agentd.conf
    chown -R zabbix.zabbix  ${ZABBIX_INSTALL_DIR}

     cat > /lib/systemd/system/zabbix-agent.service <<-EOF
[Unit]                                                                                                                 
Description=Zabbix Agent
Documentation=man:zabbix_agentd
After=network.target

[Service]
Type=simple
User=zabbix
Group=zabbix
ExecStart=${ZABBIX_INSTALL_DIR}/sbin/zabbix_agentd --foreground
Restart=on-abnormal

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable --now zabbix-agent &> /dev/null
    systemctl is-active zabbix-agent &> /dev/null ||  { ${COLOR}"Zabbix-agent 启动失败,退出!"${END} ; exit; }
    ${COLOR}"Zabbix agent安装完成"${END}
}

main(){
    os
    check_file
    install_zabbix_agent
}

main

8.安装主动模式zabbix proxy

[root@centos7-8 src]# grep "^[^#]" zabbix_proxy_active.conf 
ProxyMode=0
Server=127.0.0.1
ServerPort=10051
Hostname=Zabbix proxy
LogFile=/tmp/zabbix_proxy.log
LogFileSize=0
DebugLevel=3
EnableRemoteCommands=1
DBName=zabbix_proxy
DBUser=zabbix
ProxyLocalBuffer=360
ProxyOfflineBuffer=720
HeartbeatFrequency=60
ConfigFrequency=60
DataSenderFrequency=30
StartPollers=5
StartPollersUnreachable=1
StartTrappers=5
StartPingers=1
StartDiscoverers=1
StartHTTPPollers=1
ListenIP=0.0.0.0
HousekeepingFrequency=1
CacheSize=8M
StartDBSyncers=4
HistoryCacheSize=16M
HistoryIndexCacheSize=4M
Timeout=30
TrapperTimeout=30
UnreachablePeriod=45
UnavailableDelay=60
UnreachableDelay=15
LogSlowQueries=3000

[root@centos7-8 src]# cat install_zabbix_proxy_active.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-05-28
#FileName:      install_zabbix_proxy_active.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_zabbix_proxy_active for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

URL=https://cdn.zabbix.com/zabbix/sources/stable/
VERSION=4.0
ZABBIX_FILE=zabbix-4.0.30.tar.gz
ZABBIX_PROXY_CONF=zabbix_proxy_active.conf
ZABBIX_INSTALL_DIR=/apps/zabbix_proxy

DBUSER=proxy
PASSWORD=123456

#数据库IP地址根据实际情况修改
DBHOST=10.0.0.17

DBNAME=zabbix_proxy_active

#zabbix server ip地址根据实际情况修改
SERVER=10.0.0.7

HOSTNAME=neteagle-proxy-active

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
    OS_RELEASE_VERSION=`lsb_release -rs |awk -F'.' '{print $1}'`
}

check_file(){
    cd ${SRC_DIR}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${ZABBIX_FILE} ];then
        ${COLOR}"缺少${ZABBIX_FILE}文件"${END}
        ${COLOR}'开始下载ZABBIX源码包'${END}
        wget ${URL}${VERSION}/${ZABBIX_FILE} || { ${COLOR}"Zabbix 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"${ZABBIX_FILE}文件已准备好"${END}
    fi
    if [ ! -e ${ZABBIX_PROXY_CONF} ];then
        ${COLOR}"缺少${ZABBIX_PROXY_CONF}文件"${END}
        exit
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
}

install_zabbix_proxy(){
    [ -d ${ZABBIX_INSTALL_DIR} ] && { ${COLOR}"Zabbix proxy已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装Zabbix proxy"${END}
    id zabbix  &> /dev/null || { useradd -s /sbin/nologin -r  zabbix; $COLOR"创建zabbix用户"$END; }
    
    ${COLOR}"开始安装Zabbix proxy依赖包"${END}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc gcc-c++ mysql-devel libmysqlclient-dev libxml2-devel net-snmp-devel libevent-devel libcurl-devel libssh2 libssh2-devel java-1.8.0-openjdk-devel
    else
        apt update &> /dev/null;apt -y install libmysqld-dev libmysqlclient-dev libxml2-dev libxml2 snmp libsnmp-dev libevent-dev curl libcurl4-openssl-dev openjdk-8-jdk make &> /dev/null
    fi
    tar xf ${ZABBIX_FILE}
    ZABBIX_DIR=`echo ${ZABBIX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${ZABBIX_DIR}
    ./configure --prefix=${ZABBIX_INSTALL_DIR} --enable-proxy --enable-agent --enable-java --with-mysql --with-net-snmp --with-libcurl --with-libxml2
    make install
    [ $? -eq 0 ] && $COLOR"Zabbix编译安装成功"$END ||  { $COLOR"Zabbix编译安装失败,退出!"$END;exit; }
    echo "PATH=${ZABBIX_INSTALL_DIR}/sbin:${ZABBIX_INSTALL_DIR}/bin:${PATH}" > /etc/profile.d/zabbix.sh

    ${COLOR}"开始导入数据库"${END}
    if [[ ${OS_RELEASE_VERSION} == 8 ]] &> /dev/null;then
        yum -y install mysql &> /dev/null
    elif [[ ${OS_RELEASE_VERSION} == 7 ]] &> /dev/null;then 
        yum -y install mariadb &> /dev/null
    else
        apt -y install mysql-client &> /dev/null
    fi 
    cd ${SRC_DIR}/${ZABBIX_DIR}/database/mysql/
    mysql -u${DBUSER} -p${PASSWORD} -h${DBHOST} ${DBNAME} < schema.sql

    cd ${SRC_DIR}
    mv ${ZABBIX_PROXY_CONF} ${ZABBIX_INSTALL_DIR}/etc/zabbix_proxy.conf
    sed -i.bak -e "s/^Server=.*/Server=${SERVER}/" -e "s/^Hostname=.*/Hostname=${HOSTNAME}/" -e "s/^# DBHost=.*/DBHost=${DBHOST}/" -e "s/^DBName=.*/DBName=${DBNAME}/" -e "s/^DBUser=.*/DBUser=${DBUSER}/" -e "s/# DBPassword=/DBPassword=${PASSWORD}/" ${ZABBIX_INSTALL_DIR}/etc/zabbix_proxy.conf
    chown -R zabbix.zabbix  ${ZABBIX_INSTALL_DIR}

    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        TIMEOUTSEC=0
        ENVIRONMENTFILE_DIR=sysconfig
    else
        TIMEOUTSEC=infinity
        ENVIRONMENTFILE_DIR=default
    fi
    cat > /lib/systemd/system/zabbix-proxy.service <<-EOF
[Unit]
Description=Zabbix Proxy
After=syslog.target
After=network.target

[Service]
Environment="CONFFILE=${ZABBIX_INSTALL_DIR}/etc/zabbix_proxy.conf"
EnvironmentFile=-/etc/${ENVIRONMENTFILE_DIR}/zabbix-proxy
Type=forking
Restart=on-failure
PIDFile=/tmp/zabbix_proxy.pid
KillMode=control-group
ExecStart=${ZABBIX_INSTALL_DIR}/sbin/zabbix_proxy -c \$CONFFILE
ExecStop=/bin/kill -SIGTERM \$MAINPID
RestartSec=10s
TimeoutSec=${TIMEOUTSEC}
User=zabbix
Group=zabbix

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable --now zabbix-proxy &> /dev/null
    systemctl is-active zabbix-proxy &> /dev/null ||  { ${COLOR}"Zabbix-proxy 启动失败,退出!"${END} ; exit; }
    ${COLOR}"Zabbix proxy安装完成"${END}
}

main(){
    os
    check_file
    install_zabbix_proxy
}

main

8.安装主动模式zabbix agent使用proxy

[root@centos7-9 src]# cat install_zabbix_agent_active.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        zhanghui
#QQ:            19661891
#Date:          2021-05-29
#FileName:      install_zabbix_agent_proxy_active.sh
#URL:           www.cnblogs.com/neteagles
#Description:   install_zabbix_agent_proxy_active for centos 7/8 & ubuntu 18.04/20.04
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
SRC_DIR=/usr/local/src
COLOR="echo -e \\033[01;31m"
END='\033[0m'

URL=https://cdn.zabbix.com/zabbix/sources/stable/
VERSION=4.0
ZABBIX_FILE=zabbix-4.0.30.tar.gz
ZABBIX_AGENT_CONF=zabbix_agentd_active.conf
ZABBIX_INSTALL_DIR=/apps/zabbix_agent

#zabbix server ip地址和agent ip地址根据实际情况修改
SERVER=10.0.0.7,10.0.0.77

#agent ip地址根据实际情况修改
SERVERACTIVE=10.0.0.77

HOSTNAME=`ip addr| awk -F" +|/" '/global/{print $3}'`

os(){
    if grep -Eqi "CentOS" /etc/issue || grep -Eq "CentOS" /etc/*-release;then
        rpm -q redhat-lsb-core &> /dev/null || { ${COLOR}"安装lsb_release工具"${END};yum -y install  redhat-lsb-core &> /dev/null; }
    fi
    OS_ID=`lsb_release -is`
}

check_file(){
    cd ${SRC_DIR}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        rpm -q wget &> /dev/null || yum -y install wget &> /dev/null
    fi
    if [ ! -e ${ZABBIX_FILE} ];then
        ${COLOR}"缺少${ZABBIX_FILE}文件"${END}
        ${COLOR}'开始下载ZABBIX源码包'${END}
        wget ${URL}${VERSION}/${ZABBIX_FILE} || { ${COLOR}"Zabbix 源码下载失败"${END}; exit; } 
    else
        ${COLOR}"${ZABBIX_FILE}文件已准备好"${END}
    fi
    if [ ! -e ${ZABBIX_AGENT_CONF} ];then
        ${COLOR}"缺少${ZABBIX_AGENT_CONF}文件"${END}
        exit
    else
        ${COLOR}"相关文件已准备好"${END}
    fi
}

install_zabbix_agent(){
    [ -d ${ZABBIX_INSTALL_DIR} ] && { ${COLOR}"Zabbix agent已存在,安装失败"${END};exit; }
    ${COLOR}"开始安装Zabbix agent"${END}
    id zabbix  &> /dev/null || { useradd -s /sbin/nologin -r  zabbix; $COLOR"创建zabbix用户"$END; }
    
    ${COLOR}"开始安装Zabbix agent依赖包"${END}
    if [ ${OS_ID} == "CentOS" ] &> /dev/null;then
        yum -y install gcc gcc-c++ glibc glibc-devel pcre pcre-devel zlib-devellibevent libevent-devel
    else
        apt update &> /dev/null;apt -y install libssl-dev libpcre3 libpcre3-dev zlib1g-dev gcc make &> /dev/null
    fi
    tar xf ${ZABBIX_FILE}
    ZABBIX_DIR=`echo ${ZABBIX_FILE}| sed -nr 's/^(.*[0-9]).*/\1/p'`
    cd ${ZABBIX_DIR}
    ./configure --prefix=${ZABBIX_INSTALL_DIR} --enable-agent
    make install
    [ $? -eq 0 ] && $COLOR"Zabbix编译安装成功"$END ||  { $COLOR"Zabbix编译安装失败,退出!"$END;exit; }
    echo "PATH=${ZABBIX_INSTALL_DIR}/sbin:${ZABBIX_INSTALL_DIR}/bin:${PATH}" > /etc/profile.d/zabbix.sh

    cd ${SRC_DIR}
    mv ${ZABBIX_AGENT_CONF} ${ZABBIX_INSTALL_DIR}/etc/zabbix_proxy.conf
    sed -i.bak -e "s/^Server=.*/Server=${SERVER}/" -e "s/^ServerActive=.*/ServerActive=${SERVERACTIVE}/" -e "s/^Hostname=.*/Hostname=${HOSTNAME}/" ${ZABBIX_INSTALL_DIR}/etc/zabbix_agentd.conf
    chown -R zabbix.zabbix  ${ZABBIX_INSTALL_DIR}

     cat > /lib/systemd/system/zabbix-agent.service <<-EOF
[Unit]                                                                                                                 
Description=Zabbix Agent
Documentation=man:zabbix_agentd
After=network.target

[Service]
Type=simple
User=zabbix
Group=zabbix
ExecStart=${ZABBIX_INSTALL_DIR}/sbin/zabbix_agentd --foreground
Restart=on-abnormal

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload
    systemctl enable --now zabbix-agent &> /dev/null
    systemctl is-active zabbix-agent &> /dev/null ||  { ${COLOR}"Zabbix-agent 启动失败,退出!"${END} ; exit; }
    ${COLOR}"Zabbix agent安装完成"${END}
}

main(){
    os
    check_file
    install_zabbix_agent
}

main

posted @ 2021-05-17 20:31  网络之鹰  阅读(592)  评论(0编辑  收藏  举报