rubix 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ RUBIX_ROOT = File.expand_path('../../../../lib', __FILE__)
4
+ $: << RUBIX_ROOT unless $:.include?(RUBIX_ROOT)
5
+
6
+ require 'rubix'
7
+ require 'open-uri'
8
+
9
+ class UptimeMonitor < Rubix::Monitor
10
+
11
+ def measure
12
+ return unless `uptime`.chomp =~ /(\d+) days.*(\d+) users.*load average: ([\d\.]+), ([\d\.]+), ([\d\.]+)/
13
+
14
+ # can write one value at a time
15
+ write ['uptime', $1.to_i]
16
+
17
+ # or can use a block
18
+ write do |data|
19
+ # values can be arrays
20
+ data << ['users', $2.to_i]
21
+ # or hashes
22
+ data << { :key => 'load15', :value => $3.to_i }
23
+ data << { :key => 'load5', :value => $4.to_i }
24
+ # can even pass a different host
25
+ data << { :key => 'load1', :value => $5.to_i, :host => 'foobar-host' }
26
+ end
27
+ end
28
+ end
29
+
30
+ UptimeMonitor.run if $0 == __FILE__
@@ -1,4 +1,5 @@
1
1
  require 'configliere'
2
+ require 'open3'
2
3
 
3
4
  module Rubix
4
5
 
@@ -58,7 +59,15 @@ module Rubix
58
59
  Configliere::Param.new.tap do |s|
59
60
  s.use :commandline
60
61
 
61
- s.define :loop, :description => "Run every this many seconds", :required => false, :type => Integer
62
+ s.define :loop, :description => "Run every this many seconds", :required => false, :type => Integer
63
+
64
+ # The following options are only used when sending directly
65
+ # with <tt>zabbix_sender</tt>
66
+ s.define :server, :description => "IP of a Zabbix server", :required => false, :default => 'localhost'
67
+ s.define :port, :description => "Port of a Zabbix server", :required => false, :type => Integer, :default => 10051
68
+ s.define :host, :description => "Name of a Zabbix host", :required => false
69
+ s.define :config, :description => "Local Zabbix agentd configuration file", :required => false, :default => "/etc/zabbix/zabbix_agentd.conf"
70
+ s.define :send, :description => "Send data directlyt to Zabbix using 'zabbix_sender'", :required => false, :type => :boolean, :default => false
62
71
  end
63
72
  end
64
73
 
@@ -115,8 +124,8 @@ module Rubix
115
124
  #
116
125
  # Methods for writing data to Zabbix.
117
126
  #
118
-
119
- def write measurement=nil &block
127
+
128
+ def write measurement=nil, &block
120
129
  return unless output
121
130
  return unless measurement || block_given?
122
131
 
@@ -141,7 +150,7 @@ module Rubix
141
150
  vals << measurement[:key]
142
151
  vals << measurement[:timestamp] if measurement[:timestamp]
143
152
 
144
- value = measurement[:value] || ''
153
+ value = measurement[:value].to_s
145
154
  if value.include?(' ')
146
155
  value.insert(0, "'")
147
156
  value.insert(-1, "'")
@@ -175,10 +184,24 @@ module Rubix
175
184
  def fifo?
176
185
  !stdout? && File.exist?(output_path) && File.ftype(output_path) == 'fifo'
177
186
  end
187
+
188
+ def sender?
189
+ if settings[:send] == true
190
+ %w[server port host config].each do |var|
191
+ raise Rubix::Error.new("Cannot send values to Zabbix: Set value of --#{var}.") if settings[var.to_sym].nil?
192
+ end
193
+ true
194
+ else
195
+ false
196
+ end
197
+ end
178
198
 
179
199
  def output
180
200
  return @output if @output
181
201
  case
202
+ when sender?
203
+ @sender_stdin, @sender_stdout, @sender_stderr, @sender_wait_thr = Open3.popen3("zabbix_sender --zabbix-server #{settings[:server]} --host #{settings[:host]}")
204
+ @output = @sender_stdin
182
205
  when stdout?
183
206
  @output = $stdout
184
207
  when fifo?
@@ -196,9 +219,16 @@ module Rubix
196
219
  def close
197
220
  return unless output
198
221
  output.flush
199
- return if stdout?
200
- output.close
222
+ case
223
+ when sender?
224
+ # puts @sender_stdout.read
225
+ [@sender_stdin, @sender_stdout, @sender_stderr].each { |fh| fh.close } if sender?
226
+ when stdout?
227
+ return
228
+ else
229
+ output.close
230
+ end
201
231
  end
202
-
203
232
  end
204
233
  end
234
+
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Dhruv Bansal
@@ -98,6 +98,7 @@ files:
98
98
  - lib/rubix/models/application.rb
99
99
  - lib/rubix/examples/es_monitor.rb
100
100
  - lib/rubix/examples/mongo_monitor.rb
101
+ - lib/rubix/examples/uptime_monitor.rb
101
102
  - lib/rubix/examples/hbase_monitor.rb
102
103
  - lib/rubix/sender.rb
103
104
  - lib/rubix/log.rb