influxdb-lineprotocol-writer 0.1.0 → 0.2.0
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/.gitignore +1 -0
- data/Gemfile.lock +10 -2
- data/README.md +44 -0
- data/examples/influxdb_event_handler.rb +32 -0
- data/examples/sensu-metric-check.rb +57 -0
- data/examples/sensu-test.sh +8 -0
- data/examples/sensu_event.json +28 -0
- data/examples/sensu_event_generator.rb +12 -0
- data/examples/test.rb +23 -0
- data/influxdb-lineprotocol-writer.gemspec +3 -1
- data/lib/influxdb-lineprotocol-writer/core.rb +71 -39
- data/lib/influxdb-lineprotocol-writer/sensu-metric-check.rb +30 -0
- data/lib/influxdb-lineprotocol-writer/sensu-metric-handler.rb +46 -0
- data/lib/influxdb-lineprotocol-writer/version.rb +1 -1
- metadata +50 -3
- data/oneoff/test.rb +0 -20
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: a2307f068a4620a1b356e831401a146b86181b0a
         | 
| 4 | 
            +
              data.tar.gz: 46145262953662339106ba706239c0029ee3c6e8
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 222af5f8370017815d43eb23a07fa6d13b49f5f4831e62cf9eb0ead20ed4738277ff49a044f13c9b4a49b5e31970b7e5f71b036c59055407858f487ba5914242
         | 
| 7 | 
            +
              data.tar.gz: f2a9c4063ec8ae03cc0758150c7bad1df9da637ae065111fcbd4edd3da7b4833011a3ef0aadd5a4fda3b81c3cbb82321ebe5afe4bf4653960b343df268d41932
         | 
    
        data/.gitignore
    CHANGED
    
    
    
        data/Gemfile.lock
    CHANGED
    
    | @@ -1,18 +1,26 @@ | |
| 1 1 | 
             
            PATH
         | 
| 2 2 | 
             
              remote: .
         | 
| 3 3 | 
             
              specs:
         | 
