Advertisement
connaryscott

mcollective/agent/registration.rb

Sep 17th, 2012
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. module MCollective
  2. module Agent
  3. # A registration agent that places information from the meta
  4. # registration class into a mongo db instance.
  5. #
  6. # To get this going you need:
  7. #
  8. # - The meta registration plugin everywhere [1]
  9. # - A mongodb instance
  10. # - The mongo gem installed ideally with the bson_ext extension
  11. #
  12. # The following configuration options exist:
  13. # - plugin.registration.mongohost where the mongodb is default: localhost
  14. # - plugin.registration.mongodb the db name default: puppet
  15. # - plugin.registration.collection the collection name default: nodes
  16. # - plugin.registration.extra_yaml_dir the presense of this key indicates a directory
  17. # containing additional yaml files to push into an 'extra' key in the registration data. All files
  18. # in the directory are pushed into a hash with keys being the file name (with the
  19. # path and .yml extension stripped) and the value being the contents. default: false
  20. # Each document will have the following data:
  21. # - fqdn - the fqdn of the sender
  22. # - lastseen - last time we got data from it
  23. # - facts - a collection of facts
  24. # - agentlist - a collection of agents on the node
  25. # - classes - a collection of classes
  26. #
  27. # A unique constraint index will be created on the fqdn of the sending
  28. # hosts.
  29. #
  30. # Released under the terms of the Apache 2 licence, contact
  31. # rip@devco.net with questions
  32. class Registration
  33. attr_reader :timeout, :meta
  34.  
  35. def initialize
  36. @meta = {:license => "Apache 2",
  37. :author => "R.I.Pienaar <rip@devco.net>",
  38. :url => "https://github.com/puppetlabs/mcollective-plugins"}
  39.  
  40. require 'mongo'
  41.  
  42. @timeout = 2
  43.  
  44. @config = Config.instance
  45.  
  46. @mongohost = @config.pluginconf["registration.mongohost"] || "localhost"
  47. @mongodb = @config.pluginconf["registration.mongodb"] || "puppet"
  48. @collection = @config.pluginconf["registration.collection"] || "nodes"
  49. @yaml_dir = @config.pluginconf["registration.extra_yaml_dir"] || false
  50.  
  51. Log.instance.debug("Connecting to mongodb @ #{@mongohost} db #{@mongodb} collection #{@collection}")
  52.  
  53. @dbh = Mongo::Connection.new(@mongohost).db(@mongodb)
  54. @coll = @dbh.collection(@collection)
  55.  
  56. @coll.create_index("fqdn", {:unique => true, :dropDups => true})
  57. end
  58.  
  59. def handlemsg(msg, connection)
  60. req = msg[:body]
  61.  
  62. if (req.kind_of?(Array))
  63. Log.instance.warn("Got no facts - did you forget to add 'registration = Meta' to your server.cfg?");
  64. return nill
  65. end
  66.  
  67. req[:fqdn] = req[:facts]["fqdn"]
  68. req[:lastseen] = Time.now.to_i
  69.  
  70. # Optionally send a list of extra yaml files
  71. if (@yaml_dir != false)
  72. req[:extra] = {}
  73. Dir[@yaml_dir + "/*.yaml"].each do | f |
  74. req[:extra][File.basename(f).split('.')[0]] = YAML.load_file(f)
  75. end
  76. end
  77.  
  78. # Sometimes facter doesnt send a fqdn?!
  79. if req[:fqdn].nil?
  80. Log.instance.debug("Got stats without a FQDN in facts")
  81. return nil
  82. end
  83.  
  84. by_fqdn = {:fqdn => req[:fqdn]}
  85. #require 'pp'
  86. #pp by_fqdn
  87. doc_id = nil
  88. before = Time.now.to_f
  89. begin
  90. doc = @coll.find_and_modify(:query => by_fqdn, :update => {'$set' => req}, :new => true)
  91. doc_id = doc['_id']
  92. rescue Mongo::OperationFailure
  93. doc_id = @coll.insert(req, {:safe => true})
  94. ensure
  95. after = Time.now.to_f
  96. Log.instance.debug("Updated data for host #{req[:fqdn]} with id #{doc_id} in #{after - before}s")
  97. end
  98.  
  99. nil
  100. end
  101.  
  102. def help
  103. end
  104. end
  105. end
  106. end
  107.  
  108. # vi:tabstop=2:expandtab:ai:filetype=ruby
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement