我目前正在为我们提供的服务添加filebeat支持; 对于每项服务,我们都有几个我要跟踪的日志文件; 我有1个常见的文件拍方法,并且要区分不同服务的日志在不同属性.rb文件中; 在那些日志中,我有一个单独的定义;
所有定义都具有相同的“ fileds”配置; 我可以将它添加到所有配置都可以使用的地方吗?
我的结构:
cookbooks
common
recipes
filebeat.rb
services
attributes
service1.rb
service2.rb
servicesX.rb的内容具有以下定义:
access_log = {
'paths' => ['TBD'],
'input_type' => 'log',
'fields' => {
'hostname' => node["opsworks"]["instance"]["hostname"],
'customer' => node["opsworks"]["instance"]["layers"][0],
'internal_ip' => node["opsworks"]["instance"]["private_ip"],
'ec2id' => node["opsworks"]["instance"]["aws_instance_id"],
'os' => node["opsworks"]["instance"]["os"],
'instance_type' => node["opsworks"]["instance"]["instance_type"] },
'fields_under_root' => true
}
audit_log = {
'paths' => ['TBD'],
'input_type' => 'log',
'fields' => {
'hostname' => node["opsworks"]["instance"]["hostname"],
'customer' => node["opsworks"]["instance"]["layers"][0],
'internal_ip' => node["opsworks"]["instance"]["private_ip"],
'ec2id' => node["opsworks"]["instance"]["aws_instance_id"],
'os' => node["opsworks"]["instance"]["os"],
'instance_type' => node["opsworks"]["instance"]["instance_type"]
},
'fields_under_root' => true
}
我如何提取
'fields' => {
'hostname' => node["opsworks"]["instance"]["hostname"],
'customer' => node["opsworks"]["instance"]["layers"][0],
'internal_ip' => node["opsworks"]["instance"]["private_ip"],
'ec2id' => node["opsworks"]["instance"]["aws_instance_id"],
'os' => node["opsworks"]["instance"]["os"],
'instance_type' => node["opsworks"]["instance"]["instance_type"]
是否在同一文件(servicesX.rb)中,以便被所有日志文件定义使用?
注意:我是一个红宝石新手:/
谢谢!
答案 0 :(得分:0)
经过下面的反馈和澄清后,OP似乎希望DRY
代码并重新使用fields
定义。
最简单的方法是将其存储在变量中,然后使用该变量:
fields = {
'hostname' => node["opsworks"]["instance"]["hostname"],
'customer' => node["opsworks"]["instance"]["layers"][0],
'internal_ip' => node["opsworks"]["instance"]["private_ip"],
'ec2id' => node["opsworks"]["instance"]["aws_instance_id"],
'os' => node["opsworks"]["instance"]["os"],
'instance_type' => node["opsworks"]["instance"]["instance_type"]
}
audit_log = {
'paths' => ['TBD'],
'input_type' => 'log',
'fields' => fields
}
但是,这可能会导致如何设置node
出现问题。这实际上取决于其余脚本的流程。在厨师中(假设这是关于厨师的),node
是脚本运行的上下文,因此,太早设置fields
可能会在以后使用node
时产生问题:< / p>
fields = { hostname: node["opsworks"]["instance"]["hostname"] }
# ... do lots of stuff, like fetching, preparing, connecting and whatnot.
fields # now contains the `node` values as set before connecting etc.
如果这是一个问题,一个更好的选择是定义一个方法,该方法从传入的节点返回字段:
def fields(node)
{
'hostname' => node["opsworks"]["instance"]["hostname"],
'customer' => node["opsworks"]["instance"]["layers"][0],
'internal_ip' => node["opsworks"]["instance"]["private_ip"],
'ec2id' => node["opsworks"]["instance"]["aws_instance_id"],
'os' => node["opsworks"]["instance"]["os"],
'instance_type' => node["opsworks"]["instance"]["instance_type"]
}
end
或者,清理:
def fields(node)
instance = node["opsworks"]["instance"]
{
hostname: instance["hostname"],
customer: instance["layers"][0],
internal_ip: instance["private_ip"],
ec2id: instance["aws_instance_id"],
os: instance["os"],
instance_type: instance["instance_type"]
}
end
然后使用该功能:
audit_log = {
'paths' => ['TBD'],
'input_type' => 'log',
'fields' => fields(node)
}
答案 1 :(得分:0)
经过我的研究:实施的好处并不能证明所花的时间是合理的; 离开它;