rubix 0.0.9 → 0.0.10

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.9
1
+ 0.0.10
@@ -6,8 +6,10 @@ module Rubix
6
6
  autoload :HasManyHostGroups, 'rubix/associations/has_many_host_groups'
7
7
  autoload :HasManyUserMacros, 'rubix/associations/has_many_user_macros'
8
8
  autoload :HasManyApplications, 'rubix/associations/has_many_applications'
9
+ autoload :HasManyItems, 'rubix/associations/has_many_items'
9
10
 
10
- autoload :BelongsToHost, 'rubix/associations/belongs_to_host'
11
+ autoload :BelongsToHost, 'rubix/associations/belongs_to_host'
12
+ autoload :BelongsToTemplate, 'rubix/associations/belongs_to_template'
11
13
 
12
14
  end
13
15
  end
@@ -0,0 +1,33 @@
1
+ module Rubix
2
+ module Associations
3
+
4
+ module BelongsToTemplate
5
+
6
+ def template= h
7
+ return unless h
8
+ @template = h
9
+ @template_id = h.id
10
+ end
11
+
12
+ def template
13
+ return @template if @template
14
+ return unless @template_id
15
+ @template = Template.find(:id => @template_id)
16
+ end
17
+
18
+ def template_id= tid
19
+ return unless tid
20
+ @template_id = tid
21
+ end
22
+
23
+ def template_id
24
+ return @template_id if @template_id
25
+ return unless @template
26
+ @template_id = @template.id
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+
33
+
@@ -0,0 +1,30 @@
1
+ module Rubix
2
+ module Associations
3
+ module HasManyItems
4
+
5
+ def items= is
6
+ return unless is
7
+ @items = is
8
+ @item_ids = is.map(&:id)
9
+ end
10
+
11
+ def items
12
+ return @items if @items
13
+ return unless @item_ids
14
+ @items = @item_ids.map { |iid| Item.find(:id => iid, :host_id => (host_id || template_id)) }
15
+ end
16
+
17
+ def item_ids= iids
18
+ return unless iids
19
+ @item_ids = iids
20
+ end
21
+
22
+ def item_ids
23
+ return @item_ids if @item_ids
24
+ return unless @items
25
+ @item_ids = @items.map(&:id)
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -6,4 +6,5 @@ module Rubix
6
6
  autoload :Item, 'rubix/models/item'
7
7
  autoload :Application, 'rubix/models/application'
8
8
  autoload :UserMacro, 'rubix/models/user_macro'
9
+ autoload :Trigger, 'rubix/models/trigger'
9
10
  end
@@ -6,10 +6,29 @@ module Rubix
6
6
  # == Properties & Finding ==
7
7
  #
8
8
 
9
- # The numeric code for a Zabbix item of type 'Zabbix trapper'. The
10
- # item must have this type in order for the Zabbix server to listen
11
- # and accept data submitted by +zabbix_sender+.
12
- TRAPPER_TYPE = 2.freeze
9
+ # The numeric codes for the various item types.
10
+ #
11
+ # Items without a type will be set to 'trapper' so they can be
12
+ # easily written to manually.
13
+ TYPE_CODES = {
14
+ :zabbix => 0,
15
+ :snmpv1 => 1,
16
+ :trapper => 2,
17
+ :simple => 3,
18
+ :snmpv2c => 4,
19
+ :internal => 5,
20
+ :snmpv3 => 6,
21
+ :active => 7,
22
+ :aggregate => 8,
23
+ :httptest => 9,
24
+ :external => 10,
25
+ :db_monitor => 11,
26
+ :ipmi => 12,
27
+ :ssh => 13,
28
+ :telnet => 14,
29
+ :calculated => 15
30
+ }.freeze
31
+ TYPE_NAMES = TYPE_CODES.invert.freeze
13
32
 
14
33
  # The numeric codes for the value types of a Zabbix item. This Hash
15
34
  # is used by ZabbixPipe#value_code_from_value to dynamically set the
@@ -21,14 +40,7 @@ module Rubix
21
40
  :unsigned_int => 3, # Numeric (unsigned)
22
41
  :text => 4 # Text
23
42
  }.freeze
24
-
25
- VALUE_NAMES = {
26
- 0 => :float, # Numeric (float)
27
- 1 => :character, # Character
28
- 2 => :log_line, # Log
29
- 3 => :unsigned_int, # Numeric (unsigned)
30
- 4 => :text # Text
31
- }.freeze
43
+ VALUE_NAMES = VALUE_CODES.invert.freeze
32
44
 
33
45
  # Return the +value_type+ name (:float, :text, &c.) for a Zabbix
34
46
  # item's value type by examining the given +value+.
@@ -48,17 +60,22 @@ module Rubix
48
60
  end
49
61
 
50
62
  attr_accessor :key, :description
51
- attr_reader :value_type
63
+ attr_writer :type, :value_type
52
64
 
53
65
  def initialize properties={}
54
66
  super(properties)
55
67
  @key = properties[:key]
56
68
  @description = properties[:description]
69
+ @type = properties[:type]
57
70
 
58
71
  self.value_type = properties[:value_type]
59
72
 
60
73
  self.host = properties[:host]
61
74
  self.host_id = properties[:host_id]
75
+
76
+ self.template = properties[:template]
77
+ self.template_id = properties[:template_id]
78
+
62
79
  self.applications = properties[:applications]
63
80
  self.application_ids = properties[:application_ids]
64
81
  end
@@ -67,12 +84,12 @@ module Rubix
67
84
  "#{self.class.resource_name} #{self.key || self.id}"
68
85
  end
69
86
 
70
- def value_type= vt
71
- if vt.nil? || vt.to_s.empty? || !self.class::VALUE_CODES.include?(vt.to_sym)
72
- @value_type = :character
73
- else
74
- @value_type = vt.to_sym
75
- end
87
+ def value_type
88
+ @value_type ||= :character
89
+ end
90
+
91
+ def type
92
+ @type ||= :trapper
76
93
  end
77
94
 
78
95
  #
@@ -80,6 +97,7 @@ module Rubix
80
97
  #
81
98
 
82
99
  include Associations::BelongsToHost
100
+ include Associations::BelongsToTemplate
83
101
  include Associations::HasManyApplications
84
102
 
85
103
  #
@@ -90,7 +108,7 @@ module Rubix
90
108
  {
91
109
  :hostid => host_id,
92
110
  :description => (description || 'Unknown'),
93
- :type => self.class::TRAPPER_TYPE,
111
+ :type => self.class::TYPE_CODES[type],
94
112
  :key_ => key,
95
113
  :value_type => self.class::VALUE_CODES[value_type],
96
114
  }.tap do |p|
@@ -117,7 +135,8 @@ module Rubix
117
135
  :id => item[id_field].to_i,
118
136
  :host_id => item['hostid'].to_i,
119
137
  :description => item['description'],
120
- :value_type => item['value_type'] ? self::VALUE_NAMES[item['value_type'].to_i] : :character,
138
+ :type => TYPE_NAMES[item['type'].to_i],
139
+ :value_type => VALUE_NAMES[item['value_type'].to_i],
121
140
  :application_ids => (item['applications'] || []).map { |app| app['applicationid'].to_i },
122
141
  :key => item['key_']
123
142
  })
