redis-stat:语法错误,意外的':',预期结尾(SyntaxError)

时间:2019-11-13 07:10:54

标签: ruby

我正在使用redis-mon来监视我的redis实例,并且我想将redis-mon数据发送到elasticsearch,但是当我运行命令redis-stat 172.17.0.4 172.17.0.5 172.17.0.6 --es=http://elasticsearch:9200/redis-stat时,我收到此错误:

/root/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sinatra-1.3.6/lib/sinatra/base.rb:1070: warning: constant ::Fixnum is deprecated
/root/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sinatra-1.3.6/lib/sinatra/base.rb:1070: warning: constant ::Fixnum is deprecated
/root/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sinatra-1.3.6/lib/sinatra/base.rb:1070: warning: constant ::Fixnum is deprecated
/root/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sinatra-1.3.6/lib/sinatra/base.rb:1070: warning: constant ::Fixnum is deprecated
/root/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/sinatra-1.3.6/lib/sinatra/base.rb:1070: warning: constant ::Fixnum is deprecated
Traceback (most recent call last):
    2: from .rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/redis-stat-0.4.14/bin/redis-stat:4:in `<main>'
    1: from /root/.rbenv/versions/2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/root/.rbenv/versions/2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- redis-stat (LoadError)
    6: from .rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/redis-stat-0.4.14/bin/redis-stat:4:in `<main>'
    5: from /root/.rbenv/versions/2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:34:in `require'
    4: from /root/.rbenv/versions/2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:130:in `rescue in require'
    3: from /root/.rbenv/versions/2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:130:in `require'
    2: from /root/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/redis-stat-0.4.14/lib/redis-stat.rb:7:in `<top (required)>'
    1: from /root/.rbenv/versions/2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/root/.rbenv/versions/2.6.5/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': /root/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/redis-stat-0.4.14/lib/redis-stat/elasticsearch.rb:28: syntax error, unexpected ':', expecting end (SyntaxError)
    transport_options: {
                     ^

这是有问题的elasticsearch.rb文件:

require 'elasticsearch'
require 'date'
require 'uri'

class RedisStat
class ElasticsearchSink
  attr_reader :hosts, :info, :index, :client

  DEFAULT_INDEX = 'redis-stat'

  def self.parse_url elasticsearch
    unless elasticsearch.match(%r[^https?://])
      elasticsearch = "http://#{elasticsearch}"
    end

    uri      = URI.parse elasticsearch
    path     = uri.path
    index    = path == '' ? DEFAULT_INDEX : path.split('/').last
    uri.path = ''

    [uri.to_s, index]
  end

  def initialize hosts, elasticsearch
    url, @index  = elasticsearch
    @hosts       = hosts
    @client      = Elasticsearch::Client.new :url => url
    transport_options: {
        headers: {'Content-Type' => 'application/json'}
    }
  end

  def output info
    @hosts.each do |host|
      entries = Hash[info.map { |k, v|
        if v.has_key?(host) && raw = v[host].last
          [k, raw]
        end
      }.compact]
      next if entries.empty?

      time = entries[:at]
      entry = {
        :index => index,
        :type  => "redis",
        :body  => entries.merge({
          :@timestamp => format_time(time),
          :host       => host,
          :at         => time.to_f
        }),
      }

      client.index entry
    end
  end

private
  if RUBY_VERSION.start_with? '1.8.'
    def format_time time
      fmt = time.strftime("%FT%T%z")
      fmt[0..-3] + ':' + fmt[-2..-1]
    end
  else
    def format_time time
      time.strftime("%FT%T%:z")
    end
  end
end
end

如果我删除,效果很好:

    transport_options: {
        headers: {'Content-Type' => 'application/json'}
    }` part but then I can't send data to the elasticsearch.

我在centos7容器上使用ruby 2.6.5。

我该如何解决?

2 个答案:

答案 0 :(得分:2)

您在:url => url之后缺少逗号:

@client = Elasticsearch::Client.new url: url, # <- note the comma
                                    transport_options: {
                                      headers: {'Content-Type' => 'application/json'}
                                    }

答案 1 :(得分:1)

您使用类似于散列或关键字参数的语法,但这是常规的方法上下文。看来您应该改用赋值:

    transport_options = {
        headers: {'Content-Type' => 'application/json'}
    }

顺便说一句。脚本中未使用transport_options。您在哪里需要它们?

编辑:

我已经阅读了您在评论中发布的链接。您错过了一个逗号。应该是:

    @client      = Elasticsearch::Client.new :url => url,
      transport_options: {
        headers: {'Content-Type' => 'application/json'}
      }

相当于:

    @client = Elasticsearch::Client.new(
     url: url,
     transport_options: {
       headers: {'Content-Type' => 'application/json'}
     }
   )