Class: Rubix::Sender

Inherits:
Object
  • Object
show all
Includes:
Logs
Defined in:
lib/rubix/sender.rb

Overview

A class used to send data to Zabbix.

This sender is used to wrap zabbix_sender.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logs

#debug, #error, #fatal, #info, #warn

Constructor Details

#initialize(settings = {}) ⇒ Sender

Create a new sender with the given settings.

Parameters:

  • settings (Hash, Configliere::Param) (defaults to: {})
  • settings (String, Rubix::Host) (defaults to: {})

    host the name of the Zabbix host to write data for

  • settings (String) (defaults to: {})

    server the IP of the Zabbix server

  • settings (Fixnum) (defaults to: {})

    port the port to connect to on the Zabbix server

  • settings (String) (defaults to: {})

    config the path to the local configuration file



58
59
60
61
62
63
64
65
66
# File 'lib/rubix/sender.rb', line 58

def initialize settings={}
  @settings = settings
  self.server     = settings[:server] if settings[:server]
  self.host       = settings[:host]   if settings[:host]
  self.port       = settings[:port]   if settings[:port]
  self.config     = settings[:config] if settings[:config]
  self.timestamps = settings[:timestamps]
  confirm_settings
end

Instance Attribute Details

#configObject



37
38
39
# File 'lib/rubix/sender.rb', line 37

def config
  @config ||= '/etc/zabbix/zabbix_agentd.conf'
end

#hostString, Rubix::Hosts

Returns the Zabbix host name or Rubix::Host the sender will use by default.

Returns:

  • (String, Rubix::Hosts)

    the Zabbix host name or Rubix::Host the sender will use by default



24
25
26
# File 'lib/rubix/sender.rb', line 24

def host
  @host
end

#portObject



31
32
33
# File 'lib/rubix/sender.rb', line 31

def port
  @port ||= 10051
end

#serverObject



19
20
21
# File 'lib/rubix/sender.rb', line 19

def server
  @server ||= 'localhost'
end

#timestamps=(value) ⇒ Object (writeonly)

Whether or not to include timestamps with the data.



42
43
44
# File 'lib/rubix/sender.rb', line 42

def timestamps=(value)
  @timestamps = value
end

Instance Method Details

#closeObject

:nodoc:



122
123
124
# File 'lib/rubix/sender.rb', line 122

def close
  return
end

#confirm_settingsObject

Check that all settings are correct in order to be able to successfully write data to Zabbix.

Raises:



70
71
72
73
74
75
# File 'lib/rubix/sender.rb', line 70

def confirm_settings
  raise Error.new("Must specify a path to a local configuraiton file")    unless config
  raise Error.new("Must specify the IP of a Zabbix server")               unless server
  raise Error.new("Must specify the port of a Zabbix server")             unless port && port.to_i > 0
  raise Error.new("Must specify a default Zabbix host to write data for") unless host
end

#flushObject

:nodoc:



127
128
129
# File 'lib/rubix/sender.rb', line 127

def flush
  return
end

#puts(text) ⇒ Object

Convenience method for sending a block of text to zabbix_sender.

Parameters:

  • text (String)


112
113
114
115
116
117
118
119
# File 'lib/rubix/sender.rb', line 112

def puts text
  with_sender_subprocess do |stdin, stdout, stderr, wait_thr|
    stdin.write(text)
    stdin.close
    output = [stdout.read.chomp, stderr.read.chomp].join("\n").strip
    debug(output) if output.size > 0
  end
end

#timestamps?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rubix/sender.rb', line 43

def timestamps?
  @timestamps
end

#with_sender_subprocess {|IO, IO, IO, Thread| ... } ⇒ Object

Run a zabbix_sender subprocess in the block.

Yields:

  • (IO, IO, IO, Thread)

    Handle the subprocess.



100
101
102
103
104
105
106
# File 'lib/rubix/sender.rb', line 100

def with_sender_subprocess &block
  begin
    Open3.popen3(zabbix_sender_env, zabbix_sender_command, &block)
  rescue Errno::ENOENT, Errno::EACCES => e
    warn(e.message)
  end
end

#zabbix_sender_commandString

Construct the command that invokes Zabbix sender.

Returns:

  • (String)


91
92
93
94
95
# File 'lib/rubix/sender.rb', line 91

def zabbix_sender_command
  "timeout 3 zabbix_sender --zabbix-server #{server} --host #{host} --port #{port} --config #{config} --real-time --input-file - -vv".tap do |c|
    c += " --with-timestamps" if timestamps?
  end
end

#zabbix_sender_envHash

The environment for the Zabbix sender invocation.

Returns:

  • (Hash)


84
85
86
# File 'lib/rubix/sender.rb', line 84

def zabbix_sender_env
  {}
end