2023-10-27 第十周

一、总结各系统版本的zabbix安装。

在官方文档中找到自己要下载的版本,下面有详细的安装文档:
https://www.zabbix.com/cn/download

下面是安装rocky和ubuntu的步骤

ubuntu20.04(LNMP)安装zabbix 6.0

ubuntu

# Install Zabbix repository   安装zabbix仓库
[root@ubuntu2004 ~]#wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-4+ubuntu20.04_all.deb
[root@ubuntu2004 ~]#dpkg -i zabbix-release_6.0-4+ubuntu20.04_all.deb
#把zabbix的仓库源换成清华的
[root@ubuntu2004 ~]#vim /etc/apt/sources.list.d/zabbix.list 
# Zabbix main repository
deb https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/ubuntu focal main
deb-src https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/ubuntu focal main
[root@ubuntu2004 ~]#apt update

#安装zabbit server,Web前端,agent2
[root@ubuntu2004 ~]#apt install zabbix-server-mysql zabbix-frontend-php zabbix-nginx-conf zabbix-sql-scripts zabbix-agent2 -y
[root@ubuntu2004 ~]#apt -y install mysql-server
#对数据库进行授权
[root@ubuntu2004 ~]#mysql
mysql> create database zabbix character set utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.02 sec)

mysql>  create user zabbix@localhost identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on zabbix.* to zabbix@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> set global log_bin_trust_function_creators = 1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

#导入初始架构和数据,系统将提示您输入新创建的密码。
[root@ubuntu2004 ~]#zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p123456 zabbix
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@ubuntu2004 ~]#

#Disable log_bin_trust_function_creators option after importing database schema.
[root@ubuntu2004 ~]#mysql -uroot
mysql> set global log_bin_trust_function_creators = 0;

#为Zabbix server配置数据库,编辑配置文件 /etc/zabbix/zabbix_server.conf
[root@ubuntu2004 ~]#vim /etc/zabbix/zabbix_server.conf
DBPassword=123456

#为Zabbix前端配置PHP,编辑配置文件 /etc/zabbix/nginx.conf uncomment and set 'listen' and 'server_name' directives.
[root@ubuntu2004 ~]#vim /etc/zabbix/nginx.conf
server {
        listen          8080;
        server_name     zabbix.ma.com;

# 启动Zabbix server和agent进程,启动Zabbix server和agent进程,并为它们设置开机自启:
[root@ubuntu2004 ~]#systemctl disable --now apache2
Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install disable apache2
Removed /etc/systemd/system/multi-user.target.wants/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

在这里插入图片描述
在这里插入图片描述

Rocky8 (LAMP) 安装zabbix6.0

在这里插入图片描述

#安装zabbix的安装包
[root@rocky ~]#rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-4.el8.noarch.rpm
[root@rocky ~]#vim /etc/yum.repos.d/zabbix.repo 
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/rhel/8/x86_64/
[zabbix-non-supported]
name=Zabbix Official Repository (non-supported) - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/non-supported/rhel/8/x86_64/
[root@rocky ~]## dnf clean all

#安装zabbix server,Web前端,agent2
[root@rocky ~]#dnf install zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-sql-scripts zabbix-selinux-policy zabbix-agent2 -y
#安装数据库
[root@rocky ~]#yum -y install mysql-server
[root@rocky ~]#systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service

#创建初始数据库
[root@rocky ~]#mysql
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database zabbix character set utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.02 sec)

mysql> create user zabbix@localhost identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on zabbix.* to zabbix@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> set global log_bin_trust_function_creators = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye

#导入初始架构和数据,系统将提示您输入新创建的密码。
[root@rocky ~]#cat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p zabbix
Enter password:
[root@rocky ~]#mysql
mysql> set global log_bin_trust_function_creators = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

#为Zabbix server配置数据库,编辑配置文件 /etc/zabbix/zabbix_server.conf
[root@rocky ~]#vim /etc/zabbix/zabbix_server.conf 
DBPassword=123456

