Class: UbcMonitor::Report

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

Overview

The UbcMonitor::Report object holds information on the current state of failcounts in the /proc/user_beancounts table

The Report is able to generate different views of the data it represents. It can use a file to log reports between uses in order to only report on failcounts that have increased since its last run.

The log file contains sections for each VPS currently having failcounts. The VPS id introduces the section, followed by a newline and then resource: <failcount> pairs, one on each line. The VPS block is terminated by a double newline.

Example file:

101
privvmpages: 2
kmemsize: 4

102
privvmpages: 3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReport

Setup a new Report



28
29
30
31
# File 'lib/ubc_monitor/report.rb', line 28

def initialize
  @resource_counts = {}
  @updated = false
end

Instance Attribute Details

#updatedObject

Returns the value of attribute updated.



25
26
27
# File 'lib/ubc_monitor/report.rb', line 25

def updated
  @updated
end

Class Method Details

.load(file) ⇒ Object

Parse the ubc_monitor logfile and return a UbcMonitor::Report object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ubc_monitor/report.rb', line 34

def self.load(file)
  report = Report.new
  vps = nil

  return report if file.nil? || !File.exist?(file)

  IO.read(file).each do |line|
    if line =~ /^\s*$/
      # This is a blank line separating VPS instances. Reset vps
      vps = nil
    elsif vps.nil?
      # If the VPS has been reset, this line is expected to contain a new VPS id
      vps = line.strip.to_sym
    else
      # Otherwise, this is a resource: <value> line belonging to the current VPS
      resource, failcnt = line.split(':')
      report[vps, resource.to_sym] = { :failcnt => failcnt.to_i }
    end
  end

  return report
end

Instance Method Details

#[](vps, resource = nil) ⇒ Object

Fetch the ubc numbers array for a given VPS



85
86
87
88
89
90
91
92
# File 'lib/ubc_monitor/report.rb', line 85

def [](vps, resource = nil)
  has_vps = @resource_counts.key? vps
  has_resource = has_vps && @resource_counts[vps].key?(resource)

  return resource.nil? ?
    (has_vps ? @resource_counts[vps] : nil) :
      (has_resource ? @resource_counts[vps][resource] : nil)
end

#[]=(vps, resource, limits) ⇒ Object

Add a resource failcount. If resource and failcnt are nil, only allocate room for the VPS. Returns the VPS symbol

vps and resource are both symbols while limits should be a hash (unless resource and limits are both nil in which case no resource/limits pair is actually appended). The hash can contain the following values: :held, :maxheld, :barrier, :limit, :failcnt



78
79
80
81
82
# File 'lib/ubc_monitor/report.rb', line 78

def []=(vps, resource, limits)
  @resource_counts[vps] = {} if @resource_counts[vps].nil?
  @resource_counts[vps][resource] = limits
  vps
end

#dump!(file) ⇒ Object

Dump the report back to file



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ubc_monitor/report.rb', line 58

def dump!(file)
  File.open(file, 'w+') do |f|
    @resource_counts.each_pair do |vps, content|
      # Each VPS in its own section, starting with the VPS id and followed by all the
      # resources currently keeping a failcount, then terminated by a blank line.
      # Resources whose failcounts are 0 are not dumped
      str = ''
      content.each_pair { |resource, numbers| str += "#{resource}: #{numbers[:failcnt]}\n" unless numbers[:failcnt] == 0 }
      f.puts vps, str, '' unless str == ''
    end
  end
end

#empty?Boolean

Returns true if no VPS’s has any reported failcounts

Returns:

  • (Boolean)


95
96
97
# File 'lib/ubc_monitor/report.rb', line 95

def empty?
  return length == 0
end

#lengthObject Also known as: size

Returns the number of VPSs in the report



100
101
102
# File 'lib/ubc_monitor/report.rb', line 100

def length
  return @resource_counts.length
end

#to_sObject

Return a nicely formatted report



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ubc_monitor/report.rb', line 108

def to_s
  str = ''

  @resource_counts.dup.each do |vps, content|
    str += "VPS #{vps} has increased fail counts:\n"
    str += "#{'resource'.rjust(20)}#{'held'.rjust(15)}#{'maxheld'.rjust(15)}" +
           "#{'barrier'.rjust(15)}#{'limit'.rjust(15)}#{'failcount'.rjust(15)}\n"

    content.each_pair do |resource, report|
      str += resource.to_s.rjust(20)
      [:held, :maxheld, :barrier, :limit, :failcnt].each { |sym| str += report[sym].to_s.rjust(15) }
      str += "\n"
    end

    str += "\n"
  end

  str
end