@@ -0,0 +1,133 @@
1
+ module Rubix
2
+
3
+ class Trigger < Model
4
+
5
+ #
6
+ # == Properties & Finding ==
7
+ #
8
+
9
+ PRIORITY_NAMES = {
10
+ :not_classified => 0,
11
+ :information => 1,
12
+ :warning => 2,
13
+ :average => 3,
14
+ :high => 4,
15
+ :disaster => 5
16
+ }.freeze
17
+ PRIORITY_CODES = PRIORITY_NAMES.invert.freeze
18
+
19
+ STATUS_NAMES = {
20
+ :enabled => 0,
21
+ :disabled => 1
22
+ }.freeze
23
+ STATUS_CODES = STATUS_NAMES.invert.freeze
24
+
25
+ attr_accessor :description, :url, :status, :priority, :comments
26
+ attr_reader :expression
27
+
28
+ def initialize properties={}
29
+ super(properties)
30
+ @description = properties[:description]
31
+ @url = properties[:url]
32
+ @status = properties[:status]
33
+ @priority = properties[:priority]
34
+ @comments = properties[:comments]
35
+
36
+ self.expression = properties[:expression]
37
+
38
+ self.host = properties[:host]
39
+ self.template = properties[:template]
40
+
41
+ self.template_id = properties[:template_id]
42
+ self.host_id = properties[:host_id]
43
+
44
+ self.items = properties[:items]
45
+ self.item_ids = properties[:item_ids]
46
+ end
47
+
48
+ def expression= e
49
+ if e =~ %r!^\{(.*)\}!
50
+ trigger_condition = $1
51
+ if trigger_condition && trigger_condition =~ /:/
52
+ host_or_template_name = trigger_condition.split(':').first
53
+ host_or_template = Template.find(:name => host_or_template_name) || Host.find(:name => host_or_template_name)
54
+ case host_or_template
55
+ when Host then self.host = host_or_template
56
+ when Template then self.template = host_or_template
57
+ end
58
+ end
59
+ end
60
+ @expression = e
61
+ end
62
+
63
+ #
64
+ # == Associations ==
65
+ #
66
+
67
+ include Associations::BelongsToHost
68
+ include Associations::BelongsToTemplate
69
+ include Associations::HasManyItems
70
+
71
+ #
72
+ # == Requests ==
73
+ #
74
+
75
+ def create_params
76
+ {
77
+ :templateid => (template_id || host_id),
78
+ :description => (description || 'Unknown'),
79
+ :expression => expression,
80
+ :priority => self.class::PRIORITY_CODES[priority],
81
+ :status => self.class::STATUS_CODES[status],
82
+ :comments => comments,
83
+ :url => url
84
+ }
85
+ end
86
+
87
+ def self.get_params
88
+ super().merge(:select_items => :refer)
89
+ end
90
+
91
+ def self.find_params options={}
92
+ fp = {
93
+ :filter => {
94
+ :description => options[:description]
95
+ }
96
+ }.tap do |fp|
97
+ case
98
+ when options[:template_id]
99
+ fp[:templateids] = [options[:template_id]]
100
+ when options[:host_id]
101
+ fp[:hostids] = [options[:host_id]]
102
+ end
103
+ end
104
+ super().merge(fp)
105
+ end
106
+
107
+ def self.build trigger
108
+ new({
109
+ :id => trigger[id_field].to_i,
110
+ :description => trigger['description'],
111
+ :expression => trigger['expression'],
112
+ :comments => trigger['comments'],
113
+ :url => trigger['url'],
114
+ :status => STATUS_CODES[trigger['status'].to_i],
115
+ :priority => PRIORITY_CODES[trigger['priority'].to_i],
116
+ :item_ids => (trigger['items'] || []).map { |item| item['itemid'].to_i }
117
+ }.merge(host_or_template_params_from_id(trigger['templateid'].to_i)))
118
+ end
119
+
120
+ def self.host_or_template_params_from_id id
121
+ template_or_host = Template.find(:id => id) || Host.find(:id => id)
122
+ case template_or_host
123
+ when Template
124
+ { :template => template_or_host }
125
+ when Host
126
+ { :host => template_or_host }
127
+ else
128
+ {}
129
+ end
130
+ end
131
+
132
+ end
133
+ end
@@ -39,6 +39,7 @@ describe "Items" do
39
39
  it "can have its host, application, and properties updated" do
40
40
  @item.key = 'rubix.spec2'
41
41
  @item.description = 'rubix item description 2'
42
+ @item.type = :external
42
43
  @item.value_type = :unsigned_int
43
44
  @item.host_id = @host_2.id
44
45
  @item.applications = [@app_2]
@@ -48,6 +49,8 @@ describe "Items" do
48
49
 
