2025년 1월 18일 토요일

Logstash 필터 ruby - 6th

캡쳐그룹 순서번호는 1부터 시작한다.


첫 번째 순서번호 캡쳐를 위한 ruby 필터
ruby {
 code => "
  event.set('result', event.get('message').match(/(.).../).captures[1])
 "
}

캡쳐 실패.
[2025-01-18T20:37:14,434][INFO ][logstash.agent           ] Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
{
    "message" => "abcd\r",
     "result" => nil
}

왜 안 되지? 전체 문자를 개별 캡쳐해봤다.
ruby {
code => " event.set('result', event.get('message').match(/(.)(.)(.)(.)/).captures[1]) " }

두 번째 순서번호를 가져오네?
[2025-01-18T20:32:47,467][INFO ][logstash.agent           ] Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
{
    "message" => "abcd\r",
     "result" => "b"
}

순서번호를 빼봤다. 순서번호가 0부터 시작(..)
ruby {
 code => "
  event.set('result', event.get('message').match(/(.)(.)(.)(.)/).captures)
 "
}
[2025-01-18T20:33:35,432][INFO ][logstash.agent           ] Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
{
    "message" => "abcd\r",
     "result" => [
        [0] "a",
        [1] "b",
        [2] "c",
        [3] "d"
    ]
}

captures 구문은 헷갈려서 못쓰겠다.
ruby {
 code => "
  str = event.get('message').match(/(.)(.)(.)(.)/)
  event.set('result1', str[1])
  event.set('result2', str[2])
  event.set('result3', str[3])
  event.set('result4', str[4])
 "
}
[2025-01-18T20:35:35,052][INFO ][logstash.agent           ] Pipelines running {:count=>1, :running_pipelines=>[:main], :non_running_pipelines=>[]}
{
    "result3" => "c",
    "result2" => "b",
    "result4" => "d",
    "message" => "abcd\r",
    "result1" => "a"
}

댓글 없음:

댓글 쓰기

크리에이티브 커먼즈 라이선스