Thursday 15 July 2010

ruby - Puppet: all custom facts gets all results -



ruby - Puppet: all custom facts gets all results -

i´m trying work out way in puppet current zpool capacity numbers freebsd storage servers, storing them in custom facts , generate alert if capacity reaches "too high" level. closest match problem i´ve found far is: returning multiple custom facts puppet facter

that pointed me 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) setcode zpools = io.popen('for in $(zpool list -h -o name); echo $i; done').read.chomp.split("\n") end end def addzpoolcapacityfact(zpool) zpool_capacity = io.popen('zpool -h -o value capacity #{zpool}').read.tr('%','').chomp facter.add("capacity_" + zpool) setcode zpool_capacity end end end zpools = facter.value(:zpools) zpools.each |zpool| addzpoolcapacityfact(zpool) end end end

but doesn´t quite produce result expecting, e.g:

capacity_pool1: 10 30 capacity_pool2: 10 30

when expecting:

capacity_pool1: 10 capacity_pool2: 30

what doing wrong?

ok, solved!

the problem using io.popen 2 times in same script, though tried nil'ing variables, first split function applied variable 'zpools' run on 'zpool_capacity', think, made result like:

"capacity_pool1":"10\n12","capacity_pool2":"10\n12"

notice '\n' between numbers? i´m sure there´s ruby way able utilize io.popen multiple times don´t know how, changed commands execute plain backticks (`) , here´s 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) setcode zpools = `for in $(zpool list -h -o name); echo $i; done`.chomp.split("\n") end end def addzpoolcapacityfact(zpool) zpool_capacity = `zpool -h -o value capacity #{zpool}`.tr('%','').chomp facter.add(zpool + "_capacity") setcode zpool_capacity end end end zpools = facter.value(:zpools) zpools.each |zpool| addzpoolcapacityfact(zpool) end end end

now result looks i´d expect:

pool1_capacity: 10 pool2_capacity: 30

ruby puppet facter

No comments:

Post a Comment