Class | MCollective::Application::Inventory |
In: |
plugins/mcollective/application/inventory.rb
|
Parent: | MCollective::Application |
# File plugins/mcollective/application/inventory.rb, line 121 121: def agents 122: @node[:agents] 123: end
# File plugins/mcollective/application/inventory.rb, line 117 117: def classes 118: @node[:classes] 119: end
# File plugins/mcollective/application/inventory.rb, line 113 113: def facts 114: @node[:facts] 115: end
# File plugins/mcollective/application/inventory.rb, line 105 105: def fields(&blk) 106: @flds = blk 107: end
Helpers to create a simple DSL for scriptlets
# File plugins/mcollective/application/inventory.rb, line 101 101: def format(fmt) 102: @fmt = fmt 103: 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 193 193: def formatted_inventory(&blk) 194: require 'formatr' 195: 196: raise "Need to give a block to formatted_inventory" unless block_given? 197: 198: blk.call if block_given? 199: 200: raise "Need to define page body format" if @page_body.nil? 201: 202: body_fmt = FormatR::Format.new(@page_heading, @page_body) 203: body_fmt.setPageLength(@page_length) 204: time = Time.now 205: 206: util = rpcclient("rpcutil", :options => @options) 207: util.progress = false 208: 209: util.inventory do |t, resp| 210: @node = {:identity => resp[:sender], 211: :facts => resp[:data][:facts], 212: :classes => resp[:data][:classes], 213: :agents => resp[:data][:agents]} 214: 215: body_fmt.printFormat(binding) 216: end 217: rescue Exception => e 218: STDERR.puts "Could not create report: #{e.class}: #{e}" 219: exit 1 220: end
# File plugins/mcollective/application/inventory.rb, line 109 109: def identity 110: @node[:identity] 111: 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 145 145: def inventory(&blk) 146: raise "Need to give a block to inventory" unless block_given? 147: 148: blk.call if block_given? 149: 150: raise "Need to define a format" if @fmt.nil? 151: raise "Need to define inventory fields" if @flds.nil? 152: 153: util = rpcclient("rpcutil", :options => @options) 154: util.progress = false 155: 156: util.inventory do |t, resp| 157: @node = {:identity => resp[:sender], 158: :facts => resp[:data][:facts], 159: :classes => resp[:data][:classes], 160: :agents => resp[:data][:agents]} 161: 162: puts @fmt % @flds.call 163: end 164: end
# File plugins/mcollective/application/inventory.rb, line 228 228: def main 229: if configuration[:script] 230: if File.exist?(configuration[:script]) 231: eval(File.read(configuration[:script])) 232: else 233: raise "Could not find script to run: #{configuration[:script]}" 234: end 235: else 236: node_inventory 237: end 238: 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: end
# File plugins/mcollective/application/inventory.rb, line 133 133: def page_body(fmt) 134: @page_body = fmt 135: end
# File plugins/mcollective/application/inventory.rb, line 129 129: def page_heading(fmt) 130: @page_heading = fmt 131: end
# File plugins/mcollective/application/inventory.rb, line 125 125: def page_length(len) 126: @page_length = len 127: 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