<commit_before>#! /usr/bin/env ruby
#
#   es-node-graphite
#
# DESCRIPTION:
#   This check creates node metrics from the elasticsearch API
#
# OUTPUT:
#   metric data
#
# PLATFORMS:
#   Linux, Windows, BSD, Solaris, etc
#
# DEPENDENCIES:
#   gem: sensu-plugin
#   gem: rest-client
#   gem: json
#
# USAGE:
#   #YELLOW
#
# NOTES:
#   2014/04
#   Modifid by <NAME> @randomfrequency http://github.com/vjanelle
#   Add more metrics, fix es 1.x URLs, translate graphite stats from
#   names directly
#
#   2012/12 - Modified by <NAME> @SillySophist http://github.com/zadunn
#   To add more metrics, and correct for new versins of ES. Tested on
#   ES Version 0.19.8
#
# LICENSE:
#   Copyright 2013 <NAME> <<EMAIL>>
#   Copyright 2012 Sonian, Inc <<EMAIL>>
#   Released under the same terms as Sensu (the MIT license); see LICENSE
#   for details.
#

require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-plugin/metric/cli'
require 'rest-client'
require 'json'

class ESMetrics < Sensu::Plugin::Metric::CLI::Graphite
  option :scheme,
         description: 'Metric naming scheme, text to prepend to queue_name.metric',
         short: '-s SCHEME',
         long: '--scheme SCHEME',
         default: "#{Socket.gethostname}.elasticsearch"

  option :server,
         description: 'Elasticsearch server host.',
         short: '-h HOST',
         long: '--host HOST',
         default: 'localhost'

  option :port,
         description: 'Elasticsearch port.',
         short: '-p PORT',
         long: '--port PORT',
         proc: proc(&:to_i),
         default: 9200

  option :timeout,
         description: 'Request timeout to elasticsearch',
         short: '-t TIMEOUT',
         long: '--timeout TIMEOUT',
         proc: proc(&:to_i),
         default: 30

  option :disable_jvm_stats,
         description: 'Disable JVM statistics',
         long: '--disable-jvm-stats',
         boolean: true,
         default: false

  option :disable_os_stats,
         description: 'Disable OS Stats',
         long: '--disable-os-stat',
         boolean: true,
         default: false

  option :disable_process_stats,
         description: 'Disable process statistics',
         long: '--disable-process-stats',
         boolean: true,
         default: false

  option :disable_thread_pool_stats,
         description: 'Disable thread-pool statistics',
         long: '--disable-thread-pool-stats',
         boolean: true,
         default: false

  def get_es_resource(resource)
    r = RestClient::Resource.new("http://#{config[:server]}:#{config[:port]}/#{resource}?pretty", timeout: config[:timeout])
    JSON.parse(r.get)
  rescue Errno::ECONNREFUSED
    warning 'Connection refused'
  rescue RestClient::RequestTimeout
    warning 'Connection timed out'
  end

  def acquire_es_version
    info = get_es_resource('/')
    info['version']['number']
  end

  def run
    # invert various stats depending on if some flags are set
    os_stat = !config[:disable_os_stats]
    process_stats = !config[:disable_process_stats]
    jvm_stats = !config[:disable_jvm_stats]
    tp_stats = !config[:disable_thread_pool_stats]

    es_version = Gem::Version.new(acquire_es_version)

    if es_version >= Gem::Version.new('1.0.0')
      stats_query_array = %w(indices http network transport thread_pool)
      stats_query_array.push('jvm') if jvm_stats == true
      stats_query_array.push('os') if os_stat == true
      stats_query_array.push('process') if process_stats == true
      stats_query_array.push('tp_stats') if tp_stats == true
      stats_query_string = stats_query_array.join(',')
    else
      stats_query_string = [
        'clear=true',
        'indices=true',
        'http=true',
        "jvm=#{jvm_stats}",
        'network=true',
        "os=#{os_stat}",
        "process=#{process_stats}",
        "thread_pool=#{tp_stats}",
        'transport=true',
        'thread_pool=true'
      ].join('&')
    end

    if Gem::Version.new(acquire_es_version) >= Gem::Version.new('1.0.0')
      stats = get_es_resource("_nodes/_local/stats?#{stats_query_string}")
    else
      stats = get_es_resource("_cluster/nodes/_local/stats?#{stats_query_string}")
    end

    timestamp = Time.now.to_i
    node = stats['nodes'].values.first

    metrics = {}

    if os_stat
      metrics['os.load_average']                  = node['os']['load_average'][0]
      metrics['os.load_average.1']                = node['os']['load_average'][0]
      metrics['os.load_average.5']                = node['os']['load_average'][1]
      metrics['os.load_average.15']               = node['os']['load_average'][2]
      metrics['os.mem.free_in_bytes']             = node['os']['mem']['free_in_bytes']
      # ... Process uptime in millis?
      metrics['os.uptime']                        = node['os']['uptime_in_millis']
    end

    if process_stats
      metrics['process.mem.resident_in_bytes']    = node['process']['mem']['resident_in_bytes']
    end

    if jvm_stats
      metrics['jvm.mem.heap_used_in_bytes']       = node['jvm']['mem']['heap_used_in_bytes']
      metrics['jvm.mem.non_heap_used_in_bytes']   = node['jvm']['mem']['non_heap_used_in_bytes']
      metrics['jvm.mem.max_heap_size_in_bytes']   = 0

      node['jvm']['mem']['pools'].each do |k, v|
        metrics["jvm.mem.#{k.gsub(' ', '_')}.max_in_bytes"] = v['max_in_bytes']
        metrics['jvm.mem.max_heap_size_in_bytes'] += v['max_in_bytes']
      end

      # This makes absolutely no sense - not sure what it's trying to measure - @vjanelle
      # metrics['jvm.gc.collection_time_in_millis'] = node['jvm']['gc']['collection_time_in_millis'] + \
      # node['jvm']['mem']['pools']['CMS Old Gen']['max_in_bytes']

      node['jvm']['gc']['collectors'].each do |gc, gc_value|
        gc_value.each do |k, v|
          # this contains stupid things like '28ms' and '2s', and there's already
          # something that counts in millis, which makes more sense
          unless k.end_with? 'collection_time'
            metrics["jvm.gc.collectors.#{gc}.#{k}"] = v
          end
        end
      end

      metrics['jvm.threads.count']                = node['jvm']['threads']['count']
      metrics['jvm.threads.peak_count']           = node['jvm']['threads']['peak_count']
    end

    node['indices'].each do |type,  index|
      index.each do |k, v|
        # #YELLOW
        unless k =~ /(_time|memory|size$)/ # rubocop:disable IfUnlessModifier
          metrics["indices.#{type}.#{k}"] = v
        end
      end
    end

    node['transport'].each do |k, v|<commit_msg>return numeric values only<commit_after>#! /usr/bin/env ruby
