Class | MCollective::Application::Inventory |
In: |
plugins/mcollective/application/inventory.rb
|
Parent: | MCollective::Application |
# File plugins/mcollective/application/inventory.rb, line 123 123: def agents 124: @node[:agents] 125: end
# File plugins/mcollective/application/inventory.rb, line 119 119: def classes 120: @node[:classes] 121: end
# File plugins/mcollective/application/inventory.rb, line 115 115: def facts 116: @node[:facts] 117: end
# File plugins/mcollective/application/inventory.rb, line 107 107: def fields(&blk) 108: @flds = blk 109: end
Helpers to create a simple DSL for scriptlets
# File plugins/mcollective/application/inventory.rb, line 103 103: def format(fmt) 104: @fmt = fmt 105: end
Use the ruby formatr gem to build reports using Perls formats
It is kind of ugly but brings a lot of flexibility in report writing without building an entire reporting language.
You need to have formatr installed to enable reports like:
formatted_inventory do page_length 20 page_heading <<TOP Node Report @<<<<<<<<<<<<<<<<<<<<<<<<< time Hostname: Customer: Distribution: ------------------------------------------------------------------------- TOP page_body <<BODY @<<<<<<<<<<<<<<<< @<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< identity, facts["customer"], facts["lsbdistdescription"] @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< facts["processor0"] BODY end
# File plugins/mcollective/application/inventory.rb, line 197 197: def formatted_inventory(&blk) 198: require 'formatr' 199: 200: raise "Need to give a block to formatted_inventory" unless block_given? 201: 202: blk.call if block_given? 203: 204: raise "Need to define page body format" if @page_body.nil? 205: 206: body_fmt = FormatR::Format.new(@page_heading, @page_body) 207: body_fmt.setPageLength(@page_length) 208: time = Time.now 209: 210: util = rpcclient("rpcutil", :options => @options) 211: util.progress = false 212: 213: util.inventory do |t, resp| 214: @node = {:identity => resp[:sender], 215: :facts => resp[:data][:facts], 216: :classes => resp[:data][:classes], 217: :agents => resp[:data][:agents]} 218: 219: body_fmt.printFormat(binding) 220: end 221: 222: util.disconnect 223: rescue Exception => e 224: STDERR.puts "Could not create report: #{e.class}: #{e}" 225: exit 1 226: end
# File plugins/mcollective/application/inventory.rb, line 111 111: def identity 112: @node[:identity] 113: end
Expects a simple printf style format and apply it to each node:
inventory do format "%s:\t\t%s\t\t%s" fields { [ identity, facts["serialnumber"], facts["productname"] ] } end
# File plugins/mcollective/application/inventory.rb, line 147 147: def inventory(&blk) 148: raise "Need to give a block to inventory" unless block_given? 149: 150: blk.call if block_given? 151: 152: raise "Need to define a format" if @fmt.nil? 153: raise "Need to define inventory fields" if @flds.nil? 154: 155: util = rpcclient("rpcutil", :options => @options) 156: util.progress = false 157: 158: util.inventory do |t, resp| 159: @node = {:identity => resp[:sender], 160: :facts => resp[:data][:facts], 161: :classes => resp[:data][:classes], 162: :agents => resp[:data][:agents]} 163: 164: puts @fmt % @flds.call 165: end 166: 167: util.disconnect 168: end
# File plugins/mcollective/application/inventory.rb, line 234 234: def main 235: if configuration[:script] 236: if File.exist?(configuration[:script]) 237: eval(File.read(configuration[:script])) 238: else 239: raise "Could not find script to run: #{configuration[:script]}" 240: end 241: else 242: node_inventory 243: end 244: end
# File plugins/mcollective/application/inventory.rb, line 48 48: def node_inventory 49: node = configuration[:node] 50: 51: util = rpcclient("rpcutil", :options => options) 52: util.identity_filter node 53: util.progress = false 54: 55: nodestats = util.custom_request("daemon_stats", {}, node, {"identity" => node}) 56: 57: util.custom_request("inventory", {}, node, {"identity" => node}).each do |resp| 58: puts "Inventory for #{resp[:sender]}:" 59: puts 60: 61: if nodestats.is_a?(Array) 62: nodestats = nodestats.first[:data] 63: 64: puts " Server Statistics:" 65: puts " Version: #{nodestats[:version]}" 66: puts " Start Time: #{Time.at(nodestats[:starttime])}" 67: puts " Config File: #{nodestats[:configfile]}" 68: puts " Process ID: #{nodestats[:pid]}" 69: puts " Total Messages: #{nodestats[:total]}" 70: puts " Messages Passed Filters: #{nodestats[:passed]}" 71: puts " Messages Filtered: #{nodestats[:filtered]}" 72: puts " Replies Sent: #{nodestats[:replies]}" 73: puts " Total Processor Time: #{nodestats[:times][:utime]} seconds" 74: puts " System Time: #{nodestats[:times][:stime]} seconds" 75: 76: puts 77: end 78: 79: puts " Agents:" 80: resp[:data][:agents].sort.in_groups_of(3, "") do |agents| 81: puts " %-15s %-15s %-15s" % agents 82: end 83: puts 84: 85: puts " Configuration Management Classes:" 86: resp[:data][:classes].sort.in_groups_of(2, "") do |klasses| 87: puts " %-30s %-30s" % klasses 88: end 89: puts 90: 91: puts " Facts:" 92: resp[:data][:facts].sort_by{|f| f[0]}.each do |f| 93: puts " #{f[0]} => #{f[1]}" 94: end 95: 96: break 97: end 98: 99: util.disconnect 100: end
# File plugins/mcollective/application/inventory.rb, line 135 135: def page_body(fmt) 136: @page_body = fmt 137: end
# File plugins/mcollective/application/inventory.rb, line 131 131: def page_heading(fmt) 132: @page_heading = fmt 133: end
# File plugins/mcollective/application/inventory.rb, line 127 127: def page_length(len) 128: @page_length = len 129: end
# File plugins/mcollective/application/inventory.rb, line 38 38: def post_option_parser(configuration) 39: configuration[:node] = ARGV.shift if ARGV.size > 0 40: end