<commit_before>require "zabbix_send/version"
require 'json'
require 'socket'

module ZabbixSend
  class Sender
    def self.build_message(zhost,zkey,zvalue)
      msg = {
        "request" => "sender data",
        "data" => [
          {
            "host" => zhost,
            "key" => zkey,
            "value" => zvalue,
          }]
      }
      body = JSON.generate msg
      data_length = body.bytesize
      data_header = "ZBXD\1".encode("ascii") + \
                    [data_length].pack("i") + \
                    "\x00\x00\x00\x00"
      data_to_send = data_header + body
    end

    def self.send_message(message, server, port)
      s = TCPSocket.new(server, port)
      s.write message
      response_header = s.recv(5)
      if not response_header == "ZBXD\1"
        puts "response: #{response_header}"
        raise 'Got invalid response'
      end
      response_data_header = s.recv(8)
      response_length = response_data_header[0,4].unpack("i")[0]
      response_raw = s.recv(response_length)
      s.close
      response = JSON.load response_raw
    end

    def zabbix_send(zserver, zhost,zkey,zvalue)
      z_send = Sender.build_message zhost, zkey, zvalue
      Sender.send_message z_send, zserver, 10051
    end
  end
end
<commit_msg>Added clock into JSON and port specification to send (defaults to 10051)<commit_after>require "zabbix_send/version"
require 'json'
require 'socket'
require 'time'

module ZabbixSend
  class Sender
    def self.build_message(zhost,zkey,zvalue,ztime)
      msg = {
        "request" => "sender data",
        "data" => [
          {
            "host" => zhost,
            "key" => zkey,
            "value" => zvalue,
            "clock" => ztime
          }]
      }
      body = JSON.generate msg
      data_length = body.bytesize
      data_header = "ZBXD\1".encode("ascii") + \
                    [data_length].pack("i") + \
                    "\x00\x00\x00\x00"
      data_to_send = data_header + body
    end

    def self.send_message(message, server, port)
      s = TCPSocket.new(server, port)
      s.write message
      response_header = s.recv(5)
      if not response_header == "ZBXD\1"
        puts "response: #{response_header}"
        raise 'Got invalid response'
      end
      response_data_header = s.recv(8)
      response_length = response_data_header[0,4].unpack("i")[0]
      response_raw = s.recv(response_length)
      s.close
      response = JSON.load response_raw
    end

    def zabbix_send(zserver,zhost,zkey,zvalue,ztime=Time.now.to_i,zport=10051)
      z_send = Sender.build_message zhost, zkey, zvalue, ztime
      Sender.send_message z_send, zserver, zport
    end
  end
end
{'fraction_non_alphanumeric': 0.11194029850746269, 'fraction_numerical': 0.016791044776119403, 'mean_word_length': 2.191666666666667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6156326', 'n_tokens_mistral': 922, 'n_tokens_neox': 888, 'n_words': 185}