Class: Graphiterb::Monitors::DirectoryTree

Inherits:
PeriodicMonitor show all
Defined in:
lib/graphiterb/monitors/directory_tree.rb

Overview

A class for monitoring the contents of a directory.

Will only monitor files modified within the last hour by default.

Defined Under Namespace

Classes: Directory

Instance Attribute Summary collapse

Attributes inherited from PeriodicMonitor

#current_iter, #iter, #iter_interval, #last_time, #main_scope, #options, #started_at, #time_interval

Instance Method Summary collapse

Methods inherited from PeriodicMonitor

#enough_iterations?, #enough_time?, #inst_rate, #periodically, #rate, #run!, #scope, #sender, #since

Methods included from Utils::SystemInfo

#graphite_identifier, #hostname, #node_name

Constructor Details

#initialize(main_scope, root, options = {}) ⇒ DirectoryTree

Returns a new instance of DirectoryTree.



17
18
19
20
21
# File 'lib/graphiterb/monitors/directory_tree.rb', line 17

def initialize main_scope, root, options={}
  super(main_scope, options)
  self.root      = File.expand_path(root)
  self.filter_re = self.options[:filter_re] || /.*/
end

Instance Attribute Details

#filter_reObject

A regular expression filter that must be matched by files within the root directory to be counted.



15
16
17
# File 'lib/graphiterb/monitors/directory_tree.rb', line 15

def filter_re
  @filter_re
end

#rootObject

The root of the directory tree being monitored.



11
12
13
# File 'lib/graphiterb/monitors/directory_tree.rb', line 11

def root
  @root
end

Instance Method Details

#dirsObject



23
24
25
# File 'lib/graphiterb/monitors/directory_tree.rb', line 23

def dirs
  @dirs ||= Dir[File.join(root, '*')].select{|d| File.directory?(d) }.map { |path| Directory.new(path, filter_re) }
end

#get_metrics(metrics, since) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/graphiterb/monitors/directory_tree.rb', line 27

def get_metrics metrics, since
  recent = Directory.recency_filter
  dirs.each do |dir|
    metrics << [scope(dir.name, 'num_files', graphite_identifier), dir.num_files(&recent)   ]
    metrics << [scope(dir.name, 'size', graphite_identifier),      dir.size(&recent)        ]
    metrics << [scope(dir.name, 'lines', graphite_identifier),     dir.line_counts(&recent) ]
  end
end