mqtt

package module
v0.0.0-...-efb17c5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 25, 2021 License: MIT Imports: 11 Imported by: 0

README

Notice

This was POC project! Please use now available zabbix agent 2 module: https://git.zabbix.com/projects/ZBX/repos/zabbix/browse/src/go/plugins/mqtt

Zabbix agent2 MQTT subscriber plugin

Zabbix agent2 plugin(watcher) implementing MQTT.

image

Build

Make sure golang is installed and properly configured.

Checkout zabbix:
git clone https://git.zabbix.com/scm/zbx/zabbix.git --depth 1 zabbix-agent2
cd zabbix-agent2
Checkout this plugin repo:
git submodule add https://github.com/v-zhuravlev/zbx_plugin_mqtt.git src/go/plugins/mqtt

Edit file src/go/plugins/plugins.go by appending _ "zabbix.com/plugins/mqtt":

package plugins

import (
	_ "zabbix.com/plugins/log"
	_ "zabbix.com/plugins/systemrun"
	_ "zabbix.com/plugins/zabbix/async"
	_ "zabbix.com/plugins/zabbix/stats"
	_ "zabbix.com/plugins/zabbix/sync"
	_ "zabbix.com/plugins/mqtt"
)

./bootstrap.sh
pushd .
cd src/go/
go mod vendor
popd
Apply patches:
git apply src/go/plugins/mqtt/patches/manager.patch
(see this upstream PR):
git apply src/go/plugins/mqtt/patches/router.patch
./configure --enable-agent2 --enable-static
make

You will then find new agent with plugin included in src/go/bin dir

Test it by running ./zabbix-agent2 -t agent.ping -c ../conf/zabbix_agent2.conf

Configuration

In zabbix_agent2.conf:

  • set ServerActive= to IP of your Zabbix server or proxy
  • set Hostname= to host name you will use in Zabbix.
  • (optional) set MQTT name and password, if required by your broker:
Plugins.MQTTSubscribe.Username=<username here>
Plugins.MQTTSubscribe.Password=<password here>
  • (optional) set MQTT ClientID. If not set, ClientID will be generated automatically.
Plugins.MQTTSubscribe.ClientID=zabbix-agent2-mqtt-client
  • (optional) set MQTT Connection timeout in seconds. If not set, global Zabbix agent Timeout= will be used.
Plugins.MQTTSubscribe.Timeout=5

Supported keys

mqtt.subscribe

mqtt.subscribe[<MQTT broker URL>,<MQTT topic>]

for example:

mqtt.subscribe[tcp://192.168.1.1:1883,devices/+/values]

Item must be of type Zabbix agent(active).
Also note, that update interval is ignored, values will be received once published to the MQTT broker. Still, try to set update interval as closer as possible to the data update frequency, so you get proper graphs in Zabbix.

image

Troubleshooting

Change LogType=console and DebugLevel=4 in config file.

Known problems and limitations

  • Only Zabbix agent (active) is supported, thus impossible to use single Zabbix agent2 with multiple hosts. Waiting for Passive bulk implementation in Zabbix.
  • Global timeout is ignored if there are no plugin specific options are defined https://support.zabbix.com/browse/ZBX-17070

Next steps

  • disconnect MQTT clients when there is no active mqtt.subscribe
  • move mqtt.get to separate plugin so no Exporter invocation
  • make user + password configurable in config file
  • validate item params

and perhaps...

Changelog

v0.1

Initial version

Read more

https://medium.com/@zhuravlev.vitaly/writing-watcher-zabbix-agent2-mqtt-plugin-in-go-e00067ea1990

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	//Interval int `conf:"optional,range=0:10,default=10"`
	Username string `conf:"optional"` // MQTT username to use
	Password string `conf:"optional"` // MQTT password to use
	ClientID string `conf:"optional"`
	Timeout  int    `conf:"optional,range=0:30"` //MQTT Connect timeout
}

type Plugin

type Plugin struct {
	plugin.Base
	// contains filtered or unexported fields
}

func (*Plugin) Configure

func (p *Plugin) Configure(global *plugin.GlobalOptions, private interface{})

func (*Plugin) EventSourceByKey

func (p *Plugin) EventSourceByKey(key string) (es watch.EventSource, err error)

EventSourceByKey is used when trying to match item key with it's event source during item update

func (*Plugin) EventSourceByURI

func (p *Plugin) EventSourceByURI(uri string) (es watch.EventSource, err error)

EventSourceByURI is used to unsubscribe from the sources without items associated to them

func (*Plugin) Validate

func (p *Plugin) Validate(private interface{}) (err error)

func (*Plugin) Watch

func (p *Plugin) Watch(requests []*plugin.Request, ctx plugin.ContextProvider)

Watch MQTT plugin

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL