我试图在Puppet中找到一种方法来获取我的FreeBSD存储服务器的当前zpool容量数,将它们存储在自定义事实中,并在容量达到“过高”级别时生成警报。我到目前为止找到的问题最接近的是: Returning multiple custom facts with puppet Facter
这使我想到了这个解决方案:
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
但是并没有产生我期待的结果,例如:
capacity_pool1: 10 30
capacity_pool2: 10 30
当我真的在期待:
capacity_pool1: 10
capacity_pool2: 30
我做错了什么?
答案 0 :(得分:1)
好的,解决了!
问题是在同一个脚本中使用了IO.popen两次,即使我尝试了变量,但是应用于变量'zpools'的第一个拆分函数也在'zpool_capacity'上运行,我想,这使得结果如下:
"capacity_pool1":"10\n12","capacity_pool2":"10\n12"
注意数字之间的'\ n'?我确定有一种Ruby方式可以多次使用IO.popen,但我不知道怎么做,所以我只是用简单的反引号(`)改变命令执行,这里是工作代码:< / p>
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
现在结果看起来像我期待的那样:
pool1_capacity: 10
pool2_capacity: 30