49
50
  new_item = Rubix::Item.find(:key => 'rubix.spec2', :host_id => @host_2.id)
50
51
  new_item.should_not be_nil
52
+ new_item.value_type.should == :unsigned_int
53
+ new_item.type.should == :external
51
54
  new_item.host.name.should == @host_2.name
52
55
  new_item.applications.map(&:name).should include(@app_2.name)
53
56
  end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Triggers" do
4
+
5
+ before do
6
+ integration_test
7
+ @host_group = ensure_save(Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1'))
8
+
9
+ @template_1 = ensure_save(Rubix::Template.new(:name => 'rubix_spec_template_1', :host_groups => [@host_group]))
10
+ @template_2 = ensure_save(Rubix::Template.new(:name => 'rubix_spec_template_2', :host_groups => [@host_group]))
11
+ @template_item_1 = ensure_save(Rubix::Item.new(:key => 'rubix.spec1', :description => 'rubix template item description 1', :host_id => @template_1.id, :value_type => :character))
12
+ @template_item_2 = ensure_save(Rubix::Item.new(:key => 'rubix.spec2', :description => 'rubix template item description 2', :host_id => @template_2.id, :value_type => :character))
13
+
14
+ @host_1 = ensure_save(Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@host_group]))
15
+ @host_2 = ensure_save(Rubix::Host.new(:name => 'rubix_spec_host_2', :host_groups => [@host_group]))
16
+ @host_item_1 = ensure_save(Rubix::Item.new(:key => 'rubix.spec1', :description => 'rubix host item description 1', :host_id => @host_1.id, :value_type => :character))
17
+ @host_item_2 = ensure_save(Rubix::Item.new(:key => 'rubix.spec2', :description => 'rubix host item description 2', :host_id => @host_2.id, :value_type => :character))
18
+ end
19
+
20
+ after do
21
+ truncate_all_tables
22
+ end
23
+
24
+ describe "when not existing" do
25
+
26
+ it "returns nil on find" do
27
+ Rubix::Trigger.find(:template_id => @template_1.id, :description => 'rubix template trigger description 1').should be_nil
28
+ Rubix::Trigger.find(:host_id => @host_1.id, :description => 'rubix host trigger description 1').should be_nil
29
+ end
30
+
31
+ it "can be created for a template" do
32
+ trigger = Rubix::Trigger.new(:expression => '{rubix_spec_template_1:rubix.spec1.count(120,1)}>1', :description => 'rubix template trigger description 1', :status => :enabled)
33
+ trigger.save.should be_true
34
+ trigger.template.name.should == @template_1.name
35
+ end
36
+
37
+ it "can be created for a host" do
38
+ trigger = Rubix::Trigger.new(:expression => '{rubix_spec_host_1:rubix.spec1.count(120,1)}>1', :description => 'rubix host trigger description 1', :status => :enabled)
39
+ trigger.save.should be_true
40
+ trigger.host.name.should == @host_1.name
41
+ end
42
+
43
+ end
44
+
45
+ describe "when existing on a template" do
46
+
47
+ before do
48
+ @trigger = ensure_save(Rubix::Trigger.new(:expression => '{rubix_spec_template_1:rubix.spec1.count(120,1)}>1', :description => 'rubix template trigger description 1', :status => :enabled))
49
+ end
50
+
51
+ it "can have its host, description, and status updated" do
52
+ @trigger.status = :disabled
53
+ @trigger.description = 'rubix template trigger description 2'
54
+ # @trigger.host_id = @template_2.id
55
+ @trigger.save.should be_true
56
+
57
+ Rubix::Trigger.find(:description => 'rubix template trigger description 1', :host_id => @template_1.id).should be_nil
58
+
59
+ new_trigger = Rubix::Trigger.find(:description => 'rubix template trigger description 2', :template_id => @template_1.id)
60
+ new_trigger.should_not be_nil
61
+ new_trigger.template.name.should == @template_1.name
62
+ new_trigger.items.map(&:id).should include(@template_item_1.id)
63
+ new_trigger.status.should == :disabled
64
+ end
65
+
66
+ it "can be destroyed" do
67
+ @trigger.destroy
68
+ Rubix::Trigger.find(:description => 'rubix template trigger description 1', :template_id => @template_1.id).should be_nil
69
+ end
70
+ end
71
+
72
+ describe "when existing on a host" do
73
+
74
+ before do
75
+ @trigger = ensure_save(Rubix::Trigger.new(:expression => '{rubix_spec_host_1:rubix.spec1.count(120,1)}>1', :description => 'rubix host trigger description 1', :status => :enabled))
76
+ end
77
+
78
+ it "can have its host, description, and status updated" do
79
+ @trigger.status = :disabled
80
+ @trigger.description = 'rubix host trigger description 2'
81
+ # @trigger.host_id = @host_2.id
82
+ @trigger.save.should be_true
83
+
84
+ Rubix::Trigger.find(:description => 'rubix host trigger description 1', :host_id => @host_1.id).should be_nil
85
+
86
+ new_trigger = Rubix::Trigger.find(:description => 'rubix host trigger description 2', :host_id => @host_1.id)
87
+ new_trigger.should_not be_nil
88
+ new_trigger.host.name.should == @host_1.name
89
+ new_trigger.items.map(&:id).should include(@host_item_1.id)
90
+ end
91
+
92
+ it "can be destroyed" do
93
+ @trigger.destroy
94
+ Rubix::Trigger.find(:description => 'rubix host trigger description 1', :host_id => @host_1.id).should be_nil
95
+ end
96
+ end
97
+
98
+ end
99
+
@@ -64,7 +64,7 @@ module Rubix
64
64
  $RUBIX_INTEGRATION_TEST = true
