Class: Zabbix::Sender::Socket

Inherits:
Connection show all
Defined in:
lib/zabbix_sender_api/api.rb

Overview

Socket instances enable TCPSocket based communication with a zabbix trapper server instance

Instance Attribute Summary collapse

Attributes inherited from Connection

#pipe, #targetHost

Instance Method Summary collapse

Methods inherited from Connection

#sendBatchAtomic

Constructor Details

#initialize(proxy: Zabbix::AgentConfiguration.zabbixProxy, port: 10051) ⇒ Socket

Create a new socket connection object. Both proxy and port are optional.

An attempt is made to provide sane default values. If you have a zabbix_agentd.conf file in one of the usual places and zabbix_sender is on your path, it’ll probably just work



135
136
137
138
139
# File 'lib/zabbix_sender_api/api.rb', line 135

def initialize(proxy: Zabbix::AgentConfiguration.zabbixProxy, port: 10051)
  super(proxy: proxy)
  @port = port
  @lastres = nil
end

Instance Attribute Details

#portObject (readonly)

Returns the value of attribute port.



127
128
129
# File 'lib/zabbix_sender_api/api.rb', line 127

def port
  @port
end

Instance Method Details

#flushObject



171
172
173
174
# File 'lib/zabbix_sender_api/api.rb', line 171

def flush
  super
  return @lastres
end

#openObject

Open tcp socket to target proxy/server



143
144
145
# File 'lib/zabbix_sender_api/api.rb', line 143

def open
  @pipe = TCPSocket.new(@targetHost, @port)
end

#sendBatch(aBatch) ⇒ Object

Send aBatch to zabbix via the socket. Note that zabbix will close the connection after you send a complete message, so you *can’t* do this:

socket.open socket.sendBatch(a) socket.sendBatch(b) <– this will blow up socket.flush

…as you can with Zabbix::Sender::Pipe. You’re really best off just using sendBatchAtomic for sockets.

This assumes that the socket is already open.



161
162
163
164
165
166
167
168
169
# File 'lib/zabbix_sender_api/api.rb', line 161

def sendBatch(aBatch)
  header = %Q(ZBXD\x01)
  data = aBatch.to_senderstruct.to_json
  blob = %Q(#{header}#{[data.bytesize].pack("Q").force_encoding("UTF-8")}#{data})
  @pipe.write(blob)
  respHeader = @pipe.read(header.bytesize + 8)
  datalen = respHeader[header.bytesize, 8].unpack("Q")[0]
  @lastres = JSON.parse(@pipe.read(datalen))
end