rubix 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.0.2
@@ -1,7 +1,16 @@
1
1
  require 'rubygems'
2
+
2
3
  require 'rubix/log'
4
+ require 'rubix/models'
5
+ require 'rubix/associations'
6
+ require 'rubix/monitors'
7
+
3
8
  module Rubix
4
9
 
10
+ autoload :Connection, 'rubix/connection'
11
+ autoload :Response, 'rubix/response'
12
+ autoload :Sender, 'rubix/sender'
13
+
5
14
  def self.connect server, username=nil, password=nil
6
15
  self.connection = Connection.new(server, username, password)
7
16
  end
@@ -17,22 +26,6 @@ module Rubix
17
26
  @connection
18
27
  end
19
28
 
20
- autoload :Connection, 'rubix/connection'
21
- autoload :Response, 'rubix/response'
22
-
23
- autoload :Model, 'rubix/model'
24
- autoload :HostGroup, 'rubix/models/host_group'
25
- autoload :Template, 'rubix/models/template'
26
- autoload :Host, 'rubix/models/host'
27
- autoload :Item, 'rubix/models/item'
28
- autoload :Application, 'rubix/models/application'
29
-
30
- autoload :Monitor, 'rubix/monitor'
31
- autoload :ChefMonitor, 'rubix/monitors/chef_monitor'
32
- autoload :ClusterMonitor, 'rubix/monitors/cluster_monitor'
33
-
34
- autoload :Sender, 'rubix/sender'
35
-
36
29
  Error = Class.new(RuntimeError)
37
30
  ConnectionError = Class.new(Error)
38
31
  AuthenticationError = Class.new(Error)
@@ -0,0 +1,14 @@
1
+ module Rubix
2
+ module Associations
3
+
4
+ autoload :HasManyHosts, 'rubix/associations/has_many_hosts'
5
+ autoload :HasManyTemplates, 'rubix/associations/has_many_templates'
6
+ autoload :HasManyHostGroups, 'rubix/associations/has_many_host_groups'
7
+ autoload :HasManyUserMacros, 'rubix/associations/has_many_user_macros'
8
+ autoload :HasManyApplications, 'rubix/associations/has_many_applications'
9
+
10
+ autoload :BelongsToHost, 'rubix/associations/belongs_to_host'
11
+
12
+ end
13
+ end
14
+
@@ -0,0 +1,32 @@
1
+ module Rubix
2
+ module Associations
3
+ module BelongsToHost
4
+
5
+ def host= h
6
+ return unless h
7
+ @host = h
8
+ @host_id = h.id
9
+ end
10
+
11
+ def host
12
+ return @host if @host
13
+ return unless @host_id
14
+ @host = Host.find(:id => @host_id)
15
+ end
16
+
17
+ def host_id= hid
18
+ return unless hid
19
+ @host_id = hid
20
+ end
21
+
22
+ def host_id
23
+ return @host_id if @host_id
24
+ return unless @host
25
+ @host_id = @host.id
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+
32
+
@@ -0,0 +1,32 @@
1
+ module Rubix
2
+ module Associations
3
+ module HasManyApplications
4
+
5
+ def applications= hs
6
+ return unless hs
7
+ @applications = hs
8
+ @application_ids = hs.map(&:id)
9
+ end
10
+
11
+ def applications
12
+ return @applications if @applications
13
+ return unless @application_ids
14
+ @applications = @application_ids.map { |aid| Application.find(:id => aid, :host_id => host_id) }
15
+ end
16
+
17
+ def application_ids= aids
18
+ return unless aids
19
+ @application_ids = aids
20
+ end
21
+
22
+ def application_ids
23
+ return @application_ids if @application_ids
24
+ return unless @applications
25
+ @application_ids = @applications.map(&:id)
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+
32
+
@@ -0,0 +1,37 @@
1
+ module Rubix
2
+ module Associations
3
+ module HasManyHostGroups
4
+
5
+ def host_groups= hs
6
+ return unless hs
7
+ @host_groups = hs
8
+ @host_group_ids = hs.map(&:id)
9
+ end
10
+
11
+ def host_groups
12
+ return @host_groups if @host_groups
13
+ return unless @host_group_ids
14
+ @host_groups = @host_group_ids.map { |hgid| HostGroup.find(:id => hgid) }
15
+ end
16
+
17
+ def host_group_ids= hgids
18
+ return unless hgids
19
+ @host_group_ids = hgids
20
+ end
21
+
22
+ def host_group_ids
23
+ return @host_group_ids if @host_group_ids
24
+ return unless @host_groups
25
+ @host_group_ids = @host_groups.map(&:id)
26
+ end
27
+
28
+ def host_group_params
29
+ return [] unless host_group_ids
30
+ host_group_ids.map { |hid| { 'groupid' => hid } }
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+
37
+
@@ -0,0 +1,32 @@
1
+ module Rubix
2
+ module Associations
3
+ module HasManyHosts
4
+
5
+ def hosts= hs
6
+ return unless hs
7
+ @hosts = hs
8
+ @host_ids = hs.map(&:id)
9
+ end
10
+
11
+ def hosts
12
+ return @hosts if @hosts
13
+ return unless @host_ids
14
+ @hosts = @host_ids.map { |hid| Host.find(:id => hid) }
15
+ end
16
+
17
+ def host_ids= hids
18
+ return unless hids
19
+ @host_ids = hids
20
+ end
21
+
22
+ def host_ids
23
+ return @host_ids if @host_ids
24
+ return unless @hosts
25
+ @host_ids = @hosts.map(&:id)
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+
32
+
@@ -0,0 +1,37 @@
1
+ module Rubix
2
+ module Associations
3
+ module HasManyTemplates
4
+
5
+ def templates= hs
6
+ return unless hs
7
+ @templates = hs
8
+ @template_ids = hs.map(&:id)
9
+ end
10
+
11
+ def templates
12
+ return @templates if @templates
13
+ return unless @template_ids
14
+ @templates = @template_ids.map { |tid| Template.find(:id => tid) }
15
+ end
16
+
17
+ def template_ids= tids
18
+ return unless tids
19
+ @template_ids = tids
20
+ end
21
+
22
+ def template_ids
23
+ return @template_ids if @template_ids
24
+ return unless @templates
25
+ @template_ids = @templates.map(&:id)
26
+ end
27
+
28
+ def template_params
29
+ return [] unless template_ids
30
+ template_ids.map { |tid| { 'templateid' => tid } }
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+
37
+
@@ -0,0 +1,37 @@
1
+ module Rubix
2
+ module Associations
3
+ module HasManyUserMacros
4
+
5
+ def user_macros= hs
6
+ return unless hs
7
+ @user_macros = hs
8
+ @user_macro_ids = hs.map(&:id)
9
+ end
10
+
11
+ def user_macros
12
+ return @user_macros if @user_macros
13
+ return unless @user_macro_ids
14
+ @user_macros = @user_macro_ids.map { |umid| UserMacro.find(:id => umid) }
15
+ end
16
+
17
+ def user_macro_ids= umids
18
+ return unless umids
19
+ @user_macro_ids = umids
20
+ end
21
+
22
+ def user_macro_ids
23
+ return @user_macro_ids if @user_macro_ids
24
+ return unless @user_macros
25
+ @user_macro_ids = @user_macros.map(&:id)
26
+ end
27
+
28
+ def user_macro_params
29
+ return [] unless user_macros
30
+ user_macros.map { |um| { 'macro' => um.macro_name, 'value' => um.value } }
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+
37
+
@@ -12,29 +12,42 @@ module Rubix
12
12
  end
13
13
 
14
14
  def self.default_logger
15
- severity = Logger::INFO
16
- file = $stdout
17
-
18
- if defined?(Settings) && Settings[:log_level]
19
- begin
20
- severity_name = Settings[:log_level].to_s.upcase
21
- severity = Logger.const_get(severity_name)
22
- rescue NameError => e
23
- end
24
- end
15
+ @logger = Logger.new(default_log_path)
16
+ @logger.level = default_log_severity
17
+ p default_log_severity
18
+ @logger
19
+ end
25
20
 
26
- if defined?(Settings) && Settings[:log]
27
- begin
28
- file = Settings[:log]
29
- rescue NameError => e
30
- end
21
+ def self.default_log_severity
22
+ case
23
+ when defined?(Settings) && Settings[:log_level]
24
+ Logger.const_get(Settings[:log_level].to_s.strip)
25
+ when ENV["RUBIX_LOG_LEVEL"]
26
+ severity_name = ENV["RUBIX_LOG_LEVEL"].to_s.strip
27
+ else
28
+ severity_name = 'info'
31
29
  end
32
30
 
33
- @logger = Logger.new(file)
34
- @logger.level = severity
35
- @logger
31
+ begin
32
+ return Logger.const_get(severity_name.upcase)
33
+ rescue NameError => e
34
+ return Logger::INFO
35
+ end
36
36
  end
37
37
 
