First of all, there's very little information out there about how to do it, which makes me think it might be deprecated for some reason, or maybe we're not expected to do it, which I don't understand. E.g., the API documentation does not mention it. Can someone explain why?
Anyway, I found a couple of examples here and here but they don't work in my code, which is based on those two examples. Can anyone suggest why? Zabbix version is 5.0.
Anyway, I found a couple of examples here and here but they don't work in my code, which is based on those two examples. Can anyone suggest why? Zabbix version is 5.0.
Code:
private class ZabbixDataSender(val host: String, val port: Int)
{
val zabbixUrl = URI(host) // host is for example https://172.16.50.23/api_jsonrpc.php
fun send(hostId: String, itemKey: String, datum: String, clock: Long)
{
val socket = Socket(zabbixUrl.host, port)
val output = DataOutputStream(socket.outputStream)
val inputStream = DataInputStream(socket.inputStream)
if (socket.isConnected) { println("Socket is connected!") }
// Prepare data package JSON
val dataPack = """{"host":"$hostId","key":"$itemKey","value":"$datum","clock":$clock}"""
// Prepare header
val header = listOf('Z'.code.toByte(), 'B'.code.toByte(), 'X'.code.toByte(), 'D'.code.toByte(), 1.toByte(), dataPack.length.toByte(), 0.toByte(), 0.toByte(), 0.toByte(), 0.toByte(), 0.toByte(), 0.toByte(), 0.toByte()).toByteArray()
// Send them both to the socket
output.write(header + dataPack.toByteArray())
output.flush()
// Read back from the socket
var readCount = 0
val responseData = ByteArray(512)
while (true) {
val read = inputStream.read(responseData, readCount, 512 - readCount)
if (read < 0) {
// Always immediately shows -1
println("READ: $read")
break
}
readCount += read
}
if (readCount < 13) {
// seems zabbix server return "[]"?
}
// This always shows NOTHING read back
println("Client receiving from ${zabbixUrl.host}:$port [${String(header)}] // $dataPack // $readCount // ${String(responseData)}")
}
}
Comment