sensu-plugins / sensu-plugins-filesystem-checks

This plugin provides native instrumentation for monitoring and metrics collection, including:health, usage, and various metrics of filesystem attributes.
http://sensu-plugins.io
MIT License
8 stars 25 forks source link

New Feature: Check File Size when given a directory path #13

Open codepattern opened 8 years ago

codepattern commented 8 years ago

bin/check-file-size.rb - I had a need to loop through several files in a directory and alert when the threshold was breach .. either warning or critical. Here's a snippet .. let me know if it's worth adding it to the base

option :directory, description: 'Directory to stat (full path)', short: '-d DIR', long: '--dir DIR', required: false

def stat_directory begin

Subtract two for '.' and '..'

  num_files = Dir.entries(config[:directory]).count - 2
rescue
  unknown "Error listing files in #{config[:directory]}"
end
# Create an empty array.
critical_values = Array[]
warning_values = Array[]

# Loop through the directory array.
d = Dir.entries(config[:directory]).select {|f| !File.directory? f}
# puts d
#Dir.open(config[:directory]).each do |file_name|
d.each do |file_name|
  #next if File.directory? file_name
  temp= "#{config[:directory]}/#{file_name}"
  stat = File.stat(temp)
  $stdout.puts stat.inspect if config[:debug] == true
  @file_size = stat.size

  if @file_size >= config[:crit].to_i
    # critical "#{config[:file]} is greater than #{format_bytes(config[:crit].to_i)} bytes! [actual size: #{format_bytes(@file_size)} bytes]"
   # critical_values.push ("#{file_name} size: #{format_bytes(@file_size)} bytes" + "\r\n")
    critical_values.push ("#{file_name} size: #{format_bytes(@file_size)} bytes")
  elsif @file_size >= config[:warn].to_i
    # warning "#{config[:file]} is greater than #{format_bytes(config[:warn].to_i)} bytes! [actual size: #{format_bytes(@file_size)} bytes]"
    warning_values.push ("#{file_name} size: #{format_bytes(@file_size)} bytes")  
  end
end

if critical_values.any? 
  critical "#{critical_values} is/are greater than #{format_bytes(config[:crit].to_i)} bytes!"
elsif warning_values.any?
  warning "#{warning_values} is/are greater than #{format_bytes(config[:warn].to_i)} bytes! "
else
  ok "All files in the directory is/are within size limit"
end

end

majormoses commented 7 years ago

https://github.com/sensu-plugins/sensu-plugins-filesystem-checks/issues/17

majormoses commented 7 years ago

Is there any reason to not just make it take a glob and then check each item? that way there is really no difference between a file or directory.