#启动Zabbix server和agent进程,启动Zabbix server和agent进程,并为它们设置开机自启:
[root@rocky ~]#systemctl restart zabbix-server zabbix-agent2 httpd php-fpm
[root@rocky ~]#systemctl enable zabbix-server zabbix-agent2 httpd php-fpm
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-server.service → /usr/lib/systemd/system/zabbix-server.service.
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-agent2.service → /usr/lib/systemd/system/zabbix-agent2.service.
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.

在这里插入图片描述
在这里插入图片描述

二、总结 zabbix主动模式和被动模式简介及实现

Zabbix 监控流程中 Agent 收集数据分为主动和被动两种模式
主动模式和被动模式都是相对 zabbix agent而言的
Zabbix默认是被动模式,如果有100个监控项,被动模式需要 Zabbix Server找 Zabbix Agent要100次

主动模式是Zabbix Server给 Zabbix Agent发送一个包括100个监控项的任务清单,Zabbix Agent根据任务清单,采集好100个监控项的值,主动汇报给 Zabbix Server这100个监控项,Zabbix Agent主动模式只需要发送一次数据,大大提高了传输效率。
● 被动模式: Server 向 Agent 请求获取监控项的数据,Agent返回数据。此为默认模式,如果有100个监控项,则需要100次交互
● 主动模式: Agent 请求 Server 获取主动的监控项列表,并主动将监控项内需要检测的数据提交给server/proxy,如有100个监控项,只需要1次交互即可
注意: 两种模式可以在Zabbix Server上同时存在,可以将一部分监控项设为主动,其它设为被动模式

被动监测通信过程如下:

Server打开一个TCP连接
Server发送请求agent.ping
Agent接收到请求并且响应<HEADER><DATALEN>
Server处理接收到的数据
关闭TCP连接
被动模式每获取一个监控项都需要打开一个tcp连接,这样当监控项越来越多时,Zabbix Server会打开很多端口,就会出现server端性能瓶颈问题。

主动模式监测通信过程如下:

zabbix agent 首先向ServerActive配置的IP请求获取active items,获取并提交active items数据值给server或者proxy。
zabbix多久获取一次active items?
它会根据agent的配置文件中的RefreshActiveChecks的频率进行,如果获取失败,那么将会在60秒之后重试。

主动模式的流程分两个部分:

 ● 获取ACTIVE ITEMS列表
   Agent主动打开一个TCP连接(主动检测变成Agent打开)
   Agent请求items检测列表
   Server返回items列表
   Agent 处理响应
   关闭TCP连接
   Agent开始收集数据
   
 ● 主动检测提交数据过程
   Agent建立TCP连接
   Agent批量提交items列表收集的所有监控项数据
   Server处理数据,并返回响应状态
   关闭TCP连接

被动模式

zabbix server : 10.0.0.88
zabbix agent : 10.0.0.8
在这里插入图片描述

#在另外一台机器上安装zabbix agent
[root@rocky8 ~]#rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-4.el8.noarch.rpm
[root@rocky8 ~]#vim /etc/yum.repos.d/zabbix.repo 
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/rhel/8/x86_64/
[zabbix-non-supported]
name=Zabbix Official Repository (non-supported) - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/non-supported/rhel/8/x86_64/
[root@rocky8 ~]#dnf clean all
53 files removed

#安装agent2
[root@rocky8 ~]#dnf install zabbix-agent2 zabbix-agent2-plugin-*
#zabbix server的地址
[root@rocky8 ~]#vim /etc/zabbix/zabbix_agent2.conf 
Server=10.0.0.88

#启动agent
[root@rocky8 ~]#systemctl restart zabbix-agent2
[root@rocky8 ~]#systemctl enable zabbix-agent2
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-agent2.service → /usr/lib/systemd/system/zabbix-agent2.service.

