I cannot seem to get the syntax right for using search in a recipe. I am hoping someone can help!
What I am ultimately trying to accomplish:
When chef-client runs on each machine, it "discovers" what needs to be monitored on each machine and sets the attribute node.default['monitoring']['nagios']['nrpe']['commands'] = [ 'x' , 'y', 'z' ]. This part seems to be working fine.
Next I want to run a recipe on the nagios server that generates the configs for each machine. To do this I need to query default['monitoring']['nagios']['nrpe']['commands'] for each server to generate what needs to be monitored.
Running this from the command line retrieves what I am looking for:
knife search node "name:tempmachine2 AND monitoring:*" -a monitoring.nagios.nrpe.commands
tempmachine2:
monitoring.nagios.nrpe.commands:
check_cron_procs
check_nfs_mounts
check_gmond_procs
check_mailqueue
check_postfix_proc
...
But I am not sure how to do this in a recipe. I thought this is what I needed, but this does not work:
nrpe_hosts = search(:node, "name:tempmachine2 and monitoring:*")
nrpe_hosts.each do |nrpe_list|
nrpe_list['monitoring']['nagios']['nrpe']['commands'].each do |cmd|
Chef::Log.warn("NRPE CMD: " + cmd)
end
end
but that ends with a:
NoMethodError
-------------
undefined method `[]' for nil:NilClass
What am I missing? Or maybe I am trying to do something that is not possible?
I 'll also mention that I am running this in test kitchen, but I have dumped tempmachine2 (and other machines) configs into test/integration/nodes/tempmachine2.json with knife -Fj. Other search queries in test kitchen seem to work fine - and I also see all the monitoring attributes in the json files, so I know they are there!
example of working search in test kitchen:
newton_servers = search(:node, 'tags:auto_newton')
newton_servers.each do | curr_server |
hostname = curr_server.name.split(".")
Chef::Log.warn("NAGIOS HOST: #{hostname[0]}")
end
Thanks!