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. Closest match to my problem that I´ve found so far is: Returning multiple custom facts with puppet 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:
"capacity_pool1":"10\n12","capacity_pool2":"10\n12"
Notice the '\\n' between the numbers? 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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.