第二週 架構

美食家哇哈哈發表於2024-05-25

第二週

  1. 總結各系統版本的zabbix安裝。

    常用安裝:包安裝、二進位制安裝、原始碼編譯安裝、docker容器安裝

    如果需要中文建議現在linux安裝中文包:

    #CentOS安裝中文包,再修改語言,否則無法選擇
    [root@zabbix-server ~]#yum -y install langpacks-zh_CN
    #Ubuntu安裝下面中文包
    [root@zabbix-server ~]#apt -y install language-pack-zh-hans
    
    將中文字型上傳到 Zabbix Server 的目錄 /usr/share/zabbix/assets/fonts 下
    

    以包安裝為例:

    在Ubuntu20.04 安裝 Zabbix Server 6.0

    #Install Zabbix repository
    [root@ubuntu2004 ~]#wget
    https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-
    release_6.0-1+ubuntu20.04_all.deb
    [root@ubuntu2004 ~]#dpkg -i zabbix-release_6.0-1+ubuntu20.04_all.deb
    [root@ubuntu2004 ~]#cat /etc/apt/sources.list.d/zabbix.list
    deb https://repo.zabbix.com/zabbix/6.0/ubuntu focal main
    deb-src https://repo.zabbix.com/zabbix/6.0/ubuntu focal main
    #加速,更新為國內源(可選)
    [root@ubuntu2004 ~]#sed -i.bak's#https://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#'
    /etc/apt/sources.list.d/zabbix.list
    [root@ubuntu2004 ~]#cat /etc/apt/sources.list.d/zabbix.list
    deb https://mirrors.aliyun.com/zabbix/zabbix/6.0/ubuntu focal main
    deb-src https://mirrors.aliyun.com/zabbix/zabbix/6.0/ubuntu focal main
    [root@ubuntu2004 ~]#apt update
    #安裝Zabbix server,Web前端,agent2
    [root@ubuntu2004 ~]#apt -y install zabbix-server-mysql zabbix-frontend-php
    zabbix-nginx-conf zabbix-sql-scripts zabbix-agent2 zabbix-get
    #安裝nginx
    [root@ubuntu2004 ~]#nginx -v
    nginx version: nginx/1.18.0 (Ubuntu)
    #安裝php-fpm
    [root@ubuntu2004 ~]#php-fpm7.4 -v
    PHP 7.4.3 (fpm-fcgi) (built: May 6 2022 00:16:42)
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
    #安裝zabbix_agent2
    [root@ubuntu2004 ~]#zabbix_agent2 -V
    zabbix_agent2 (Zabbix) 6.0.5
    .......
    #建立初始資料庫
    [root@ubuntu2004 ~]#apt -y install mysql-server
    [root@ubuntu2004 ~]#mysql
    Server version: 8.0.29-0ubuntu0.20.04.3 (Ubuntu)
    mysql> create database zabbix character set utf8mb4 collate utf8mb4_bin;
    mysql> create user zabbix@localhost identified by '123456';
    mysql> grant all privileges on zabbix.* to zabbix@localhost;
    mysql> set global log_bin_trust_function_creators = 1;
    mysql> quit;
    #匯入初始架構和資料,系統將提示您輸入新建立的密碼。
    [root@ubuntu2004 ~]#zcat /usr/share/doc/zabbix-sql-scripts/mysql/server.sql.gz |
    mysql -uzabbix -p123456 zabbix
    #驗證資料庫和表
    [root@ubuntu2004 ~]#mysql -uzabbix -p123456 zabbix -e 'show tables;' |head
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Tables_in_zabbix
    acknowledges
    actions
    alerts
    auditlog
    autoreg_host
    conditions
    config
    config_autoreg_tls
    corr_condition
    [root@ubuntu2004 ~]#mysql -uroot -p
    password
    mysql> set global log_bin_trust_function_creators = 0;
    mysql> quit;
    #為Zabbix server配置資料庫
    [root@ubuntu2004 ~]#vim /etc/zabbix/zabbix_server.conf
    DBPassword=123456
    #確認資料庫配置
    [root@ubuntu2004 ~]#grep -E '^DB|DBHost=' /etc/zabbix/zabbix_server.conf
    # DBHost=localhost
    DBName=zabbix
    DBUser=zabbix
    DBPassword=123456
    #為Zabbix前端配置PHP
    [root@ubuntu2004 ~]#vim /etc/zabbix/nginx.conf
    #取消下面兩行的註釋並修改
    listen 80;
    server_name zabbix.wang.org;
    #預設安裝apache2和nginx衝突,禁用apache2
    [root@ubuntu2004 ~]#systemctl disable --now apache2.service
    #重啟服務
    [root@ubuntu2004 ~]#systemctl restart zabbix-server zabbix-agent2 nginx php7.4-
    fpm
    [root@ubuntu2004 ~]#systemctl enable zabbix-server zabbix-agent2 nginx php7.4-
    fpm
    [root@ubuntu2004 ~]#ss -ntl
    State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
    LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
    LISTEN 0 128 127.0.0.1:6010 0.0.0.0:*
    LISTEN 0 128 127.0.0.1:6011 0.0.0.0:*
    LISTEN 0 4096 0.0.0.0:10051 0.0.0.0:*
    條件檢測
    LISTEN 0 70 127.0.0.1:33060 0.0.0.0:*
    LISTEN 0 151 127.0.0.1:3306 0.0.0.0:*
    LISTEN 0 511 0.0.0.0:80 0.0.0.0:*
    LISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*
    LISTEN 0 128 [::]:22 [::]:*
    LISTEN 0 128 [::1]:6010 [::]:*
    LISTEN 0 128 [::1]:6011 [::]:*
    LISTEN 0 4096 *:10050 *:*
    LISTEN 0 4096 [::]:10051 [::]:*
    LISTEN 0 511 [::]:80 [::]:*
    #配置Zabbix前端
    #連線到新安裝的Zabbix前端: http://server_ip_or_name
    #根據Zabbix檔案裡步驟操作: Installing frontend
    

    rocky8下安裝zabbix6.0

    yum安裝zabbix
    
    安裝zabbix-server
    https://www.zabbix.com/cn/download?zabbix=6.0&os_distribution=rocky_linux&os_version=9&components=server_frontend_agent&db=mysql&ws=apache
    
    # rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/9/x86_64/zabbix-release-6.0-4.el9.noarch.rpm
    # dnf clean all
    
    # dnf install zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-sql-scripts zabbix-selinux-policy zabbix-agent
    
    安裝資料庫和初始化資料庫
    #yum install -y mysql-server
    [root@Rocky8 ~]# mysql
    mysql> create database zabbix character set utf8mb4 collate utf8mb4_bin;
    mysql> grant all privileges on zabbix.* to zabbix@localhost;
    mysql> set global log_bin_trust_function_creators = 1;
    
    #匯入初始架構和資料
    # zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p zabbix
    #驗證資料庫
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | sys                |
    | zabbix             |
    +--------------------+
    5 rows in set (0.00 sec)
    
    mysql> use zabbix;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A
    
    Database changed
    mysql> show tables;
    +----------------------------+
    | Tables_in_zabbix           |
    +----------------------------+
    | acknowledges               |
    | actions                    |
    | alerts                     |
    | auditlog                   |
    | autoreg_host               |
    | conditions                 |
    | config                     |
    | config_autoreg_tls         |
    | corr_condition             |
    | corr_condition_group       |
    | corr_condition_tag         |
    | corr_condition_tagpair     |
    | corr_condition_tagvalue    |
    | corr_operation             |
    | correlation                |
    | dashboard                  |
    | dashboard_page             |
    | dashboard_user             |
    | dashboard_usrgrp           |
    | dbversion                  |
    | dchecks                    |
    | dhosts                     |
    | drules                     |
    | dservices                  |
    | escalations                |
    | event_recovery             |
    | event_suppress             |
    | event_tag                  |
    | events                     |
    | expressions                |
    | functions                  |
    | globalmacro                |
    | globalvars                 |
    | graph_discovery            |
    | graph_theme                |
    | graphs                     |
    | graphs_items               |
    | group_discovery            |
    | group_prototype            |
    | ha_node                    |
    | history                    |
    | history_log                |
    | history_str                |
    | history_text               |
    | history_uint               |
    | host_discovery             |
    | host_inventory             |
    | host_tag                   |
    | hostmacro                  |
    | hosts                      |
    | hosts_groups               |
    | hosts_templates            |
    | housekeeper                |
    | hstgrp                     |
    | httpstep                   |
    | httpstep_field             |
    | httpstepitem               |
    | httptest                   |
    | httptest_field             |
    | httptest_tag               |
    | httptestitem               |
    | icon_map                   |
    | icon_mapping               |
    | ids                        |
    | images                     |
    | interface                  |
    | interface_discovery        |
    | interface_snmp             |
    | item_condition             |
    | item_discovery             |
    | item_parameter             |
    | item_preproc               |
    | item_rtdata                |
    | item_tag                   |
    | items                      |
    | lld_macro_path             |
    | lld_override               |
    | lld_override_condition     |
    | lld_override_opdiscover    |
    | lld_override_operation     |
    | lld_override_ophistory     |
    | lld_override_opinventory   |
    | lld_override_opperiod      |
    | lld_override_opseverity    |
    | lld_override_opstatus      |
    | lld_override_optag         |
    | lld_override_optemplate    |
    | lld_override_optrends      |
    | maintenance_tag            |
    | maintenances               |
    | maintenances_groups        |
    | maintenances_hosts         |
    | maintenances_windows       |
    | media                      |
    | media_type                 |
    | media_type_message         |
    | media_type_param           |
    | module                     |
    | opcommand                  |
    | opcommand_grp              |
    | opcommand_hst              |
    | opconditions               |
    | operations                 |
    | opgroup                    |
    | opinventory                |
    | opmessage                  |
    | opmessage_grp              |
    | opmessage_usr              |
    | optemplate                 |
    | problem                    |
    | problem_tag                |
    | profiles                   |
    | proxy_autoreg_host         |
    | proxy_dhistory             |
    | proxy_history              |
    | regexps                    |
    | report                     |
    | report_param               |
    | report_user                |
    | report_usrgrp              |
    | rights                     |
    | role                       |
    | role_rule                  |
    | script_param               |
    | scripts                    |
    | service_alarms             |
    | service_problem            |
    | service_problem_tag        |
    | service_status_rule        |
    | service_tag                |
    | services                   |
    | services_links             |
    | sessions                   |
    | sla                        |
    | sla_excluded_downtime      |
    | sla_schedule               |
    | sla_service_tag            |
    | sysmap_element_trigger     |
    | sysmap_element_url         |
    | sysmap_shape               |
    | sysmap_url                 |
    | sysmap_user                |
    | sysmap_usrgrp              |
    | sysmaps                    |
    | sysmaps_element_tag        |
    | sysmaps_elements           |
    | sysmaps_link_triggers      |
    | sysmaps_links              |
    | tag_filter                 |
    | task                       |
    | task_acknowledge           |
    | task_check_now             |
    | task_close_problem         |
    | task_data                  |
    | task_remote_command        |
    | task_remote_command_result |
    | task_result                |
    | timeperiods                |
    | token                      |
    | trends                     |
    | trends_uint                |
    | trigger_depends            |
    | trigger_discovery          |
    | trigger_queue              |
    | trigger_tag                |
    | triggers                   |
    | users                      |
    | users_groups               |
    | usrgrp                     |
    | valuemap                   |
    | valuemap_mapping           |
    | widget                     |
    | widget_field               |
    +----------------------------+
    173 rows in set (0.00 sec)
    
    mysql> 
    #為Zabbix server配置資料庫
    [root@Rocky8 ~]# cat /etc/zabbix/zabbix_server.conf
    DBPassword=123456
    #重啟Zabbix server和agent程序
    systemctl restart zabbix-server zabbix-agent httpd php-fpm
    
    #zabbix網頁配置就完成安裝zabbix-server了
    #注意問題:
    修改時區問題
    中文亂碼問題
    #zabbix-agent安裝
    # rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/9/x86_64/zabbix-release-6.0-4.el9.noarch.rpm
    # dnf clean all
    #dnf install zabbix-agent
    vi /etc/zabbix/zabbix_agentd.conf
    。。。。
    server 10.0.0.4 #指向當前zabbix-server
    ListenPort 10050 #監聽埠
    startAgents=3 #被動狀態時預設啟動的例項數
    Hostname=10.0.0.24 #區分大小寫在zabbix-server唯一的值
    
    重啟zabbix——agent服務
    [root@localhost src]# systemctl restart zabbix-agent.service
    [root@localhost src]# systemctl status zabbix-agent.service 
    
  2. 總結 zabbix主動模式和被動模式簡介及實現
    Zabbix 監控流程中 Agent 收集資料分為主動和被動兩種模式
    
    主動模式和被動模式都是相對 zabbix agent 而言的
    
    Zabbix Agent 預設是被動模式,需要修改配置才能支援主動模式
    Zabbix Agent 的關健配置有下面三項:
    Server=<Zabbix Server> #被動模式指向 Zabbix Server的IP或FQDN,預設模式,在主動
    模式此項可選的,建議配置,否則ZBX顯示紅色
    ServerActive=<Zabbix Server> #主動模式指向 Zabbix Server的IP或FQDN
    Hostname=<當前主機IP> #當前主機的IP,此項必須和Zabbix Server中設定的主機名稱相
    同,預設使用本機的主機名
    HostnameItem=system.hostname #也可以使用此system.hostname監控項自動生成主機名稱,默
    認就是使用本機的主機名
    
    
    被動模式:由Server建立TCP連結並向Agent端傳送請求。Agent需啟動監聽埠,等待Server連線。
    
    [root@centos8 ~]#ss -nta|grep 10050
    LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48786
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48790
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48866
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48884
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48860
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48848
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48846
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48826
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48774
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48808
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48836
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48824
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48772
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48840
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48820
    TIME-WAIT 0 0 10.0.0.103:10050 10.0.0.100:48886
    
    主動模式:由Agent主動建立TCP連結並向Server端傳送請求。因為是Agent主動發起的TCP連線,所以Agent所在機器本身無需再啟動監聽Agent埠。
    Zabbix Agent配置主動模式配置:
    [root@centos8 ~]#grep '^[^#]' /etc/zabbix/zabbix_agentd.conf
    PidFile=/var/run/zabbix/zabbix_agentd.pid
    LogFile=/var/log/zabbix/zabbix_agentd.log
    LogFileSize=0
    Server=10.0.0.100 #被動模式指向 Zabbix Server的IP或FQDN
    ServerActive=10.0.0.100 #主動模式指向 Zabbix Server的IP或FQDN
    Hostname=10.0.0.104 #當前主機的IP,此項必須和Zabbix Server中設定的主機名稱相
    同
    Timeout=30 #建議修改
    Include=/etc/zabbix/zabbix_agentd.d/*.conf
    [root@centos8 ~]#systemctl restart zabbix-agent.service
    [root@centos8 ~]#ss -ntl
    State Recv-Q Send-Q Local Address:Port Peer Address:Port
    LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
    LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
    LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
    LISTEN 0 128 [::]:22 [::]:*
    3.10.2.2.2 基於現有模板將包含的監控項修改為主動模式的監控項
    注意: Zabbix 5.0 內建了主動模式的模板 Template OS Linux by Zabbix agent active 無需修改
    Zabbix4.0 之前沒有主動模式的模板,需要自行修改
    以下操作為Zabbix 4.0上實現
    LISTEN 0 100 [::1]:25 [::]:*
    LISTEN 0 128 [::]:10050 [::]:*
    #Zabbix agent主動模式對應的程序數量只有1個,不能調整
    #Zabbix agent被動模式對應的程序數量預設3個,可以調整
    
  3. 總結 zabbix proxy主動及被動案例
    proxy主動模式案列
    yum安裝zabbix-proxy
    
    rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-4.el8.noarch.rpm
    dnf install zabbix-proxy-mysql zabbix-sql-scripts zabbix-selinux-policy
    
    在資料庫中建立zabbix_proxy_active庫
    mysql> create database zabbix_proxy_active character set utf8mb4 collate utf8mb4_bin;
    Query OK, 1 row affected (0.00 sec)
    
    mysql> create user zabbix@'10.0.0.%' identified by '123456';
    Query OK, 0 rows affected (0.01 sec)
    
    mysql> grant all privileges on zabbix_proxy_active.* to zabbix@'10.0.0.%';
    Query OK, 0 rows affected (0.00 sec)
    
    #修改zabbix_proxy_conf配置檔案
    [17:41:12 root@proxy ~]#grep '^[a-Z]' /etc/zabbix/zabbix_proxy.conf 
    ProxyMode=0
    Server=10.0.0.4
    Hostname=10.0.0.18-active
    ListenPort=10051
    LogFile=/var/log/zabbix/zabbix_proxy.log
    LogFileSize=0
    PidFile=/run/zabbix/zabbix_proxy.pid
    SocketDir=/run/zabbix
    DBHost=10.0.0.4
    DBName=zabbix_proxy_active
    DBUser=zabbix
    DBPassword=123456
    DBPort=3306
    ProxyLocalBuffer=720
    ProxyOfflineBuffer=720
    HeartbeatFrequency=60
    StartPollers=5
    SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
    Timeout=30
    LogSlowQueries=3000
    StatsAllowedIP=127.0.0.1
    #重啟服務
    [17:44:52 root@proxy ~]#systemctl restart zabbix-proxy.service 
    #檢視埠起來情況
    [17:44:52 root@proxy ~]#ss -ntl
    State    Recv-Q   Send-Q            Local Address:Port        Peer Address:Port   Process   
    LISTEN   0        128                     0.0.0.0:22               0.0.0.0:*                
    LISTEN   0        128                     0.0.0.0:10051            0.0.0.0:*                
    LISTEN   0        128                        [::]:22                  [::]:*                
    LISTEN   0        128                        [::]:10051               [::]:*                
    LISTEN   0        1            [::ffff:127.0.0.1]:8005                   *:*                
    LISTEN   0        100                           *:8080                   *:*           
    
  4. 總結自定義監控,監控win, nginx, 並基於簡訊/微信報警。
    安裝zabbix-agent的win版本客戶端,然後執行框輸入services.msc在服務中可以看到一個zabbix-agent服務在執行,然後再zabbix-web新增win主機,選擇適合win的監控模板。
    
    
    
    安裝nginx
    [root@ubuntu200406 ~]#apt install nginx -y
    #然後配置nginx狀態頁
    [root@ubuntu200406 ~]#vim /etc/nginx/sites-enabled/default
    location /basic_status {      #basic_status是宏定義路徑,Zabbix系統預設監控路徑
                stub_status;
            }
    #語法檢查沒有問題
    [root@ubuntu200406 ~]#nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    #重新載入nginx即可
    [root@ubuntu200406 ~]#nginx -s reload
    為nginx新增監控模板
    
  5. 完成1鍵安裝zabbix agent指令碼,可以用於ubuntu系統。
    #CentOS和Ubuntu 通用
     
    [root@ubuntu]#cat install_zabbix_agent_src.sh
    #!/bin/bash
     
    ZABBIX_VER=5.0.13
    FILE=zabbix-${ZABBIX_VER}.tar.gz
    INSTALL_DIR=/apps/zabbix_agent
    SERVER=10.0.0.100
    HOST=`hostname -I`
    . /etc/os-release
    color () {
        RES_COL=60
        MOVE_TO_COL="echo -en \\033[${RES_COL}G"
        SETCOLOR_SUCCESS="echo -en \\033[1;32m"
        SETCOLOR_FAILURE="echo -en \\033[1;31m"
        SETCOLOR_WARNING="echo -en \\033[1;33m"
        SETCOLOR_NORMAL="echo -en \E[0m"
        echo -n "$1" && $MOVE_TO_COL
        echo -n "["
        if [ $2 = "success" -o $2 = "0" ] ;then
            ${SETCOLOR_SUCCESS}
            echo -n $" OK "
        elif [ $2 = "failure" -o $2 = "1" ] ;then
            ${SETCOLOR_FAILURE}
            echo -n $"FAILED"
        else
            ${SETCOLOR_WARNING}
            echo -n $"WARNING"
        fi
        ${SETCOLOR_NORMAL}
        echo -n "]"
        echo
    }
    check(){
        wget https://cdn.zabbix.com/zabbix/sources/stable/${ZABBIX_VER%.*}/zabbix-$ZABBIX_VER
    .tar.gz
    [ -e ${FILE} ] || { color "$FILE 檔案不存在,請檢查" 1 ; exit 1; }
    }
     
    prepare (){
        if [ $ID = "centos" $ID = "rocky" ];then
            yum -y install gcc make pcre-devel || { color "zabbix agent 相關包安裝失
    敗,請檢查" 1 ;exit; }
        else
            apt -y install gcc make || { color "zabbix agent 相關包安裝失敗,請檢查" 1
    ;exit; }
        fi
        id zabbix &> /dev/null || useradd -r zabbix
    }
     
    install() {
        tar xf $FILE
        cd zabbix-$ZABBIX_VER
        ./configure --prefix=${INSTALL_DIR} --enable-agent
        make && make install
    }
     
    config () {
        sed -i.bak -e "s/Server=127.0.0.1/Server=$SERVER/" -e "s/Hostname=Zabbix
    server/Hostname=$HOST/" ${INSTALL_DIR}/etc/zabbix_agentd.conf
        echo 'PATH=${INSTALL_DIR}/bin/:${INSTALL_DIR}/sbin/:$PATH' >
    /etc/profile.d/zabbix_agent.sh
    }
     
    service (){
        cat > /lib/systemd/system/zabbix-agent.service <<EOF
    [Unit]
    Description=Zabbix Agent
    After=syslog.target
    After=network.target
    [Service]
    Type=forking
    Restart=on-failure
    PIDFile=/tmp/zabbix_agentd.pid
    KillMode=control-group
    ExecStart=${INSTALL_DIR}/sbin/zabbix_agentd -c
    ${INSTALL_DIR}/etc/zabbix_agentd.conf
    ExecStop=/bin/kill -SIGTERM $MAINPID
    RestartSec=10s
    User=zabbix
    Group=zabbix
    [Install]
    WantedBy=multi-user.target
    EOF
    systemctl daemon-reload
    systemctl enable --now zabbix-agent.service
    systemctl is-active zabbix-agent.service && color "Zabbix Agent 安裝完成並啟動"
    0
    }
    check
    prepare
    install
    config
    service
    
  6. 完成1鍵指令碼,可以基於zabbix api新增zabbix agent到zabbix web。
    [root@zabbix-server ~]#vim ./zabbix-api-addhost.sh
    #!/bin/bash
     
    ZABBIX_SERVER=10.0.0.100
    TOKEN=$(./zabbix-api-token.sh| awk -F'"' '{print $8}')
    NET=10.0.0
    
    for HOST in $NET.{200..210};do
    
    curl -s -XPOST -H "Content-Type: application/json-rpc" -d '
    {
    "jsonrpc": "2.0",
    "method": "host.create",
    "params": {
        "host": "'web-api-$HOST'", #建立主機名稱
        "name": "'web-api-$HOST'", #可見的名稱
        "interfaces": [
            {
            "type": 1, #型別為1表示agent,2是SNMP,3是IPMI,4是JMX
            "main": 1, #主要介面
            "useip": 1, #0是使用DNS,1是使用IP地址
            "ip": "'$HOST'", #新增的zabbix agent的IP地址
            "dns": "",
            "port": "10050" #agent使用的埠
            }
        ],
        "groups": [
            {
                "groupid": "2" #新增到的組的ID
            }
        ],
        "templates": [
            {
                "templateid": "10001" #關聯的模板的ID
            }
        ]
    },
    "id": 1,
    "auth": "'$TOKEN'"
    }' http://${ZABBIX_SERVER}/zabbix/api_jsonrpc.php | python3 -m json.tool
    
    

相關文章