我正在根据特定主机所具有的角色配置Splunk转发器/opt/splunkforwarder/etc/apps/search/local/inputs.conf文件。我已经定义了监视器,索引和源类型角色特定值,这些值显然存储为JSON。
大多数示例显示如何使用配方中的模板资源创建文件,其中传递给模板资源的参数作为条目存储在attributes / default.rb文件中。
现在我发现了一个与我正在尝试做的事情接近的问题here,但它假设属性是哈希而不是JSON。
我对使用这种方法的犹豫不仅是格式是哈希而不是JSON,而且还有为我们需要发送的主机上运行的每个应用程序有多个监视器,索引和源类型条目的事实到Splunk索引器。因此template.erg将需要遍历JSON并为每个应用程序日志追加新条目。下面是JSON角色的一个小例子。
{
"chef_type": "role",
"default_attributes": {
"gateway_common_input": {
"monitor": "/home/app/logs/common",
"index": "gateway_common",
"sourcetype": "common" },
"gateway_recv_counter_input": {
"monitor": "/home/app/logs/recv_counter",
"index": "gateway_recv_counter",
"sourcetype": "recv_counter" },
"gateway_send_counter_input": {
"monitor": "/home/app/logs/send_counter",
"index": "gateway_send_counter",
"sourcetype": "send_counter" }
},
"description": "Role for Gateway hosts",
"env_run_lists": {},
"json_class": "Chef::Role",
"name": "rgateway",
"override_attributes": {},
"run_list": [
"role[gateway]",
"recipe[gateway_dimensions]"
]
}
编辑: 为了帮助澄清上述定义“rgateway”角色的JSON中的问题,需要将三个应用程序日志添加到Splunk Forwarder inputs.conf文件中; gateway_common_input,gateway_recv_counter_input和gateway_send_counter_input各自具有“monitor”,“index”和“sourcetype”的设置。
inputs.conf文件将有三个这样的条目:
[monitor:///home/app/logs/common/]
disabled = false
index = gateway_common
sourcetype = common
[monitor:///home/app/logs/recv_counter/]
disabled = false
index = gateway_recv_counter
sourcetype = recv_counter
[monitor:///home/app/logs/send_counter]
disabled = false
index = gateway_send_counter
sourcetype = send_counter
其他角色可能有五个条目或只有一个。
答案 0 :(得分:0)
通常在从Chef输出JSON时,您将在Ruby代码中构建数据结构,然后只需在其上调用.to_json
即可获得字符串。
答案 1 :(得分:0)
我找到了我要找的东西here。你是正确的代码陌生人,因为我并没有完全掌握角色属性的运作方式。所以我不太了解如何在我的角色JSON中解析嵌套属性。答案在于正确配置我的模板ERB。在cookbook中,我链接到节点角色属性作为哈希传入,然后ERB迭代哈希。这是来自该食谱的模板ERB来说明
<% @splunk_monitors.each_with_index do |parameters, index| -%>
# Begin new monitor stanza
#
<% parameters.each_key do |monitor_name| -%>
[monitor://<%= parameters[monitor_name]['location'] %>]
disabled = false
<%= "sourcetype = " + parameters[monitor_name]['sourcetype'] if parameters[monitor_name]['sourcetype'] %>
<%= "index = " + parameters[monitor_name]['index'] if parameters[monitor_name]['index'] %>
<%= "whitelist = " + parameters[monitor_name]['whitelist'] if parameters[monitor_name]['whitelist'] %>
<%= "blacklist = " + parameters[monitor_name]['blacklist'] if parameters[monitor_name]['blacklist'] %>
<%= "host = " + parameters[monitor_name]['host'] if parameters[monitor_name]['host'] %>
<%= "crcSalt = <SOURCE>" if parameters[monitor_name]['crcSalt'] %>
<% end -%>
<% end -%>
默认配方以下列方式调用此方法:
template "#{node['splunk']['forwarder']['home']}/etc/apps/search/local/inputs.conf" do
source "inputs.conf.erb"
if node["os"] == "linux"
owner "root"
group "root"
mode "0600"
end
variables ({
:splunk_monitors => node['splunk']['monitors'],
:splunk_scripts => node['splunk']['scripts']
})
notifies :restart, resources(:service => servicename)
end
正如您所见,配方以通常的方式调用模板资源。 &#34;监视&#34;然后使用knife命令设置数组(在我的情况下,它已经由角色定义)。
"override_attributes": { "splunk": { "monitors": [ { "thisistheshortlogname": { "location": "/var/log/fileordirectory", "index": "couldomitthis", "sourcetype": "", #optional "whitelist": "", #optional "blacklist": "", #optional "crcSalt": "" #optional } } ], ...