See www.zabbix.com for the official Zabbix site.

Docs/protocols/zabbix sender/1.8/java example

From Zabbix.org
Jump to: navigation, search

Zabbix sender protocol implementation in Java

Below is a simple Java code snippet taken from ZabbixTool.

                String report = buildJSonString(...);
                writeMessage(stream, report.getBytes());
...
	private String buildJSonString(String host, String item, String value)
	{
		return 		  "{"
        + "\"request\":\"sender data\",\n"
        + "\"data\":[\n"
        +        "{\n"
        +                "\"host\":\"" + host + "\",\n"
        +                "\"key\":\"" + item + "\",\n"
        +                "\"value\":\"" + value.replace("\\", "\\\\") + "\"}]}\n" ;
	}

	protected void writeMessage(OutputStream out, byte[] data) throws IOException {
		int length = data.length;
		
		out.write(new byte[] {
				'Z', 'B', 'X', 'D', 
				'\1',
				(byte)(length & 0xFF), 
				(byte)((length >> 8) & 0x00FF), 
				(byte)((length >> 16) & 0x0000FF), 
				(byte)((length >> 24) & 0x000000FF),
				'\0','\0','\0','\0'});
		
		out.write(data);
	}