logstash-input-mongoprofile 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/logstash/inputs/mongoprofile.rb +15 -11
- data/lib/mongo/mongo.rb +117 -0
- data/logstash-input-mongoprofile.gemspec +2 -1
- metadata +16 -1
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: ad2c83c07c1e370fa683203d213142bc81851b63
         | 
| 4 | 
            +
              data.tar.gz: 6f40e525678199523c6327c6c5509d45604854ec
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 440a31af594147a9a061438c714a6c59c4326830b552fa6e6c69037058df12bca57493bd758666f285d722a3292f301a7a3d21daef1e2a6c12dc2b63bd82eb20
         | 
| 7 | 
            +
              data.tar.gz: 8045bf03213d5fd602f3f17ef243d1688f21eedfb0e4e64ed92065b97dfa19bc398172325206b73e2beab1b315296d5f87b66caae13afa00c50178c4efe7f065
         | 
| @@ -3,6 +3,7 @@ require "logstash/inputs/base" | |
| 3 3 | 
             
            require "logstash/namespace"
         | 
| 4 4 | 
             
            require "stud/interval"
         | 
| 5 5 | 
             
            require "socket" # for Socket.gethostname
         | 
| 6 | 
            +
            require '../../../lib/mongo/mongo'
         | 
| 6 7 |  | 
| 7 8 | 
             
            # Generate a repeating message.
         | 
| 8 9 | 
             
            #
         | 
| @@ -11,28 +12,31 @@ require "socket" # for Socket.gethostname | |
| 11 12 | 
             
            class LogStash::Inputs::Mongoprofile < LogStash::Inputs::Base
         | 
| 12 13 | 
             
              config_name "mongoprofile"
         | 
| 13 14 |  | 
| 14 | 
            -
              # If undefined, Logstash will complain, even if codec is unused.
         | 
| 15 15 | 
             
              default :codec, "plain"
         | 
| 16 16 |  | 
| 17 | 
            -
               | 
| 18 | 
            -
              config : | 
| 19 | 
            -
             | 
| 20 | 
            -
               | 
| 21 | 
            -
               | 
| 22 | 
            -
              # The default, `1`, means send a message every second.
         | 
| 23 | 
            -
              config :interval, :validate => :number, :default => 1
         | 
| 17 | 
            +
              config :message, :validate => :string, :default => "Default message"
         | 
| 18 | 
            +
              config :interval, :validate => :number, :default => 10
         | 
| 19 | 
            +
              config :url, :validate => :string, :required => true
         | 
| 20 | 
            +
              config :path, :validate => :string, :required => true
         | 
| 21 | 
            +
              config :client_host, :validate => :string, :default => '127.0.0.1'
         | 
| 24 22 |  | 
| 25 23 | 
             
              public
         | 
| 26 24 | 
             
              def register
         | 
| 27 25 | 
             
                @host = Socket.gethostname
         | 
| 26 | 
            +
                @controller = Controller.new(@host, @url, 'system.profile', 1000, @path, @client_host)
         | 
| 28 27 | 
             
              end # def register
         | 
| 29 28 |  | 
| 30 29 | 
             
              def run(queue)
         | 
| 31 30 | 
             
                # we can abort the loop if stop? becomes true
         | 
| 32 31 | 
             
                while !stop?
         | 
| 33 | 
            -
             | 
| 34 | 
            -
                   | 
| 35 | 
            -
             | 
| 32 | 
            +
             | 
| 33 | 
            +
                  @controller.get_next_events.each do |event|
         | 
| 34 | 
            +
                    @logger.info("Send event #{event}")
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                    decorate(event)
         | 
| 37 | 
            +
                    queue << event
         | 
| 38 | 
            +
                  end
         | 
| 39 | 
            +
             | 
| 36 40 | 
             
                  # because the sleep interval can be big, when shutdown happens
         | 
| 37 41 | 
             
                  # we want to be able to abort the sleep
         | 
| 38 42 | 
             
                  # Stud.stoppable_sleep will frequently evaluate the given block
         | 
    
        data/lib/mongo/mongo.rb
    ADDED
    
    | @@ -0,0 +1,117 @@ | |
