Class: Zabbix::Sender

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

Defined Under Namespace

Classes: 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.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/zabbix/sender.rb', line 10

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.



8
9
10
# File 'lib/zabbix/sender.rb', line 8

def configured
  @configured
end

Instance Method Details

#configured?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/zabbix/sender.rb', line 25

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

#connectObject



30
31
32
# File 'lib/zabbix/sender.rb', line 30

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

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/zabbix/sender.rb', line 49

def send_data(key, value, opts={})
  return false unless configured? 
  host   = opts[:host] || Socket.gethostname
  ts     = opts[:ts]   || Time.now.to_i 
  socket = connect 

  # construct json
  json = Yajl::Encoder.encode({ 
    :request => "agent data",
    :clock   => ts,
    :data    => [{
      :host       => host,
      :key        => key,
      :value      => value,
      :clock      => ts
    }]
  })

  # send the data
  socket.write "ZBXD\x01"
  socket.write [json.size].pack('q')
  socket.write json
  socket.flush

  # read the response message if desired
  #header = socket.read(5)
  #len    = socket.read(8)
  #len    = len.unpack('q').shift
  #msg    = socket.read(len)
  socket.close 

  return true
end

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



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

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

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



35
36
37
# File 'lib/zabbix/sender.rb', line 35

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

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



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

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