这是原厂英文文档的翻译页面. 欢迎帮助我们 完善文档.
2022 Zabbix中国峰会
2022 Zabbix中国峰会

14 插件

插件扩展了Zabbix监控功能。这些插件是用Go编程语言编写的,只支持Zabbix agent2。它们提供了可加载模块(用C编写)和其它扩展Zabbix功能的方法,例如用户参数(agent指标),外部检查(无agent监控)和system.run[] Zabbix agent监控项

以下是agent2及其插件功能:

*单个配置文件(所有插件配置参数与agent本身的参数位于同一个文件中);
       *基于调度和任务并发的任务队列管理;
       *插件级超时。

开箱即用的插件

所有支持Zabbix agent2的指标都由插件收集。以下是Zabbix agent2可直接使用插件:

插件名称 描述 支持监控项 备注
Agent Zabbix agent使用的监控项。 agent. ostname, agent.ping, agent.version 与Zabbi agent支持的参数keys相同。
Ceph 监控Ceph。 ce h.df.details, ceph.osd.stats, ceph.osd.discovery, ceph.osd.dump,  支持ceph.ping, ceph.pool.discovery, ceph.status keys 仅能用于Zabbix agent2。
CPU 监控系统CPU (number of CPUs/CPU cores, discovered CPUs, utilization percentage)。 syst m.cpu.discovery, system.cpu.num, system.cpu.util 与Zab ix agent支持的参数 keys相同。
Docker 监控Docker容器。 dock r.container_info, docker.container_stats, docker.containers, docker.containers.discovery,  可使用*docker.data_usage, docker.images, docker.images.discovery, docker.info, docker.ping pp Docker*监控模版。

支持的 keys 仅能用于Zabbix agent2。
File 文件属性信息搜集。 vfs.file cksum, vfs.file.contents, vfs.file.exists, vfs.file.md5sum,  与Zabbixvfs.file.regexp, vfs.file.regmatch, vfs.file.size, vfs.file.time gent支持的参数 keys相同。
Kernel 监控内核。 kern l.maxfiles, kernel.maxproc 与Zab ix agent支持的参数 keys相同。
Log 监控日志文件。 log, l g.count, logrt, logrt.count 与Zabbi agent支持的参数 keys相同。
Memcached 监控缓存服务器。 memcach d.ping, memchached.stats 可使用*App Memchached*监控模版。

支持的 keys 仅能用于Zabbix agent2。
MySQL 监控MySQL数据库及其相关内容。 mysql.db.di covery, mysql.db.size, mysql.get_status_variables,  可以使用*DB MySmysql.ping, mysql.replication.discovery, mysql.replication.get_slave_status, mysql.version L by Zabbix agent 2*监控模版。

支持的keys 仅能用于Zabbix agent2。

默认配置: URI=tcp://localhost:3306, username=root, ''password= ''.
NetIf 监控网络接口。 net.if collisions, net.if.discovery, net.if.in, net.if.out, net.if.total 与Zabbi agent支持的参数 keys相同。
Oracle 监控Oracle数据库。 oracl .diskgroups.stats, oracle.diskgroups.discovery, oracle.archive.info, oracle.archive.discovery,  *仅适用于oracle.cdb.info, oracle.custom.query, oracle.datafiles.stats, oracle.db.discovery,
oracle.fra.stats, oracle.instance.info, oracle.pdb.info, oracle.pdb.discovery,
oracle.pga.stats, oracle.ping, oracle.proc.stats, oracle.redolog.info,
oracle.sga.stats, oracle.sessions.stats, oracle.sys.metrics, oracle.sys.params,
oracle.ts.stats, oracle.ts.discovery, oracle.user.info
abbix Agent2* 可以使用监控模版。

在使用插件之前安装 Oracle Instant Client

支持的 keys仅能用于Zabbix agent2 。

默认配置: URI=tcp://localhost:1521, service=XE (service name)。

插件的功能可以通过自定义用户定义的查询进行扩展 - 参见配置样例 插件文档
PostgreSQL 监控PostgreSQL及其相关内容。 pgsql.pi g, pgsql.db.discovery, pgsql.db.size, pgsql.db.age, pgsql.database.bloating_tables,  *仅能用于Zabpgsql.replication_lag.sec, pgsql.replication_lag.b, pgsql.replication.count, pgsql.replication.status, pgsql.replication.recovery_role,
pgsql.cache.hit, pgsql.connections, pgsql.archive, pgsql.bgwriter, pgsql.dbstat.sum, pgsql.dbstat,
pgsql.wal.stat, pgsql.locks, pgsql.pgsql.oldest.xid, pgsql.uptime
ix agent2* 可以使用监控模版。

支持的 keys 仅能用于Zabbix agent2。
Proc 监控CPU使用率。 proc. pu.util 与Zabb x agent支持的参数 key相同。
Redis 监控Redis服务器。 redis config, redis.info, redis.ping, redis.slowlog.count 可以使用* B Redis*监控模版.

