본문으로 건너뛰기

[L] nagios로 서버 모니터링하기

💡 요약 정리

  • Nagios는 서버 상태를 실시간으로 모니터링할 수 있는 오픈소스 시스템입니다
  • 모니터링 서버와 클라이언트 구조로 동작하며, NRPE를 통해 원격 모니터링을 수행합니다
  • RPMForge 저장소에서 패키지를 설치하고 Apache 웹서버와 연동하여 사용합니다
  • 웹 인터페이스를 통해 여러 서버의 상태를 한눈에 확인할 수 있습니다

Nagios는 Linux/Unix 서버의 상태를 실시간으로 모니터링할 수 있는 오픈소스 모니터링 시스템입니다.

모니터링 서버와 클라이언트 서버로 구성되며, NRPE(Nagios Remote Plugin Executor)를 통해 원격 서버의 상태를 수집합니다.

이 가이드에서는 다음과 같은 구성으로 설명합니다:

  • server (모니터링 서버): 192.168.122.1
  • vm1 (클라이언트): 192.168.122.20
  • vm2 (클라이언트): 192.168.122.30

1. 모니터링 서버 설정

① RPMForge 저장소 추가

RPM 버전의 Nagios를 설치하기 위해 RPMForge yum repository를 추가합니다.

[root@jook ~]# wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
[root@jook ~]# rpm -Uvh rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm

② Nagios 패키지 설치

모니터링 서버에 nagios 관련 패키지를 설치합니다.

[root@jook ~]# yum install nagios nagios-devel nagios-plugins-nrpe nagios-plugins

설치 과정에서 다음 패키지들이 함께 설치됩니다:

  • nagios: Nagios 코어
  • nagios-plugins: 모니터링 플러그인
  • nagios-plugins-nrpe: 원격 실행 플러그인
  • httpd: Apache 웹서버 (의존성)
  • php: PHP (의존성)

③ Apache 설정

httpd.conf 파일에 Nagios 관련 설정을 추가하거나, yum 설치 시 자동으로 생성된 /etc/httpd/conf.d/nagios.conf 파일을 사용합니다.

# Add for nagios
ScriptAlias /nagios/cgi-bin "/usr/lib/nagios/cgi"
<Directory "/usr/lib64/nagios/cgi">
    Options ExecCGI
    AllowOverride None
    Order allow,deny
    Allow from all
    AuthName "Nagios Access"
    AuthType Basic
    AuthUserFile /etc/nagios/htpasswd.users
    Require valid-user
</Directory>

Alias /nagios "/usr/share/nagios"
<Directory "/usr/share/nagios">
    Options None
    AllowOverride None
    Order allow,deny
    Allow from all
    AuthName "Nagios Access"
    AuthType Basic
    AuthUserFile /etc/nagios/htpasswd.users
    Require valid-user
</Directory>

④ Apache 인증 설정

Nagios 웹 인터페이스 접근을 위한 인증 파일을 생성합니다.

# 첫 번째 사용자 생성 (관리자)
/home/apache/bin/htpasswd -c /etc/nagios/htpasswd.users nagiosadmin

# 추가 사용자 생성 (게스트)
/home/apache/bin/htpasswd /etc/nagios/htpasswd.users guest

⑤ Nagios 설정 파일

Nagios 설정 파일은 /etc/nagios/objects/ 디렉토리에 위치합니다:

  • /etc/nagios/nagios.cfg: 메인 설정 파일
  • /etc/nagios/cgi.cfg: CGI 설정
  • /etc/nagios/objects/timeperiods.cfg: 시간대 설정
  • /etc/nagios/objects/contacts.cfg: 연락처 설정
  • /etc/nagios/objects/templates.cfg: 템플릿 설정
  • /etc/nagios/objects/localhost.cfg: 로컬호스트 모니터링 설정

기본 설정으로도 사용 가능하며, localhost.cfg 설정에 따라 기본적으로 로컬호스트만 모니터링합니다.

⑥ 설정 파일 오류 검사

Nagios 설정 파일에 오류가 없는지 검사합니다.

[root@jook nagios]# nagios -v nagios.cfg

Nagios Core 3.2.3
Copyright (c) 2009-2010 Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 10-03-2010
License: GPL

Website: http://www.nagios.org
Reading configuration data...
   Read main config file okay...
Processing object config file '/etc/nagios/objects/commands.cfg'...
Processing object config file '/etc/nagios/objects/contacts.cfg'...
Processing object config file '/etc/nagios/objects/timeperiods.cfg'...
Processing object config file '/etc/nagios/objects/templates.cfg'...
Processing object config file '/etc/nagios/objects/localhost.cfg'...
   Read object config files okay...

Running pre-flight check on configuration data...

Checking services...
        Checked 8 services.
Checking hosts...
        Checked 1 hosts.
Checking host groups...
        Checked 1 host groups.