默认就是被动,所以安装上agent,添加到server之后就自动使用了被动模式
添加主机
在这里插入图片描述

#被动模式下zabbix server端口打开情况
[root@rocky ~]#ps ax|grep poller
     41 ?        I<     0:00 [edac-poller]
  20755 ?        S      0:00 /usr/sbin/zabbix_server: http poller #1 [got 0 values in 0.000376 sec, idle 5 sec]
  20768 ?        S      0:00 /usr/sbin/zabbix_server: proxy poller #1 [exchanged data with 0 proxies in 0.000027 sec, idle 5 sec]
  20774 ?        S      0:00 /usr/sbin/zabbix_server: poller #1 [got 0 values in 0.000008 sec, idle 1 sec]
  20775 ?        S      0:00 /usr/sbin/zabbix_server: poller #2 [got 0 values in 0.000009 sec, idle 1 sec]
  20776 ?        S      0:00 /usr/sbin/zabbix_server: poller #3 [got 0 values in 0.000014 sec, idle 1 sec]
  20777 ?        S      0:00 /usr/sbin/zabbix_server: poller #4 [got 0 values in 0.000014 sec, idle 1 sec]
  20778 ?        S      0:00 /usr/sbin/zabbix_server: poller #5 [got 1 values in 0.000423 sec, idle 1 sec]
  20779 ?        S      0:00 /usr/sbin/zabbix_server: unreachable poller #1 [got 0 values in 0.000025 sec, idle 5 sec]
  20788 ?        S      0:00 /usr/sbin/zabbix_server: history poller #1 [got 2 values in 0.000086 sec, idle 1 sec]
  20789 ?        S      0:00 /usr/sbin/zabbix_server: history poller #2 [got 0 values in 0.000026 sec, idle 1 sec]
  20790 ?        S      0:00 /usr/sbin/zabbix_server: history poller #3 [got 0 values in 0.000024 sec, idle 1 sec]
  20792 ?        S      0:00 /usr/sbin/zabbix_server: history poller #4 [got 0 values in 0.000026 sec, idle 1 sec]
  20793 ?        S      0:00 /usr/sbin/zabbix_server: history poller #5 [got 0 values in 0.000030 sec, idle 1 sec]
  20796 ?        S      0:00 /usr/sbin/zabbix_server: odbc poller #1 [got 0 values in 0.000034 sec, idle 5 sec]
  22388 pts/2    S+     0:00 grep --color=auto poller

#agent连接情况
[root@rocky8 ~]#ss -nta|grep 10050
LISTEN    0      128                    *:10050                  *:*           
TIME-WAIT 0      0      [::ffff:10.0.0.8]:10050 [::ffff:10.0.0.88]:44214       
TIME-WAIT 0      0      [::ffff:10.0.0.8]:10050 [::ffff:10.0.0.88]:44222       
TIME-WAIT 0      0      [::ffff:10.0.0.8]:10050 [::ffff:10.0.0.88]:44230       
TIME-WAIT 0      0      [::ffff:10.0.0.8]:10050 [::ffff:10.0.0.88]:44226       
TIME-WAIT 0      0      [::ffff:10.0.0.8]:10050 [::ffff:10.0.0.88]:44248       
TIME-WAIT 0      0      [::ffff:10.0.0.8]:10050 [::ffff:10.0.0.88]:44208       
TIME-WAIT 0      0      [::ffff:10.0.0.8]:10050 [::ffff:10.0.0.88]:44204      

主动模式

zabbix server : 10.0.0.129
zabbix agent : 10.0.0.100
在这里插入图片描述

#安装agent包
[root@ubuntu2004 ~]#wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-4+ubuntu20.04_all.deb
[root@ubuntu2004 ~]#dpkg -i zabbix-release_6.0-4+ubuntu20.04_all.deb