| 1 | 
            +
            require 'mongo'
         | 
| 2 | 
            +
            require 'logstash/inputs/base'
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            class MongoAccessor
         | 
| 5 | 
            +
              def initialize(url, collection, client_host)
         | 
| 6 | 
            +
                connection = Mongo::Client.new(url)
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                @mongodb = connection.database
         | 
| 9 | 
            +
                @collection = @mongodb.collection(collection)
         | 
| 10 | 
            +
                @client_host = client_host
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              def get_documents_by_ts(date, limit)
         | 
| 14 | 
            +
                @collection.find({:ts => {:$gt => date}, :client => {:$ne => @client_host}}).limit(limit)
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
              def get_documents(limit)
         | 
| 18 | 
            +
                @collection.find({:client => {:$ne => @client_host}}).limit(limit)
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
            end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            class ProfileCollection
         | 
| 23 | 
            +
              def initialize(documents, parser)
         | 
| 24 | 
            +
                @documents = documents
         | 
| 25 | 
            +
                @parser = parser
         | 
| 26 | 
            +
              end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
              def each
         | 
| 29 | 
            +
                @documents.each do |document|
         | 
| 30 | 
            +
                  document['_id'] = generate_id
         | 
| 31 | 
            +
                  yield @parser.parse(document)
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
              end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              def get_last_document_date
         | 
| 36 | 
            +
                @documents[-1]['ts']
         | 
| 37 | 
            +
              end
         | 
| 38 | 
            +
             | 
| 39 | 
            +
              private
         | 
| 40 | 
            +
              def generate_id
         | 
| 41 | 
            +
                # noinspection RubyArgCount
         | 
| 42 | 
            +
                BSON::ObjectId.new
         | 
| 43 | 
            +
              end
         | 
| 44 | 
            +
            end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
            class DocumentParser
         | 
| 47 | 
            +
              def initialize(host)
         | 
| 48 | 
            +
                @host = host
         | 
| 49 | 
            +
              end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
              def parse(document)
         | 
| 52 | 
            +
                event = LogStash::Event.new('host' => @host)
         | 
| 53 | 
            +
             | 
| 54 | 
            +
                document.each do |key, value|
         | 
| 55 | 
            +
                  event.set(key, value)
         | 
| 56 | 
            +
                end
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                event
         | 
| 59 | 
            +
              end
         | 
| 60 | 
            +
            end
         | 
| 61 | 
            +
             | 
| 62 | 
            +
            class LastValueStore
         | 
| 63 | 
            +
              def initialize(path, name)
         | 
| 64 | 
            +
                @file_full_name = "#{path}/#{name}"
         | 
| 65 | 
            +
              end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
              def save_last_value(value)
         | 
| 68 | 
            +
                file = File.open(@file_full_name, 'a+')
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                file.truncate(0)
         | 
| 71 | 
            +
                file.puts(value)
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                file.close
         | 
| 74 | 
            +
              end
         | 
| 75 | 
            +
             | 
| 76 | 
            +
              def get_last_value
         | 
| 77 | 
            +
                File.read(@file_full_name)
         | 
| 78 | 
            +
              end
         | 
| 79 | 
            +
            end
         | 
| 80 | 
            +
             | 
| 81 | 
            +
            class Controller
         | 
| 82 | 
            +
              def initialize(event, url, collection, limit, path, client_host)
         | 
| 83 | 
            +
                @mongo_accessor = MongoAccessor.new(url, collection, client_host)
         | 
| 84 | 
            +
                @last_value_store = LastValueStore.new(path, collection)
         | 
| 85 | 
            +
                @document_parser = DocumentParser.new(event)
         | 
| 86 | 
            +
                @limit = limit
         | 
| 87 | 
            +
              end
         | 
| 88 | 
            +
             | 
| 89 | 
            +
              def get_next_events
         | 
| 90 | 
            +
                last_date_value = @last_value_store.get_last_value
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                if last_date_value == ''
         | 
| 93 | 
            +
                  documents = @mongo_accessor.get_documents(@limit)
         | 
| 94 | 
            +
                else
         | 
| 95 | 
            +
                  documents = @mongo_accessor.get_documents_by_ts(last_date_value, @limit)
         | 