Checking service groups...
        Checked 0 service groups.
Checking contacts...
        Checked 1 contacts.
Checking contact groups...
        Checked 1 contact groups.
Checking service escalations...
        Checked 0 service escalations.
Checking service dependencies...
        Checked 0 service dependencies.
Checking host escalations...
        Checked 0 host escalations.
Checking host dependencies...
        Checked 0 host dependencies.
Checking commands...
        Checked 24 commands.
Checking time periods...
        Checked 5 time periods.
Checking for circular paths between hosts...
Checking for circular host and service dependencies...
Checking global event handlers...
Checking obsessive compulsive processor commands...
Checking misc settings...

Total Warnings: 0
Total Errors:   0

Things look okay - No serious problems were detected during the pre-flight check

Total Warnings: 0, Total Errors: 0이 표시되면 설정이 정상입니다.

⑦ Nagios 데몬 실행

Nagios 데몬을 시작하고 웹 브라우저로 접속합니다.

[root@jook ~]# /etc/init.d/nagios start
Starting nagios:                                           [  OK  ]

웹 브라우저에서 다음 URL로 접속합니다:

  • http://서버IP/nagios/

2. 클라이언트 서버 추가하여 모니터링하기

① 클라이언트에 패키지 설치

클라이언트 서버에도 RPMForge repository를 추가한 후 nagios-nrpe와 nagios-plugins를 설치합니다.

[root@vm1 ~]# wget http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.i686.rpm
[root@vm1 ~]# rpm -Uvh rpmforge-release-0.5.2-2.el6.rf.i686.rpm

[root@vm1 ~]# yum install nagios-nrpe nagios-plugins

② 클라이언트 NRPE 설정 - 서버 접근 허용

서버의 nagios 데몬과 클라이언트의 nrpe 데몬이 서로 통신하여 모니터링을 수행합니다.

/etc/nagios/nrpe.cfg 파일에 모니터링 서버의 IP를 허용 목록에 추가합니다.

[root@vm1 ~]# vi /etc/nagios/nrpe.cfg

# 변경 전
allowed_hosts=127.0.0.1

# 변경 후 (모니터링 서버 IP 추가)
allowed_hosts=192.168.122.1

③ 클라이언트 NRPE 설정 - Command 등록

/etc/nagios/nrpe.cfg 파일에 command가 등록되어 있습니다. 서버는 이 파일에 등록된 command만 실행할 수 있습니다.

/usr/lib/nagios/plugins/ 디렉토리(64비트의 경우 /usr/lib64/nagios/plugins/)에 있는 플러그인을 command로 등록할 수 있습니다.

기본 등록된 command 예시:

command[check_users]=/usr/lib/nagios/plugins/check_users -w 5 -c 10
command[check_load]=/usr/lib/nagios/plugins/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/xvda1
command[check_zombie_procs]=/usr/lib/nagios/plugins/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/lib/nagios/plugins/check_procs -w 150 -c 200
주의: 클라이언트의 nrpe.cfg에 등록된 command만 서버가 실행할 수 있습니다.

④ NRPE 데몬 실행 및 테스트

클라이언트에서 nrpe 데몬을 시작합니다.

[root@vm1 ~]# /etc/init.d/nrpe restart
Shutting down Nagios NRPE daemon (nrpe):                  [  OK  ]
Starting Nagios NRPE daemon (nrpe):                       [  OK  ]

서버에서 클라이언트 command 실행 테스트:

모니터링 서버에서 check_nrpe 명령으로 클라이언트 정보를 가져올 수 있습니다.

# 로드 평균 확인
[root@jook nagios]# /usr/lib64/nagios/plugins/check_nrpe -H 192.168.122.20 -c check_load
OK - load average: 0.00, 0.00, 0.00|load1=0.000;15.000;30.000;0; load5=0.000;10.000;25.000;0; load15=0.000;5.000;20.000;0;

# 디스크 사용량 확인
[root@jook nagios]# /usr/lib64/nagios/plugins/check_nrpe -H 192.168.122.20 -c check_hda1
DISK OK - free space: /boot 167 MB (92% inode=99%);| /boot=12MB;151;170;0;189

# 로그인 사용자 수 확인
[root@jook nagios]# /usr/lib64/nagios/plugins/check_nrpe -H 192.168.122.20 -c check_users
USERS OK - 2 users currently logged in |users=2;5;10;0

# 등록되지 않은 command 실행 시 오류
[root@jook nagios]# /usr/lib64/nagios/plugins/check_nrpe -H 192.168.122.20 -c check_swap
NRPE: Command 'check_swap' not defined

check_swap, check_ssh 등 추가로 모니터링할 항목은 클라이언트의 /etc/nagios/nrpe.cfg 파일에 command를 등록해야 합니다.

⑤ 서버에서 check_nrpe Command 등록

서버의 설정 파일 중 commands.cfg 파일에 check_nrpe 명령을 추가합니다.

