Use zabbix_sender to send monitoring data to zabbix server

Source: Internet
Author: User
Tags json time interval

Generally, most zabbix Keys define data capturing behaviors (such as script capturing, time interval, and unit) on the server in advance ), then, the server requests data from the agent based on this behavior, or the agent sends data to the server based on this behavior, but the concept of "timing" cannot be avoided, that is, the data capture interval is fixed. In one scenario, the data to be monitored is not obtained at any time (such as cpu or load), but is generated by some programs from time to time (such as crontab and data triggered by some users ), at this time, it is not so elegant to use the conventional zabbix key, and it is difficult to achieve real-time. The ideal solution is to send the data to zabbix server while the program generates monitoring data, this method is similar to the well-known newrelic. By deploying an agent on the program side, the agent then sends data to the newrelic server. Fortunately, zabbix provides similar functions to help us fulfill this requirement.
Zabbix_sender:
The binary program provided by zabbix can be used to send data to zabbix server. The usage is as follows:

>>> Zabbix_sender-z 10.0.0.1-p 10051-s "rs_solrmaster"-k "get_number_of_solr_index_update"-o 4
It should be noted that the key get_number_of_solr_index_update needs to be created on the rs_solrmaster host in advance and is of the zabbix trapper type and zabbix trapper type.
# Usage: zabbix_sender [-Vhv] {[-zpsI]-ko | [-zpI]-T-I-r} [-c]
# Options:
#-C -- absolute path of the config file
#-Z -- IP address of zabbix-server zabbix server or zabbix proxy
#-P -- port the port of zabbix server or zabbix proxy. The default port is 10051.
#-S -- host name, which indicates the host name configured in zabbix
#-I -- source-address: source ip address.
#-K -- key zabbix trapper key name
#-O -- value the metric value to be sent
#-I -- input-file <input type = "text"/> send data in batches using a file. Each record is recorded in the format of hostname, key, and value, separated by spaces, if the hostname contains spaces, use double quotation marks.
>>> Cat trapper_key.txt
"Zabbix server" key1 10
"Zabbix proxy" key2 20
10.0.0.2 key3 30
>>> Zabbix_sender-z 10.0.0.1-I trapper_key.txt
In this way, we can send monitoring data to zabbix anytime, anywhere. Because zabbix only provides the zabbix_sender binary program to send data, if you want to call it in the program, it is ugly to call a two-Process program directly. Is there any more elegant method? (the zabbix api does not provide a description of the trapper key ). Yes. Pay attention to the High Energy ahead.
Zabbix_sender communicates with zabbix_server over tcp. That is to say, zabbix_sender sends a tcp packet with a fixed DATA segment to zabbix_server, and then the server parses and processes the request. Therefore, if the DATA format of this DATA segment can be determined, we can use a program to simulate zabbix_sender.
Let's take a look at two articles:
Research on zabbix sender protocol | itnihao
Zabbix sender 2.0 protocol document
A complete zabbix_sender request packet is formatted as follows:
SOURCE Port target Port
32-bit serial number
32-bit confirmation number
4-bit header length reserved (6-bit) urg ack psh pst syn fin 16-bit window size
16-bit checksum and 16-bit emergency pointer
Optional
Z B X D protocol version 8-bit data length request or response json data
We can see that the basic zabbix_sender data protocol is 'Z' 'B' 'X' d' + 1-bit protocol version number + 8-bit data length + json data composition, for example, the json data format of the request is:
{
"Request": "sender data ",
"Data ":[
                {
"Host": "Host name 1 ",
"Key": "item_key ",
"Value": "33 "},
                {
"Host": "Host name 2 ",
"Key": "item_key ",
"Value": "55"
                }
        ]
}
In this way, the basic program logic comes out, namely:
& Spades; construct a packet that complies with the zabbix_sender protocol in the format of '4sbqns' (n is the length of the json string)
& Spades; port for sending data packets to zabbix_server
& Spades; processing response
The following is an example of python implementation:

#! /Usr/bin/env python

Import struct
Import json
Import socket


Class zabbix_sender:
Def _ init _ (self, zbx_server_host, zbx_server_port ):
Self. zbx_server_host = zbx_server_host
Self. zbx_server_port = zbx_server_port
Self. zbx_header = 'zbxd'
Self. zbx_protocols_version = 1
Self. zbx_send_value = {'request': 'sender data', 'data': []}
Def adddata (self, host, key, value ):
Add_data = {'host': host, 'key': key, 'value': value}
Self. zbx_send_value ['data']. append (add_data)
# Package encapsulation by protocol
Def makesenddata (self ):
Zbx_send_json = json. dumps (self. zbx_send_value)
Zbx_send_json_len = len (zbx_send_json)
Self. zbx_send_data = struct. pack ("<4sBq" + str (zbx_send_json_len) + "s", 'zbxd', 1, zbx_send_json_len, zbx_send_json)
Def send (self ):
Self. makesenddata ()
Zbx_server_socket = socket. socket ()
Zbx_server_socket.connect (self. zbx_server_host, self. zbx_server_port ))
Zbx_server_write_df = zbx_server_socket.makefile ('WB ')
Zbx_server_write_df.write (self. zbx_send_data)
Zbx_server_write_df.close ()
Zbx_server_read_df = zbx_server_socket.makefile ('RB ')
Zbx_response_package = zbx_server_read_df.read ()
Zbx_server_read_df.close ()
# Parse data packets by protocol
Zbx_response_data = struct. unpack ("<4sBq" + str (len (zbx_response_package)-struct. calcsize ("<4sBq") + "s", zbx_response_package)
Return zbx_response_data [3]

If _ name _ = '_ main __':
Zabbix_sender = zabbix_sender ('10. 0.0.1 ', 10051)
Zabbix_sender.adddata ('solrmaster', 'get _ number_of_solr_index_update ', 60)
Response = zabbix_sender.send ()
Print response

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.