| 96 | 
            +
                end
         | 
| 97 | 
            +
             | 
| 98 | 
            +
                profile_collection = ProfileCollection.new(documents, @document_parser)
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                @last_value_store.save_last_value(profile_collection.get_last_document_date)
         | 
| 101 | 
            +
             | 
| 102 | 
            +
                profile_collection
         | 
| 103 | 
            +
              end
         | 
| 104 | 
            +
            end
         | 
| 105 | 
            +
             | 
| 106 | 
            +
            lv = LastValueStore.new('/home/artem', 'mex.txt')
         | 
| 107 | 
            +
             | 
| 108 | 
            +
            lv.save_last_value("blasaldkfj")
         | 
| 109 | 
            +
            puts lv.get_last_value
         | 
| 110 | 
            +
             | 
| 111 | 
            +
            #mongo = MongoAccessor.new('mongodb://192.168.1.37/eleet-v2-dev', 'system.profile')
         | 
| 112 | 
            +
             | 
| 113 | 
            +
            #ProfileCollection.new(mongo.get_documents(10)).each do |document|
         | 
| 114 | 
            +
              #puts document['_id']
         | 
| 115 | 
            +
            #end
         | 
| 116 | 
            +
             | 
| 117 | 
            +
             | 
| @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            Gem::Specification.new do |s|
         | 
| 2 2 | 
             
              s.name          = 'logstash-input-mongoprofile'
         | 
| 3 | 
            -
              s.version       = '0.1. | 
| 3 | 
            +
              s.version       = '0.1.1'
         | 
| 4 4 | 
             
              s.licenses      = ['Apache License (2.0)']
         | 
| 5 5 | 
             
              s.summary       = 'MongoDB system.profile input plugin'
         | 
| 6 6 | 
             
              s.description   = 'MongoDB system.profile input plugin'
         | 
| @@ -21,5 +21,6 @@ Gem::Specification.new do |s| | |
| 21 21 | 
             
              s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
         | 
| 22 22 | 
             
              s.add_runtime_dependency 'logstash-codec-plain'
         | 
| 23 23 | 
             
              s.add_runtime_dependency 'stud', '>= 0.0.22'
         | 
| 24 | 
            +
              s.add_runtime_dependency 'mongo', '>= 2.0.0'
         | 
| 24 25 | 
             
              s.add_development_dependency 'logstash-devutils', '>= 0.0.16'
         | 
| 25 26 | 
             
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: logstash-input-mongoprofile
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.1. | 
| 4 | 
            +
              version: 0.1.1
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Artem Antonov
         | 
| @@ -52,6 +52,20 @@ dependencies: | |
| 52 52 | 
             
                - - ">="
         | 
| 53 53 | 
             
                  - !ruby/object:Gem::Version
         | 
| 54 54 | 
             
                    version: 0.0.22
         | 
| 55 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 56 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 57 | 
            +
                requirements:
         | 
| 58 | 
            +
                - - ">="
         | 
| 59 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 60 | 
            +
                    version: 2.0.0
         | 
| 61 | 
            +
              name: mongo
         | 
| 62 | 
            +
              prerelease: false
         | 
| 63 | 
            +
              type: :runtime
         | 
| 64 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 65 | 
            +
                requirements:
         | 
| 66 | 
            +
                - - ">="
         | 
| 67 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 68 | 
            +
                    version: 2.0.0
         | 
| 55 69 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 56 70 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| 57 71 | 
             
                requirements:
         | 
| @@ -79,6 +93,7 @@ files: | |
| 79 93 | 
             
            - LICENSE
         | 
| 80 94 | 
             
            - README.md
         | 
| 81 95 | 
             
            - lib/logstash/inputs/mongoprofile.rb
         | 
| 96 | 
            +
            - lib/mongo/mongo.rb
         | 
| 82 97 | 
             
            - logstash-input-mongoprofile.gemspec
         | 
| 83 98 | 
             
            - spec/inputs/mongoprofile_spec.rb
         | 
| 84 99 | 
             
            homepage: https://github.com/aantonovdevelop/logstash-input-mongoprofile
         |