Class: Rubix::Monitor

Inherits:
Object
  • Object
show all
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

ChefMonitor

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#settingsObject (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_settingsObject

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

.runObject



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.message
    exit(1)
  end
  new(settings).run
end

Instance Method Details

#closeObject



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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (Boolean)


77
78
79
# File 'lib/rubix/monitors/monitor.rb', line 77

def loop?
  loop_period > 0
end

#loop_periodObject



81
82
83
# File 'lib/rubix/monitors/monitor.rb', line 81

def loop_period
  settings[:loop].to_i
end

#measureObject

Raises:

  • (NotImplementedError)


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

#outputObject



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_pathObject



127
128
129
# File 'lib/rubix/monitors/monitor.rb', line 127

def output_path
  settings.rest.first
end

#runObject



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

Returns:

  • (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 options={}, &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(options).to_json

  begin
    output.puts(text)
  rescue Errno::ENXIO
    # FIFO's reader isn't alive...
  end
end