[ZBX-16123] Self-written sender in bash - Header length Created: 2019 May 13  Updated: 2024 Apr 10  Resolved: 2019 May 14

Status: Closed
Project: ZABBIX BUGS AND ISSUES
Component/s: Server (S)
Affects Version/s: 4.0.7
Fix Version/s: None

Type: Documentation task Priority: Minor
Reporter: Dominique Assignee: Vladislavs Sokurenko
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

CentOS Linux release 7.4.1708 (Core)
3.10.0-693.17.1.el7.x86_64 GNU/Linux


Team: Team A
Sprint: Sprint 52 (May 2019)

 Description   

We're using self-written senders on some dedicated systems. As described in 4 Header we used the example with bash. Sending is done by nc (netcat).

example.sh
DATA='{ "request":"sender data", "data":[ { "host":"host1", "key":"test.item[1]", "value":"0"}] }'
printf -v LENGTH '%016x' "${#DATA}"
PACK=""
for i in {14..0..-2}; do PACK="$PACK\\x${LENGTH:$i:2}"; done
printf "ZBXD\1$PACK%s" $DATA | \
        nc -v -s 127.0.0.1 127.0.0.1 10051

Executing the code:

# ./example.sh
Ncat: Version 6.40 ( http://nmap.org/ncat )
Ncat: Connected to 127.0.0.1:10051.
Ncat: 218 bytes sent, 0 bytes received in 0.01 seconds.

Zabbix server log says:

zabbix_server.log
Message from 127.0.0.1 is longer than expected 109 bytes. Message ignored.

For some reason the header length is calculated wrong in my example.sh.

Executing the script without nc prints this:

ZBXDm{ZBXDm"request":"senderZBXDmdata",ZBXDm"data":[ZBXDm{ZBXDm"host":"host1",ZBXDm"key":"test.item[1]",ZBXDm"value":"0"}]ZBXDm}


 Comments   
Comment by Vladislavs Sokurenko [ 2019 May 13 ]

Please try this:

DATA='{ "request":"sender data", "data":[ { "host":"host1", "key":"test.item[1]", "value":"0"}] }'
printf -v LENGTH '%016x' "${#DATA}"
PACK=""
for i in {14..0..-2}; do PACK="$PACK\\x${LENGTH:$i:2}"; done
printf "ZBXD\1$PACK%s" "$DATA"
Comment by Glebs Ivanovskis [ 2019 May 13 ]

Nice job, vso! Double quoting $DATA should solve the issue. Check out scripts attached to ZBXNEXT-3764, no one seems to complain about them.

Comment by Vladislavs Sokurenko [ 2019 May 14 ]

Updated following pages:
https://www.zabbix.com/documentation/4.0/manual/appendix/protocols/header_datalen
https://www.zabbix.com/documentation/4.2/manual/appendix/protocols/header_datalen
https://www.zabbix.com/documentation/4.4/manual/appendix/protocols/header_datalen

Comment by Dominique [ 2019 May 14 ]

Thanks, it's working with the double quotes!

However, on some of our systems with a different bash version we still have issues:

error
line 79: LENGTH: {14..0..-2}: syntax error: operand expected (error token is "{14..0..-2}")
bash version
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
Comment by Vladislavs Sokurenko [ 2019 May 14 ]

Yes cyclone, scripts in ZBXNEXT-3764 look very interesting I will keep those in my mind ! Documentation has been updated to include quoting.

Comment by Vladislavs Sokurenko [ 2019 May 14 ]

This should do the trick Birchler

DATA='{ "request":"sender data", "data":[ { "host":"host1", "key":"test.item[1]", "value":"0"}] }'
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"
Comment by Dominique [ 2019 May 14 ]

Great, it works! Thank you for your support.

Comment by Vladislavs Sokurenko [ 2019 May 14 ]

Can you please confirm that edited script works for you ?

Comment by Dominique [ 2019 May 14 ]

Yes, it works for me, thank you vso.

Comment by Glebs Ivanovskis [ 2019 May 14 ]

{START..END..INCREMENT} syntax requires Bash v4.0+ according to this source.

Generated at Sat Apr 27 07:05:48 EEST 2024 using Jira 9.12.4#9120004-sha1:625303b708afdb767e17cb2838290c41888e9ff0.