| 4 | 
            -
                influxdb-lineprotocol-writer (0. | 
| 5 | 
            -
                  excon (>= 0.45.3)
         | 
| 4 | 
            +
                influxdb-lineprotocol-writer (0.2.0)
         | 
| 5 | 
            +
                  excon (~> 0.45, >= 0.45.3)
         | 
| 6 6 |  | 
| 7 7 | 
             
            GEM
         | 
| 8 8 | 
             
              remote: https://rubygems.org/
         | 
| 9 9 | 
             
              specs:
         | 
| 10 10 | 
             
                excon (0.45.3)
         | 
| 11 | 
            +
                json (1.8.3)
         | 
| 12 | 
            +
                mixlib-cli (1.5.0)
         | 
| 13 | 
            +
                net-ping (1.7.7)
         | 
| 11 14 | 
             
                rake (10.1.0)
         | 
| 15 | 
            +
                sensu-plugin (1.1.0)
         | 
| 16 | 
            +
                  json
         | 
| 17 | 
            +
                  mixlib-cli (>= 1.1.0)
         | 
| 12 18 |  | 
| 13 19 | 
             
            PLATFORMS
         | 
| 14 20 | 
             
              ruby
         | 
| 15 21 |  | 
| 16 22 | 
             
            DEPENDENCIES
         | 
| 17 23 | 
             
              influxdb-lineprotocol-writer!
         | 
| 24 | 
            +
              net-ping
         | 
| 18 25 | 
             
              rake
         | 
| 26 | 
            +
              sensu-plugin (~> 1.1, >= 1.1.0)
         | 
    
        data/README.md
    CHANGED
    
    | @@ -1,2 +1,46 @@ | |
| 1 1 | 
             
            # influxdb-lineprotocol-writer-ruby
         | 
| 2 2 | 
             
            A Ruby Library for Writing to InfluxDB v0.9+ using the new Line Protocol (https://github.com/influxdb/influxdb/pull/2696)
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            *This is alpha-quality code*
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            Currently supports all param options made available except SSL b/c I'm too lazy to test that at this time.
         | 
| 7 | 
            +
             | 
| 8 | 
            +
            Likewise, this sorts the keys and fields to further hopefully make this the fastest writer currently out there (sorting is strongly recommended in the referenced PR).
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            If you fork this and make it better, I'll review, merge, increment version, and push an updated gem in a reasonable timeframe.
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            Default URI params are documented in lib/influxdb-lineprotocol-writer-ruby/core.rb in the class initializer. 
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            ## Install
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            ```shell
         | 
| 17 | 
            +
            $ gem install influxdb-lineprotocol-writer
         | 
| 18 | 
            +
            ```
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            ## Usage
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            ```ruby
         | 
| 23 | 
            +
            require 'influxdb-lineprotocol-writer'
         | 
| 24 | 
            +
             | 
| 25 | 
            +
             | 
| 26 | 
            +
            client = InfluxDB::LineProtocolWriter::Core.new host: '<host/IP>', 
         | 
| 27 | 
            +
                                                              db: '<DB>', 
         | 
| 28 | 
            +
                                                            user: '<USER>', 
         | 
| 29 | 
            +
                                                            pass: '<PASS>'
         | 
| 30 | 
            +
            client.debug = true # Enable debug Excon output
         | 
| 31 | 
            +
            client.connect
         | 
| 32 | 
            +
             | 
| 33 | 
            +
            client.add_metric 'test', {test: 'test', test2: 'test2', a: 'val'}, {value: 1.0}
         | 
| 34 | 
            +
            sleep 0.1
         | 
| 35 | 
            +
            client.add_metric 'test', {z: 'val2', test: 'test', test2: 'test2'}, {value: 2.0}
         | 
| 36 | 
            +
            sleep 0.1
         | 
| 37 | 
            +
            client.add_metric 'test', {z: 'val2', test: 'test', test2: 'test2'}, {value: 2.0}
         | 
| 38 | 
            +
            client.add_metric 'test', {test: 'test', test2: 'test2'}, {value: 3.0}
         | 
| 39 | 
            +
            sleep 0.1
         | 
| 40 | 
            +
            client.add_metric 'test', nil, {value: 3.0}
         | 
| 41 | 
            +
            client.add_metric 'test3', nil, {value: 3.0, test: 'string', deploy: 'v2.0'}
         | 
| 42 | 
            +
            sleep 0.1
         | 
| 43 | 
            +
            client.add_metric 'test3', nil, {value: 3.0, test: 'string', deploy: 'v2.1'}, 1434077536000
         | 
| 44 | 
            +
             | 
| 45 | 
            +
            client.write
         | 
| 46 | 
            +
            ```
         | 
| @@ -0,0 +1,32 @@ | |
| 1 | 
            +
            #!/usr/bin/env ruby
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'influxdb-lineprotocol-writer/sensu-metric-handler'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            class InfluxDBHandler < ::InfluxDB::LineProtocolWriter::Metric::Handler
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              def filter; end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              def handle
         | 
| 10 | 
            +
                metrics, output = split_metrics_from_output
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                begin
         | 
| 13 | 
            +
                  settings = settings['influxdb']
         | 
| 14 | 
            +
                rescue
         | 
| 15 | 
            +
                  settings = {}
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
                settings['host'] ||= '10.0.1.159'
         | 
| 18 | 
            +
                settings['user'] ||= 'admin'
         | 
| 19 | 
            +
                settings['pass'] ||= 'admin'
         | 
| 20 | 
            +
                settings['db']   ||= 'graphite'
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                hostname, instance_id = @event['client']['name'].split('--')
         | 
| 23 | 
            +
                write_metrics metrics, settings do |metric|
         | 
| 24 | 
            +
                  metric['tags']['source'] = hostname
         | 
| 25 | 
            +
                  metric['tags']['instance_id'] = instance_id unless instance_id.nil?
         | 
| 26 | 
            +
                end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                puts output
         | 
| 29 | 
            +
             | 
| 30 | 
            +
              end
         | 
| 31 | 
            +
            end
         | 
| 32 | 
            +
             | 
| @@ -0,0 +1,57 @@ | |
| 1 | 
            +
            #!/usr/bin/env ruby
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'influxdb-lineprotocol-writer/sensu-metric-check'
         | 
| 4 | 
            +
            require 'net/ping'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            class PingMetric < InfluxDB::LineProtocolWriter::Metric::CLI
         | 
| 7 | 
            +
              option :host,
         | 
| 8 | 
            +
                     short: '-h host',
         | 
| 9 | 
            +
                     default: 'localhost'
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              option :timeout,
         | 
| 12 | 
            +
                     short: '-T timeout',
         | 
| 13 | 
            +
                     proc: proc(&:to_i),
         | 
| 14 | 
            +
                     default: 5
         | 
| 15 | 
            +
             | 
| 16 | 
            +
              option :count,
         | 
| 17 | 
            +
                     short: '-c count',
         | 
| 18 | 
            +
                     description: 'The number of ping requests',
         | 
| 19 | 
            +
                     proc: proc(&:to_i),
         | 
| 20 | 
            +
                     default: 1
         | 
| 21 | 
            +
             | 
| 22 | 
            +
              option :interval,
         | 
| 23 | 
            +
                     short: '-i interval',
         | 
| 24 | 
            +
                     description: 'The number of seconds to wait between ping requests',
         | 
| 25 | 
            +
                     proc: proc(&:to_f),
         | 
| 26 | 
            +
                     default: 1
         | 
| 27 | 
            +
             | 
| 28 | 
            +
              option :'measurement-name',
         | 
| 29 | 
            +
                     short: '-m measurement_name',
         | 
| 30 | 
            +
                     description: 'An alternate name for the metric',
         | 
| 31 | 
            +
                     proc: proc(&:to_s),
         | 
| 32 | 
            +
                     default: 'ping'
         | 
| 33 | 
            +
             | 
| 34 | 
            +
              def run
         | 
| 35 | 
            +
                result_arr = []
         | 
| 36 | 
            +
                pt = Net::Ping::External.new(config[:host], nil, config[:timeout])
         | 
| 37 | 
            +
                config[:count].times do |i|
         | 
| 38 | 
            +
                  sleep(config[:interval]) unless i == 0
         | 
| 39 | 
            +
                  unless pt.ping?
         | 
| 40 | 
            +
                    warning "Failed to ping #{config[:host]}"
         | 
| 41 | 
            +
                  else
         | 
| 42 | 
            +
                    result_arr << pt.duration
         | 
| 43 | 
            +
                  end
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
                duration = result_arr.inject{ |sum, el| sum + el }.to_f / result_arr.size
         | 
| 46 | 
            +
                begin
         | 
| 47 | 
            +
                  output measurement: config[:'measurement-name'],
         | 
| 48 | 
            +
                         tags: { host: config[:host] },
         | 
| 49 | 
            +
                         fields: { value: duration }
         | 
| 50 | 
            +
                rescue InfluxDB::LineProtocolWriter::OptionError => e
         | 
| 51 | 
            +
                  warning e.message
         | 
| 52 | 
            +
                rescue Exception => e
         | 
| 53 | 
            +
                  critical e.message
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
                ok "Ping check of #{config[:host]} Successful"
         | 
| 56 | 
            +
              end
         | 
| 57 | 
            +
            end
         | 
| @@ -0,0 +1,28 @@ | |
| 1 | 
            +
            {
         | 
| 2 | 
            +
              "action": "create",
         | 
| 3 | 
            +
              "occurrences": 1,
         | 
| 4 | 
            +
              "client": {
         | 
| 5 | 
            +
                "name": "vpc-test-server--i-424242",
         | 
| 6 | 
            +
                "address": "8.8.8.8",
         | 
| 7 | 
            +
                "subscriptions": [
         | 
| 8 | 
            +
                  "production",
         | 
| 9 | 
            +
                  "webserver",
         | 
| 10 | 
            +
                  "mysql"
         | 
| 11 | 
            +
                ],
         | 
| 12 | 
            +
                "timestamp": 1326390159
         | 
| 13 | 
            +
              },
         | 
| 14 | 
            +
              "check":{
         | 
| 15 | 
            +
                "name": "frontend_http_check",
         | 
| 16 | 
            +
                "issued": 1326390169,
         | 
| 17 | 
            +
                "subscribers":[
         | 
| 18 | 
            +
                  "frontend"
         | 
| 19 | 
            +
                ],
         | 
| 20 | 
            +
                "interval": 60,
         | 
| 21 | 
            +
                "status": 2,
         | 
| 22 | 
            +
                "handler": "influxdb",
         | 
| 23 | 
            +
                "history": [
         | 
| 24 | 
            +
                  "0",
         | 
| 25 | 
            +
                  "2"
         | 
| 26 | 
            +
                ]
         | 
| 27 | 
            +
              }
         | 
| 28 | 
            +
            }
         | 
| @@ -0,0 +1,12 @@ | |
| 1 | 
            +
            #!/usr/bin/env ruby
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'json'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            check_output = ARGF.read
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            event_json_base = JSON.parse(File.read('sensu_event.json'))
         | 
| 8 | 
            +
            event_json_base["output"] = check_output.to_s
         | 
| 9 | 
            +
            event_json_base["command"] = "test.rb -c 1"
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            puts JSON.pretty_generate(event_json_base)
         | 
| 12 | 
            +
             | 
    
        data/examples/test.rb
    ADDED
    
    | @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            #!/usr/bin/env ruby
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require 'influxdb-lineprotocol-writer'
         | 
| 4 | 
            +
            require 'benchmark'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
             | 
| 7 | 
            +
            Benchmark.bm(7) do |x|
         | 
| 8 | 
            +
              client = InfluxDB::LineProtocolWriter::Core.new host: '10.0.1.159', db: 'graphite', user: 'admin', pass: 'admin'
         | 
| 9 | 
            +
              client.debug = true
         | 
| 10 | 
            +
              x.report(__LINE__) { client.connect }
         | 
| 11 | 
            +
              
         | 
| 12 | 
            +
              x.report(__LINE__) { client.add_metric 'test', {test: 'test', test2: 'test2', a: 'val'}, {value: 1.0}, 'ms'}
         | 
| 13 | 
            +
              x.report(__LINE__) { client.add_metric 'test', {z: 'val2', test: 'test', test2: 'test2'}, {value: 2.0}}
         | 
| 14 | 
            +
              x.report(__LINE__) { client.add_metric 'test', {z: 'val2', test: 'test', test2: 'test2'}, {value: 2.0}}
         | 
| 15 | 
            +
              x.report(__LINE__) { client.add_metric 'test', {test: 'test', test2: 'test2'}, {value: 3.0}}
         | 
| 16 | 
            +
              x.report(__LINE__) { client.add_metric 'test', nil, {value: 3.0}}
         | 
| 17 | 
            +
              x.report(__LINE__) { client.add_metric 'test3', nil, {value: 3.0, test: 'string', deploy: 'v2.0'}}
         | 
| 18 | 
            +
              x.report(__LINE__) { client.add_metric 'test3', nil, {value: 3.0, test: 'string', deploy: 'v2.1'}, 'ms', 1434077536000}
         | 
| 19 | 
            +
              
         | 
| 20 | 
            +
            #  x.report(__LINE__) { puts client.metrics}
         | 
| 21 | 
            +
              
         | 
| 22 | 
            +
              x.report(__LINE__) { client.write}
         | 
| 23 | 
            +
            end
         | 
| @@ -26,6 +26,8 @@ EOF | |
| 26 26 | 
             
              s.required_ruby_version = '>= 2.0.0'
         | 
| 27 27 | 
             
              s.homepage    = 'https://github.com/randywallace/influxdb-lineprotocol-writer-ruby'
         | 
| 28 28 |  | 
| 29 | 
            -
              s.add_runtime_dependency 'excon', '>= 0.45.3'
         | 
| 29 | 
            +
              s.add_runtime_dependency 'excon', '~> 0.45', '>= 0.45.3'
         | 
| 30 30 | 
             
              s.add_development_dependency 'rake'
         | 
| 31 | 
            +
              s.add_development_dependency 'sensu-plugin', '~> 1.1', '>= 1.1.0'
         | 
| 32 | 
            +
              s.add_development_dependency 'net-ping'
         | 
| 31 33 | 
             
            end
         | 
| @@ -2,20 +2,62 @@ require 'excon' | |
| 2 2 |  | 
| 3 3 | 
             
            module InfluxDB
         | 
| 4 4 | 
             
              module LineProtocolWriter
         | 
| 5 | 
            +
                module Util
         | 
| 6 | 
            +
                  def build_metric(measurement, tags, fields, precision='ms', timestamp=nil)
         | 
| 7 | 
            +
                    raise OptionError, 'measurement does not implement to_s' unless measurement.respond_to? :to_s
         | 
| 8 | 
            +
                    raise OptionError, 'fields does not implement to_h' unless fields.respond_to? :to_h
         | 
| 9 | 
            +
                    raise OptionError, 'precision does not implement to_s' unless precision.respond_to? :to_s
         | 
| 10 | 
            +
                    timestamp = get_now_timestamp(precision.to_s) unless timestamp
         | 
| 11 | 
            +
                    working = measurement.to_s
         | 
| 12 | 
            +
                    if tags.respond_to?(:to_h) && !tags.to_h.empty?
         | 
| 13 | 
            +
                      working += ',' + tags.sort.map{|k,v|"#{k.to_s}=#{v.to_s}"}.join(",")
         | 
| 14 | 
            +
                    end
         | 
| 15 | 
            +
                    working += ' '
         | 
| 16 | 
            +
                    working += fields.sort.map{|k,v|"#{k.to_s}=#{if v.is_a?(String);'"'+v+'"';else;v;end}"}.join(",")
         | 
| 17 | 
            +
                    working += " #{timestamp}"
         | 
| 18 | 
            +
                  end
         | 
| 19 | 
            +
                  def check_precision(val)
         | 
| 20 | 
            +
                    if %[n u ms s m h].include?(val.to_s)
         | 
| 21 | 
            +
                      true
         | 
| 22 | 
            +
                    else
         | 
| 23 | 
            +
                      false
         | 
| 24 | 
            +
                    end
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
                  def get_now_timestamp precision='ms'
         | 
| 27 | 
            +
                    check_precision precision
         | 
| 28 | 
            +
                    case precision
         | 
| 29 | 
            +
                    when 'n'
         | 
| 30 | 
            +
                      raise NotSupportedError, 'Nanosecond resolution not currently supported.  Pass timestamp in manually'
         | 
| 31 | 
            +
                    when 'u'
         | 
| 32 | 
            +
                      raise NotSupportedError, 'Microsecond resolution not currently supported.  Pass timestamp in manually'
         | 
| 33 | 
            +
                    when 'ms'
         | 
| 34 | 
            +
                      ( Time.now.to_f * 1000 ).to_i
         | 
| 35 | 
            +
                    when 's'
         | 
| 36 | 
            +
                      Time.now.to_i
         | 
| 37 | 
            +
                    when 'm'
         | 
| 38 | 
            +
                      ( Time.now.to_i / 60 ).to_i
         | 
| 39 | 
            +
                    when 'h'
         | 
| 40 | 
            +
                      ( Time.now.to_i / 3600 ).to_i
         | 
| 41 | 
            +
                    end
         | 
| 42 | 
            +
                  end
         | 
| 43 | 
            +
                end
         | 
| 5 44 | 
             
                class Core
         | 
| 45 | 
            +
                  include Util
         | 
| 46 | 
            +
             | 
| 6 47 | 
             
                  attr_reader :host, :port, :user, :pass, :ssl, :db, :precision, :consistency, :retentionPolicy, :debug
         | 
| 48 | 
            +
             | 
| 7 49 | 
             
                  def initialize(opts={})
         | 
| 8 50 | 
             
                    # set the defaults
         | 
| 9 | 
            -
                    opts | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 51 | 
            +
                    opts = {host: 'localhost',
         | 
| 52 | 
            +
                            port: '8086',
         | 
| 53 | 
            +
                            user: 'root',
         | 
| 54 | 
            +
                            pass: 'root',
         | 
| 55 | 
            +
                            ssl: false,
         | 
| 56 | 
            +
                            db: 'influxdb',
         | 
| 57 | 
            +
                            precision: 'ms',
         | 
| 58 | 
            +
                            consistency: 'one',
         | 
| 59 | 
            +
                            retentionPolicy: 'default',
         | 
| 60 | 
            +
                            debug: false}.merge(opts)
         | 
| 19 61 | 
             
                    self.host = opts[:host]
         | 
| 20 62 | 
             
                    self.port = opts[:port]
         | 
| 21 63 | 
             
                    self.user = opts[:user]
         | 
| @@ -33,17 +75,12 @@ module InfluxDB | |
| 33 75 | 
             
                    @conn = Excon.new(get_uri, debug: debug)
         | 
| 34 76 | 
             
                  end
         | 
| 35 77 |  | 
| 36 | 
            -
                  def add_metric( | 
| 37 | 
            -
                     | 
| 38 | 
            -
                    working += ',' + tags.sort.map{|k,v|"#{k.to_s}=#{v.to_s}"}.join(",") if tags.is_a?(Hash)
         | 
| 39 | 
            -
                    working += ' '
         | 
| 40 | 
            -
                    working += fields.sort.map{|k,v|"#{k.to_s}=#{if v.is_a?(String);'"'+v+'"';else;v;end}"}.join(",")
         | 
| 41 | 
            -
                    working += " #{timestamp}"
         | 
| 42 | 
            -
                    puts "Adding #{working}"
         | 
| 43 | 
            -
                    @metrics << working
         | 
| 78 | 
            +
                  def add_metric(*args)
         | 
| 79 | 
            +
                    @metrics << build_metric(*args)
         | 
| 44 80 | 
             
                  end
         | 
| 45 81 |  | 
| 46 82 | 
             
                  def write
         | 
| 83 | 
            +
                    puts "Writing to InfluxDB at #{get_uri} with params #{get_query_hash} and metrics:\n#{metrics}"
         | 
| 47 84 | 
             
                    @conn.request( expects: [204],
         | 
| 48 85 | 
             
                                   method:  :post,
         | 
| 49 86 | 
             
                                   headers: get_headers,
         | 
| @@ -65,7 +102,19 @@ module InfluxDB | |
| 65 102 | 
             
                  end
         | 
| 66 103 |  | 
| 67 104 | 
             
                  def metrics
         | 
| 68 | 
            -
                    @metrics. | 
| 105 | 
            +
                    if @metrics.respond_to? :to_a
         | 
| 106 | 
            +
                      @metrics.join("\n")
         | 
| 107 | 
            +
                    else
         | 
| 108 | 
            +
                      @metrics
         | 
| 109 | 
            +
                    end
         | 
| 110 | 
            +
                  end
         | 
| 111 | 
            +
             | 
| 112 | 
            +
                  def metrics= val
         | 
| 113 | 
            +
                    if val.respond_to?(:to_a) or val.respond_to?(:to_s)
         | 
| 114 | 
            +
                      @metrics = val
         | 
| 115 | 
            +
                    else
         | 
| 116 | 
            +
                      raise OptionError, 'Metrics must implement #to_a or #to_s'
         | 
| 117 | 
            +
                    end
         | 
| 69 118 | 
             
                  end
         | 
| 70 119 |  | 
| 71 120 | 
             
                  def host= val
         | 
| @@ -100,8 +149,9 @@ module InfluxDB | |
| 100 149 | 
             
                    @retentionPolicy = val
         | 
| 101 150 | 
             
                  end
         | 
| 102 151 |  | 
| 152 | 
            +
             | 
| 103 153 | 
             
                  def precision= val
         | 
| 104 | 
            -
                    if  | 
| 154 | 
            +
                    if check_precision val
         | 
| 105 155 | 
             
                      @precision = val.to_s
         | 
| 106 156 | 
             
                    else
         | 
| 107 157 | 
             
                      raise OptionError, 'Precision must be one of (n, u, ms, s, m, h)'
         | 
| @@ -133,7 +183,7 @@ module InfluxDB | |
| 133 183 | 
             
                  end
         | 
| 134 184 |  | 
| 135 185 | 
             
                  def get_uri
         | 
| 136 | 
            -
                    if  | 
| 186 | 
            +
                    if ssl
         | 
| 137 187 | 
             
                      uri = "https://"
         | 
| 138 188 | 
             
                    else
         | 
| 139 189 | 
             
                      uri = "http://"
         | 
| @@ -144,24 +194,6 @@ module InfluxDB | |
| 144 194 | 
             
                  def get_headers
         | 
| 145 195 | 
             
                    { 'Content-Type' => 'plain/text' }
         | 
| 146 196 | 
             
                  end
         | 
| 147 | 
            -
             | 
| 148 | 
            -
                  def get_now_timestamp
         | 
| 149 | 
            -
                    case precision
         | 
| 150 | 
            -
                    when 'n'
         | 
| 151 | 
            -
                      raise NotSupportedError, 'Nanosecond resolution not currently supported.  Pass timestamp in manually'
         | 
| 152 | 
            -
                    when 'u'
         | 
| 153 | 
            -
                      raise NotSupportedError, 'Microsecond resolution not currently supported.  Pass timestamp in manually'
         | 
| 154 | 
            -
                    when 'ms'
         | 
| 155 | 
            -
                      ( Time.now.to_f * 1000 ).to_i
         | 
| 156 | 
            -
                    when 's'
         | 
| 157 | 
            -
                      Time.now.to_i
         | 
| 158 | 
            -
                    when 'm'
         | 
| 159 | 
            -
                      ( Time.now.to_i / 60 ).to_i
         | 
| 160 | 
            -
                    when 'h'
         | 
| 161 | 
            -
                      ( Time.now.to_i / 3600 ).to_i
         | 
| 162 | 
            -
                    end
         | 
| 163 | 
            -
                  end
         | 
| 164 | 
            -
             | 
| 165 197 | 
             
                end
         | 
| 166 198 | 
             
              end
         | 
| 167 199 | 
             
            end
         | 
| @@ -0,0 +1,30 @@ | |
| 1 | 
            +
            require 'influxdb-lineprotocol-writer'
         | 
| 2 | 
            +
            require 'sensu-plugin/cli'
         | 
| 3 | 
            +
            require 'json'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module InfluxDB
         | 
| 6 | 
            +
              module LineProtocolWriter
         | 
| 7 | 
            +
                class Metric
         | 
| 8 | 
            +
                  class CLI < Sensu::Plugin::CLI
         | 
| 9 | 
            +
                    include InfluxDB::LineProtocolWriter::Util
         | 
| 10 | 
            +
                    def output opts=''
         | 
| 11 | 
            +
                      if opts.respond_to? :to_h
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                        opts[:measurement] ||= File.basename($0, ".*")
         | 
| 14 | 
            +
                        opts[:tags]        ||= {}
         | 
| 15 | 
            +
                        opts[:precision]   ||= 'ms'
         | 
| 16 | 
            +
                        opts[:timestamp]   ||= get_now_timestamp(opts[:precision])
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                        if !opts.key?(:fields) || !opts[:fields].respond_to?(:to_h) || opts[:fields].to_h.keys.length == 0
         | 
| 19 | 
            +
                          raise OptionError, "At least one field is required.  For example: { fields: { key: value } }"
         | 
| 20 | 
            +
                        end
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                        puts opts.to_json
         | 
| 23 | 
            +
                      else
         | 
| 24 | 
            +
                        puts opts.to_s
         | 
| 25 | 
            +
                      end
         | 
| 26 | 
            +
                    end
         | 
| 27 | 
            +
                  end
         | 
| 28 | 
            +
                end
         | 
| 29 | 
            +
              end
         | 
| 30 | 
            +
            end
         | 
| @@ -0,0 +1,46 @@ | |
| 1 | 
            +
            require 'influxdb-lineprotocol-writer'
         | 
| 2 | 
            +
            require 'sensu-handler'
         | 
| 3 | 
            +
            require 'json'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module InfluxDB
         | 
| 6 | 
            +
              module LineProtocolWriter
         | 
| 7 | 
            +
                class Metric
         | 
| 8 | 
            +
                  class Handler < Sensu::Handler
         | 
| 9 | 
            +
                    include ::InfluxDB::LineProtocolWriter::Util
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                    def split_metrics_from_output
         | 
| 12 | 
            +
                      metrics = []
         | 
| 13 | 
            +
                      output = ''
         | 
| 14 | 
            +
                      @event['output'].each_line do |line|
         | 
| 15 | 
            +
                        begin
         | 
| 16 | 
            +
                          metrics << JSON.parse(line.chomp).to_h
         | 
| 17 | 
            +
                        rescue
         | 
| 18 | 
            +
                          output += line.chomp + ' '
         | 
| 19 | 
            +
                        end
         | 
| 20 | 
            +
                      end
         | 
| 21 | 
            +
                      return metrics, output
         | 
| 22 | 
            +
                    end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                    def write_metrics(metrics, settings, debug=false)
         | 
| 25 | 
            +
                      metrics.chunk{ |i| i['precision'] }.each do |precision_group|
         | 
| 26 | 
            +
                        settings[:precision] = precision_group[0]
         | 
| 27 | 
            +
                        settings = Hash[settings.map{ |k,v| [k.to_sym, v] } ]
         | 
| 28 | 
            +
                        settings[:debug] = debug
         | 
| 29 | 
            +
                        client = Core.new settings
         | 
| 30 | 
            +
                        client.connect
         | 
| 31 | 
            +
                        precision_group[1].each do |metric|
         | 
| 32 | 
            +
                          yield metric if block_given?
         | 
| 33 | 
            +
                          client.add_metric metric['measurement'],
         | 
| 34 | 
            +
                                            metric['tags'],
         | 
| 35 | 
            +
                                            metric['fields'],
         | 
| 36 | 
            +
                                            metric['precision'],
         | 
| 37 | 
            +
                                            metric['timestamp']
         | 
| 38 | 
            +
                        end
         | 
| 39 | 
            +
                        client.write
         | 
| 40 | 
            +
                      end
         | 
| 41 | 
            +
                    end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                  end
         | 
| 44 | 
            +
                end
         | 
| 45 | 
            +
              end
         | 
| 46 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,19 +1,22 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: influxdb-lineprotocol-writer
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0. | 
| 4 | 
            +
              version: 0.2.0
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Randy D. Wallace Jr.
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2015-06- | 
| 11 | 
            +
            date: 2015-06-15 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: excon
         | 
| 15 15 | 
             
              requirement: !ruby/object:Gem::Requirement
         | 
| 16 16 | 
             
                requirements:
         | 
| 17 | 
            +
                - - "~>"
         | 
| 18 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 19 | 
            +
                    version: '0.45'
         | 
| 17 20 | 
             
                - - ">="
         | 
| 18 21 | 
             
                  - !ruby/object:Gem::Version
         | 
| 19 22 | 
             
                    version: 0.45.3
         | 
| @@ -21,6 +24,9 @@ dependencies: | |
| 21 24 | 
             
              prerelease: false
         | 
| 22 25 | 
             
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 23 26 | 
             
                requirements:
         | 
| 27 | 
            +
                - - "~>"
         | 
| 28 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 29 | 
            +
                    version: '0.45'
         | 
| 24 30 | 
             
                - - ">="
         | 
| 25 31 | 
             
                  - !ruby/object:Gem::Version
         | 
| 26 32 | 
             
                    version: 0.45.3
         | 
| @@ -38,6 +44,40 @@ dependencies: | |
| 38 44 | 
             
                - - ">="
         | 
| 39 45 | 
             
                  - !ruby/object:Gem::Version
         | 
| 40 46 | 
             
                    version: '0'
         | 
| 47 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 48 | 
            +
              name: sensu-plugin
         | 
| 49 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 50 | 
            +
                requirements:
         | 
| 51 | 
            +
                - - "~>"
         | 
| 52 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 53 | 
            +
                    version: '1.1'
         | 
| 54 | 
            +
                - - ">="
         | 
| 55 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 56 | 
            +
                    version: 1.1.0
         | 
| 57 | 
            +
              type: :development
         | 
| 58 | 
            +
              prerelease: false
         | 
| 59 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 60 | 
            +
                requirements:
         | 
| 61 | 
            +
                - - "~>"
         | 
| 62 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 63 | 
            +
                    version: '1.1'
         | 
| 64 | 
            +
                - - ">="
         | 
| 65 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 66 | 
            +
                    version: 1.1.0
         | 
| 67 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 68 | 
            +
              name: net-ping
         | 
| 69 | 
            +
              requirement: !ruby/object:Gem::Requirement
         | 
| 70 | 
            +
                requirements:
         | 
| 71 | 
            +
                - - ">="
         | 
| 72 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 73 | 
            +
                    version: '0'
         | 
| 74 | 
            +
              type: :development
         | 
| 75 | 
            +
              prerelease: false
         | 
| 76 | 
            +
              version_requirements: !ruby/object:Gem::Requirement
         | 
| 77 | 
            +
                requirements:
         | 
| 78 | 
            +
                - - ">="
         | 
| 79 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 80 | 
            +
                    version: '0'
         | 
| 41 81 | 
             
            description: |
         | 
| 42 82 | 
             
              This is a very basic gem that provides a library that, via Excon, reliably
         | 
| 43 83 | 
             
              publishes metrics to InfluxDB 0.9.0rc33+ via the LineProtocol Interface.
         | 
| @@ -58,13 +98,20 @@ files: | |
| 58 98 | 
             
            - README.md
         | 
| 59 99 | 
             
            - Rakefile
         | 
| 60 100 | 
             
            - bin/influxdb-lineprotocol-writer
         | 
| 101 | 
            +
            - examples/influxdb_event_handler.rb
         | 
| 102 | 
            +
            - examples/sensu-metric-check.rb
         | 
| 103 | 
            +
            - examples/sensu-test.sh
         | 
| 104 | 
            +
            - examples/sensu_event.json
         | 
| 105 | 
            +
            - examples/sensu_event_generator.rb
         | 
| 106 | 
            +
            - examples/test.rb
         | 
| 61 107 | 
             
            - influxdb-lineprotocol-writer.gemspec
         | 
| 62 108 | 
             
            - lib/influxdb-lineprotocol-writer.rb
         | 
| 63 109 | 
             
            - lib/influxdb-lineprotocol-writer/cli.rb
         | 
| 64 110 | 
             
            - lib/influxdb-lineprotocol-writer/core.rb
         | 
| 65 111 | 
             
            - lib/influxdb-lineprotocol-writer/exceptions.rb
         | 
| 112 | 
            +
            - lib/influxdb-lineprotocol-writer/sensu-metric-check.rb
         | 
| 113 | 
            +
            - lib/influxdb-lineprotocol-writer/sensu-metric-handler.rb
         | 
| 66 114 | 
             
            - lib/influxdb-lineprotocol-writer/version.rb
         | 
| 67 | 
            -
            - oneoff/test.rb
         | 
| 68 115 | 
             
            homepage: https://github.com/randywallace/influxdb-lineprotocol-writer-ruby
         | 
| 69 116 | 
             
            licenses:
         | 
| 70 117 | 
             
            - MIT
         | 
    
        data/oneoff/test.rb
    DELETED
    
    | @@ -1,20 +0,0 @@ | |
| 1 | 
            -
            require 'influxdb-lineprotocol-writer'
         | 
| 2 | 
            -
             | 
| 3 | 
            -
             | 
| 4 | 
            -
            client = InfluxDB::LineProtocolWriter::Core.new host: '10.0.1.159', db: 'graphite', user: 'admin', pass: 'admin'
         | 
| 5 | 
            -
            client.debug = true
         | 
| 6 | 
            -
            client.connect
         | 
| 7 | 
            -
             | 
| 8 | 
            -
            client.add_metric 'test', {test: 'test', test2: 'test2', a: 'val'}, {value: 1.0}
         | 
| 9 | 
            -
            sleep 0.1
         | 
| 10 | 
            -
            client.add_metric 'test', {z: 'val2', test: 'test', test2: 'test2'}, {value: 2.0}
         | 
| 11 | 
            -
            sleep 0.1
         | 
| 12 | 
            -
            client.add_metric 'test', {z: 'val2', test: 'test', test2: 'test2'}, {value: 2.0}
         | 
| 13 | 
            -
            client.add_metric 'test', {test: 'test', test2: 'test2'}, {value: 3.0}
         | 
| 14 | 
            -
            sleep 0.1
         | 
| 15 | 
            -
            client.add_metric 'test', nil, {value: 3.0}
         | 
| 16 | 
            -
            client.add_metric 'test3', nil, {value: 3.0, test: 'string', deploy: 'v2.0'}
         | 
| 17 | 
            -
            sleep 0.1
         | 
| 18 | 
            -
            client.add_metric 'test3', nil, {value: 3.0, test: 'string', deploy: 'v2.1'}, 1434077536000
         | 
| 19 | 
            -
             | 
| 20 | 
            -
            client.write
         |