Living without zabbix-sender

If you like Zabbix and you’re using it, you know that sometimes you need to send a value from the monitored host to the zabbix server/proxy. One of the most common scenario, for me, is the status of a backup job when this is managed by the host: at the end of the job I usually call a script that sends the backup status (OK or KO) through zabbix-sender. Like this:
do_backup && STATUS=OK || STATUS=KO
zabbix_sender --zabbix-server $ZABBIX_SERVER --host "$ZABBIX_HOSTNAME" \
--key $ZABBIX_HOSTKEY --value "Backup $STATUS"On the zabbix server side, there’s an item of type “trapper” in the corresponding host, which will be updated. And with a couple of triggers (“backup error” and “no good backups in the last 12 hours”) we can sleep well.
You don’t need the sender
Sadly, sometimes, you can’t have the zabbix_sender. Typically because you’re on a strange architecture, or have very limited resources, or running on an AS/400.
Here’s a workaround script, in python, that doesn’t need to be fed into the zabbix_sender. This is based on this protocol.
#!/usr/bin/env python
import datetime
import json
import socket
ZABBIX_SERVER = '333.12.33.44'
ZABBIX_HOSTNAME = 'TEST'
ZABBIX_ITEMKEY = 'test'
PORT = 10051
BUFFER_SIZE = 1024
# just replace this with the desired value
msg = datetime.datetime.now().strftime("Message timestamp: %Y/%m/%d-%H:%M:%S")
msg_json = json.dumps({
"request":"sender data",
"data":[
{
"host": ZABBIX_HOSTNAME,
"key": ZABBIX_ITEMKEY,
"value": msg
}
]
})
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
(family, socktype, proto, garbage, address)=socket.getaddrinfo(HOST, PORT)[0]
sock = socket.socket(family, socktype, proto)
sock.connect(address)
sock.send(msg_json)
recv_data = sock.recv(BUFFER_SIZE)
sock.close()
# you don't really need to print
# you're receiving something like this:
# ZBXDZ{"response":"success","info":"processed: 1; failed: 0; total: 1; seconds spent: 0.000200"}
print "received data:", recv_data