서버 모니터링 · 6 min read · Oct 08, 2025

완벽한 로드 밸런싱 및 고가용성 웹 클러스터: Ubuntu 8.04 하디 헤론에서 Xen을 실행하는 2대 서버 - 페이지 7

13. munin 및 monit를 이용한 서버 모니터링 (web1, web2)

이 장에서는 munin과 monit를 사용하여 웹 서버 노드를 모니터링하는 방법을 설명합니다. Munin은 서버의 거의 모든 측면(로드 평균, 메모리 사용량, CPU 사용량, MySQL 처리량, eth0 트래픽 등)에 대한 멋진 그래픽을 생성하며, 많은 구성 없이도 사용할 수 있습니다. 반면, monit는 Apache, MySQL, Postfix와 같은 서비스의 가용성을 확인하고, 서비스가 예상대로 작동하지 않을 경우 재시작과 같은 적절한 조치를 취합니다. 두 가지의 조합은 현재 또는 다가오는 문제를 인식할 수 있는 그래픽과 모니터링된 서비스의 가용성을 보장하는 감시자를 제공합니다.

munin은 여러 서버를 모니터링할 수 있지만, 여기서는 설치된 시스템의 모니터링만 논의합니다.

13.1 munin 설치 및 구성

apt-get install munin munin-node

다음으로, munin 구성 파일 /etc/munin/munin.conf를 편집해야 합니다.

mv /etc/munin/munin.conf /etc/munin/munin.conf.bak
vi /etc/munin/munin.conf

web1.example.com에서

dbdir   /var/lib/munin
htmldir /var/www/example/web/monitoring
logdir  /var/log/munin
rundir  /var/run/munin
tmpldir /etc/munin/templates
[web1.example.com]
    address 127.0.0.1
    use_node_name yes

web2.example.com에서

dbdir   /var/lib/munin
htmldir /var/www/example/web/monitoring
logdir  /var/log/munin
rundir  /var/run/munin
tmpldir /etc/munin/templates
[web2.example.com]
    address 127.0.0.1
    use_node_name yes

web1 및 web2에서

다음으로, /var/www/example/web/monitoring 디렉토리를 생성하고 소유권을 munin 사용자 및 그룹으로 변경합니다. 그렇지 않으면 munin이 해당 디렉토리에 출력을 배치할 수 없습니다. 그런 다음 munin을 재시작합니다:

mkdir -p /var/www/example/web/monitoring
chown munin:munin /var/www/example/web/monitoring
/etc/init.d/munin-node restart

이제 모든 사람이 서버에 대한 모든 작은 통계를 볼 수 없도록 하려면 /var/www/example/web/monitoring 디렉토리에 비밀번호 보호를 설정하는 것이 좋습니다.

이를 위해 /var/www/example/web/monitoring에 .htaccess 파일을 생성합니다:

vi /var/www/example/web/monitoring/.htaccess

AuthType Basic
AuthName "Members Only"
AuthUserFile /var/www/example/monitoring/.htpasswd

require valid-user

그런 다음 /var/www/example/.htpasswd에 비밀번호 파일을 생성해야 합니다. admin 사용자 이름으로 로그인하고 싶으므로 다음과 같이 합니다:

htpasswd -c /var/www/example/web/monitoring/.htpasswd admin

admin의 비밀번호를 입력하면 완료됩니다!

이제 다음 주소에서 보고서에 접근할 수 있습니다(데이터 수집에 몇 분이 걸릴 수 있습니다):

http://www.example.com:10001/monitoring (web1.example.com용)

http://www.example.com:20001/monitoring (web2.example.com용).

13.2 monit 설치 및 구성

monit을 설치하려면 다음과 같이 합니다:

apt-get install monit

이제 /etc/monit/monitrc를 편집해야 합니다. 기본 /etc/monit/monitrc에는 많은 예제가 있으며, http://www.tildeslash.com/monit/doc/examples.php에서 더 많은 구성 예제를 찾을 수 있습니다. 그러나 제 경우에는 proftpd, mysql, apache 및 postfix를 모니터링하고 싶고, 포트 2812에서 monit 웹 인터페이스를 활성화하고 싶으며, https 웹 인터페이스에 로그인할 때 사용자 이름 admin과 비밀번호 test를 사용하고 싶고, monit이 root@localhost로 이메일 알림을 보내도록 하고 싶습니다. 그래서 제 파일은 다음과 같습니다:

