Jul 3, 2019 - logstash 설정 방법

ELK 를 사용할때,

엘라스틱서치나, 키바나의 경우 공통적인 부분이 많아 처리하는 부분이 쉽지만, logstash 의 경우는 본인이 엘라스틱서치에 전달할 데이터포맷이, 솔류션마다 다르기 때문에 해당 작업을 함에 있어, 어려움이 많아 해당 내용을 정리합니다.

input {
  file {
    path => "C:/Users/bymin/Desktop/file/RTBViewLog.20190702"
    mode => "read"
    start_position => "beginning"
  }
}

filter {
 grok  {
   match => {
     "message" => "%{DATA:timestamp}\,%{USER:tagid}\, %{WORD:s}\, %{USER:userid}\, %{USER:auid}\, %{NUMBER:bidfloor:float}\, %{NUMBER:bid:float}\, %{NUMBER:wind:float}\, "
    }
  }
   date {
        match => [ "timestamp", "YYYYMMdd-HHmmss" ]
        target => "@timestamp"
        add_field => { "debug" => "timestampMatched"}
   }
}

output {
  elasticsearch {
    hosts => ["127.0.0.1:9200"]
    index => "_20190702_1"
  }
}

path : 파일이 있는 경로를 지정합니다. \ 로 작성시에 해당 파일을 못읽을 수도 있어 안정하게 / 로 변경해주시기 바랍니다.
mode => read : 해당 파일을 전부 읽어드리겠다는 모드 설정입니다.
해당 값을 안할 경우, log 에서 한줄씩 읽어서 엘라스틱서치에 보내게 됩니다. read 설정시 디폴트가 해당 파일을 삭제합니다.

