Class: Rubix::Monitor
- Inherits:
-
Object
- Object
- Rubix::Monitor
- Defined in:
- lib/rubix/monitors/monitor.rb
Overview
A generic monitor class for constructing Zabbix monitors.
This class handles the low-level logic of sleeping, waking up, and sending data to Zabbix.
It’s up to a subclass to determine how to make a measurement.
Here’s an example of a script which measures the uptime of the current machine.
#!/usr/bin/env ruby
# in uptime_monitor
class UptimeMonitor < Rubix::Monitor
def measure
return unless `uptime`.chomp =~ /(\d+) days/
write do |data|
data << ([['uptime', $1.to_i]])
end
end
end
UptimeMonitor.run if $0 == __FILE__
See what the script measures by running it directly.
$ ./uptime_monitor
Or have it send its output to another file or FIFO
$ ./uptime_monitor /path/to/some/file
Or have it loop every 30 seconds
$ ./uptime_monitor --loop=30 /path/to/some/file &
Direct Known Subclasses
Instance Attribute Summary collapse
-
#settings ⇒ Object
readonly
Instance-level settings that provide logic for running once or looping.
Class Method Summary collapse
-
.default_settings ⇒ Object
Class-level settings and a function to run a monito.
- .run ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #fifo? ⇒ Boolean
- #file? ⇒ Boolean
-
#initialize(settings) ⇒ Monitor
constructor
A new instance of Monitor.
- #loop? ⇒ Boolean
- #loop_period ⇒ Object
- #measure ⇒ Object
- #output ⇒ Object
- #output_path ⇒ Object
- #run ⇒ Object
- #stdout? ⇒ Boolean
-
#write(options = {}, &block) ⇒ Object
Methods for writing data to Zabbix.
Constructor Details
#initialize(settings) ⇒ Monitor
Returns a new instance of Monitor.
73 74 75 |
# File 'lib/rubix/monitors/monitor.rb', line 73 def initialize settings @settings = settings end |
Instance Attribute Details
#settings ⇒ Object (readonly)
Instance-level settings that provide logic for running once or looping.
71 72 73 |
# File 'lib/rubix/monitors/monitor.rb', line 71 def settings @settings end |
Class Method Details
.default_settings ⇒ Object
Class-level settings and a function to run a monito
47 48 49 50 51 52 53 |
# File 'lib/rubix/monitors/monitor.rb', line 47 def self.default_settings Configliere::Param.new.tap do |s| s.use :commandline s.define :loop, :description => "Run every this many seconds", :required => false, :type => Integer end end |
.run ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rubix/monitors/monitor.rb', line 55 def self.run settings = default_settings begin settings.resolve! rescue => e puts e. exit(1) end new(settings).run end |
Instance Method Details
#close ⇒ Object
159 160 161 162 163 164 |
# File 'lib/rubix/monitors/monitor.rb', line 159 def close return unless output output.flush return if stdout? output.close end |
#fifo? ⇒ Boolean
139 140 141 |
# File 'lib/rubix/monitors/monitor.rb', line 139 def fifo? !stdout? && File.exist?(output_path) && File.ftype(output_path) == 'fifo' end |
#file? ⇒ Boolean
135 136 137 |
# File 'lib/rubix/monitors/monitor.rb', line 135 def file? !stdout? && (!File.exist?(output_path) || File.ftype(output_path) == 'file') end |
#loop? ⇒ Boolean
77 78 79 |
# File 'lib/rubix/monitors/monitor.rb', line 77 def loop? loop_period > 0 end |
#loop_period ⇒ Object
81 82 83 |
# File 'lib/rubix/monitors/monitor.rb', line 81 def loop_period settings[:loop].to_i end |
#measure ⇒ Object
101 102 103 |
# File 'lib/rubix/monitors/monitor.rb', line 101 def measure raise NotImplementedError.new("Override the 'measure' method in a subclass to conduct a measurement.") end |
#output ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rubix/monitors/monitor.rb', line 143 def output return @output if @output case when stdout? @output = $stdout when fifo? begin @output = open(output_path, (File::WRONLY | File::NONBLOCK)) rescue Errno::ENXIO # FIFO's reader isn't alive... end else @output = File.open(output_path, 'a') end end |
#output_path ⇒ Object
127 128 129 |
# File 'lib/rubix/monitors/monitor.rb', line 127 def output_path settings.rest.first end |
#run ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rubix/monitors/monitor.rb', line 85 def run begin if loop? while true measure output.flush if output sleep loop_period end else measure end ensure close end end |
#stdout? ⇒ Boolean
131 132 133 |
# File 'lib/rubix/monitors/monitor.rb', line 131 def stdout? output_path.nil? end |
#write(options = {}, &block) ⇒ Object
Methods for writing data to Zabbix.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rubix/monitors/monitor.rb', line 109 def write ={}, &block return unless output data = [] block.call(data) if block_given? text = { :data => data.map do |measurement| key, value = measurement { :key => key, :value => value } end }.merge().to_json begin output.puts(text) rescue Errno::ENXIO # FIFO's reader isn't alive... end end |