#修改agent源
[root@ubuntu2004 ~]#vim /etc/apt/sources.list.d/zabbix.list 
deb https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/ubuntu/ focal main
deb-src https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/ubuntu/ focal main

[root@ubuntu2004 ~]#vim /etc/apt/sources.list.d/zabbix-agent2-plugins.list 
deb [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix-agent2-plugins/1/ubuntu/ focal main
deb-src [arch=amd64] https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix-agent2-plugins/1/ubuntu/ focal main

[root@ubuntu2004 ~]#apt update

#安装zabbix agent2
[root@ubuntu2004 ~]#apt install zabbix-agent2 zabbix-agent2-plugin-*

#指定zabbix server的地址
[root@ubuntu2004 ~]#vim /etc/zabbix/zabbix_agent2.conf 
Server=10.0.0.129

[root@ubuntu2004 ~]#systemctl restart zabbix-agent2
[root@ubuntu2004 ~]#systemctl enable zabbix-agent2
Synchronizing state of zabbix-agent2.service with SysV service script with /lib/systemd/systemd-sysv-install.

在这里插入图片描述
在这里插入图片描述

这里是默认的被动模式,下面需要修改zabbix agent来实现主动模式

[root@ubuntu2004 ~]#vim /etc/zabbix/zabbix_agent2.conf 
Server=10.0.0.129                  #被动模式指向 Zabbix Server的IP或FQDN
ServerActive=10.0.0.129            #主动模式指向 Zabbix Server的IP或FQDN
Hostname=10.0.0.100         #这个地方的名字,一定要跟上面在zabbix server的web界面中,添加主机时的主机名称完全相同

[root@ubuntu2004 ~]#systemctl restart zabbix-agent2.service

添加主动模式的模板
在这里插入图片描述
在这里插入图片描述

三、总结 zabbix proxy主动及被动案例

Zabbix Proxy 也分主动模式和被动模式,通信方式与zabbix server主动模式和被动模式一样
Zabbix Proxy的模式 是从Zabbix Proxy 角度来说的
zabbix proxy在主动模式下要主动地向zabbix server周期性的申请获取zabbix agent的监控项信息,此模式可以大副降低Zabbix Server的压力,生产推荐使用
abbix Proxy在被动模式下要被动地等待zabbix server的连接,并接受zabbix server发送的监控项指令,然后再由zabbix proxy向zabbix agent发起请求获取数据。
注意: Zabbix Proxy的大版本必须要和zabbix server版本相同,否则很可能会导致出现 zabbi server与zabbix proxy不兼容问题

实验架构
在这里插入图片描述

两个agent和一个server在上面已经安装过了,这里不做赘述,下面只演示,proxy的安装和配置。

Rocky安装proxy
在这里插入图片描述

#安装zabbix proxy的yum源
[root@zabbix-proxy-active ~]#rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-4.el8.noarch.rpm
[root@zabbix-proxy-active ~]#dnf clean all
64 files removed

[root@zabbix-proxy-active ~]#vim /etc/yum.repos.d/zabbix.repo 
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/rhel/8/x86_64/

[zabbix-non-supported]
name=Zabbix Official Repository (non-supported) - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/zabbix/non-supported/rhel/8/x86_64/

#安装zabbix proxy
[root@zabbix-proxy-active ~]# dnf install zabbix-proxy-mysql zabbix-sql-scripts zabbix-selinux-policy zabbix-agent2

#安装数据库
[root@zabbix-proxy-active ~]#yum -y install mysql-server

#创建mysql数据库
[root@zabbix-proxy-active ~]#systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@zabbix-proxy-active ~]#mysql
mysql> create database zabbix_proxy character set utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.00 sec)

mysql> create user zabbix@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on zabbix_proxy.* to zabbix@localhost;
Query OK, 0 rows affected (0.00 sec)

mysql> set global log_bin_trust_function_creators = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye

#导入初始架构和数据,系统将提示输入新创建的密码
[root@zabbix-proxy-active ~]#cat /usr/share/zabbix-sql-scripts/mysql/proxy.sql | mysql --default-character-set=utf8mb4 -uzabbix -p zabbix_proxy
Enter password: 

#导入数据库架构后禁用log_bin_trust_function_creators选项。
[root@zabbix-proxy-active ~]#mysql
mysql> set global log_bin_trust_function_creators = 0;
Query OK, 0 rows affected (0.00 sec)

#编辑配置文件 /etc/zabbix/zabbix_proxy.conf
[root@zabbix-proxy-active ~]#vim /etc/zabbix/zabbix_proxy.conf
DBPassword=123456

#开启zabbix proxy
[root@zabbix-proxy-active ~]#systemctl enable --now zabbix-proxy.service 
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-proxy.service → /usr/lib/systemd/system/zabbix-proxy.service.

Ubuntu安装proxy
在这里插入图片描述

#下载zabbix proxy源
[root@zabbix-proxy-passive ~]#wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-4+ubuntu20.04_all.deb
[root@zabbix-proxy-passive ~]#dpkg -i zabbix-release_6.0-4+ubuntu20.04_all.deb

#修改为清华源
[root@zabbix-proxy-passive ~]#vim /etc/apt/sources.list.d/zabbix.list 
deb https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/ubuntu/ focal main
deb-src https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/6.0/ubuntu/ focal main
[root@zabbix-proxy-passive ~]#apt update

#安装zabbix proxy
[root@zabbix-proxy-passive ~]#apt install zabbix-proxy-mysql zabbix-sql-scripts zabbix-agent2

#安装数据库
[root@zabbix-proxy-passive ~]#apt -y install mysql-server

[root@zabbix-proxy-passive ~]#mysql
mysql> create database zabbix_proxy character set utf8mb4 collate utf8mb4_bin;
Query OK, 1 row affected (0.00 sec)

mysql> create user zabbix@localhost identified by '123456';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on zabbix_proxy.* to zabbix@localhost;
Query OK, 0 rows affected (0.01 sec)

mysql> set global log_bin_trust_function_creators = 1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit;
Bye

[root@zabbix-proxy-passive ~]#cat /usr/share/zabbix-sql-scripts/mysql/proxy.sql | mysql --default-character-set=utf8mb4 -uzabbix -p zabbix_proxy
Enter password:

[root@zabbix-proxy-passive ~]#mysql
mysql> set global log_bin_trust_function_creators = 0;

[root@zabbix-proxy-passive ~]#vim /etc/zabbix/zabbix_proxy.conf
DBPassword=password

[root@zabbix-proxy-passive ~]#systemctl restart zabbix-proxy.service 
[root@zabbix-proxy-passive ~]#systemctl enable zabbix-proxy.service 
Synchronizing state of zabbix-proxy.service with SysV service script with /lib/systemd/systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable zabbix-proxy
Created symlink /etc/systemd/system/multi-user.target.wants/zabbix-proxy.service → /lib/systemd/system/zabbix-proxy.service.
#修改mysql8.0之后的密码登录策略,在18和129上都要执行

#rocky18的路径
[root@zabbix-proxy-active ~]#echo 'default-authentication-plugin=mysql_native_password' >> /etc/my.cnf.d/mysql-server.cnf
[root@zabbix-proxy-active ~]#systemctl restart mysqld.service

#ubuntu129的路径
[root@zabbix-proxy-passive ~]#echo 'default-authentication-plugin=mysql_native_password' >> vim /etc/mysql/mysql.conf.d/mysqld.cnf
[root@zabbix-proxy-passive ~]#systemctl restart mysql.service 

1、主动模式的配置

