rubix 0.0.6 → 0.0.7

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.6
1
+ 0.0.7
@@ -63,6 +63,10 @@ module Rubix
63
63
  self.application_ids = properties[:application_ids]
64
64
  end
65
65
 
66
+ def resource_name
67
+ "#{self.class.resource_name} #{self.key || self.id}"
68
+ end
69
+
66
70
  def self.find_request options={}
67
71
  request('item.get', 'hostids' => [options[:host_id]], 'filter' => {'key_' => options[:key], 'id' => options[:id]}, "select_applications" => "refer", "output" => "extend")
68
72
  end
data/lib/rubix/sender.rb CHANGED
@@ -28,51 +28,63 @@ module Rubix
28
28
  def initialize settings
29
29
  @settings = settings
30
30
  confirm_settings
31
- self.host = Host.new(:name => settings['host'])
32
- if settings['fast']
33
- info("Forwarding for #{self.host.name}...") if settings['verbose']
31
+ if fast?
32
+ info("Forwarding for #{settings['host']}...") if settings['verbose']
34
33
  else
35
34
  initialize_hostgroups
36
35
  initialize_templates
37
36
  initialize_host
38
37
  initialize_applications
39
- info("Forwarding for #{self.host.name}...") if settings['verbose'] && host.exists?
38
+ info("Forwarding for #{host.name}...") if settings['verbose']
40
39
  end
41
40
  end
42
41
 
43
- def alive?
44
- settings['fast'] || host.exists?
42
+ def fast?
43
+ settings['fast']
45
44
  end
46
45
 
46
+ def auto_vivify?
47
+ !fast?
48
+ end
49
+
47
50
  def initialize_hostgroups
48
- self.host_groups = settings['host_groups'].split(',').flatten.compact.map { |group_name | HostGroup.find_or_create_by_name(group_name.strip) }
51
+ self.host_groups = settings['host_groups'].split(',').flatten.compact.map { |group_name | HostGroup.find_or_create(:name => group_name.strip) }
49
52
  end
50
53
 
51
54
  def initialize_templates
52
- self.templates = (settings['templates'] || '').split(',').flatten.compact.map { |template_name | Template.find_or_create_by_name(template_name.strip) }
55
+ self.templates = (settings['templates'] || '').split(',').flatten.compact.map { |template_name | Template.find(:name => template_name.strip) }.compact
53
56
  end
54
57
 
55
58
  def initialize_host
56
- unless host.exists?
57
- host.host_groups = host_groups
58
- host.templates = templates
59
- host.create
60
- end
61
- # if settings['verbose']
62
- # puts "Forwarding data for Host '#{settings['host']}' (#{host_id}) from #{settings['pipe']} to #{settings['server']}"
63
- # puts "Creating Items in Application '#{settings['application']}' (#{application_id}) at #{settings['api_server']} as #{settings['username']}"
64
- # end
59
+ self.host = (Host.find(:name => settings['host']) || Host.new(:name => settings['host']))
60
+ host.host_groups = host_groups
61
+ host.templates = templates
62
+ host.save
65
63
  end
66
64
 
67
65
  def initialize_applications
68
- self.applications = (settings['applications'] || '').split(',').flatten.compact.map { |app_name| Application.find_or_create_by_name_and_host(app_name, host) }
66
+ application_names = (settings['applications'] || '').split(',').flatten.compact
67
+ self.applications = []
68
+ application_names.each do |app_name|
69
+ app = Application.find(:name => app_name, :host_id => host.id)
70
+ if app
71
+ self.applications << app
72
+ else
73
+ app = Application.new(:name => app_name, :host_id => host.id)
74
+ if app.save
75
+ self.applications << app
76
+ else
77
+ warn("Could not create application '#{app_name}' for host #{host.name}")
78
+ end
79
+ end
80
+ end
69
81
  end
70
82
 
71
83
  def confirm_settings
72
84
  raise ConnectionError.new("Must specify a Zabbix server to send data to.") unless settings['server']
73
85
  raise Error.new("Must specify the path to a local configuraiton file") unless settings['configuration_file'] && File.file?(settings['configuration_file'])
74
86
  raise ConnectionError.new("Must specify the name of a host to send data for.") unless settings['host']
75
- raise ValidationError.new("Must define at least one host group.") if settings['host_groups'].nil? || settings['host_groups'].empty?
87
+ raise ValidationError.new("Must define at least one host group.") if auto_vivify? && (settings['host_groups'].nil? || settings['host_groups'].empty?)
76
88
  end
77
89
 
78
90
  #
@@ -80,7 +92,6 @@ module Rubix
80
92
  #
81
93
 
82
94
  def run
83
- return unless alive?
84
95
  case
85
96
  when settings['pipe']
86
97
  process_pipe
@@ -196,8 +207,8 @@ module Rubix
196
207
  # Or when sending for another host:
197
208
  #
198
209
  # {
199
- # 'hostname': 'shazaam',
200
- # 'application': 'silly',
210
+ # 'host': 'shazaam',
211
+ # 'applications': 'silly',
201
212
  # 'data': [
202
213
  # {'key': 'foo.bar.baz', 'value': 10},
203
214
  # {'key': 'snap.crackle.pop', 'value': 8 }
@@ -226,7 +237,6 @@ module Rubix
226
237
  # present in the line.
227
238
  begin
228
239
  daughter_pipe = self.class.new(settings.stringify_keys.merge(json))
229
- return unless daughter_pipe.alive?
230
240
  rescue Error => e
