Class: Rubix::Sender
Overview
A class used to send data to Zabbix.
This sender is used to wrap zabbix_sender
.
Instance Attribute Summary collapse
- #config ⇒ Object
-
#host ⇒ String, Rubix::Hosts
The Zabbix host name or Rubix::Host the sender will use by default.
- #port ⇒ Object
- #server ⇒ Object
-
#timestamps ⇒ Object
writeonly
Whether or not to include timestamps with the data.
Instance Method Summary collapse
-
#close ⇒ Object
:nodoc:.
-
#confirm_settings ⇒ Object
Check that all settings are correct in order to be able to successfully write data to Zabbix.
-
#flush ⇒ Object
:nodoc:.
-
#initialize(settings = {}) ⇒ Sender
constructor
Create a new sender with the given
settings
. -
#puts(text) ⇒ Object
Convenience method for sending a block of text to
zabbix_sender
. - #timestamps? ⇒ Boolean
-
#with_sender_subprocess {|IO, IO, IO, Thread| ... } ⇒ Object
Run a
zabbix_sender
subprocess in the block. -
#zabbix_sender_command ⇒ String
Construct the command that invokes Zabbix sender.
-
#zabbix_sender_env ⇒ Hash
The environment for the Zabbix sender invocation.
Methods included from Logs
#debug, #error, #fatal, #info, #warn
Constructor Details
#initialize(settings = {}) ⇒ Sender
Create a new sender with the given settings
.
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. = settings[:timestamps] confirm_settings end |
Instance Attribute Details
#config ⇒ Object
37 38 39 |
# File 'lib/rubix/sender.rb', line 37 def config @config ||= '/etc/zabbix/zabbix_agentd.conf' end |
#host ⇒ String, Rubix::Hosts
Returns 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 |
#port ⇒ Object
31 32 33 |
# File 'lib/rubix/sender.rb', line 31 def port @port ||= 10051 end |
#server ⇒ Object
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 (value) @timestamps = value end |
Instance Method Details
#close ⇒ Object
:nodoc:
122 123 124 |
# File 'lib/rubix/sender.rb', line 122 def close return end |
#confirm_settings ⇒ Object
Check that all settings are correct in order to be able to successfully write data to Zabbix.
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 |
#flush ⇒ Object
: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
.
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
43 44 45 |
# File 'lib/rubix/sender.rb', line 43 def @timestamps end |
#with_sender_subprocess {|IO, IO, IO, Thread| ... } ⇒ Object
Run a zabbix_sender
subprocess in the block.
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.) end end |
#zabbix_sender_command ⇒ String
Construct the command that invokes Zabbix sender.
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 end end |
#zabbix_sender_env ⇒ Hash
The environment for the Zabbix sender invocation.
84 85 86 |
# File 'lib/rubix/sender.rb', line 84 def zabbix_sender_env {} end |