Ad Widget

Collapse

Unable to push/send data -- Kotlin

Collapse
This topic has been answered.
X
X
 
  • Time
  • Show
Clear All
new posts
  • michaelNGV
    Junior Member
    • Jul 2022
    • 27

    #1

    Unable to push/send data -- Kotlin

    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.

    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)}")
        }
    }
  • Answer selected by michaelNGV at 11-07-2022, 04:36.
    Markku
    Senior Member
    Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
    • Sep 2018
    • 1781

    You are looking for the Zabbix trapper/sender protocol documentation?


    (Linked from https://www.zabbix.com/documentation.../zabbix_sender)

    Markku

    Comment

    • Markku
      Senior Member
      Zabbix Certified SpecialistZabbix Certified ProfessionalZabbix Certified Expert
      • Sep 2018
      • 1781

      #2
      You are looking for the Zabbix trapper/sender protocol documentation?


      (Linked from https://www.zabbix.com/documentation.../zabbix_sender)

      Markku

      Comment

      Working...