[英]Puppet: all custom facts gets all results
I´m trying to work out a way in Puppet to get the current zpool capacity numbers for my FreeBSD storage servers, storing them in custom facts and to generate alert if capacity reaches a "too high" level. 我正在尝试在Puppet中找到一种方法来获取FreeBSD存储服务器的当前zpool容量数字,将其存储在自定义事实中,并在容量达到“过高”水平时生成警报。 Closest match to my problem that I´ve found so far is: Returning multiple custom facts with puppet Facter 到目前为止,与我发现的问题最接近的匹配是:使用: Facter返回多个自定义事实
That pointed me to this solution: 那使我想到了这个解决方案:
operatingsystem = Facter.value('operatingsystem')
case operatingsystem
when "FreeBSD"
present_zpools = IO.popen('zpool list -H -o name').read.chomp
if ! present_zpools.empty?
Facter.add(:zpools) do
setcode do
zpools = IO.popen('for i in $(zpool list -H -o name); do echo $i; done').read.chomp.split("\n")
end
end
def addZpoolCapacityFact(zpool)
zpool_capacity = IO.popen('zpool get -H -o value capacity #{zpool}').read.tr('%','').chomp
Facter.add("capacity_" + zpool) do
setcode do
zpool_capacity
end
end
end
zpools = Facter.value(:zpools)
zpools.each do |zpool|
addZpoolCapacityFact(zpool)
end
end
end
But doesn´t quite produce the result I was expecting, eg: 但是并没有产生我所期望的结果,例如:
capacity_pool1: 10 30
capacity_pool2: 10 30
When I was really expecting: 当我真的期望:
capacity_pool1: 10
capacity_pool2: 30
What am I doing wrong? 我究竟做错了什么?
OK, solved! 好,解决了!
The problem was using IO.popen two times in same script, even though I tried nil'ing the variables, the first split function applied to variable 'zpools' was also run on 'zpool_capacity', I think, which made the result look like: 问题是我在同一脚本中两次使用IO.popen,即使我尝试对变量进行nil操作,我认为,应用于变量'zpools'的第一个拆分函数也运行在'zpool_capacity'上,这使得结果看起来像:
"capacity_pool1":"10\n12","capacity_pool2":"10\n12"
Notice the '\\n' between the numbers? 注意数字之间的“ \\ n”吗? I´m sure there´sa Ruby way to be able to use IO.popen multiple times but I don´t know how, so I just changed the commands to execute with plain backticks (`) and here´s the working code: 我敢肯定,有一种可以多次使用IO.popen的Ruby方法,但是我不知道如何,所以我只更改了命令以使用普通反引号(`)执行,这是工作代码:
operatingsystem = Facter.value('operatingsystem')
case operatingsystem
when "FreeBSD"
present_zpools = `zpool list -H -o name`.chomp
if ! present_zpools.empty?
Facter.add(:zpools) do
setcode do
zpools = `for i in $(zpool list -H -o name); do echo $i; done`.chomp.split("\n")
end
end
def addZpoolCapacityFact(zpool)
zpool_capacity = `zpool get -H -o value capacity #{zpool}`.tr('%','').chomp
Facter.add(zpool + "_capacity") do
setcode do
zpool_capacity
end
end
end
zpools = Facter.value(:zpools)
zpools.each do |zpool|
addZpoolCapacityFact(zpool)
end
end
end
Now result looks like I´d expect: 现在结果看起来像我期望的那样:
pool1_capacity: 10
pool2_capacity: 30
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.