Class: UbcMonitor::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/ubc_monitor/monitor.rb

Overview

The Monitor provides an interface for inspecting /proc/user_beancounters as well as a number of ways to report numbers that exceed given limits

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Monitor

Returns a new instance of Monitor.



11
12
13
# File 'lib/ubc_monitor/monitor.rb', line 11

def initialize(options = {})
  @options = ({ :file => nil }).merge(options)
end

Instance Method Details

#runObject

Run the monitor. Uses a UbcMonitor::Report object to track runs so as to not report back errors that have already been reported



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ubc_monitor/monitor.rb', line 17

def run
  filename = @options[:file]

  # Fetch data from the previous run, this will be the baseline
  report = filename.nil? ? Report.new : Report.load(filename)

  # Update the report by scanning /proc/user_beancounters
  report = scan(report)

  # Log report back to file and return report
  report.dump!(filename) unless filename.nil? || report.length == 0

  report
end

#scan(report = Report.new) ⇒ Object

Generate the /proc/user_beancounters report by running cat on it. The method takes a UbcMonitor::Report object and returns a fresh report containing only VPS’s that have increased failcounts



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ubc_monitor/monitor.rb', line 35

def scan(report = Report.new)
  vps = nil
  nrep = Report.new

  proc_user_beancounters.split("\n").each do |line|
    if line =~ /^\s+(\d+):/
      vps = $1.to_sym
    end

    # If the VPS is set, and the line (that is, the failcount) does not end with
    # a 0, set the resource/failcount pair
    unless vps.nil? || line =~ /\s0$/
      pieces = line.gsub(/^\s*(\d+:)?\s*/, '').split(/\s+/)
      resource = pieces[0].to_sym

      if report[vps, resource].nil? || report[vps, resource][:failcnt].nil? ||
         pieces[5].to_i > report[vps, resource][:failcnt]
        nrep[vps, resource] = { :held => pieces[1].to_i, :maxheld => pieces[2].to_i,
                                :barrier => pieces[3].to_i, :limit => pieces[4].to_i,
                                :failcnt => pieces[5].to_i }

        # Mark report as updated if failcount has increased
        nrep.updated = true if report[vps, resource].nil? || pieces[5].to_i > report[vps, resource][:failcnt]
      end
    end
  end

  nrep
end