System Engineering/Zabbix

Zabbix로 컨테이너 로그 수집하기

Hopulence 2021. 10. 28. 16:57
반응형

개요

Zabbix에서 Path와 Filename에 대해 정규표현식과 문자열로 Pod 또는 Container의 Log를 수집하여 MariaDB의 zabbix.history_log에 보관한다.

 

준비

  • 컨테이너의 Log 저장 경로 확인
docker inspect {Container_ID} | grep -i log

"LogPath": "/var/lib/docker/containers/2f9e8a1c6097baa648533c4b7c3c8b6fedd66f80b425e3a61f7aa6ada454ace4/2f9e8a1c6097baa648533c4b7c3c8b6fedd66f80b425e3a61f7aa6ada454ace4-json.log",

1. Zabbix-server에서 agent를 통해 구동 중인 컨테이너의 Full ID를 주기적으로 받는다.

2. 위 경로에 접근하여 Log를 수집한다.

 

Zabbix-Agent 설치 및 설정

  • Zabbix-Server 설치는 생략(이전 글 참고)
  • 모니터링 대상 서버에 Zabbix-agent 설치
rpm -Uvh https://repo.zabbix.com/zabbix/5.4/rhel/7/x86_64/zabbix-release-5.4-1.el7.noarch.rpm
yum install zabbix-agent

systemctl enable zabbix-agent
systemctl start zabbix-agent
### Option: Server
#       List of comma delimited IP addresses, optionally in CIDR notation, or DNS names of Zabbix servers and Zabbix proxies.
#       Incoming connections will be accepted only from the hosts listed here.
#       If IPv6 support is enabled then '127.0.0.1', '::127.0.0.1', '::ffff:127.0.0.1' are treated equally
#       and '::/0' will allow any IPv4 or IPv6 address.
#       '0.0.0.0/0' can be used to allow any IPv4 address.
#       Example: Server=127.0.0.1,192.168.1.0/24,::1,2001:db8::/32,zabbix.example.com
#
# Mandatory: yes, if StartAgents is not explicitly set to 0
# Default:
Server={Zabbix Server IP Addr}

### Option: ServerActive
#       List of comma delimited IP:port (or DNS name:port) pairs of Zabbix servers and Zabbix proxies for active checks.
#       If port is not specified, default port is used.
#       IPv6 addresses must be enclosed in square brackets if port for that host is specified.
#       If port is not specified, square brackets for IPv6 addresses are optional.
#       If this parameter is not specified, active checks are disabled.
#       Example: ServerActive=127.0.0.1:20051,zabbix.domain,[::1]:30051,::1,[12fc::1]
#
# Mandatory: no
# Default:
# ServerActive=
ServerActive={Zabbix Server IP Addr}

### Option: Hostname
#       List of comma delimited unique, case sensitive hostnames.
#       Required for active checks and must match hostnames as configured on the server.
#       Value is acquired from HostnameItem if undefined.
#
# Mandatory: no
# Default:
# Hostname=
Hostname=   ### zabbix 서버의 web에서 등록할 hostname과 일치해야 한다.

### Option: AllowRoot
#       Allow the agent to run as 'root'. If disabled and the agent is started by 'root', the agent
#       will try to switch to the user specified by the User configuration option instead.
#       Has no effect if started under a regular user.
#       0 - do not allow
#       1 - allow
#
# Mandatory: no
# Default:
AllowRoot=1   # 값을 1로 주어도 컨테이너 로그 파일에 액세스가 안된다...

### Option: UserParameter
#       User-defined parameter to monitor. There can be several user-defined parameters.
#       Format: UserParameter=<key>,<shell command>
#       See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
UserParameter=docker.fcid.discovery,/home/zabbix/docker.fcid.discovery.py
systemctl restart zabbix-agent

 

  • zabbix 계정을 root 그룹에 포함

컨테이너 로그에 접근하기 위해 루트 권한이 필요하다.

vi /etc/passwd

zabbix:x:0:993:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin

 

  • Pod 정보 수집 스크립트 생성
mkdir /home/zabbix
chown zabbix:zabbix /home/zabbix
vi /home/zabbix/docker.fcid.discovery.py

#!/usr/bin/python

import os
import sys
import json

if __name__ == "__main__":
#FCID is FUll Container ID
    FCID = os.popen('docker ps -a --no-trunc -q').read()

    ILLEGAL_CHARS = ["\\", "'", "\"", "`", "*", "?", "[", "]", "{", "}", "~", "$", "!", "&", ";", "(", ")", "<", ">", "|", "#", "@", "0x0a"]

    DISCOVERY_LIST = {"data": []}

    LINES = FCID.splitlines()
    for l in LINES:
        CID = l
        if len(CID) == 64:
            if not [ele for ele in ILLEGAL_CHARS if(ele in CID)]:
                DISCOVERY_LIST["data"].append({"{#FCID}": CID})

    JSON = json.dumps(DISCOVERY_LIST)

    print(JSON)
cd /home/zabbix
chmod 755 docker.fcid.discovery.py
chown zabbix:zabbix docker.fcid.discovery.py

* JSON 출력 테스트

### zabbix-agent에서 확인
zabbix_agentd -t docker.fcid.discovery
zabbix_agentd -t pod.discovery

### zabbix-server에서 확인
zabbix_get -s 192.168.102.111 -k pod.discovery
zabbix_get -s 192.168.102.111 -k docker.fcid.discovery

 

Zabbix-Server 설정

  • Template 생성 ( Configuration - Templates - Create template )

호스트에서 사용할 템플릿 생성

 

  • Template 내 Discovery rule 생성

Zabbix-Agent에 만들어준 Python 스크립트로 컨테이너 아이디를 Discovery 한다.

 

 

  • Discovery rule 내 Item Prototype 생성

Discover 된 컨테이너 아이디로 해당 경로에서 로그 수집

* 참고

https://www.zabbix.com/documentation/current/manual/config/items/itemtypes/zabbix_agent

 

1 Zabbix agent [Zabbix Documentation 5.4]

 

www.zabbix.com

 

 

 

  • 모니터링할 Host 생성 ( Configuration - Hosts - Create host )

호스트를 생성하고 만들어준 템플릿을 링크한다. (Hostname은 zabbix-angetd.conf의 hostname과 일치해야 한다.)

 

  • 수집 확인 ( Monitoring - Host - Latest data )

 

반응형