[root@zabbix-proxy-active ~]#vim /etc/zabbix/zabbix_proxy.conf
13 ProxyMode=0                              #主动模式为0,被动模式为1,默认为0即主动模式
32 Server=10.0.0.88                         #指向Zabbix Server
42 Hostname=Zabbix-proxy-active             #此名称必须和后面Web管理页的agent代理程序名称相同
157 DBHost=localhost                        #MySQL服务器地址
171 DBName=zabbix_proxy                     #MySQL 数据库名
186 DBUser=zabbix                           #连接MySQL的用户
194 DBPassword=123456                       #连接MySQL的用户密码

[root@zabbix-proxy-active ~]#systemctl restart zabbix-proxy.service

1、在 88 server端的web界面上添加proxy

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、修改Zabbix Agent (Rocky 8)配置文件为主动模式的 Zabbix Proxy

[root@zabbix-agent1 ~]#vim /etc/zabbix/zabbix_agent2.conf
80 Server=10.0.0.88,10.0.0.18               #指向Zabbix Server(可选)和Zabbix Proxy(必选)
133 ServerActive=10.0.0.18                  #指向Zabbix Proxy
144 Hostname=10.0.0.8                       #对angent来说是被动,可写成和web界面一致的名字(可选)

[root@zabbix-agent1 ~]#systemctl restart zabbix-agent2.service

#8上的angent2的日志
[root@zabbix-agent1 ~]#systemcttail /var/log/zabbix/zabbix_agent2.log 
2023/11/06 13:03:53.255412 using plugin 'VFSDir' (built-in) providing following interfaces: exporter
2023/11/06 13:03:53.255416 using plugin 'VfsFs' (built-in) providing following interfaces: exporter
2023/11/06 13:03:53.255421 using plugin 'WebCertificate' (built-in) providing following interfaces: exporter, configurator
2023/11/06 13:03:53.255425 using plugin 'WebPage' (built-in) providing following interfaces: exporter, configurator
2023/11/06 13:03:53.255429 using plugin 'ZabbixAsync' (built-in) providing following interfaces: exporter
2023/11/06 13:03:53.255434 using plugin 'ZabbixStats' (built-in) providing following interfaces: exporter, configurator
2023/11/06 13:03:53.255437 lowering the plugin ZabbixSync capacity to 1 as the configured capacity 100 exceeds limits
2023/11/06 13:03:53.255441 using plugin 'ZabbixSync' (built-in) providing following interfaces: exporter
2023/11/06 13:03:53.255505 Plugin communication protocol version is 6.0.13
2023/11/06 13:03:53.255512 Zabbix Agent2 hostname: [10.0.0.8]

#18上proxy的日志
[root@zabbix-proxy-active ~]#tail /var/log/zabbix/zabbix_proxy.log -f
 31358:20231106:130324.874 proxy #18 started [history syncer #3]
 31357:20231106:130324.875 proxy #17 started [history syncer #2]
 31354:20231106:130324.875 proxy #14 started [http poller #1]
 31351:20231106:130324.876 proxy #11 started [heartbeat sender #1]
 31364:20231106:130324.877 proxy #24 started [poller #3]
 31370:20231106:130324.877 proxy #30 started [availability manager #1]
 31341:20231106:130324.928 received configuration data from server at "10.0.0.88", datalen 19644
 31348:20231106:130324.959 proxy #8 started [preprocessing worker #1]
 31350:20231106:130324.959 proxy #10 started [preprocessing worker #3]
 31362:20231106:130329.872 enabling Zabbix agent checks on host "10.0.0.8": interface became available

在这里插入图片描述

2、被动模式配置

#配置proxy文件
[root@zabbix-proxy-passive ~]#vim /etc/zabbix/zabbix_proxy.conf
13 ProxyMode=1                                     #被动模式为1
32 Server=10.0.0.88                                #指向zabbix server的地址
42 Hostname=zabbix-proxy-passive
157 DBHost=localhost                               #指定MySQL的地址
171 DBName=zabbix_proxy                            #指定MySQL数据库名称
186 DBUser=zabbix                                  #指定MySQL的用户
194 DBPassword=123456                              #指定MySQL的用户密码

