Skip to content

Instantly share code, notes, and snippets.

@andre
Last active December 16, 2015 03:59
Show Gist options
  • Save andre/5373610 to your computer and use it in GitHub Desktop.
Save andre/5373610 to your computer and use it in GitHub Desktop.
# A Scout plugin to perform simple count of files in a directory using regex.
#
# path - Provide the path to a directory you would like to check.
# file_regex - Provide the regex you want to use to look for file under the path.
# http://ruby-doc.org/core-2.0/Dir.html#method-c-glob
#
# Created by Gerric Chaplin <[email protected]>
class SimpleFileCount < Scout::Plugin
OPTIONS=<<-EOS
path:
name: path
notes: The path to a directory (including trailing slash) you would like to check for files.
default: "/tmp/"
file_regex:
name: file_regex
notes: Provide the regex you want to use to look for file under the path.
default: "*.tmp"
EOS
def build_report
path = option(:path) || "/tmp/"
file_regex = option(:file_regex) || "*.tmp"
exists = File.exists?(path)
if exists
count = Dir.glob("#{path}#{file_regex}").size
report(:file_count => count)
else
alert(:subject => "Error running Simple File Count plugin. Directory does not exist.", :body => "#{path} does not exist")
end
rescue Exception => e
error(:subject => 'Error running Simple File Count plugin', :body => e)
return -1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment