A zabbix sender protocol implementation in Lua, for pushing monitoring data to Zabbix trapper items directly from your Lua code.
I am not a programmer, so here’s a warning: This code was written in an exploratory way. If you encounter problems, see something wrong or something was implemented in a weird way, I would be happy if you tell me about it or create a pull request. Thank you. :)
local inspect = require('inspect')
local zbx_sender = require('zabbix-sender')
local sender = zbx_sender.new({
server = 'localhost',
port = 10051,
monitored_host = 'node01'
})
local resp = sender:add_item('trap1', 'test1')
:add_item('trap2', 'test2')
:add_item('trap1', 'test1', 'node02')
:send()
print(inspect(resp))
{
failed = 0,
processed = 3,
total = 3
}
local socket = require('socket')
local copas = require('copas')
local zbx_sender = require('zabbix-sender')
local sender = zbx_sender.new({
server = 'localhost',
port = 10051,
monitored_host = 'node01',
socket = function() return copas.wrap(socket.tcp()) end
})
copas.addthread(function()
sender:add_item('trap1', 'test1')
:add_item('trap2', 'test2')
copas.sleep(1)
sender:add_item('trap1', 'test1', 'node02')
end)
copas.addthread(function()
while sender:has_unsent_items() do
sender:send()
copas.sleep(2)
end
end)
copas.loop()
Creates a new zabbix sender.
Parameter:
- opts: (table) options (opt)
- opts.server: (string) Zabbix server URL/IP (default=
localhost
) - opts.port: (number) Zabbix server port (default=
10051
) - opts.monitored_host: (string) The hostname the items belongs to. (default=
nil
) - opts.timestamps: (boolean) Whether or not add timestamps to items - if
opts.nanoseconds
is set totrue
opts.timestamps
is set totrue
too (default=false
) - opts.nanoseconds: (boolean) Whether or not add nanoseconds to items (default=
false
) - opts.timeout: (number) connection timeout (default=
0.5
) - opts.socket: (function) a function which returns a LuaSocket TCP master object. For example this can be used to wrap the socket with copas (See the example above). (default=
socket.tcp
)
Returns:
(table) zabbix sender
Adds an item to the request payload. The item(s) are stored until send()
is invoked. Multiple calls to add_item()
can be chained and completed by a final call to send()
. See example above.
Parameter:
- key: (string) the items key
- value: (string | number) the items value
- mhost: (string) Is needed if monitored_host was not set at creation or if the monitoring data is ment for another host.
Returns:
(table) self
Raises:
- Error if
key
orvalue
is missing - Error if
host
is missing andmonitored_host
was not set
Adds multiple items from a given table. The items
table is an array of tables in the format of:
{
{ 'key1', 'val1', 'host1' },
{ 'key2', 'val2', 'host2' },
...
}
Parameter:
- items: (table)
Returns:
(table) self
Raises:
- Error if
items
is not given or not atable
Removes all unsent items.
Returns:
(self)
Checks if there are currently unsent items.
Returns:
- (boolean)
Sends all added items.
Returns:
- (table | boolean) response from server |
false
in case of error - (string) error message in case of error