Skip to content

Instantly share code, notes, and snippets.

@pablobm
Created May 27, 2014 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablobm/30cfe041224740182005 to your computer and use it in GitHub Desktop.
Save pablobm/30cfe041224740182005 to your computer and use it in GitHub Desktop.
require 'pp'
def substitute_command_tokens(check)
unmatched_tokens = Array.new
substituted = check[:command].gsub(/:::([^:]*?):::/) do
token, default = $1.to_s.split('|', -1)
matched = token.split('.').inject(@settings[:client]) do |client, attribute|
if client[attribute].nil?
default.nil? ? break : default
else
client[attribute]
end
end
pp matched
if matched.nil?
unmatched_tokens << token
end
matched
end
[substituted, unmatched_tokens]
end
check = {
command: "PATH=$PATH:/usr/local/nagios/libexec check_activemq -H :::amq.host|localhost-8161|||::: -q mailer.fast -w 5:5 -c 2:5 -C"
}
@settings = {
client: {
'amq' => {
'host' => 'foobar'
}
}
}
pp substitute_command_tokens(check)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment