Advertising:

Zabbix Sender en puro BASH

From Zabbix-ES
Revision as of 07:26, 29 June 2021 by Unknown user (talk)
Jump to navigation Jump to search

zabbix_sender.sh

#!/bin/bash

export zbx_server="zabbix_server_ip"
export zbx_port="10051"
export zbx_hostname="hostname"
export zbx_key="trap01"
export zbx_value="{ \\\"AlertName\\\" : \\\"$1\\\", \\\"Result\\\" : $3, \\\"HitCount\\\" : $4, \\\"From\\\" : $5, \\\"To\\\" : $6 }"
export DATA='{"request":"sender data","data":[{"host":"'${zbx_hostname}'","key":"'${zbx_key}'","value":"'${zbx_value}'"}]}'

printf -v LENGTH '%016x' "${#DATA}"
PACK=""
for (( i=14; i>=0; i-=2 )); do PACK="$PACK\\x${LENGTH:$i:2}"; done
printf "ZBXD\1$PACK%s" "$DATA" > /dev/tcp/${zbx_server}/${zbx_port}

zabbix_sender.sh + Netcat

Nota: Con NC tenemos un mejor control del socker y por consiguiente de los errores.
#!/bin/bash

export zbx_server="zabbix_server_ip"
export zbx_port="10051"
export zbx_hostname="hostname"
export zbx_key="trap01"
export zbx_value="{ \\\"AlertName\\\" : \\\"$1\\\", \\\"Result\\\" : $3, \\\"HitCount\\\" : $4, \\\"From\\\" : $5, \\\"To\\\" : $6 }"
export DATA='{"request":"sender data","data":[{"host":"'${zbx_hostname}'","key":"'${zbx_key}'","value":"'${zbx_value}'"}]}'

printf -v LENGTH '%016x' "${#DATA}"
PACK=""
for (( i=14; i>=0; i-=2 )); do PACK="$PACK\\x${LENGTH:$i:2}"; done
resp=$(printf "ZBXD\1$PACK%s" "$DATA" | nc ${zbx_server} ${zbx_port})
if [ -z "${resp##*failed\:\ 1*}" ] ; then
   >&2 echo "Err: sending failed"
   exit 1
fi