rubix 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.2.0
@@ -21,16 +21,12 @@ module Rubix
21
21
  # webserver = chef_node_from_node_name('webserver')
22
22
  # begin
23
23
  # if Net::HTTP.get_response(URI.parse("http://#{webserver['ec2']['public_hostname']}")).code.to_i == 200
24
- # write do |data|
25
- # data << ['webserver.available', 1]
26
- # end
24
+ # write ['webserver.available', 1]
27
25
  # return
28
26
  # end
29
27
  # rescue => e
30
28
  # end
31
- # write do |data|
32
- # data << ([['webserver.available', 0]])
33
- # end
29
+ # write ['webserver.available', 0]
34
30
  # end
35
31
  # end
36
32
  #
@@ -1,5 +1,4 @@
1
1
  require 'configliere'
2
- require 'json'
3
2
 
4
3
  module Rubix
5
4
 
@@ -18,9 +17,20 @@ module Rubix
18
17
  # class UptimeMonitor < Rubix::Monitor
19
18
  #
20
19
  # def measure
21
- # return unless `uptime`.chomp =~ /(\d+) days/
20
+ # return unless `uptime`.chomp =~ /(\d+) days.*(\d+) users.*load average: ([\d\.]+), ([\d\.]+), ([\d\.]+)/
21
+ #
22
+ # # can write one value at a time
23
+ # write ['uptime', $1.to_i]
24
+ #
25
+ # # or can use a block
22
26
  # write do |data|
23
- # data << ([['uptime', $1.to_i]])
27
+ # # values can be arrays
28
+ # data << ['users', $2.to_i]
29
+ # # or hashes
30
+ # data << { :key => 'load15', :value => $3.to_i }
31
+ # data << { :key => 'load5', :value => $4.to_i }
32
+ # # can even pass a different host
33
+ # data << { :key => 'load1', :value => $5.to_i, :host => 'foobar-host' }
24
34
  # end
25
35
  # end
26
36
  # end
@@ -106,16 +116,14 @@ module Rubix
106
116
  # Methods for writing data to Zabbix.
107
117
  #
108
118
 
109
- def write options={}, &block
119
+ def write measurement=nil &block
110
120
  return unless output
111
- data = []
121
+ return unless measurement || block_given?
122
+
123
+ data = [measurement]
112
124
  block.call(data) if block_given?
113
- text = {
114
- :data => data.map do |measurement|
115
- key, value = measurement
116
- { :key => key, :value => value }
117
- end
118
- }.merge(options).to_json
125
+
126
+ text = data.compact.map { |measurement| format_measurement(measurement) }.compact.join("\n")
119
127
 
120
128
  begin
121
129
  output.puts(text)
@@ -124,8 +132,36 @@ module Rubix
124
132
  end
125
133
  end
126
134
 
135
+ def format_measurement measurement
136
+ # <hostname> key <timestamp> value
137
+ [].tap do |vals|
138
+ case measurement
139
+ when Hash
140
+ vals << (measurement[:host].nil? ? '-' : measurement[:host])
141
+ vals << measurement[:key]
142
+ vals << measurement[:timestamp] if measurement[:timestamp]
143
+
144
+ value = measurement[:value] || ''
145
+ if value.include?(' ')
146
+ value.insert(0, "'")
147
+ value.insert(-1, "'")
148
+ end
149
+ vals << value
150
+ when Array
151
+ if measurement.length == 2
152
+ vals << '-'
153
+ vals.concat(measurement)
154
+ else
155
+ vals.concat(measurement)
156
+ end
157
+ else
158
+ return
159
+ end
160
+ end.map(&:to_s).join(' ')
161
+ end
162
+
127
163
  def output_path
128
- settings.rest.first
164
+ settings.rest && settings.rest.first
129
165
  end
130
166
 
131
167
  def stdout?
@@ -149,6 +185,7 @@ module Rubix
149
185
  begin
150
186
  @output = open(output_path, (File::WRONLY | File::NONBLOCK))
151
187
  rescue Errno::ENXIO
188
+ nil
152
189
  # FIFO's reader isn't alive...
153
190
  end
154
191
  else
@@ -3,29 +3,93 @@ require 'tempfile'
3
3
 
4
4
  describe Rubix::Monitor do
5
5
 
6
+ def monitor_wrapper(&block)
7
+ Class.new(Rubix::Monitor).tap do |wrapper|
8
+ wrapper.class_eval do
9
+ define_method(:measure, &block)
10
+ end
11
+ end
12
+ end
13
+
6
14
  before do