#
#   es-node-graphite
#
# DESCRIPTION:
#   This check creates node metrics from the elasticsearch API
#
# OUTPUT:
#   metric data
#
# PLATFORMS:
#   Linux, Windows, BSD, Solaris, etc
#
# DEPENDENCIES:
#   gem: sensu-plugin
#   gem: rest-client
#   gem: json
#
# USAGE:
#   #YELLOW
#
# NOTES:
#   2014/04
#   Modifid by <NAME> @randomfrequency http://github.com/vjanelle
#   Add more metrics, fix es 1.x URLs, translate graphite stats from
#   names directly
#
#   2012/12 - Modified by <NAME> @SillySophist http://github.com/zadunn
#   To add more metrics, and correct for new versins of ES. Tested on
#   ES Version 0.19.8
#
# LICENSE:
#   Copyright 2013 <NAME> <<EMAIL>>
#   Copyright 2012 Sonian, Inc <<EMAIL>>
#   Released under the same terms as Sensu (the MIT license); see LICENSE
#   for details.
#

require 'rubygems' if RUBY_VERSION < '1.9.0'
require 'sensu-plugin/metric/cli'
require 'rest-client'
require 'json'

class ESMetrics < Sensu::Plugin::Metric::CLI::Graphite
  option :scheme,
         description: 'Metric naming scheme, text to prepend to queue_name.metric',
         short: '-s SCHEME',
         long: '--scheme SCHEME',
         default: "#{Socket.gethostname}.elasticsearch"

  option :server,
         description: 'Elasticsearch server host.',
         short: '-h HOST',
         long: '--host HOST',
         default: 'localhost'

  option :port,
         description: 'Elasticsearch port.',
         short: '-p PORT',
         long: '--port PORT',
         proc: proc(&:to_i),
         default: 9200

  option :timeout,
         description: 'Request timeout to elasticsearch',
         short: '-t TIMEOUT',
         long: '--timeout TIMEOUT',
         proc: proc(&:to_i),
         default: 30

  option :disable_jvm_stats,
         description: 'Disable JVM statistics',
         long: '--disable-jvm-stats',
         boolean: true,
         default: false

  option :disable_os_stats,
         description: 'Disable OS Stats',
         long: '--disable-os-stat',
         boolean: true,
         default: false

  option :disable_process_stats,
         description: 'Disable process statistics',
         long: '--disable-process-stats',
         boolean: true,
         default: false

  option :disable_thread_pool_stats,
         description: 'Disable thread-pool statistics',
         long: '--disable-thread-pool-stats',
         boolean: true,
         default: false

  def get_es_resource(resource)
    r = RestClient::Resource.new("http://#{config[:server]}:#{config[:port]}/#{resource}?pretty", timeout: config[:timeout])
    JSON.parse(r.get)
  rescue Errno::ECONNREFUSED
    warning 'Connection refused'
  rescue RestClient::RequestTimeout
    warning 'Connection timed out'
  end

  def acquire_es_version
    info = get_es_resource('/')
    info['version']['number']
  end

  def run
    # invert various stats depending on if some flags are set
    os_stat = !config[:disable_os_stats]
    process_stats = !config[:disable_process_stats]
    jvm_stats = !config[:disable_jvm_stats]
    tp_stats = !config[:disable_thread_pool_stats]

    es_version = Gem::Version.new(acquire_es_version)

    if es_version >= Gem::Version.new('1.0.0')
      stats_query_array = %w(indices http network transport thread_pool)
      stats_query_array.push('jvm') if jvm_stats == true
      stats_query_array.push('os') if os_stat == true
      stats_query_array.push('process') if process_stats == true
      stats_query_array.push('tp_stats') if tp_stats == true
      stats_query_string = stats_query_array.join(',')
    else
      stats_query_string = [
        'clear=true',
        'indices=true',
        'http=true',
        "jvm=#{jvm_stats}",
        'network=true',
        "os=#{os_stat}",
        "process=#{process_stats}",
        "thread_pool=#{tp_stats}",
        'transport=true',
        'thread_pool=true'
      ].join('&')
    end

    if Gem::Version.new(acquire_es_version) >= Gem::Version.new('1.0.0')
      stats = get_es_resource("_nodes/_local/stats?#{stats_query_string}")
    else
      stats = get_es_resource("_cluster/nodes/_local/stats?#{stats_query_string}")
    end

    timestamp = Time.now.to_i
    node = stats['nodes'].values.first

    metrics = {}

    if os_stat
      metrics['os.load_average']                  = node['os']['load_average'][0]
      metrics['os.load_average.1']                = node['os']['load_average'][0]
      metrics['os.load_average.5']                = node['os']['load_average'][1]
      metrics['os.load_average.15']               = node['os']['load_average'][2]
      metrics['os.mem.free_in_bytes']             = node['os']['mem']['free_in_bytes']
      # ... Process uptime in millis?
      metrics['os.uptime']                        = node['os']['uptime_in_millis']
    end

    if process_stats
      metrics['process.mem.resident_in_bytes']    = node['process']['mem']['resident_in_bytes']
    end

    if jvm_stats
      metrics['jvm.mem.heap_used_in_bytes']       = node['jvm']['mem']['heap_used_in_bytes']
      metrics['jvm.mem.non_heap_used_in_bytes']   = node['jvm']['mem']['non_heap_used_in_bytes']
      metrics['jvm.mem.max_heap_size_in_bytes']   = 0

      node['jvm']['mem']['pools'].each do |k, v|
        metrics["jvm.mem.#{k.gsub(' ', '_')}.max_in_bytes"] = v['max_in_bytes']
        metrics['jvm.mem.max_heap_size_in_bytes'] += v['max_in_bytes']
      end

      # This makes absolutely no sense - not sure what it's trying to measure - @vjanelle
      # metrics['jvm.gc.collection_time_in_millis'] = node['jvm']['gc']['collection_time_in_millis'] + \
      # node['jvm']['mem']['pools']['CMS Old Gen']['max_in_bytes']

      node['jvm']['gc']['collectors'].each do |gc, gc_value|
        gc_value.each do |k, v|
          # this contains stupid things like '28ms' and '2s', and there's already
          # something that counts in millis, which makes more sense
          unless k.end_with? 'collection_time'
            metrics["jvm.gc.collectors.#{gc}.#{k}"] = v
          end
        end
      end

      metrics['jvm.threads.count']                = node['jvm']['threads']['count']
      metrics['jvm.threads.peak_count']           = node['jvm']['threads']['peak_count']
    end

    node['indices'].each do |type,  index|
      index.each do |k, v|
        # #YELLOW
        unless k =~ /(_time$)/ || v =~ /\d+/ # rubocop:disable IfUnlessModifier
          metrics["indices.#{type}.#{k}"] = v
        end
      end
    end

    node['transport'].each do |k, v|
{'fraction_non_alphanumeric': 0.14537950011465262, 'fraction_numerical': 0.007949247114576168, 'mean_word_length': 2.758690031600115, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2206740', 'n_tokens_mistral': 4296, 'n_tokens_neox': 4173, 'n_words': 906}