web1.example.com에서

cp /etc/monit/monitrc /etc/monit/monitrc_orig
cat /dev/null > /etc/monit/monitrc
vi /etc/monit/monitrc

set daemon  60
set logfile syslog facility log_daemon
set mailserver localhost
set mail-format { from: [email protected] }
set alert [email protected]
set httpd port 2812 and
     SSL ENABLE
     PEMFILE  /var/certs/monit.pem
     allow admin:test
#check process vsftpd with pidfile /var/run/vsftpd/vsftpd.pid
#   start program = "/etc/init.d/vsftpd start"
#   stop program  = "/etc/init.d/vsftpd stop"
#   if failed host 192.168.1.104 port 21 protocol ftp then restart
#   if 5 restarts within 5 cycles then timeout
check process mysql with pidfile /var/run/mysqld/mysqld.pid
   group database
   start program = "/etc/init.d/mysql start"
   stop program = "/etc/init.d/mysql stop"
   if failed host 127.0.0.1 port 3306 then restart
   if 5 restarts within 5 cycles then timeout
check process apache with pidfile /var/run/apache2.pid
   group www
   start program = "/etc/init.d/apache2 start"
   stop program  = "/etc/init.d/apache2 stop"
   if failed host 192.168.1.104 port 80  protocol http
      and request "/example/web/monit/token" then restart
   if cpu is greater than 60% for 2 cycles then alert
   if cpu > 80% for 5 cycles then restart
   if children > 250 then restart
   if loadavg(5min) greater than 10 for 8 cycles then stop
   if 3 restarts within 5 cycles then timeout
check process postfix with pidfile /var/spool/postfix/pid/master.pid
   group mail
   start program = "/etc/init.d/postfix start"
   stop  program = "/etc/init.d/postfix stop"
   if failed port 25 protocol smtp then restart
   if 5 restarts within 5 cycles then timeout
check process named with pidfile /var/lib/named/var/run/bind/run/named.pid
   group bind
   start program = "/etc/init.d/bind9 start"
   stop  program = "/etc/init.d/bind9 stop"
   if failed port 53 then restart
   if 5 restarts within 5 cycles then timeout

web2.example.com에서

cp /etc/monit/monitrc /etc/monit/monitrc_orig
cat /dev/null > /etc/monit/monitrc
vi /etc/monit/monitrc

set daemon  60
set logfile syslog facility log_daemon
set mailserver localhost
set mail-format { from: [email protected] }
set alert [email protected]
set httpd port 2812 and
     SSL ENABLE
     PEMFILE  /var/certs/monit.pem
     allow admin:test
#check process vsftpd with pidfile /var/run/vsftpd/vsftpd.pid
#   start program = "/etc/init.d/vsftpd start"
#   stop program  = "/etc/init.d/vsftpd stop"
#   if failed host 192.168.1.105 port 21 protocol ftp then restart
#   if 5 restarts within 5 cycles then timeout
check process mysql with pidfile /var/run/mysqld/mysqld.pid
   group database
   start program = "/etc/init.d/mysql start"
   stop program = "/etc/init.d/mysql stop"
   if failed host 127.0.0.1 port 3306 then restart
   if 5 restarts within 5 cycles then timeout
check process apache with pidfile /var/run/apache2.pid
   group www
   start program = "/etc/init.d/apache2 start"
   stop program  = "/etc/init.d/apache2 stop"
   if failed host 192.168.1.105 port 80  protocol http
      and request "/example/web/monit/token" then restart
   if cpu is greater than 60% for 2 cycles then alert
   if cpu > 80% for 5 cycles then restart
   if children > 250 then restart
   if loadavg(5min) greater than 10 for 8 cycles then stop
   if 3 restarts within 5 cycles then timeout
check process postfix with pidfile /var/spool/postfix/pid/master.pid
   group mail
   start program = "/etc/init.d/postfix start"
   stop  program = "/etc/init.d/postfix stop"
   if failed port 25 protocol smtp then restart
   if 5 restarts within 5 cycles then timeout