/usr/lib64/nagios/plugins/ 디렉토리에 있는 command들을 commands.cfg 파일에 추가하여 사용할 수 있습니다.

# 'check_nrpe' command definition
define command{
        command_name    check_nrpe
        command_line    /usr/lib64/nagios/plugins/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }

⑥ 서버에서 클라이언트 설정 파일 생성

/etc/nagios/objects/ 디렉토리에 각 클라이언트 설정 파일을 생성합니다.

localhost.cfg 파일을 복사하여 만들면 편리합니다. group 설정 부분은 제외하고 host와 service 부분만 정의합니다.

예시: vm1.cfg 파일 생성

###############################################################################
# HOST DEFINITION
###############################################################################

define host{
        use                     linux-server            ; Name of host template to use
        host_name               vm1
        alias                   vm1
        address                 192.168.122.20
        }

###############################################################################
# SERVICE DEFINITIONS
###############################################################################

# SSH 서비스 모니터링
define service{
        use                             local-service
        host_name                       vm1
        service_description             SSH
        check_command                   check_ssh
        notifications_enabled           0
        }

# HTTP 서비스 모니터링
define service{
        use                             local-service
        host_name                       vm1
        service_description             HTTP
        check_command                   check_http
        notifications_enabled           0
        }

# 현재 로그인 사용자 수 모니터링
define service{
        use                             local-service
        host_name                       vm1
        service_description             Current Users
        check_command                   check_nrpe!check_users
        }

# 전체 프로세스 수 모니터링
define service{
        use                             local-service
        host_name                       vm1
        service_description             Total Processes
        check_command                   check_nrpe!check_total_procs
        }

# 현재 로드 평균 모니터링
define service{
        use                             local-service
        host_name                       vm1
        service_description             Current Load
        check_command                   check_nrpe!check_load
        }

# /dev/xvda1 디스크 사용량 모니터링
define service{
        use                             local-service
        host_name                       vm1
        service_description             /dev/xvda1 Free Space
        check_command                   check_nrpe!check_hda1
        }

⑦ Nagios 설정 파일에 클라이언트 추가

메인 설정 파일인 nagios.cfg에 생성한 클라이언트 설정 파일을 추가합니다.

[root@jook objects]# vi /etc/nagios/nagios.cfg

# 기존 localhost 설정
cfg_file=/etc/nagios/objects/localhost.cfg

# vm1 클라이언트 설정 추가
cfg_file=/etc/nagios/objects/vm1.cfg

# vm2 클라이언트 설정 추가
cfg_file=/etc/nagios/objects/vm2.cfg

⑧ 설정 적용 및 Nagios 재시작

설정 파일에 오류가 없는지 검사한 후 Nagios를 재시작합니다.

# 설정 파일 오류 검사
[root@jook nagios]# nagios -v nagios.cfg

# 오류가 없으면 Nagios 재시작
[root@jook nagios]# /etc/init.d/nagios restart
Stopping nagios:                                           [  OK  ]
Starting nagios:                                           [  OK  ]

3. 웹 인터페이스로 모니터링 확인

웹 브라우저에서 Nagios 웹 인터페이스로 접속하여 모니터링 상태를 확인합니다.

  • URL: http://서버IP/nagios/
  • 로그인: 앞서 생성한 nagiosadmin 계정으로 로그인

웹 인터페이스에서 확인할 수 있는 정보:

  • Hosts: 등록된 호스트 목록 및 상태
  • Services: 각 호스트의 서비스 모니터링 상태
  • Current Status: 현재 시스템 상태 요약
  • Reports: 모니터링 리포트

모니터링 상태는 다음과 같이 표시됩니다:

  • OK: 정상
  • WARNING: 경고
  • CRITICAL: 심각
  • Unknown: 알 수 없음

4. 추가 설정

① 모니터링 항목 추가

클라이언트에서 추가로 모니터링할 항목이 있다면:

  1. 클라이언트의 /etc/nagios/nrpe.cfg에 command 추가
  2. nrpe 데몬 재시작
  3. 서버의 클라이언트 설정 파일(예: vm1.cfg)에 service 정의 추가
  4. Nagios 재시작

② 알림 설정

/etc/nagios/objects/contacts.cfg 파일에서 알림 받을 이메일 주소를 설정할 수 있습니다.

define contact{
        contact_name                    nagiosadmin
        alias                           Nagios Admin
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,u,c,r
        host_notification_options       d,u,r
        service_notification_commands   notify-service-by-email
        host_notification_commands      notify-host-by-email
        email                           your-email@example.com
        }

③ 자동 시작 설정

서버 부팅 시 Nagios가 자동으로 시작되도록 설정합니다.

# 모니터링 서버
[root@jook ~]# chkconfig nagios on

# 클라이언트 서버
[root@vm1 ~]# chkconfig nrpe on