서버 모니터링 · 4 min read · Oct 17, 2025

CentOS 7에서 Munin 및 Monit로 서버 모니터링 - 페이지 2

5 Monit 설치 및 구성

다음으로 Monit를 설치합니다:

yum install monit

그런 다음 monit의 시스템 시작 링크를 생성합니다:

systemctl enable monit  
systemctl start monit

Monit의 기본 구성 파일은 /etc/monitrc이며, 여기에서 몇 가지 구성 예제를 찾을 수 있습니다(더 많은 구성 예제는 http://mmonit.com/wiki/Monit/ConfigurationExamples에서 확인할 수 있습니다) 이들은 모두 주석 처리되어 있지만, monit에게 /etc/monit.d 디렉토리에서도 구성 파일을 찾도록 지시합니다.

이 경우 모니터링할 항목은 다음과 같습니다:

  • proftpd
  • sshd
  • mysql
  • apache
  • postfix

또한 Monit에 대해 다음 설정을 구성합니다:

  • 포트 2812에서 Monit 웹 인터페이스 활성화.
  • 웹 인터페이스에 HTTP 대신 HTTPS 사용.
  • 웹 인터페이스에 대한 비밀번호 보호 로그인 구성.
  • Monit는 root@localhost로 이메일 알림을 전송해야 합니다.

먼저 인증 설정을 구성하겠습니다. 파일 /etc/monit.d/monitrc를 엽니다.

nano /etc/monit.d/monitrc

그리고 아래로 스크롤하여 이 섹션을 찾습니다:

set httpd port 2812 and  
   use address localhost # localhost에서만 연결 수락  
   allow localhost # localhost가 서버에 연결할 수 있도록 허용  
   allow admin:monit # 'admin' 사용자와 'monit' 비밀번호 필요  
   allow @monit # 'monit' 그룹의 사용자 연결 허용 (rw)  
   allow @users readonly # 'users' 그룹의 사용자 읽기 전용 연결 허용

다음 설정으로 교체합니다:

set httpd port 2812 and  
   use address 0.0.0.0  
   SSL ENABLE  
   PEMFILE /var/certs/monit.pem  
   allow admin:test

“test”라는 단어는 비밀번호입니다. 이를 안전한 비밀번호로 교체하고, “admin”이라는 사용자 이름도 쉽게 추측할 수 없는 이름으로 변경하는 것이 좋습니다.

이제 모니터링할 서비스에 대한 구성을 추가합니다. /etc/monitrc를 수정하는 대신 새 구성 파일 /etc/monit.d/monitrc를 생성합니다.

내 파일은 다음과 같습니다:

nano /etc/monit.d/monitrc
set logfile syslog facility log_daemon
  
# 이 메일 서버를 통해 이메일 전송  
set mailserver localhost
  
# 경고 이메일의 발신자 주소 설정  
set mail-format { from: [email protected] }
  
# 이 주소로 알림 전송  
set alert root@localhost
  
# Proftpd 서비스 모니터링
check process proftpd with pidfile /var/run/proftpd/proftpd.pid
   start program = "/usr/bin/systemctl start proftpd"
   stop program  = "/usr/bin/systemctl stop proftpd"
   if failed port 21 protocol ftp then restart
   if 5 restarts within 5 cycles then timeout
  
# SSH 서비스 모니터링
check process sshd with pidfile /var/run/sshd.pid
   start program  "/usr/bin/systemctl start sshd"
   stop program  "/usr/bin/systemctl stop sshd"
   if failed port 22 protocol ssh then restart
   if 5 restarts within 5 cycles then timeout
  
# MySQL 모니터링
check process mysql with pidfile /var/run/mysqld/mysqld.pid
   group database
   start program = "/usr/bin/systemctl start mysqld"
   stop program = "/usr/bin/systemctl stop mysqld"
   if failed host 127.0.0.1 port 3306 then restart
   if 5 restarts within 5 cycles then timeout
  
# apache 웹 서버 모니터링
check process apache with pidfile /var/run/httpd/httpd.pid
   group www
   start program = "/usr/bin/systemctl start httpd"
   stop program  = "/usr/bin/systemctl stop httpd"
   if failed host localhost port 80 protocol http
      and request "/monit_token" then restart
   if cpu is greater than 60% for 2 cycles then alert
   if cpu > 80% for 5 cycles then restart
   if totalmem > 500 MB 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
  
# postfix 메일 서버 모니터링
check process postfix with pidfile /var/spool/postfix/pid/master.pid
   group mail
   start program = "/usr/bin/systemctl start postfix"
   stop  program = "/usr/bin/systemctl stop postfix"
   if failed port 25 protocol smtp then restart
   if 5 restarts within 5 cycles then timeout

(서버에 실제로 존재하는 프로세스만 모니터링하도록 확인하십시오. 그렇지 않으면 monit가 시작되지 않습니다. 예를 들어, monit에게 Postfix를 확인하도록 지시했지만 Postfix가 시스템에 설치되어 있지 않으면 monit가 시작되지 않습니다.)

구성 파일은 상당히 자명합니다. 옵션에 대해 확신이 서지 않으면 monit 문서를 참조하십시오: http://mmonit.com/monit/documentation/monit.html

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

   if failed host localhost port 80 protocol http
      and request "/monit_token" then restart

이는 monit가 포트 80에서 localhost에 연결을 시도하고 /monit_token 파일에 접근하려고 시도함을 의미합니다. 이 파일은 /var/www/html/monit_token입니다. 우리의 웹 사이트의 문서 루트는 /var/www/html입니다. Monit가 성공하지 못하면 Apache가 실행되고 있지 않다는 의미이며, Monit는 이를 재시작할 것입니다. 이제 /var/www/html/monit_token 파일을 생성하고 그 안에 임의의 문자열을 작성해야 합니다:

touch /var/www/html/monit_token

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

mkdir /var/certs  
cd /var/certs

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

nano /var/certs/monit.cnf
# RSA 인증서 생성 - 서버

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

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

systemctl restart monit

이제 브라우저를 https://www.example.com:2812/로 이동하십시오(포트 2812가 방화벽에 의해 차단되지 않았는지 확인하십시오), admin과 test로 로그인하면 Monit 웹 인터페이스를 볼 수 있습니다. 다음과 같이 보여야 합니다:

Monit 메인 화면.

(메인 화면)

Monit apache 세부정보.

(Apache 상태 페이지)

/etc/monit.d/monitrc의 구성에 따라 Monit는 서비스가 실패할 경우 이를 재시작하고 서비스의 프로세스 ID가 변경되면 알림 이메일을 전송합니다.

6 링크

Share: X/Twitter LinkedIn

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

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