Class: Zabbix::Sender

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix/sender.rb

Direct Known Subclasses

Buffer

Defined Under Namespace

Classes: Buffer, Easy

Constant Summary collapse

DEFAULT_SERVER_PORT =
10051

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Sender

Returns a new instance of Sender.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/zabbix/sender.rb', line 7

def initialize(opts={})
  if opts[:config_file]
    config = Zabbix::Agent::Configuration.read(opts[:config_file])
    @host  = config.server
    @port  = config.server_port || DEFAULT_SERVER_PORT
  end

  if opts[:host]
    @host = opts[:host]
    @port = opts[:port] || DEFAULT_SERVER_PORT
  end

end

Instance Attribute Details

#configuredObject (readonly)

Returns the value of attribute configured.



5
6
7
# File 'lib/zabbix/sender.rb', line 5

def configured
  @configured
end

Instance Method Details

#configured?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/zabbix/sender.rb', line 22

def configured?
  @configured ||= !(@host.nil? and @port.nil?)
end

#connectObject



27
28
29
# File 'lib/zabbix/sender.rb', line 27

def connect
  @socket ||= TCPSocket.new(@host, @port)
end

#disconnectObject



31
32
33
34
35
36
37
# File 'lib/zabbix/sender.rb', line 31

def disconnect
  if @socket
    @socket.close unless @socket.closed?
    @socket = nil 
  end
  return true
end

#send_data(key, value, opts = {}) ⇒ Object



53
54
55
56
57
58
# File 'lib/zabbix/sender.rb', line 53

def send_data(key, value, opts={})
  return false unless configured? 
  host  = opts[:host]
  clock = opts[:ts  ]
  return send_zabbix_request([ cons_zabbix_data_element(host, key, value, clock) ])
end

#send_heartbeat(key, msg = "", opts = {}) ⇒ Object



49
50
51
# File 'lib/zabbix/sender.rb', line 49

def send_heartbeat(key, msg="", opts={})
  send_data("#{key}.heartbeat", msg, opts)
end

#send_start(key, opts = {}) ⇒ Object



39
40
41
# File 'lib/zabbix/sender.rb', line 39

def send_start(key, opts={})
  send_data("#{key}.start", 1, opts)
end

#send_stop(key, opts = {}) ⇒ Object



44
45
46
# File 'lib/zabbix/sender.rb', line 44

def send_stop(key, opts={}) 
  send_data("#{key}.stop", 1, opts)
end