Skip to content
This repository has been archived by the owner on Jun 13, 2020. It is now read-only.

Commit

Permalink
Added support to specify a directory for custom formatters.
Browse files Browse the repository at this point in the history
- The builtin formatters are source first.
- If a directory containing custom formatters is specified (via 'formatter_dir' in the config or -F on the command line)
 they will be sourced second.
- Any conflicts (i.e. custom formatters with the same name as a built in) will be handled by giving priority to the custom formatter(s).
  • Loading branch information
Ryan Frantz committed Jun 5, 2014
1 parent ec2f551 commit 0fdff00
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
49 changes: 40 additions & 9 deletions lib/nagios-herald/formatter_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,51 @@ module NagiosHerald
class FormatterLoader
include NagiosHerald::Logging

attr_accessor :formatter_path
attr_accessor :builtin_formatter_path
attr_accessor :custom_formatter_path

# Public: Initialize the formatter loader module.
# Adds the path to the built-in formatters to the module variable @formatter_paths.
# Adds the path to custom formatters if that option is passed to `nagios-herald`
# via '-F|--formatter-dir' on the command line or via the 'formatter-dir'
# option in the configuration file.
def initialize
# TODO: add support for @options.formatter_path
@formatter_path = File.expand_path("formatters", File.dirname(__FILE__))
@builtin_formatter_path = File.expand_path("formatters", File.dirname(__FILE__))
@custom_formatter_path = Config.config['formatter_dir'] if Config.config['formatter_dir']
end

# Public: Enumerate the available formatter class files.
#
# Returns an array of the class files' absolute paths
def enum_formatter_class_files(formatter_path)
formatter_class_files = Dir.glob(File.expand_path("*.rb", formatter_path))
# Returns a hash of the class files' absolute paths whose
# key is the formatter file's basename and the
# value is the full path to the file.
def enum_formatter_class_files
formatter_class_files = {}

# Iterate over the builtin formatters first.
builtin_formatter_class_files = Dir.glob(File.expand_path("*.rb", @builtin_formatter_path))
builtin_formatter_class_files.each do |builtin_formatter_class_file|
formatter_class_files[File.basename(builtin_formatter_class_file)] = builtin_formatter_class_file
end
# If we've been told about custom formatters, add them to the hash.
# If there are conflicts, naively merge them, with the custom formatters getting
# priority.
if @custom_formatter_path
custom_formatters = {}
custom_formatter_class_files = Dir.glob(File.expand_path("*.rb", @custom_formatter_path))
custom_formatter_class_files.each do |custom_formatter_class_file|
custom_formatters[File.basename(custom_formatter_class_file)] = custom_formatter_class_file
end
formatter_class_files.merge!(custom_formatters)
end
formatter_class_files
end

# Public: An array of class files' paths.
#
# Returns an array of class files' paths.
def formatter_class_files
@formatter_class_files ||= enum_formatter_class_files(@formatter_path)
@formatter_class_files ||= enum_formatter_class_files
end

# Public: Load the formatters into the namespace.
Expand All @@ -37,10 +63,15 @@ def formatter_class_files
# Returns nothing but loads the classes into the namespace.
def load_formatters
if formatter_class_files.empty?
logger.fatal "#{$0}: No formatters were found in '#{@formatter_path}'"
if @custom_formatter_path
puts "#{$0}: No formatters were found in '#{@builtin_formatter_path}'" \
" or '#{@custom_formatter_path}' (as defined by the 'formatter_dir' option)!"
else
puts "#{$0}: No formatters were found in '#{@builtin_formatter_path}'!"
end
exit 1
else
formatter_class_files.each do |formatter_class_file|
formatter_class_files.each do |formatter_class, formatter_class_file|
Kernel.load formatter_class_file
end
end
Expand Down
3 changes: 0 additions & 3 deletions lib/nagios-herald/formatters/check_disk.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# CheckDisk formatter
# Colorizes and bolds text generated by the 'check_disk' NRPE check.
# Downloads a Ganglia graph of the host's overall disk utilization.
# Queries Splunk to determine how many times this alert has fired for this host
# in the last 7 days.

module NagiosHerald
class Formatter
Expand Down

0 comments on commit 0fdff00

Please sign in to comment.