rubix 0.0.12 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/rubix/associations.rb +1 -0
- data/lib/rubix/associations/belongs_to_item.rb +34 -0
- data/lib/rubix/models.rb +1 -0
- data/lib/rubix/models/item.rb +14 -5
- data/lib/rubix/models/time_series.rb +101 -0
- data/spec/requests/item_request_spec.rb +4 -2
- data/spec/requests/time_series_request_spec.rb +55 -0
- data/spec/rubix/monitors/monitor_spec.rb +3 -3
- metadata +10 -12
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/lib/rubix/associations.rb
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
module Rubix
|
2
|
+
module Associations
|
3
|
+
|
4
|
+
module BelongsToItem
|
5
|
+
|
6
|
+
def item= i
|
7
|
+
return unless i
|
8
|
+
@item = i
|
9
|
+
@item_id = i.id
|
10
|
+
end
|
11
|
+
|
12
|
+
def item
|
13
|
+
return @item if @item
|
14
|
+
return unless @item_id
|
15
|
+
@item = Item.find(:id => @item_id)
|
16
|
+
end
|
17
|
+
|
18
|
+
def item_id= iid
|
19
|
+
puts "I AM TRYING TO SET ITEM_ID=#{iid}"
|
20
|
+
return unless iid
|
21
|
+
@item_id = iid
|
22
|
+
end
|
23
|
+
|
24
|
+
def item_id
|
25
|
+
return @item_id if @item_id
|
26
|
+
return unless @item
|
27
|
+
@item_id = @item.id
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
|
data/lib/rubix/models.rb
CHANGED
data/lib/rubix/models/item.rb
CHANGED
@@ -59,7 +59,7 @@ module Rubix
|
|
59
59
|
self::VALUE_CODES[value_type_from_value(value)]
|
60
60
|
end
|
61
61
|
|
62
|
-
attr_accessor :key, :description
|
62
|
+
attr_accessor :key, :description, :units
|
63
63
|
attr_writer :type, :value_type
|
64
64
|
|
65
65
|
def initialize properties={}
|
@@ -67,6 +67,7 @@ module Rubix
|
|
67
67
|
@key = properties[:key]
|
68
68
|
@description = properties[:description]
|
69
69
|
@type = properties[:type]
|
70
|
+
@units = properties[:units]
|
70
71
|
|
71
72
|
self.value_type = properties[:value_type]
|
72
73
|
|
@@ -110,9 +111,10 @@ module Rubix
|
|
110
111
|
:description => (description || 'Unknown'),
|
111
112
|
:type => self.class::TYPE_CODES[type],
|
112
113
|
:key_ => key,
|
113
|
-
:value_type => self.class::VALUE_CODES[value_type]
|
114
|
+
:value_type => self.class::VALUE_CODES[value_type]
|
114
115
|
}.tap do |p|
|
115
116
|
p[:applications] = application_ids if application_ids
|
117
|
+
p[:units] = units if units
|
116
118
|
end
|
117
119
|
end
|
118
120
|
|
@@ -122,12 +124,14 @@ module Rubix
|
|
122
124
|
|
123
125
|
def self.find_params options={}
|
124
126
|
super().merge({
|
125
|
-
:hostids => [options[:host_id]],
|
126
127
|
:filter => {
|
127
128
|
:key_ => options[:key],
|
128
129
|
:id => options[:id]
|
129
130
|
}
|
130
|
-
}
|
131
|
+
}.tap do |o|
|
132
|
+
o[:hostids] = [options[:host_id]] if options[:host_id]
|
133
|
+
end
|
134
|
+
)
|
131
135
|
end
|
132
136
|
|
133
137
|
def self.build item
|
@@ -138,9 +142,14 @@ module Rubix
|
|
138
142
|
:type => TYPE_NAMES[item['type'].to_i],
|
139
143
|
:value_type => VALUE_NAMES[item['value_type'].to_i],
|
140
144
|
:application_ids => (item['applications'] || []).map { |app| app['applicationid'].to_i },
|
141
|
-
:key => item['key_']
|
145
|
+
:key => item['key_'],
|
146
|
+
:units => item['units']
|
142
147
|
})
|
143
148
|
end
|
149
|
+
|
150
|
+
def time_series options={}
|
151
|
+
TimeSeries.find(options.merge(:item_id => self.id, :item => self))
|
152
|
+
end
|
144
153
|
|
145
154
|
end
|
146
155
|
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module Rubix
|
2
|
+
|
3
|
+
class TimeSeries < Model
|
4
|
+
|
5
|
+
#
|
6
|
+
# == Properties & Finding ==
|
7
|
+
#
|
8
|
+
|
9
|
+
attr_writer :from, :upto
|
10
|
+
attr_writer :raw_data
|
11
|
+
|
12
|
+
def initialize properties={}
|
13
|
+
super(properties)
|
14
|
+
@from = properties[:from]
|
15
|
+
@upto = properties[:upto]
|
16
|
+
|
17
|
+
self.item = properties[:item]
|
18
|
+
self.item_id = properties[:item_id]
|
19
|
+
puts "MY ITEM ID IS #{self.item_id}"
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.zabbix_name
|
23
|
+
'history'
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.default_from
|
27
|
+
(Time.now - 3600).utc
|
28
|
+
end
|
29
|
+
|
30
|
+
def from
|
31
|
+
@from ||= self.class.default_from
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.default_upto
|
35
|
+
Time.now.utc
|
36
|
+
end
|
37
|
+
|
38
|
+
def upto
|
39
|
+
@upto ||= self.class.default_upto
|
40
|
+
end
|
41
|
+
|
42
|
+
def raw_data
|
43
|
+
@raw_data ||= []
|
44
|
+
end
|
45
|
+
|
46
|
+
#
|
47
|
+
# == Associations ==
|
48
|
+
#
|
49
|
+
|
50
|
+
include Associations::BelongsToItem
|
51
|
+
|
52
|
+
#
|
53
|
+
# == Requests ==
|
54
|
+
#
|
55
|
+
|
56
|
+
def self.get_params
|
57
|
+
super().merge(:output => :extend)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.find_params options={}
|
61
|
+
super().merge({
|
62
|
+
:itemids => [options[:item_id].to_s],
|
63
|
+
:time_from => (options[:from] || default_from).to_i.to_s,
|
64
|
+
:time_till => (options[:upto] || default_upto).to_i.to_s
|
65
|
+
})
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.find options={}
|
69
|
+
response = find_request(options)
|
70
|
+
case
|
71
|
+
when response.success?
|
72
|
+
new(options.merge(:raw_data => response.result))
|
73
|
+
else
|
74
|
+
error("Error finding #{resource_name} using #{options.inspect}: #{response.error_message}")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
#
|
79
|
+
# == Transformations ==
|
80
|
+
#
|
81
|
+
|
82
|
+
def parsed_data
|
83
|
+
return @parsed_data if @parsed_data
|
84
|
+
caster = case
|
85
|
+
when item.nil? then nil
|
86
|
+
when item.value_type == :float then :to_f
|
87
|
+
when item.value_type == :unsigned_int then :to_i
|
88
|
+
end
|
89
|
+
@parsed_data = raw_data.map do |point|
|
90
|
+
next unless point.is_a?(Hash) && point['clock'] && point['value']
|
91
|
+
timestamp = point['clock'].to_i
|
92
|
+
next if timestamp == 0
|
93
|
+
{
|
94
|
+
'time' => Time.at(timestamp),
|
95
|
+
'value' => (caster ? point['value'].send(caster) : point['value'])
|
96
|
+
}
|
97
|
+
end.compact
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
@@ -22,7 +22,7 @@ describe "Items" do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
it "can be created" do
|
25
|
-
item = Rubix::Item.new(:key => 'rubix.spec1', :description => 'rubix item description 1', :host_id => @host_1.id, :value_type => :character, :applications => [@app_1])
|
25
|
+
item = Rubix::Item.new(:key => 'rubix.spec1', :description => 'rubix item description 1', :host_id => @host_1.id, :value_type => :character, :applications => [@app_1], :units => 'B')
|
26
26
|
item.save.should be_true
|
27
27
|
item.host.name.should == @host_1.name
|
28
28
|
item.applications.map(&:name).should include(@app_1.name)
|
@@ -33,7 +33,7 @@ describe "Items" do
|
|
33
33
|
describe "when existing" do
|
34
34
|
|
35
35
|
before do
|
36
|
-
@item = ensure_save(Rubix::Item.new(:key => 'rubix.spec1', :description => 'rubix item description 1', :host_id => @host_1.id, :value_type => :character, :applications => [@app_1]))
|
36
|
+
@item = ensure_save(Rubix::Item.new(:key => 'rubix.spec1', :description => 'rubix item description 1', :host_id => @host_1.id, :value_type => :character, :applications => [@app_1], :units => 'B'))
|
37
37
|
end
|
38
38
|
|
39
39
|
it "can have its host, application, and properties updated" do
|
@@ -42,6 +42,7 @@ describe "Items" do
|
|
42
42
|
@item.type = :external
|
43
43
|
@item.value_type = :unsigned_int
|
44
44
|
@item.host_id = @host_2.id
|
45
|
+
@item.units = 'MB'
|
45
46
|
@item.applications = [@app_2]
|
46
47
|
@item.save.should be_true
|
47
48
|
|
@@ -52,6 +53,7 @@ describe "Items" do
|
|
52
53
|
new_item.value_type.should == :unsigned_int
|
53
54
|
new_item.type.should == :external
|
54
55
|
new_item.host.name.should == @host_2.name
|
56
|
+
new_item.units.should == 'MB'
|
55
57
|
new_item.applications.map(&:name).should include(@app_2.name)
|
56
58
|
end
|
57
59
|
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "TimeSeries" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
integration_test
|
7
|
+
@host_group = ensure_save(Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1'))
|
8
|
+
@host = ensure_save(Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@host_group]))
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
truncate_all_tables
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "setting a timeframe" do
|
16
|
+
|
17
|
+
it "should set a default timeframe" do
|
18
|
+
Rubix::TimeSeries.new.from.should_not be_nil
|
19
|
+
Rubix::TimeSeries.new.upto.should_not be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should accept a given timeframe when querying" do
|
23
|
+
Rubix::TimeSeries.find_params(:item_id => 100, :from => '1327543430', :upto => Time.at(1327543450))[:time_from].should == '1327543430'
|
24
|
+
Rubix::TimeSeries.find_params(:item_id => 100, :from => '1327543430', :upto => Time.at(1327543450))[:time_till].should == '1327543450'
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
describe "when the item doesn't exist" do
|
30
|
+
|
31
|
+
it "returns an empty TimeSeries" do
|
32
|
+
@ts = Rubix::TimeSeries.find(:item_id => 100)
|
33
|
+
@ts.should_not be_nil
|
34
|
+
@ts.raw_data.should be_empty
|
35
|
+
@ts.parsed_data.should be_empty
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "when the item exists" do
|
41
|
+
|
42
|
+
before do
|
43
|
+
@item = ensure_save(Rubix::Item.new(:host_id => @host.id, :key => 'foo.bar.baz', :value_type => :unsigned_int, :description => "rubix item description"))
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should parse the results properly" do
|
47
|
+
@ts = Rubix::TimeSeries.find(:item_id => @item.id)
|
48
|
+
@ts.should_not be_nil
|
49
|
+
@ts.should_receive(:raw_data).and_return([{'clock' => '1327543429', 'value' => '3'}, {'clock' => '1327543430'}])
|
50
|
+
@ts.parsed_data.should == [{'time' => Time.at(1327543429), 'value' => 3}]
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -20,7 +20,7 @@ describe Rubix::Monitor do
|
|
20
20
|
|
21
21
|
it "should be the default behavior when run with no arguments" do
|
22
22
|
::ARGV.replace([])
|
23
|
-
$stdout.should_receive(:puts).with(
|
23
|
+
$stdout.should_receive(:puts).with(/data/)
|
24
24
|
@wrapper.run
|
25
25
|
end
|
26
26
|
|
@@ -47,13 +47,13 @@ describe Rubix::Monitor do
|
|
47
47
|
it "should create a new file if called with a path that doesn't exist" do
|
48
48
|
FileUtils.rm(@file.path) if File.exist?(@file.path)
|
49
49
|
@wrapper.run
|
50
|
-
File.read(@file.path).should
|
50
|
+
File.read(@file.path).should match(/data/)
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should append to an existing file" do
|
54
54
|
File.open(@file.path, 'w') { |f| f.puts('old content') }
|
55
55
|
@wrapper.run
|
56
|
-
File.read(@file.path).should
|
56
|
+
File.read(@file.path).should match(/data/)
|
57
57
|
File.read(@file.path).should include('old content')
|
58
58
|
end
|
59
59
|
end
|
metadata
CHANGED
@@ -1,13 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
4
|
+
prerelease: false
|
6
5
|
segments:
|
7
6
|
- 0
|
7
|
+
- 1
|
8
8
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.12
|
9
|
+
version: 0.1.0
|
11
10
|
platform: ruby
|
12
11
|
authors:
|
13
12
|
- Dhruv Bansal
|
@@ -15,7 +14,8 @@ autorequire:
|
|
15
14
|
bindir: bin
|
16
15
|
cert_chain: []
|
17
16
|
|
18
|
-
date:
|
17
|
+
date: 2012-02-01 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
name: rspec
|
@@ -25,7 +25,6 @@ dependencies:
|
|
25
25
|
requirements:
|
26
26
|
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
hash: 3
|
29
28
|
segments:
|
30
29
|
- 0
|
31
30
|
version: "0"
|
@@ -39,7 +38,6 @@ dependencies:
|
|
39
38
|
requirements:
|
40
39
|
- - ">="
|
41
40
|
- !ruby/object:Gem::Version
|
42
|
-
hash: 3
|
43
41
|
segments:
|
44
42
|
- 0
|
45
43
|
version: "0"
|
@@ -53,7 +51,6 @@ dependencies:
|
|
53
51
|
requirements:
|
54
52
|
- - ">="
|
55
53
|
- !ruby/object:Gem::Version
|
56
|
-
hash: 3
|
57
54
|
segments:
|
58
55
|
- 0
|
59
56
|
version: "0"
|
@@ -67,7 +64,6 @@ dependencies:
|
|
67
64
|
requirements:
|
68
65
|
- - ">="
|
69
66
|
- !ruby/object:Gem::Version
|
70
|
-
hash: 31
|
71
67
|
segments:
|
72
68
|
- 0
|
73
69
|
- 4
|
@@ -96,6 +92,7 @@ files:
|
|
96
92
|
- lib/rubix/models/host.rb
|
97
93
|
- lib/rubix/models/host_group.rb
|
98
94
|
- lib/rubix/models/model.rb
|
95
|
+
- lib/rubix/models/time_series.rb
|
99
96
|
- lib/rubix/models/application.rb
|
100
97
|
- lib/rubix/examples/es_monitor.rb
|
101
98
|
- lib/rubix/examples/mongo_monitor.rb
|
@@ -110,6 +107,7 @@ files:
|
|
110
107
|
- lib/rubix/associations/belongs_to_template.rb
|
111
108
|
- lib/rubix/associations/has_many_host_groups.rb
|
112
109
|
- lib/rubix/associations/belongs_to_host.rb
|
110
|
+
- lib/rubix/associations/belongs_to_item.rb
|
113
111
|
- lib/rubix/associations/has_many_items.rb
|
114
112
|
- lib/rubix/associations/has_many_templates.rb
|
115
113
|
- lib/rubix/associations/has_many_user_macros.rb
|
@@ -125,6 +123,7 @@ files:
|
|
125
123
|
- spec/rubix/response_spec.rb
|
126
124
|
- spec/rubix/sender_spec.rb
|
127
125
|
- spec/rubix/connection_spec.rb
|
126
|
+
- spec/requests/time_series_request_spec.rb
|
128
127
|
- spec/requests/item_request_spec.rb
|
129
128
|
- spec/requests/host_group_request_spec.rb
|
130
129
|
- spec/requests/user_macro_request_spec.rb
|
@@ -139,6 +138,7 @@ files:
|
|
139
138
|
- LICENSE
|
140
139
|
- README.rdoc
|
141
140
|
- VERSION
|
141
|
+
has_rdoc: true
|
142
142
|
homepage: http://github.com/dhruvbansal/rubix
|
143
143
|
licenses: []
|
144
144
|
|
@@ -152,7 +152,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
152
152
|
requirements:
|
153
153
|
- - ">="
|
154
154
|
- !ruby/object:Gem::Version
|
155
|
-
hash: 3
|
156
155
|
segments:
|
157
156
|
- 0
|
158
157
|
version: "0"
|
@@ -161,14 +160,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
161
160
|
requirements:
|
162
161
|
- - ">="
|
163
162
|
- !ruby/object:Gem::Version
|
164
|
-
hash: 3
|
165
163
|
segments:
|
166
164
|
- 0
|
167
165
|
version: "0"
|
168
166
|
requirements: []
|
169
167
|
|
170
168
|
rubyforge_project:
|
171
|
-
rubygems_version: 1.
|
169
|
+
rubygems_version: 1.3.7
|
172
170
|
signing_key:
|
173
171
|
specification_version: 3
|
174
172
|
summary: A Ruby client for configuring and writing data to Zabbix
|