class Riemann::Tools::Elasticsearch

Public Instance Methods

health_url() click to toggle source
# File bin/riemann-elasticsearch, line 46
def health_url
  make_es_url("_cluster/health")
end
indices_url() click to toggle source
# File bin/riemann-elasticsearch, line 50
def indices_url
  make_es_url("_stats/store")
end
is_bad?(response, uri) click to toggle source
# File bin/riemann-elasticsearch, line 59
def is_bad?(response, uri)
  if response.success?
    false
  else
    report(:host => uri.host,
           :service => "elasticsearch health",
           :state => "critical",
           :description => response.nil? ? "HTTP response is empty!" : "HTTP connection error: #{response.status} - #{response.body}"
    )
  end
end
make_es_url(path) click to toggle source
# File bin/riemann-elasticsearch, line 39
def make_es_url(path)
  path_prefix = options[:path_prefix]
  path_prefix[0] = '' if path_prefix[0]=='/'
  path_prefix[path_prefix.length-1] = '' if path_prefix[path_prefix.length-1]=='/'
  "http://#{options[:es_host]}:#{options[:es_port]}#{path_prefix.length>0?'/':''}#{path_prefix}/#{path}"
end
safe_get(uri) click to toggle source

Handles HTTP connections and GET requests safely

# File bin/riemann-elasticsearch, line 20
def safe_get(uri)
    # Handle connection timeouts
    response = nil
    begin
      connection = Faraday.new(uri)
      response = connection.get do |req|
        req.options[:timeout] = options[:read_timeout]
        req.options[:open_timeout] = options[:open_timeout]
      end
    rescue => e
      report(:host => uri.host,
        :service => "elasticsearch health",
        :state => "critical",
        :description => "HTTP connection error: #{e.class} - #{e.message}"
      )
    end
    response
end
search_url() click to toggle source
# File bin/riemann-elasticsearch, line 54
def search_url
  es_search_index = options[:es_search_index]
  make_es_url("#{es_search_index}/_stats/search")
end
tick() click to toggle source
# File bin/riemann-elasticsearch, line 117
def tick
  begin
    tick_indices
    tick_search
  rescue Exception => e
    report(:host => options[:es_host],
           :service => "elasticsearch error",
           :state => "critical",
           :description => "Elasticsearch cluster error: #{e.message}")
  end
  uri = URI(health_url)
  response = safe_get(uri)

  return if is_bad?(response, uri)

  # Assuming that a 200 will give json
  json = JSON.parse(response.body)
  cluster_name = json.delete("cluster_name")
  cluster_status = json.delete("status")
  state = case cluster_status
            when "green"
              "ok"
            when "yellow"
              "warning"
            when "red"
              "critical"
          end

  report(:host => uri.host,
         :service => "elasticsearch health",
         :state => state,
         :description => "Elasticsearch cluster: #{cluster_name} - #{cluster_status}")

  json.each_pair do |k,v|
    report(:host => uri.host,
           :service => "elasticsearch #{k}",
           :metric => v,
           :description => "Elasticsearch cluster #{k}"
    )

  end

end
tick_indices() click to toggle source
# File bin/riemann-elasticsearch, line 71
def tick_indices
  uri = URI(indices_url)
  response = safe_get(uri)

  return if is_bad?(response, uri)

  # Assuming that a 200 will give json
  json = JSON.parse(response.body)

  json["indices"].each_pair do |k,v|
    report(:host => uri.host,
           :service => "elasticsearch index/#{k}/primaries/size_in_bytes",
           :metric => v["primaries"]["store"]["size_in_bytes"]
    )
    report(:host => uri.host,
           :service => "elasticsearch index/#{k}/total/size_in_bytes",
           :metric => v["total"]["store"]["size_in_bytes"]
    )
  end
end