check process named with pidfile /var/lib/named/var/run/bind/run/named.pid
   group bind
   start program = "/etc/init.d/bind9 start"
   stop  program = "/etc/init.d/bind9 stop"
   if failed port 53 then restart
   if 5 restarts within 5 cycles then timeout

구성 파일은 꽤 자가 설명적입니다. 옵션에 대해 확실하지 않은 경우 monit 문서를 참조하십시오: http://www.tildeslash.com/monit/doc/manual.php

monit 구성의 apache 부분에서 다음을 찾을 수 있습니다:

if failed host www.example.com port 80 protocol http
      and request "/example/web/monit/token" then restart

이는 monit가 www.example.com의 포트 80에 연결을 시도하고 /monit/token 파일에 접근하려고 시도한다는 것을 의미합니다. 이는 /var/www/example/web/monit/token에 해당합니다. 웹 사이트의 문서 루트가 /var/www/example/web이기 때문입니다. monit가 성공하지 못하면 Apache가 실행되고 있지 않다는 의미이며, monit는 이를 재시작할 것입니다.

web1 및 web2에서

이제 /var/www/example/web/monit/token 파일을 생성하고 그 안에 임의의 문자열을 작성해야 합니다:

mkdir /var/www/example/web/monit
echo “hello” > /var/www/example/web/monit/token

다음으로, SSL 암호화된 monit 웹 인터페이스에 필요한 pem 인증서(/var/certs/monit.pem)를 생성합니다:

mkdir /var/certs
cd /var/certs

인증서를 생성하기 위해 OpenSSL 구성 파일이 필요합니다. 다음과 같이 보일 수 있습니다:

vi /var/certs/monit.cnf

# create RSA certs - Server
RANDFILE = ./openssl.rnd
[ req ]
default_bits = 1024
encrypt_key = yes
distinguished_name = req_dn
x509_extensions = cert_type
[ req_dn ]
countryName = Country Name (2 letter code)
countryName_default = MO
stateOrProvinceName             = State or Province Name (full name)
stateOrProvinceName_default     = Monitoria
localityName                    = Locality Name (eg, city)
localityName_default            = Monittown
organizationName                = Organization Name (eg, company)
organizationName_default        = Monit Inc.
organizationalUnitName          = Organizational Unit Name (eg, section)
organizationalUnitName_default  = Dept. of Monitoring Technologies
commonName                      = Common Name (FQDN of your server)
commonName_default              = server.monit.mo
emailAddress                    = Email Address
emailAddress_default            = [email protected]
[ cert_type ]
nsCertType = server

이제 다음과 같이 인증서를 생성합니다:

openssl req -new -x509 -days 365 -nodes -config ./monit.cnf -out /var/certs/monit.pem -keyout /var/certs/monit.pem

openssl gendh 512 >> /var/certs/monit.pem

openssl x509 -subject -dates -fingerprint -noout -in /var/certs/monit.pem

chmod 700 /var/certs/monit.pem

그 후 /etc/default/monit를 편집하여 monit 데몬을 활성화합니다. startup을 1로 변경하고, monit가 시스템을 확인할 간격을 초 단위로 설정합니다. 저는 60(초)을 선택했으므로 제 파일은 다음과 같습니다:

vi /etc/default/monit

# Defaults for monit initscript
# sourced by /etc/init.d/monit
# installed at /etc/default/monit by maintainer scripts
# Fredrik Steen 
# You must set this variable to for monit to start
startup=1
# To change the intervals which monit should run uncomment
# and change this variable.
CHECK_INTERVALS=60

마지막으로, monit를 시작할 수 있습니다:

/etc/init.d/monit start

이제 브라우저를 https://192.168.1.104:2812/ 또는 https://192.168.1.105:2812/로 이동시키고, admin과 test로 로그인하면 monit 웹 인터페이스를 볼 수 있습니다.

Share: X/Twitter LinkedIn

새 게시물을 받은 편지함에서 받기

스팸은 없습니다. 언제든지 구독 해지 가능합니다.