Module: Rubix

Defined in:
lib/rubix.rb,
lib/rubix/log.rb,
lib/rubix/models.rb,
lib/rubix/sender.rb,
lib/rubix/monitors.rb,
lib/rubix/response.rb,
lib/rubix/connection.rb,
lib/rubix/auto_sender.rb,
lib/rubix/models/host.rb,
lib/rubix/models/item.rb,
lib/rubix/models/user.rb,
lib/rubix/associations.rb,
lib/rubix/models/model.rb,
lib/rubix/models/action.rb,
lib/rubix/models/medium.rb,
lib/rubix/models/script.rb,
lib/rubix/models/trigger.rb,
lib/rubix/models/template.rb,
lib/rubix/models/condition.rb,
lib/rubix/models/operation.rb,
lib/rubix/monitors/monitor.rb,
lib/rubix/models/host_group.rb,
lib/rubix/models/media_type.rb,
lib/rubix/models/user_group.rb,
lib/rubix/models/user_macro.rb,
lib/rubix/models/application.rb,
lib/rubix/models/time_series.rb,
lib/rubix/monitors/chef_monitor.rb,
lib/rubix/monitors/zabbix_monitor.rb,
lib/rubix/monitors/cluster_monitor.rb,
lib/rubix/associations/has_many_hosts.rb,
lib/rubix/associations/has_many_items.rb,
lib/rubix/associations/has_many_users.rb,
lib/rubix/associations/belongs_to_host.rb,
lib/rubix/associations/belongs_to_item.rb,
lib/rubix/associations/belongs_to_user.rb,
lib/rubix/associations/belongs_to_action.rb,
lib/rubix/associations/has_many_templates.rb,
lib/rubix/associations/belongs_to_template.rb,
lib/rubix/associations/has_many_conditions.rb,
lib/rubix/associations/has_many_host_groups.rb,
lib/rubix/associations/has_many_user_groups.rb,
lib/rubix/associations/has_many_user_macros.rb,
lib/rubix/associations/belongs_to_media_type.rb,
lib/rubix/associations/belongs_to_user_group.rb,
lib/rubix/associations/has_many_applications.rb

Defined Under Namespace

Modules: Associations, ChefMonitor, ClusterMonitor, Logs, ZabbixMonitor Classes: Action, Application, AutoSender, Condition, Connection, Host, HostGroup, Item, MediaType, Medium, Model, Monitor, Operation, Response, Script, Sender, Template, TimeSeries, Trigger, User, UserGroup, UserMacro

Constant Summary collapse

Error =

Base class for Rubix errors.

Class.new(RuntimeError)
ConnectionError =

Errors with connecting to a Zabbix API.

Class.new(Error)
AuthenticationError =

Error authenticating with a Zabbix API.

Class.new(Error)
RequestError =

Error in a request to a Zabbix API.

Class.new(Error)
ValidationError =

Error detected locally in an API resource that will prevent it from being saved by the Zabbix API (i.e. - no host group for a host).

Class.new(Error)
ArgumentError =

Given an incorrect argument.

Class.new(Error)

Class Method Summary collapse

Class Method Details

.authorized?true, false

Is Rubix presently connected and authorized with a Zabbix server?

Returns:

  • (true, false)


55
56
57
# File 'lib/rubix.rb', line 55

def self.authorized?
  connection && connection.authorized?
end

.connect(server, username = nil, password = nil) ⇒ Rubix::Connection

Set up a Connection to a Zabbix API server.

Only needs to be called once.

# These are the defaults
Rubix.connect 'localhost', 'admin', 'zabbix'

# A server running on a custom port with different
# credentials...
Rubix.connect 'my.server.com:8080', 'foobar', 'bazbooz'

Parameters:

  • server (URI, String)

    the address of the Zabbix API server to connect to

  • username (String) (defaults to: nil)

    the username of an existing Zabbix API User account with API access

  • password (String) (defaults to: nil)

    the password for this account

Returns:



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

def self.connect server, username=nil, password=nil
  self.connection = Connection.new(server, username, password)
end

.connected?true, false

Is Rubix presently connected to a Zabbix server?

Returns:

  • (true, false)


48
49
50
# File 'lib/rubix.rb', line 48

def self.connected?
  @connection && @connection.authorized?
end

.connectionRubix::Connection

Return the current connection to a Zabbix API. Useful for directly sending queries.

Rubix.connection.request 'host.get', :filter => { "name" => "foobar" }

Returns:

Raises:



65
66
67
68
69
70
# File 'lib/rubix.rb', line 65

def self.connection
  @connection ||= Connection.new('http://localhost/api_jsonrpc.php', 'admin', 'zabbix')
  return @connection if @connection.authorized?
  raise ConnectionError.new("Could not authorize with Zabbix API at #{@connection.uri}") unless @connection.authorize!
  @connection
end

.connection=(connection) ⇒ Rubix::Connection

Explicitly set the connection using a Rubix::Connection object.

Rubix.connection = Rubix::Connection.new('http://localhost/api_jsonrpc.php', 'admin', 'zabbix')

Parameters:

Returns:



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

def self.connection= connection
  @connection = connection
end

.default_log_pathObject

The default logger’s path.

Will attempt to read from

  • Settings[:log] if Settings is defined (see Configliere)

  • the RUBIX_LOG_PATH environment variable if defined

Defaults to writing stdout.



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubix/log.rb', line 64

def self.default_log_path
  case
  when defined?(Settings) && Settings[:log]
    Settings[:log]
  when ENV["RUBIX_LOG_PATH"] == '-'
    $stdout
  when ENV["RUBIX_LOG_PATH"]
    ENV["RUBIX_LOG_PATH"]
  else
    $stdout
  end
end

.default_log_severityFixnum

The default logger’s severity.

Will attempt to read from

  • Settings[:log_level] if Settings is defined (see Configliere)

  • the RUBIX_LOG_LEVEL environment variable if defined

The default is ‘info’.

Returns:

  • (Fixnum)


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rubix/log.rb', line 39

def self.default_log_severity
  case
  when defined?(Settings) && Settings[:log_level]
    Logger.const_get(Settings[:log_level].to_s.strip)
  when ENV["RUBIX_LOG_LEVEL"]
    severity_name = ENV["RUBIX_LOG_LEVEL"].to_s.strip
  else
    severity_name = 'info'
  end
  
  begin
    return Logger.const_get(severity_name.upcase)
  rescue NameError => e
    return Logger::INFO
  end
end

.default_loggerLogger

The default logger.

Returns:

  • (Logger)


23
24
25
26
27
# File 'lib/rubix/log.rb', line 23

def self.default_logger
  @logger       = Logger.new(default_log_path)
  @logger.level = default_log_severity
  @logger
end

.loggerLogger?

The current Rubix logger.

Returns:

  • (Logger, nil)


15
16
17
18
# File 'lib/rubix/log.rb', line 15

def self.logger
  return @logger unless @logger.nil?
  @logger = default_logger
end

.logger=(l) ⇒ Object

Set the Rubix logger. Set to nil to disable all logging.

Parameters:

  • l (Logger)

    the logger to use



8
9
10
# File 'lib/rubix/log.rb', line 8

def self.logger= l
  @logger = l
end

.versionString

Return the current Rubix version.

Returns:

  • (String)


75
76
77
78
79
80
81
# File 'lib/rubix.rb', line 75

def self.version
  @version ||= begin
    File.read(File.expand_path('../../VERSION', __FILE__)).chomp
  rescue => e
    'unknown'
  end    
end