grok : grok 플러그인을 통해, 해당 패턴을 json 형태로 변환해줍니다.
grok 에 관해서는 해당 url에서 패턴을 찾을 수 있습니다. (https://grokdebug.herokuapp.com/)

날짜는 파일에 있는 timestamp 로 index를 잡아야하므로, timestamp 패턴을 date 에 적용 후, 해당 값을 실제 timestamp 에 target 시킵니다.

logstash -f "config 파일 경로"

로 파일을 실행시킵니다.

Jun 19, 2019 - 윈도우 10 우분투에 레디스 설치

원하는 경로에 redis를 설치합니다.

wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make

make 명령어를 치니, make가 설치가 안되었다고 뜹니다.

root@DESKTOP-RNJILIO:/usr/local/redis/redis-stable# make

Command 'make' not found, but can be installed with:

apt install make
apt install make-guile

apt install make 명령어를 사용하여 설치하게 됩니다.

make[3]: Entering directory '/usr/local/redis/redis-stable/deps/hiredis'
gcc -std=c99 -pedantic -c -O3 -fPIC  -Wall -W -Wstrict-prototypes -Wwrite-strings -g -ggdb  net.c
make[3]: gcc: Command not found
Makefile:156: recipe for target 'net.o' failed
make[3]: *** [net.o] Error 127
make[3]: Leaving directory '/usr/local/redis/redis-stable/deps/hiredis'
Makefile:45: recipe for target 'hiredis' failed
make[2]: *** [hiredis] Error 2
make[2]: Leaving directory '/usr/local/redis/redis-stable/deps'
Makefile:190: recipe for target 'persist-settings' failed
make[1]: [persist-settings] Error 2 (ignored)
    CC adlist.o
/bin/sh: 1: cc: not found
Makefile:248: recipe for target 'adlist.o' failed
make[1]: *** [adlist.o] Error 127
make[1]: Leaving directory '/usr/local/redis/redis-stable/src'
Makefile:6: recipe for target 'all' failed
make: *** [all] Error 2

make[3]: gcc: Command not found 라는 에러가 뜹니다. gcc를 설치해야합니다.

apt install gcc

gcc 를 설치합니다. gcc 설치 중,

E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/l/linux/linux-libc-dev_4.15.0-39.42_amd64.deb 404 Not Found [IP: 91.189.88.31 80]

이런 에러가 발생합니다.

apt-get update

apt-get 을 update 합니다.

apt install gcc

업데이트 후 gcc를 설치하면 404 Not Found 가 발생하지 않고 정상적으로 잘 설치가 됩니다.

gcc 설치 후, 다시 make를 치면 다음과 같은 에러가 납니다.

root@DESKTOP-RNJILIO:/usr/local/redis/redis-stable# make
cd src && make all
make[1]: Entering directory '/usr/local/redis/redis-stable/src'
    CC adlist.o
In file included from adlist.c:34:0:
zmalloc.h:50:10: fatal error: jemalloc/jemalloc.h: No such file or directory
 #include <jemalloc/jemalloc.h>
          ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Makefile:248: recipe for target 'adlist.o' failed
make[1]: *** [adlist.o] Error 1
make[1]: Leaving directory '/usr/local/redis/redis-stable/src'
Makefile:6: recipe for target 'all' failed
make: *** [all] Error 2

최초 설치시 gcc가 없어 실패하면서, 무언가 제대로 설치가 안된거 같습니다.

단순하게 distclean 후 다시 설치하도록 하겠습니다.

make distclean
sudo make install

그럼 이제 정상적으로 redis 가 compile 되기 시작합니다.

설치가 완료되면, src 폴더로 이동합니다.

cd src

-rwxr-xr-x 1 root    root    5216008 Jun 19 11:14 redis-cli
-rwxr-xr-x 1 root    root    8770008 Jun 19 11:14 redis-server

redis-server 실행을 합니다. 근데 실행이 안됩니다. ㅠ-ㅜ

deps 폴더에 가서, 별도의 명령어를 치는 방법.

cd deps
make hiredis jemalloc linenoise lua geohash-int

deps 로 가서 jemalloc 가 기타 등등을 실행시킵니다.

그리고 다시 make 로 설치를 진행합니다.

sudo make install

그리고 다시 ./src 폴더로 가면, redis-server를 실행시킬 수 있습니다.

root@DESKTOP-RNJILIO:/usr/local/redis/redis-stable/src# redis-server
3963:C 19 Jun 2019 11:57:22.660 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3963:C 19 Jun 2019 11:57:22.661 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3963, just started
3963:C 19 Jun 2019 11:57:22.662 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
3963:M 19 Jun 2019 11:57:22.663 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 5.0.5 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 3963
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

3963:M 19 Jun 2019 11:57:22.701 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3963:M 19 Jun 2019 11:57:22.704 # Server initialized
3963:M 19 Jun 2019 11:57:22.706 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
3963:M 19 Jun 2019 11:57:22.713 * Ready to accept connections
root@DESKTOP-RNJILIO:/usr/local/redis/redis-stable/src# nohup ./redis-server &
[1] 3982
root@DESKTOP-RNJILIO:/usr/local/redis/redis-stable/src# nohup: ignoring input and appending output to 'nohup.out'

root@DESKTOP-RNJILIO:/usr/local/redis/redis-stable/src#
root@DESKTOP-RNJILIO:/usr/local/redis/redis-stable/src#
root@DESKTOP-RNJILIO:/usr/local/redis/redis-stable/src# ps -ef|grep redis
root      3982    16  0 12:00 tty1     00:00:00 ./redis-server
root      3987    16  0 12:00 tty1     00:00:00 grep --color=auto redis
root@DESKTOP-RNJILIO:/usr/local/redis/redis-stable/src# redis-cli
127.0.0.1:6379> set key 'melong'
OK
127.0.0.1:6379> get key
"melong"
127.0.0.1:6379>

nohup 을 이용해, redis-server 를 실행시킵니다.
redis-cli 를 이용해 간단한 테스트를 진행해봅니다.

정상적으로 key와 value 가 찍히는 것을 확인할 수 있었습니다.

이제 레디스를 종료하기 위해, 레디스를 종료했습니다.

^C4004:signal-handler (1560924252) Received SIGINT scheduling shutdown...
4004:M 19 Jun 2019 15:04:12.255 # User requested shutdown...
4004:M 19 Jun 2019 15:04:12.256 * Saving the final RDB snapshot before exiting.
4004:M 19 Jun 2019 15:04:12.263 * DB saved on disk
4004:M 19 Jun 2019 15:04:12.263 # Redis is now ready to exit, bye bye...

라는 메시지가 뜨며 종료가 되었으나, client 에서 여전히 서버에 접속이 되는 것을 확인했습니다.

root@DESKTOP-RNJILIO:/usr/local/redis/redis-stable/src# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name

현재 열려있는 port 를 확인해봤으나 검색이 되지 않습니다…..

혹시… 윈도우의 cmd 창을 띄운 후에 ,

C:\Users\bymin>netstat -a -o | find ":6379"

를 실행해봤습니다. 맙소사 윈도우 10 프로세스에 떠있습니다.

C:\WINDOWS\system32>taskkill /f /pid 4176
성공: 프로세스(PID 4176)가 종료되었습니다.

관리자 권한으로 프로세스를 강제로 킬했습니다.
이제 서버가 강제로 중지되었으며 해당 redis-server에 접속이 안되는 것을 확인했습니다.

윈도우 10에서 완벽하게 우분투를 설치해서 실제로 우분투 환경의 테스트를 하기는 아직 힘든거 같네요. 관련 프로세스를 리눅스에 온전히 띄우는 방법을 찾으면 다시 정리해서 올리도록 하겠습니다.

Jun 19, 2019 - WSL에서 netstat 사용하기

윈도우에서 실행되어있는 프로세스를 확인 후 삭제하는 것은 다음과 같습니다.

\apache-tomcat-8.5.23\bin>netstat -a -o | find ":52124"
  TCP    192.168.100.11:52124   5.62.53.224:http       CLOSE_WAIT      2728

이제 PID 번호(2728)를 확인한 다음에 해당 프로세스ID를 종료시키면됩니다.

taskkill /f /pid PID번호

리눅스에서는 보통 다음과 같은 방법으로 프로세스를 확인하고 저는 삭제합니다.

root@DESKTOP-RNJILIO:~# ps -ef|grep redis
root        74    15  0 15:41 tty1     00:00:00 ./redis-server
root        96    15  0 15:49 tty1     00:00:00 grep --color=auto redis
root@DESKTOP-RNJILIO:~# kill -9 74

이렇게 할 경우 74번 프로세스가 삭제되면서 자동적으로 해당 port 가 사라집니다.

근데 WSL(Windows Subsystem For Linux)에서 프로세스를 실행하면, 윈도우의 열려진 port 를 확인할 수 있지,

netstat -tnlp

를 쳐도 해당값이 보여지거나 하지는 않습니다.

2018년 4월 기준으로 WSL의 문제로 netstat 명령은 올바르게 동작하지 않는다고합니다. 참고주소에서 그럴 경우, WSL의 리눅스 배포판 내에서는 윈도우용 응용 프로그램의 실행을 허용하기 때문에 윈도우의 NETSTAT.EXE를 alias로 가져와서 사용할 수 있다고 하여, 해당값을 그대로 사용할 수 있는 것을 확인했습니다.

root@DESKTOP-RNJILIO:~# /mnt/c/Windows/System32/NETSTAT.EXE -a | grep ':50'
  TCP    0.0.0.0:5040           DESKTOP-RNJILIO:0      LISTENING
  TCP    0.0.0.0:50391          DESKTOP-RNJILIO:0      LISTENING
  TCP    0.0.0.0:50392          DESKTOP-RNJILIO:0      LISTENING
  TCP    127.0.0.1:50351        www:50352              ESTABLISHED
  TCP    127.0.0.1:50352        www:50351              ESTABLISHED
  TCP    127.0.0.1:50353        www:50354              ESTABLISHED
  TCP    127.0.0.1:50354        www:50353              ESTABLISHED
  TCP    127.0.0.1:50374        DESKTOP-RNJILIO:0      LISTENING

옵션과 일부 사용법의 경우 윈도우의 NETSTAT와 리눅스의 netstat 간에 상이할 수 있어 사용옵션은 윈도우를 따르며 grep 방법은 리눅스 방법을 따라야합니다.