[root@zabbix-proxy-passive ~]#systemctl restart zabbix-proxy.service 

1、在server的web界面上添加被动的proxy

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、修改Zabbix Agent(ubuntu 100) 配置文件为被动模式的 Zabbix Proxy

[root@zabbix-agent2 ~]#vim /etc/zabbix/zabbix_agent2.conf
80 Server=10.0.0.88,10.0.0.129            #必须指向Proxy代理地址,否则Proxy无法监控,Zabbix Server地址则是可选项
133 #ServerActive=10.0.0.88               #注释此行

[root@zabbix-agent2 ~]#systemctl restart zabbix-agent2.service

在这里插入图片描述

四、总结自定义监控,监控win, nginx, 并基于短信/微信报警。

在这里插入图片描述
在上面有这三个机器搭建的过程,这里不做过多赘述,直接开始安装nginx和自定义监控项

[root@zabbix-agent1 ~]#yum -y install nginx

#编写nginx配置文件
[root@zabbix-agent1 ~]#vim /etc/nginx/default.d/zabbix-nginx.conf
location /nginx_status {
    stub_status;
    access_log off;
    allow 127.0.0.1;
    deny all;
}
[root@zabbix-agent1 ~]#systemctl enable --now nginx.service 
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

#编写agent监控脚本
[root@zabbix-agent1 ~]#vim /etc/zabbix/zabbix_agent2.d/nginx_status.sh
#!/bin/bash
# Description:zabbix监控nginx性能以及进程状态
HOST="127.0.0.1"
PORT="80"
URL=nginx_status
# 检测nginx进程是否存在
case $1 in
ping)
   pidof nginx | wc -l
   ;;
# 检测nginx性能
active)
    curl -s "http://$HOST:$PORT/$URL/" | awk 'NR==1{print $NF}'
   ;;

reading)
    curl -s "http://$HOST:$PORT/$URL/" | awk 'NR==4{print $2}'
   ;;
writing)
 curl -s "http://$HOST:$PORT/$URL/" | awk 'NR==4{print $4}'
   ;;

waiting)
    curl -s "http://$HOST:$PORT/$URL/" | awk 'NR==4{print $6}'
   ;;
accepts)
    curl -s "http://$HOST:$PORT/$URL/" |awk 'NR==3{print $1}'
   ;;
handled)
    curl -s "http://$HOST:$PORT/$URL/" | awk 'NR==3{print $2}'
   ;;

requests)
    curl -s "http://$HOST:$PORT/nginx_status/" | awk 'NR==3{print $3}'
   ;;

*)
    echo "Usage `basename $0` {ping | active | reading | writing | waiting | 
accepts | handled | requests }"
esac

[root@zabbix-agent1 ~]#chmod +x /etc/zabbix/zabbix_agent2.d/nginx_status.sh
[root@zabbix-agent1 ~]#ll /etc/zabbix/zabbix_agent2.d/nginx_status.sh
-rwxr-xr-x 1 root root 900 Nov  6 15:03 /etc/zabbix/zabbix_agent2.d/nginx_status.sh

[root@zabbix-agent1 ~]#/etc/zabbix/zabbix_agent2.d/nginx_status.sh active
1

#添加监控设置
[root@zabbix-agent1 ~]#vim /etc/zabbix/zabbix_agent2.d/nginx.conf
UserParameter=nginx.status[*],/etc/zabbix/zabbix_agent2.d/nginx_status.sh $1

#本机测试
[root@zabbix-agent1 ~]#zabbix_agent2 -t nginx.status[ping]
nginx.status[ping]                            [s|1]
[root@zabbix-agent1 ~]#zabbix_agent2 -t nginx.status[active]
nginx.status[active]                          [s|1]

[root@zabbix-agent1 ~]#systemctl restart zabbix-agent2.service

#在zabbix server上安装zabbix-get工具
[root@zabbix-server ~]#yum -y install zabbix-get