7
- @measurement = '{"data":[{"value":"bar","key":"foo"}]}'
8
-
9
- @wrapper = Class.new(Rubix::Monitor)
10
- @wrapper.class_eval do
11
- def measure
15
+ @wrapper = monitor_wrapper do
16
+ write ['key', 'value']
17
+ end
18
+ ::ARGV.replace([])
19
+ end
20
+
21
+ describe "making measurements" do
22
+
23
+ it "should accept a two-element Array of [key, value]" do
24
+ $stdout.should_receive(:puts).with('- key value')
25
+ @wrapper.run
26
+ end
27
+
28
+ it "should accept a three-element Array of [host, key, value]" do
29
+ @wrapper = monitor_wrapper do
30
+ write ['host', 'key', 'value']
31
+ end
32
+ $stdout.should_receive(:puts).with('host key value')
33
+ @wrapper.run
34
+ end
35
+
36
+ it "should accept a four-element Array of [host, key, timestamp, value]" do
37
+ @wrapper = monitor_wrapper do
38
+ write ['host', 'key', 1328875053, 'value']
39
+ end
40
+ $stdout.should_receive(:puts).with('host key 1328875053 value')
41
+ @wrapper.run
42
+ end
43
+
44
+ it "should accept a hash with just a key and a value" do
45
+ @wrapper = monitor_wrapper do
46
+ write({ :key => 'key', :value => 'value' })
47
+ end
48
+ $stdout.should_receive(:puts).with('- key value')
49
+ @wrapper.run
50
+ end
51
+
52
+ it "should accept a hash with a host, key, and a value" do
53
+ @wrapper = monitor_wrapper do
54
+ write({ :host => 'host', :key => 'key', :value => 'value' })
55
+ end
56
+ $stdout.should_receive(:puts).with('host key value')
57
+ @wrapper.run
58
+ end
59
+
60
+ it "should accept a hash with a host, key, timestamp, and a value" do
61
+ @wrapper = monitor_wrapper do
62
+ write({ :host => 'host', :key => 'key', :timestamp => 1328875053, :value => 'value' })
63
+ end
64
+ $stdout.should_receive(:puts).with('host key 1328875053 value')
65
+ @wrapper.run
66
+ end
67
+
68
+ it "should accept multiple values from a block" do
69
+ @wrapper = monitor_wrapper do
12
70
  write do |data|
13
- data << ['foo', 'bar']
71
+ data << ['key', 'value']
72
+ data << { :key => 'key', :value => 'value' }
14
73
  end
74
+ $stdout.should_receive(:puts).with("key value\nkey value")
75
+ @wrapper.run
15
76
  end
77
+
16
78
  end
17
79
  end
18
-
80
+
19
81
  describe 'writing to STDOUT' do
20
82
 
21
- it "should be the default behavior when run with no arguments" do
83
+ before do
22
84
  ::ARGV.replace([])
23
- $stdout.should_receive(:puts).with(/data/)
85
+ end
86
+
87
+ it "should be the default behavior when run with no arguments" do
88
+ $stdout.should_receive(:puts).with('- key value')
24
89
  @wrapper.run
25
90
  end
26
91
 
27
92
  it "should flush after each write" do
28
- ::ARGV.replace([])
29
93
  $stdout.stub!(:puts)
30
94
  $stdout.should_receive(:flush).twice()
31
95
  @wrapper.run
@@ -37,7 +101,7 @@ describe Rubix::Monitor do
37
101
 
38
102
  before do
39
103
  @file = Tempfile.new('monitor', '/tmp')
40
- ::ARGV.replace([@file.path])
104
+ ::ARGV.replace([@file.path])
41
105
  end
42
106
 
43
107
  after do
@@ -47,13 +111,13 @@ describe Rubix::Monitor do
47
111
  it "should create a new file if called with a path that doesn't exist" do
48
112
  FileUtils.rm(@file.path) if File.exist?(@file.path)
49
113
  @wrapper.run
50
- File.read(@file.path).should match(/data/)
114
+ File.read(@file.path).should match('- key value')
51
115
  end
52
116
 
53
117
  it "should append to an existing file" do
54
118
  File.open(@file.path, 'w') { |f| f.puts('old content') }
55
119
  @wrapper.run
56
- File.read(@file.path).should match(/data/)
120
+ File.read(@file.path).should include('- key value')
57
121
  File.read(@file.path).should include('old content')
58
122
  end
59
123
  end
@@ -78,4 +142,3 @@ describe Rubix::Monitor do
78
142
  end
79
143
 
80
144
  end
81
-
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 5
9
- version: 0.1.5
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Dhruv Bansal