module Nagios::TimedParse

Keep track of last parsed time and last changed time of the status/cache file to avoid parsing on each HTTP request.

Constants

TTL

Set some minimum interval for re-parsing of the status file: even if file changes often, we do not want to parse it more often, then this number of seconds.

Attributes

last_parsed[RW]
parse_interval[RW]

Public Class Methods

included(base) click to toggle source

Override constructor and parse method from ::Nagios::Objects or ::Nagios::Status classes, add instance variables to handle modification and parseing times of status file. Original methods are aliased:

  • initialize -> constructor

  • parse -> parse!

See also www.ruby-forum.com/topic/969161

# File lib/nagira/timed_parse.rb, line 22
def self.included(base)
  base.class_eval do
    alias_method :parse!, :parse
    alias_method :constructor, :initialize

    # @method initialize
    # Extend current constructor with some additional data to
    # track file change time
    #
    # @param [String] file Path to status file
    # @param [Fixnum] parse_interval Number of seconds between
    #     re-parsing of the file
    def initialize(file, parse_interval=TTL)
      constructor(file)

      # Time when status file was last time parsed, set it to 0 secs
      # epoch to make sure it will be parsed
      @last_parsed = Time.at(0)

      # Last time file was changed
      @last_changed = File.mtime(@path)
      @parse_interval = parse_interval
    end


    # Extend original parse method: parse file only if it needs
    # parsing and set time of parser run to current time.
    def parse
      if need_parsing?
        parse!
        @last_parsed = Time.now
      end
    end
  end
end
new(file, parse_interval=TTL) click to toggle source

@method initialize Extend current constructor with some additional data to track file change time

@param [String] file Path to status file @param [Fixnum] parse_interval Number of seconds between

re-parsing of the file
# File lib/nagira/timed_parse.rb, line 34
def initialize(file, parse_interval=TTL)
  constructor(file)

  # Time when status file was last time parsed, set it to 0 secs
  # epoch to make sure it will be parsed
  @last_parsed = Time.at(0)

  # Last time file was changed
  @last_changed = File.mtime(@path)
  @parse_interval = parse_interval
end

Public Instance Methods

changed?() click to toggle source

Return true if file is changed since it was parsed last time

# File lib/nagira/timed_parse.rb, line 65
def changed?
  self.last_changed > self.last_parsed
end
last_changed() click to toggle source
# File lib/nagira/timed_parse.rb, line 60
def last_changed
  @last_changed = File.mtime(@path)
end
need_parsing?() click to toggle source

Check if:

  • file changed?

  • was it parsed recently?

# File lib/nagira/timed_parse.rb, line 72
def need_parsing?
  changed? && ((Time.now - self.last_parsed) > @parse_interval)
end
parse() click to toggle source

Extend original parse method: parse file only if it needs parsing and set time of parser run to current time.

# File lib/nagira/timed_parse.rb, line 49
def parse
  if need_parsing?
    parse!
    @last_parsed = Time.now
  end
end