支持的 keys 仅能用于Zabbix agent2。
Swap 交换空间大小/比率。 system.s ap.size 支持Zabbix 5.0.5及其以上版本.
与Zabbix agent支持的参数 key相同。
SystemRun 执行特定的命令。 system. un 与Zabbix agent支持的参数 key相同。
Systemd 监控系统服务。 system .unit.discovery, systemd.unit.get, systemd.unit.info 支持的 [k ys](/manual/config/items/itemtypes/zabbix_agent/zabbix_agent2) 仅能用于Zabbix agent2。
TCP TCP连通性检测。 net.t p.port 与Zabb x agent支持的参数 key相同。
UDP 监控UDP服务及其性能。 net.udp. ervice, net.udp.service.perf 与Zabbix gent支持的参数 keys相同。
Uname 查询系统信息。 system hostname, system.sw.arch, system.uname 与Zabbi agent支持的参数 keys相同。
Uptime 系统指标信息搜集。 system.u time 与Zabbix gent支持的参数 key相同。
VFSDev 虚拟文件系统设备指标搜集。 vfs.dev.disc very, vfs.dev.read, vfs.dev.write 与Zabbix agen 支持的参数 keys相同。
Web Web页面监控。 web. age.get, web.page.perf, web.page.regexp 与Zab ix agent支持的参数 keys相同。
ZabbixAsync 异步指标搜集。 net.tc .listen, net.udp.listen, sensor, system.boottime, system.cpu.intr, system.cpu.load,  与Zabbisystem.cpu.switches, system.hw.cpu, system.hw.macaddr, system.localtime, system.sw.os,
system.swap.in, system.swap.out, vfs.fs.discovery
agent支持的参数 keys相同。
ZabbixStats Zabbix server/proxy内部指标或者队列延迟数量统计。 zabbix.stats 与Zabbix agent支 的参数 keys相同。
ZabbixSync 同步指标搜集。 net.dn , net.dns.record, net.tcp.service, net.tcp.service.perf, proc.mem,  与Zabbiproc.num, system.hw.chassis, system.hw.devices, system.sw.packages,
system.users.num, vfs.dir.count, vfs.dir.size, vfs.fs.get, vfs.fs.inode,
vfs.fs.size, vm.memory.size
agent支持的参数 keys相同。

配置插件

本节介绍了常见的配置原则和最佳实践。

有关插件配置的具体示例,请参见:

所有插件都是使用Zabbix agent2的plugins.*参数配置文件配置的。与其它agent参数不同,它不是参数的键/值类型。它是一个单独的部分,可以描述插件的特定参数。每个参数应具有以下结构:

Plugins.<PluginName>.<Parameter>=<Value>

参数名称应符合以下要求:

  • 建议将插件的名称大写;
  • 参数应大写;
  • 不允许使用特殊字符;
  • 嵌套不受最大级别的限制;
  • 参数的数量不受限制。
Default values

Since Zabbix 5.0.35, you can set default values for the connection-related parameters (URI, username, password, etc.) in the configuration file in the format:

Plugins.<PluginName>.Default.<Parameter>=<Value>

For example, Plugins.Mysql.Default.Username=zabbix, Plugins.MongoDB.Default.Uri=tcp://127.0.0.1:27017, etc.

If a value for such parameter is not provided in an item key or in the named session parameters, the plugin will use the default value. If a default parameter is also undefined, hardcoded defaults will be used.

If an item key does not have any parameters, Zabbix agent 2 will attempt to collect the metric using values defined in the default parameters section.

Named sessions

Named sessions represent an additional level of plugin parameters and can be used to specify separate sets of authentication parameters for each of the instances being monitored. Each named session parameter should have the following structure:

Plugins.<PluginName>.Sessions.<SessionName>.<Parameter>=<Value>

A session name can be used as a connString item key parameter instead of specifying a URI, username, and/or password separately.

In item keys, the first parameter can be either a connString or a URI. If the first key parameter doesn't match any session name, it will be treated as a URI. Note that embedding credentials into a URI is not supported, use named session parameters instead.

The list of available named session parameters depends on the plugin, see Zabbix agent 2 (UNIX) / Zabbix agent 2 (Windows) for details.

In Zabbix versions before 5.0.34, when providing a connString (session name) in key parameters, item key parameters for username and password must be empty. The values will be taken from the session parameters. If an authentication parameter is not specified for the named session, a hardcoded default value will be used.

Since Zabbix 5.0.34, it is possible to override session parameters by specifying new values in the item key parameters (see example).

Since Zabbix 5.0.35, if a parameter is not defined for the named session, Zabbix agent 2 will use the value defined in the default plugin parameter.

Parameter priority

Since version 5.0.34, Zabbix agent 2 plugins search for connection-related parameter values in the following order:

  1. The first item key parameter is compared to session names. If no match is found it is treated as an actual value; in this case, step 3 will be skipped. If a match is found, the parameter value (usually, a URI) must be defined in the named session.
  2. Other parameters will be taken from the item key if defined.
  3. If an item key parameter (for example, password) is empty, plugin will look for the corresponding named session parameter.
  4. If the session parameter is also not specified, the value defined in the corresponding default parameter will be used.
  5. If all else fails, the plugin will use the hardcoded default value.