231
241
  error(e.message)
232
242
  return
@@ -257,9 +267,23 @@ module Rubix
257
267
  # If the +key+ doesn't exist for this local agent's host, it will be
258
268
  # added.
259
269
  def send key, value, timestamp
260
- item = Item.new(:key => key, :host => host, :applications => applications, :value_type => Item.value_type_from_value(value))
261
- unless settings['fast'] || item.exists?
262
- return unless item.create
270
+ ensure_item_exists(key, value) unless fast?
271
+ command = "#{settings['sender']} --config #{settings['configuration_file']} --zabbix-server #{settings['server']} --host #{settings['host']} --key #{key} --value '#{value}'"
272
+ process_zabbix_sender_output(key, `#{command}`)
273
+
274
+ # command = "zabbix_sender --config #{configuration_file} --zabbix-server #{server} --input-file - --with-timestamps"
275
+ # open(command, 'w') do |zabbix_sender|
276
+ # zabbix_sender.write([settings['host'], key, timestamp.to_i, value].map(&:to_s).join("\t"))
277
+ # zabbix_sender.close_write
278
+ # process_zabbix_sender_output(zabbix_sender.read)
279
+ # end
280
+ end
281
+
282
+ def ensure_item_exists key, value
283
+ item = Item.find(:key => key, :host_id => host.id)
284
+ unless item
285
+ Item.new(:key => key, :host_id => host.id, :applications => applications, :value_type => Item.value_type_from_value(value)).save
286
+
263
287
  # There is a time lag of about 15-30 seconds between (successfully)
264
288
  # creating an item on the Zabbix server and having the Zabbix accept
265
289
  # new data for that item.
@@ -274,17 +298,8 @@ module Rubix
274
298
  # points you send to Zabbix, then don't worry about it.
275
299
  sleep settings['create_item_sleep']
276
300
  end
277
- command = "#{settings['sender']} --config #{settings['configuration_file']} --zabbix-server #{settings['server']} --host #{settings['host']} --key #{key} --value '#{value}'"
278
- process_zabbix_sender_output(key, `#{command}`)
279
-
280
- # command = "zabbix_sender --config #{configuration_file} --zabbix-server #{server} --input-file - --with-timestamps"
281
- # open(command, 'w') do |zabbix_sender|
282
- # zabbix_sender.write([settings['host'], key, timestamp.to_i, value].map(&:to_s).join("\t"))
283
- # zabbix_sender.close_write
284
- # process_zabbix_sender_output(zabbix_sender.read)
285
- # end
286
301
  end
287
-
302
+
288
303
  # Parse the +text+ output by +zabbix_sender+.
289
304
  def process_zabbix_sender_output key, text
290
305
  return unless settings['verbose']
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'tempfile'
3
2
 
4
3
  describe Rubix::ChefMonitor do
5
4
 
@@ -1,5 +1,4 @@
1
1
  require 'spec_helper'
2
- require 'tempfile'
3
2
 
4
3
  describe Rubix::ClusterMonitor do
5
4
 
@@ -3,6 +3,20 @@ require 'spec_helper'
3
3
  describe Rubix::Sender do
4
4
 
5
5
  before do
6
+ @config_file = Tempfile.new('sender', '/tmp')
7
+ end
8
+
9
+ describe "running in --fast mode" do
10
+
11
+ it "should not attempt to make any calls to the Zabbix API when writing values" do
12
+ Rubix.connection.should_not_receive(:request)
13
+ @sender = Rubix::Sender.new(mock_settings('configuration_file' => @config_file.path, 'host' => 'foohost', 'server' => 'fooserver', 'fast' => true, 'sender' => 'echo'))
14
+ @sender.process_line("foo.bar.baz 123\n")
15
+ @sender.process_line({:host => 'newhost', :host_groups => 'foobar,baz', :data => [{:key => 'foo.bar.baz', :value => 123}]}.to_json)
16
+ end
17
+ end
18
+
19
+ describe "running in auto-vivify mode" do
6
20
  end
7
21
 
8
22
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'tempfile'
1
2
  require 'rspec'
2
3
 
3
4
  RUBIX_ROOT = File.expand_path(__FILE__, '../../lib')
@@ -12,6 +13,7 @@ RSpec.configure do |config|
12
13
  config.mock_with :rspec
13
14
  config.include Rubix::ResponseSpecs
14
15
  config.include Rubix::IntegrationHelper
16
+ config.include Rubix::ConfigliereHelper
15
17
 
16
18
  test_yml_path = File.expand_path('../test.yml', __FILE__)
17
19
  if File.exist?(test_yml_path)
@@ -0,0 +1,12 @@
1
+ module Rubix
2
+ module ConfigliereHelper
3
+ def mock_settings options={}, rest=[]
4
+ param = options
5
+ param.stub!(:rest).and_return(rest)
6
+ param.stub!(:stringify_keys).and_return(param) # FIXME...
7
+ param
8
+ end
9
+ end
10
+ end
11
+
12
+
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: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 6
10
- version: 0.0.6
9
+ - 7
10
+ version: 0.0.7
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-11-22 00:00:00 Z
18
+ date: 2011-11-23 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec
@@ -117,6 +117,7 @@ files:
117
117
  - spec/spec_helper.rb
118
118
  - spec/support/integration_helper.rb
119
119
  - spec/support/response_helper.rb
120
+ - spec/support/configliere_helper.rb
120
121
  - LICENSE
121
122
  - README.rdoc
122
123
  - VERSION