Class: Graphiterb::Monitors::DirectoryTree::Directory
- Inherits:
-
Object
- Object
- Graphiterb::Monitors::DirectoryTree::Directory
- Defined in:
- lib/graphiterb/monitors/directory_tree.rb
Overview
A class for monitoring the contents of a directory.
Constant Summary collapse
- MAX_FILES =
Only consider the last this-many files.
30
Instance Attribute Summary collapse
-
#filter_re ⇒ Object
Wildcard sequence for files under the current directory.
-
#path ⇒ Object
Path to sample for files.
-
#recent_window ⇒ Object
A recent file was modified within this window.
Class Method Summary collapse
Instance Method Summary collapse
- #avg_size(&filter_block) ⇒ Object
-
#files(&filter_block) ⇒ Object
Lists all files in the pool.
-
#initialize(path, filter_re = /.*/, options = {}) ⇒ Directory
constructor
A new instance of Directory.
- #line_counts(&filter_block) ⇒ Object
- #lines_in_result_of(command, *args) ⇒ Object
-
#name ⇒ String
Name for this pool, suitable for inclusion in a Graphite target.
- #num_files(&filter_block) ⇒ Object
- #size(&filter_block) ⇒ Object
- #sizes(&filter_block) ⇒ Object
- #wc ⇒ Object
Constructor Details
#initialize(path, filter_re = /.*/, options = {}) ⇒ Directory
Returns a new instance of Directory.
51 52 53 54 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 51 def initialize path, filter_re=/.*/, ={} self.path = path self.filter_re = filter_re end |
Instance Attribute Details
#filter_re ⇒ Object
Wildcard sequence for files under the current directory.
43 44 45 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 43 def filter_re @filter_re end |
#path ⇒ Object
Path to sample for files
40 41 42 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 40 def path @path end |
#recent_window ⇒ Object
A recent file was modified within this window.
46 47 48 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 46 def recent_window @recent_window end |
Class Method Details
.recency_filter ⇒ Object
115 116 117 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 115 def self.recency_filter Proc.new{|file| recent?(file) } end |
.recent?(file) ⇒ Boolean
111 112 113 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 111 def self.recent? file (Time.now - File.mtime(file)) < 3600 end |
Instance Method Details
#avg_size(&filter_block) ⇒ Object
88 89 90 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 88 def avg_size &filter_block sizes(&filter_block).sum.to_f / num_files(&filter_block).to_f end |
#files(&filter_block) ⇒ Object
Lists all files in the pool
68 69 70 71 72 73 74 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 68 def files &filter_block Dir[File.join(path, '**/*')]. reject{|f| File.directory?(f) }. select{|f| f =~ filter_re }. sort.reverse[0..MAX_FILES]. select(&filter_block) end |
#line_counts(&filter_block) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 104 def line_counts &filter_block files = files(&filter_block) ; return 0 if files.blank? result = lines_in_result_of(wc, '-l', *files) or return 0 counts = result.map{|string| string =~ /^\s*(\d+)\s+/ and $1 }.compact counts.map(&:to_i).sum end |
#lines_in_result_of(command, *args) ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 92 def lines_in_result_of command, *args begin escaped_args = args.map{|f| "'#{f}'" } result = `#{command} #{escaped_args.join(" ")}`.chomp result.split(/[\r\n]+/) rescue StandardError => e ; warn(e.backtrace, e) ; return nil ; end end |
#name ⇒ String
Name for this pool, suitable for inclusion in a Graphite target.
60 61 62 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 60 def name path.gsub(/\./,'_').gsub(%r{/}, '.').gsub(%r{(^\.|\.$)},'') end |
#num_files(&filter_block) ⇒ Object
76 77 78 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 76 def num_files &filter_block files(&filter_block).count end |
#size(&filter_block) ⇒ Object
84 85 86 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 84 def size &filter_block sizes(&filter_block).sum end |
#sizes(&filter_block) ⇒ Object
80 81 82 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 80 def sizes &filter_block files(&filter_block).map{|f| File.size(f) rescue nil }.compact end |
#wc ⇒ Object
100 101 102 |
# File 'lib/graphiterb/monitors/directory_tree.rb', line 100 def wc @wc ||= `which wc`.chomp end |