Class: Rubix::AutoSender

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

Overview

A class used to send data to Zabbix.

This sender is used to implement the logic for the zabbix_pipe utility. It is initialized with some metadata about a host, its host groups and templates, and applications into which items should be written, and it can then accept data and forward it to a Zabbix server using the zabbix_sender utility that comes with Zabbix.

A sender can be given data in either TSV or JSON formats. With the JSON format, it is possible to embed data for hosts, host groups, &c. distinct from that with which this sender was initialized. This is a useful way to send many different kinds of data through the same process.

The sender will also auto-vivify any hosts, host gruops, templates, applications, and items it needs in order to be able to write data. This is expensive in terms of time so it can be turned off using the --fast option.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logs

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

Constructor Details

#initialize(settings) ⇒ AutoSender

Create a new sender with the given settings.

Parameters:

  • settings (Hash, Configliere::Param)
  • settings (String)

    host the name of the Zabbix host to write data for

  • settings (String)

    host_groups comma-separated names of Zabbix host groups the host should belong to

  • settings (String)

    templates comma-separated names of Zabbix templates the host should belong to

  • settings (String)

    applications comma-separated names of applications created items should be scoped under

  • settings (String)

    server URL for the Zabbix server – not the URL for the Zabbix API

  • settings (String)

    configuration_file path to a local Zabbix configuration file as used by the zabbix_sender utility

  • settings (true, false)

    verbose be verbose during execution

  • settings (true, false)

    fast auto-vivify (slow) or not (fast)

  • settings (String)

    pipe path to a named pipe to be read from

  • settings (Fixnum)

    pipe_read_sleep seconds to sleep after an empty read from the a named pipe

  • settings (Fixnum)

    create_item_sleep seconds to sleep after creating a new item



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubix/auto_sender.rb', line 61

def initialize settings
  @settings = settings
  confirm_settings
  if fast?
    info("Forwarding for #{settings['host']}...") if settings['verbose']
  else
    initialize_host_groups
    initialize_templates
    initialize_host
    initialize_applications
    info("Forwarding for #{host.name}...") if settings['verbose']
  end
end

Instance Attribute Details

#applicationsArray<Rubix::Application>

Returns The applications used to create items.

Returns:



41
42
43
# File 'lib/rubix/auto_sender.rb', line 41

def applications
  @applications
end

#hostRubix::Host

Returns the host the Sender will send data for.

Returns:

  • (Rubix::Host)

    the host the Sender will send data for



32
33
34
# File 'lib/rubix/auto_sender.rb', line 32

def host
  @host
end

#host_groupsArray<Rubix::HostGroup>

Returns the hostgroups used to create this host.

Returns:



35
36
37
# File 'lib/rubix/auto_sender.rb', line 35

def host_groups
  @host_groups
end

#settingsHash

Returns settings.

Returns:

  • (Hash)

    settings



29
30
31
# File 'lib/rubix/auto_sender.rb', line 29

def settings
  @settings
end

#templatesArray<Rubix::Template>

Returns the templates used to create this host.

Returns:



38
39
40
# File 'lib/rubix/auto_sender.rb', line 38

def templates
  @templates
end

Instance Method Details

#auto_vivify?true, false

Will this sender auto-vivify hosts, groups, items, &c.?

Returns:

  • (true, false)


86
87
88
# File 'lib/rubix/auto_sender.rb', line 86

def auto_vivify?
  !fast?
end

#fast?true, false

Is this sender running in ‘fast’ mode? If so, it will not auto-vivify any hosts, groups, items, &c.

Returns:

  • (true, false)


79
80
81
# File 'lib/rubix/auto_sender.rb', line 79

def fast?
  settings['fast']
end

#process_line(line) ⇒ Object

Process a single line of text.

Parameters:

  • line (String)


250
251
252
253
254
255
256
# File 'lib/rubix/auto_sender.rb', line 250

def process_line line
  if looks_like_json?(line)
    process_line_of_json_in_new_pipe(line)
  else
    process_line_of_tsv_in_this_pipe(line)
  end
end

#runObject

Run this sender.

Will read from the correct source of data and exit the Ruby process once the source is consumed.



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rubix/auto_sender.rb', line 173

def run
  case
  when settings['pipe']
    process_pipe
  when settings.rest.size > 0
    settings.rest.each do |path|
      process_file(path)
    end
  else
    process_stdin
  end
  exit(0)
end