38
+ def self.default_log_path
39
+ case
40
+ when defined?(Settings) && Settings[:log]
41
+ Settings[:log]
42
+ when ENV["RUBIX_LOG_PATH"] == '-'
43
+ $stdout
44
+ when ENV["RUBIX_LOG_PATH"]
45
+ ENV["RUBIX_LOG_PATH"]
46
+ else
47
+ $stdout
48
+ end
49
+ end
50
+
38
51
  module Logs
39
52
 
40
53
  def log_name
@@ -0,0 +1,9 @@
1
+ module Rubix
2
+ autoload :Model, 'rubix/models/model'
3
+ autoload :HostGroup, 'rubix/models/host_group'
4
+ autoload :Template, 'rubix/models/template'
5
+ autoload :Host, 'rubix/models/host'
6
+ autoload :Item, 'rubix/models/item'
7
+ autoload :Application, 'rubix/models/application'
8
+ autoload :UserMacro, 'rubix/models/user_macro'
9
+ end
@@ -2,74 +2,68 @@ module Rubix
2
2
 
3
3
  class Application < Model
4
4
 
5
- attr_accessor :name, :host
5
+ #
6
+ # == Properties & Finding ==
7
+ #
6
8
 
7
9
  def initialize properties={}
8
10
  super(properties)
9
- @name = properties[:name]
10
- @host = properties[:host]
11
+ @name = properties[:name]
12
+
13
+ self.host_id = properties[:host_id]
14
+ self.host = properties[:host]
11
15
  end
16
+
17
+ attr_accessor :name
12
18
 
13
- def params
14
- {
15
- :name => name,
16
- :hostid => host.id
17
- }
19
+ def self.find_request options={}
20
+ request('application.get', 'hostids' => [options[:host_id]], 'filter' => {'name' => options[:name], 'applicationid' => options[:id]}, "output" => "extend")
18
21
  end
19
22
 
20
- def log_name
21
- "APP #{name || id}@#{host.name}"
22
- end
23
+ def self.build app
24
+ params = {
25
+ :id => app['applicationid'].to_i,
26
+ :name => app['name']
27
+ }
23
28
 
24
- def load
25
- response = request('application.get', 'hostids' => [host.id], 'filter' => {'name' => name, 'id' => id}, "output" => "extend")
26
- case
27
- when response.has_data?
28
- app = response.first
29
- @id = app['applicationid'].to_i
30
- @name = app['name']
31
- @exists = true
32
- @loaded = true
33
- when response.success?
34
- @exists = false
35
- @loaded = true
29
+ # use the host id if available, else use the template id
30
+ if app['hosts'] && app['hosts'].first && app['hosts'].first['hostid']
31
+ params[:host_id] = app['hosts'].first['hostid'].to_i
36
32
  else
37
- error("Could not load: #{response.error_message}")
33
+ params[:host_id] = app['templateid']
38
34
  end
35
+ new(params)
36
+ end
37
+
38
+ def log_name
39
+ "APP #{name || id}@#{host.name}"
39
40
  end
40
41
 
41
- def create
42
- response = request('application.create', params)
43
- if response.has_data?
44
- @id = response['applicationids'].first.to_i
45
- @exists = true
46
- info("Created")
47
- else
48
- error("Could not create: #{response.error_message}")
49
- end
42
+ def self.id_field
43
+ 'applicationid'
50
44
  end
45
+
46
+
47
+ #
48
+ # == Associations ==
49
+ #
51
50
 
52
- def update
53
- # noop
54
- info("Updated")
51
+ include Associations::BelongsToHost
52
+
53
+ #
54
+ # == CRUD ==
55
+ #
56
+
57
+ def create_request
58
+ request('application.create', 'name' => name, 'hostid' => host_id)
55
59
  end
56
60
 
57
- def destroy
58
- response = request('application.delete', [id])
59
- case
60
- when response.has_data? && response['applicationids'].first.to_i == id
61
- info("Deleted")
62
- when response.zabbix_error? && response.error_message =~ /does not exist/i
63
- # was never there...
64
- else
65
- error("Could not delete: #{response.error_message}.")
66
- end
61
+ def update_request
62
+ request('application.update', 'applicationid' => id, 'name' => name, 'hosts' => {'hostid' => host_id})
67
63
  end
68
64
 
69
- def self.find_or_create_by_name_and_host name, host
70
- new(:name => name, :host => host).tap do |app|
71
- app.create unless app.exists?
72
- end
65
+ def destroy_request
66
+ request('application.delete', [id])
73
67
  end
74
68
 
75
69
  end