Example 1

Monitoring of two instances “MySQL1” and “MySQL2”.

Configuration parameters:

Plugins.Mysql.Sessions.MySQL1.Uri=tcp://127.0.0.1:3306
       Plugins.Mysql.Sessions.MySQL1.User=mysql1_user
       Plugins.Mysql.Sessions.MySQL1.Password=unique_password
       Plugins.Mysql.Sessions.MySQL2.Uri=tcp://192.0.2.0:3306
       Plugins.Mysql.Sessions.MySQL2.User=mysql2_user
       Plugins.Mysql.Sessions.MySQL2.Password=different_password

Item keys: mysql.ping[MySQL1], mysql.ping[MySQL2]

Example 2

Providing some of the parameters in the item key (supported since Zabbix 5.0.34).

Configuration parameters:

Plugins.Postgres.Sessions.Session1.Uri=tcp://192.0.2.234:5432
       Plugins.Postgres.Sessions.Session1.User=old_username
       Plugins.Postgres.Sessions.Session1.Password=session_password

Item key: pgsql.ping[session1,new_username,,postgres]

As a result of this configuration, the agent will connect to PostgreSQL using the following parameters:

  • URI from session parameter: 192.0.2.234:5432
  • Username from the item key: new_username
  • Password from session parameter (since it is omitted in the item key): session_password
  • Database name from the item key: postgres
Example 3

Collecting a metric using default configuration parameters.

Configuration parameters:

Plugins.Postgres.Default.Uri=tcp://192.0.2.234:5432
       Plugins.Postgres.Default.User=zabbix
       Plugins.Postgres.Default.Password=password

Item key: pgsql.ping[,,,postgres]

As a result of this configuration, the agent will connect to PostgreSQL using the parameters:

  • Default URI: 192.0.2.234:5432
  • Default username: zabbix
  • Default password: password
  • Database name from the item key: postgres

连接

一些插件支持同时从多个实例收集度量。可以监视本地和远程实例。支持TCP和Unix套接字连接。

建议配置插件,使实例的连接保持在打开状态。这样做的好处是减少了网络拥塞、延迟以及由于连接数较少而导致的CPU和内存使用。客户端库负责这个。

未使用的连接保持打开的时间段可以由Plugins.<PluginName>.KeepAlive参数确定。
示例:Plugins.Memcached.KeepAlive

编写插件

插件是定义结构并实现一个或多个插件接口(Exporter、Collector、Runner、Watcher)的Go包:

  • 插件导出器 plugin.Exporter

Exporter是执行轮询并返回值(values)、nothing和error的最简单接口。它接受预先准备好的项键、参数和上下文。Exporter接口是唯一可以同时访问的接口。所有其它插件接口访问都是独占的,当插件已经在执行某些任务时,不能调用任何方法。此外,每个插件最多只能调用100个concurrent Export(),可以根据需要减少每个插件的调用次数。

  • 插件收集器 plugin.Collector

收集器用于插件需要定期收集数据时。此接口通常与导出器接口一起用于导出收集的数据。

  • 插件配置器 plugin.Configurator

Configurator用于从agent2配置文件向插件提供配置参数。

  • 插件运行器 plugin.Runner

Runner接口提供了在插件启动(激活)时执行一些初始化,在插件停止(停用)时执行取消初始化的方法。例如,插件可以通过实现Runner接口来启动/停止一些后台goroutine。

  • 插件.观察者 plugin.Watcher

Watcher允许插件实现自己的度量轮询,而不使用agent的内部调度程序,例如在基于陷阱的插件中。

默认情况下,插件处于非活动状态,只有在监视插件提供的度量时才会激活。

插件位于插件目录树中,按含义分组,例如plugins/system/uptime/uptime.go.

实施步骤

插件必须导入zabbix.com/pkg/plugin包。

import "zabbix.com/pkg/plugin"

插件必须定义结构并嵌入plugin.Base结构。

type Plugin struct {
           plugin.Base
       }
       var impl Plugin

一个插件必须实现一个或多个插件接口。

func (p *Plugin) Export(key string, params []string, ctx plugin.ContextProvider) (result interface{}, err error) {
           if len(params) > 0 {
               p.Debugf("received %d parameters while expected none", len(params))
               return nil, errors.New("Too many parameters")
           }
           return time.Now().Format(time.RFC3339)
       }

插件必须在初始化期间注册自身。

func init() {
           plugin.RegisterMetrics(&impl, "Time", "system.time", "Returns time string in RFC 3999 format.")
       }

其中RegisterMetrics 参数为:

  • 指向插件实现的指针
  • 插件名称(大写)
  • 指标#1名称(项目键)
  • 指标#1描述(以大写字符开始,以点结束)
  • 指标#2名称(项目键)(可选)
  • 指标#2描述(以大写字符开始,以点结束)(可选)
  • ...

如果需要日志记录,插件必须使用 plugin.Base 提供的日志记录功能(见上面的例子)。它基本上是一个标准日志的包装器,但是它会在日志信息前面加上[<plugin name>]。