65
65
  end
66
66
 
67
- RUBIX_TABLES_TO_TRUNCATE = %w[applications groups hostmacro hosts hosts_groups hosts_profiles hosts_profiles_ext hosts_templates items items_applications profiles triggers]
67
+ RUBIX_TABLES_TO_TRUNCATE = %w[applications groups hostmacro hosts hosts_groups hosts_profiles hosts_profiles_ext hosts_templates items items_applications profiles triggers trigger_depends]
68
68
 
69
69
  def self.truncate_all_tables
70
70
  return unless $RUBIX_INTEGRATION_TEST
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubix
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 9
10
- version: 0.0.9
9
+ - 10
10
+ version: 0.0.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dhruv Bansal
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-12-05 00:00:00 Z
18
+ date: 2011-12-12 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec
@@ -90,6 +90,7 @@ files:
90
90
  - bin/zabbix_pipe
91
91
  - lib/rubix.rb
92
92
  - lib/rubix/models/template.rb
93
+ - lib/rubix/models/trigger.rb
93
94
  - lib/rubix/models/user_macro.rb
94
95
  - lib/rubix/models/item.rb
95
96
  - lib/rubix/models/host.rb
@@ -106,8 +107,10 @@ files:
106
107
  - lib/rubix/response.rb
107
108
  - lib/rubix/associations/has_many_hosts.rb
108
109
  - lib/rubix/associations/has_many_applications.rb
110
+ - lib/rubix/associations/belongs_to_template.rb
109
111
  - lib/rubix/associations/has_many_host_groups.rb
110
112
  - lib/rubix/associations/belongs_to_host.rb
113
+ - lib/rubix/associations/has_many_items.rb
111
114
  - lib/rubix/associations/has_many_templates.rb
112
115
  - lib/rubix/associations/has_many_user_macros.rb
113
116
  - lib/rubix/monitors/chef_monitor.rb
@@ -126,6 +129,7 @@ files:
126
129
  - spec/requests/host_group_request_spec.rb
127
130
  - spec/requests/user_macro_request_spec.rb
128
131
  - spec/requests/template_request_spec.rb
132
+ - spec/requests/trigger_request_spec.rb
129
133
  - spec/requests/host_request_spec.rb
130
134
  - spec/requests/application_request_spec.rb
131
135
  - spec/spec_helper.rb