#服务端进行测试
[root@zabbix-server ~]#zabbix_get -s 10.0.0.8 -k nginx.status[ping]
1
[root@zabbix-server ~]#zabbix_get -s 10.0.0.8 -k nginx.status[active]
1

在server的web界面进行添加监控项

创建模板
在这里插入图片描述
创建监控项
在这里插入图片描述
在这里插入图片描述
给主机添加上创建的模板
在这里插入图片描述
测试
在这里插入图片描述
创建触发器,判断nginx是否存活(1为启动、0为关闭)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
测试触发器
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
创建报警媒介(QQ邮箱可能会不成功,建议用网易)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
给用户添加报警媒介
在这里插入图片描述
创建动作
在这里插入图片描述
指定操作
在这里插入图片描述
测试
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

五、完成1键安装zabbix agent脚本,可以用于ubuntu系统。

[root@zabbix-agent2 ~]#vim install-zabbix-agent2.sh

#!/bin/bash
#以今天用的最新版zabbix 6.0(ubutu20.04) 为例
#由于zabbix不同版本直接需要下载源包的地址不同,还有Ubuntu各版本下载地址不同,所以脚本不具有兼容性
#如果需要调整可以去查看官网下载地址:https://www.zabbix.com/cn/download
wget https://repo.zabbix.com/zabbix/6.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_6.0-4+ubuntu20.04_all.deb

dpkg -i zabbix-release_6.0-4+ubuntu20.04_all.deb

sed -i.bak 's#http://repo.zabbix.com#https://mirror.tuna.tsinghua.edu.cn/zabbix#' /etc/apt/sources.list.d/zabbix.list

apt update

apt install zabbix-agent2 zabbix-agent2-plugin-*

systemctl restart zabbix-agent2;systemctl enable zabbix-agent2

六、完成1键脚本,可以基于zabbix api添加zabbix agent到zabbix web。

#安装python3
[root@zabbix-server ~]#apt -y install python3

#在添加主机之前需要先获取到登陆的token
[root@zabbix-server ~]#vim zabbix-api-token.sh
#!/bin/bash
ZABBIX_SERVER=10.0.0.88

curl -s -XPOST -H "Content-Type: application/json-rpc" -d '                     
                      
{
"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": "Admin",
"password": "zabbix"
},
"id": 1,
"auth": null
}' http://${ZABBIX_SERVER}/zabbix/api_jsonrpc.php


#批量添加主机
[root@zabbix-server ~]#vim zabbix-api-add-hosts.sh
#!/bin/bash

ZABBIX_SERVER=10.0.0.88
TOKEN=$(./zabbix-api-token.sh| awk -F'"' '{print $8}')
NET=10.0.0

for HOST in $NET.{1..250};do
TOKEN=$(./zabbix-api-token.sh| awk -F'"' '{print $8}')
NET=10.0.0

for HOST in $NET.{1..250};do

curl -s -XPOST -H "Content-Type: application/json-rpc" -d '
{
"jsonrpc": "2.0",
"method": "host.create",
"params": {
    "host":"'$HOST'",             #创建主机名称
    "name":"'web-$HOST'",         #可见名称
    "interfaces": [
       {
        "type": 1,                #类型为1表示agent,2是SNMP,3是IPMI,4是JMX
        "main": 1,                #主要接口
        "useip": 1,               #0是使用DNS,1是使用IP地址
        "ip": "'$HOST'",          #添加的zabbix agent的地址
        "dns": "",
        "port": "10050"           #agent使用的端口
       }
   ],
    "groups": [ 
       {
            "groupid": "13"       #添加到组的id
       }
   ],
    "templates": [ 
       {
            "templateid": "10598" #关联模板的id
       } 
   ]
 },
"id": 1,
"auth": "'$TOKEN'"
}' http://${ZABBIX_SERVER}/zabbix/api_jsonrpc.php
done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值