전체 코드

require 'elasticsearch'

class EsTest

  attr_accessor :esClient, :action_array
  def initialize
    @esClient = Elasticsearch::Client.new
    @action_array = Array.new
  end

  def totalIndex
    ## 전체 인덱스 조회
    response   = esClient.cat.indices
    if ! response.nil?
      totalIndex = response.split("\n")
      totalIndex.each_with_index do |datas,index|
        index_name = datas.split(" ")[2]

        if index_name.chars.first != "."
          ## document가 몇개 인지 확인___________________________________________________________
          total_doc_count = esClient.count index: index_name, body: {query: {match_all:{}}}

          puts " nu: #{index}"

          action_array.push({"index"=> {"_index"=> "my_total_index","_id"=> index,"data"=> {"index_name"=> index_name,"index_count"=> total_doc_count["count"]}}})
        end
      end # end of totalIndex each
      esClient.bulk body: action_array
    end # end of nil?
  end
end

def register(params)
  @es = EsTest.new
end

def filter(event)

  event.set("work_time", Time.now.strftime("%Y%m%d"))

  if event.get("result") != "es server access error"
    @es.totalIndex
    return [event]

  else
    return [event]
  end
end




 

생성자

: ruby 에서 생성자는 initialize 이름의 함수로 사용된다.

=> @esClient : elasticsearch 인스턴스

=> @action_array : Array 인스턴스 ( 코드상에서 bulk 구조로 데이터를 insert 하기 위해 선언된 부분 )

=================================================================

 

1) esClient.cat.indices 는 전체 인덱스의 목록을 리턴해준다. 

2) response.nil 은 해당 변수가 null 값인지를 물어보는 부분이다.

3) split 은 문자열을 특정 문자를 기준으로 자르게 된다.  ( 자르고 리턴된 값은 Array 이다. )

4) each_with_index 는 python 에서 enumerate 하고 동일

=================================================================

 

1) .chars.first 는 문자열에서 첫번째 문자 즉 인덱스에서 0 에 위치에 시작하는 값이다.

'ELK > logstash' 카테고리의 다른 글

event 처리  (0) 2020.12.22
특정 필드의 값 변경  (0) 2020.11.30
logstash jdbc mssql output  (0) 2020.07.04
logstash eve_odd  (0) 2020.06.17
logstash file json absolute path  (0) 2020.06.17