HowTo : collectd 설치하기

collectd logo

가볍고 사용하기 쉬운 network monitoring system “collectd”

Background

평소 SNMP(Simple Network Management Protocol)MRTG(Multi Router Traffic Grapher)를 이용하여 System Resource Monitoring을 해왔으나, 지표 추가하는데 MRTG 설정 파일 수정이 복잡하고, 귀찮아서 좀 더 편하게 System Monitoring을 하고자 자료 좀 찾아봤다. Network monitoring tool에 대한 위키페이지를 보던 중 회사, 해외 *nix community에서 종종 접해 눈에 익숙한 collectd를 선택했다.

What is collectd?

collectd는 한 host의 system resources data를 수집하고 central host로 data를 전송하는 daemon이다. 특별한 기능이 있지는 않지만 풍부한 Plug-in이 많으며 설치/설정이 간단한 것이 특징이다. 지속적인 서버 유지보수를 위해서 간단한 설정이 요구된다고 말 하는 것은 말하는 내 입만 아프게 할 뿐이다. collectd 공식 홈페이지에서 설명하는 major feature들을 살펴보자.

* Modularity / Portability
POSIX 준수는 기본이다. 장점이라기 보다는 오히려 지키지 않는 것이 문제 있는 것 아닌가?

* Reasonable defaults
대부분의 plug-in 설정을 수정하지 않고 사용할 수 있다. Ubuntu Server(10.10)의 경우 apt-get으로 collectd를 설치하면 별도 설정 수정없이 11개(cpu, df, disk, entropy, interface, irq, load, memory, processes, swap, users)지표를 볼 수 있다. 배포판 마다 enabled plug-in 수는 다르다. CentOS의 경우 4개(cpu, interface, load, memory) plug-in이 enable 되어 있다.

* High-resolution statistics
C language로 implementation되어 server에 부담이 없기 때문에 10초 단위로 system resources data를 수집한다. 혹시라도 service에 지장을 줄 수 있으니 적용하는 서버에 맞게 interval을 조정하는 것을 추천한다.

* Sophisticated network code
unicast, multicast, proxy는 기본이며 encrypted data transfer까지 지원한다. router의 “accept multicast” option을 enable 시키고 collectd data transfer 설정을 multicast로 해두면 Server가 증설 될 때 보다 설정이 간편해 진다.

* Custom extensions
collectd의 기본 plug-in에 없는 metric을 수집하기 위해 충분한 Custom extension을 제공한다. C, Perl, Python, Java, Execute binaries or scripts extension으로 못 하는 것이 있을까?

* Build to scale
가벼운 daemon, 가벼운 udp datagram, multicast 지원으로 scalability도 괜찮을 것 같다.

* SNMP support
위에 언급한 Custom extension으로 수집할 수 있는 data를 별도의 SNMP Plug-in을 지원함으로써 보다 간단한 설정으로 SNMP query result를 수집 할 수 있다.

* Integration with monitoring solutions
collectd 는 monitoring solution이 아니기 때문에 현재 monitoring system feature가 없다고 하면서 sophisticated monitoring solution을 발견하면 collectd와 통합할 계획이라고 한다.

Environment Setup

network diagram

machine information

[table id=1 /]

Installation

CentOS(5.5) guide

* Enable EPEL (Extra Packages for Enterprise Linux) Repository
CentOS-5의 Base, Extras repository에는 collectd package가 없기 때문에 EPEL repository를 enable 해야 한다.

rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm

* install yum-priorities
대부분의 system administrator들이 CentOS를 선택한 이유는 stable version package를 사용하기 위해서다. EPEL를 enable하고 나서 “yum update”를 하면 EPEL의 package 들이 기존 package들을 overwrite하기 때문에 system을 망칠 수 있다. 그래서 CentOS base repository를 보호하기 위해서 yum-protectbase package를 설치하면 해결되지만, 필자는 repository의 priority를 1부터 99까지 세세하게 설정할 수 있는(more fine-grained repository ordering) yum-priorities를 설치하였다. 왜냐하면 필자의 개발 서버에는 3rd party repositories를 enable하여 여러 repository간의 priority를 정해야 하기 때문이다.

yum install yum-priorities

자세한 내용은 공식 CentOS package management wiki page를 참고하기 바란다.

위 설정이 귀찮아 혹시라도 shell alias의 yum command에 “–disablerepo=epel” option을 추가하여 간단히 해결하고자 한다면 필자는 적극 말리고 싶다. 운영중인 server에 다른 sysadmin이 default shell이 아닌 다른 shell로 “yum update”를 실행 할 수도 있으며, 언제 3rd party repository를 enable할지 모르기 때문이다.

* install collectd

yum install collectd-*

* Hostname 설정
/etc/collectd.conf file을 열어 server hostname과 동일하게 맞춘다.

Hostname    "server01.svc"

* rrd DataDir path 설정
default save DataDir은 “/var/lib/collectd” 로 되어 있지만, rrd도 하나의 plug-in이기 때문에 “/var/lib/collectd/rrd”로 설정을 하였다.
/etc/collectd.conf file을 열어 rrdtool plug-in 설정을 아래와 같이 설정한다.

#   DataDir "/usr/var/lib/collectd/rrd"
    DataDir "/var/lib/collectd/rrd"
#   CacheTimeout 120
#   CacheFlush   900

CGI script의 default read DataDir도 “/var/lib/collectd”로 되어 있기 때문에 위에서 설정한 동일한 경로로 수정한다.
/etc/collection.conf file을 열어 아래와 같이 수정한다.

DataDir "/var/lib/collectd/rrd/"

* start collectd

/etc/init.d/collectd start

rrd file이 정상적으로 쌓이고 있는지 확인하자.

ls /var/lib/collectd/rrd/server01.svc

* extract RRD data in a graphical format
엄밀히 말해서 collectd의 R&R은 rrdtool plug-in을 이용하여 rrd file을 생성하는 것이지만, 친절하게도(?) perl CGI script를 제공하고 있다. 따라서 외부에서 접근 못 하도록 ACL만 수정하면 된다.

/etc/httpd/conf.d/collectd.conf file을 열어서 private network에서만 접근 가능하도록 ACL 을 수정한다.

    AddHandler cgi-script .cgi
    DirectoryIndex bin/index.cgi
    Order Deny,Allow
    Deny from all
    Allow from 127.0.0.1
    Allow from 192.168.0.0/24

자 이제 웹 브라우저 주소창에 아래 주소를 입력하면 collectd가 쌓아놓은 데이터를 그래프로 볼 수 있다.

http://192.168.0.101/collectd/

Ubuntu(10.10) guide

* install collectd

sudo apt-get install collectd

* Hostname 설정
/etc/collectd/collectd.conf file을 열어 server hostname과 동일하게 맞춘다.

Hostname    "server01.box"

* disable rrdtool plug-in (option)
server01.box 에서는 rrd file을 남기지 않고 network를 통해 server01.svc로 전송 할 예정이므로 rrdtool plug-in 을 disable 하여 불필요한 리소스(CPU, Disk) 사용을 줄이는 것이 좋겠다.
/etc/collectd/collectd.conf file을 열어 “LoadPlugin rrdtool” 라인을 주석처리한다.

#LoadPlugin rrdtool

* start collectd

sudo /etc/init.d/collectd start

Centralize Data

collectd는 UDP를 이용하여 data를 전송한다. 간단하게 unicast 방식으로 설정할 수 있으며, switch 장비가 multicast stream을 지원한다면 multicast 방식을 사용하는 것이 more sustainable 하다고 생각한다. 왜냐하면 data를 집중화하는 server 를 옮길 때 unicast 방식은 다른 서버들의 설정 수정이 불가피하기 때문이다. 물론 내부 DNS server를 운영하여 앞서말한 문제를 해결 할 수도 있지만, multicast 가 가진 장점을 이용한다면 좀 더 유연하게 문제들을 대처할 수 있으리라 생각한다.

unicast

* server01.svc
/etc/collectd.conf file을 열어 NIC address를 Listen에 등록한다.

LoadPlugin network

Listen "192.168.0.101" "25826"

* server0[1-4].box
/etc/collectd/collectd.conf file을 열어 전송하고자 하는 server address를 등록한다.

LoadPlugin network

Server "192.168.0.101" "25826"

multicast

* server01.svc
/etc/collectd.conf file을 열어 Multicast Group Address를 Listen에 등록한다.

LoadPlugin network

Listen "239.192.74.66" "25826"

* server0[1-4].box
/etc/collectd/collectd.conf file을 열어 Multicast Group Address를 Server에 등록한다.

LoadPlugin network

Server "239.192.74.66" "25826"

* enable multicast
대부분의 switch, router 장비는 multicast disabled 되어 있을 것이다. 필자는 web으로 router(DIR-615)에 접속하여 enable multicast stream 을 체크하였다.

multicast address(239.192.74.66)가 router 장비에 등록 되어있음을 확인 할 수 있다.

Plug-in Configuration

collectd plug-in 페이지를 참고하여 몇가지 plug-in을 등록해 보았다. 다시 한번 말하지만 간단한 설정파일 수정으로 짧은 시간 내에 여러 가지 지표를 볼 수 있는 것이 collectd의 가장 큰 매력이다.

* logfile
default로 syslog plug-in이 enable되어 있어 /var/log/messages 에서 collectd의 log를 확인 할 수 있다. 하지만 다른 facility들과 섞여 있어 가독성이 좋지 않아서 별도 파일로 기록했다.

LoadPlugin logfile

LogLevel "info"
File "/var/log/collectd.log"
Timestamp true

* apache
apache 지표를 보려면 apache status module을 load하고 적절히 ACL을 수정해야 한다. 자세한 설정은 apache module mod_status document를 참고하기 바란다.

LoadPlugin apache

URL http://localhost/server-status?auto

* cpu (default)

* df
shell에 “df -T”를 입력하여 device와 filesystem을 확인하고 그대로 등록을 해주면 된다.

LoadPlugin df

Device "/dev/mapper/VolGroup00-LogVol00"
FSType "ext3"
IgnoreSelected false
ReportByDevice true
ReportReserved false
ReportInodes false

* disk
/proc/diskstats (Linux 2.6) or /proc/partitions (Linux 2.4)를 참고하여 perl regular expression으로 등록을 해주면 된다. (대부분의 경우 아래 주석처리한 첫 번째 라인으로 등록 가능함)

LoadPlugin disk

#Disk "/^[hs]d[a-f][0-9]?$/"
Disk "/^sd[a-f][0-9]?$/"
Disk "/^dm-[0-9]?$/"
IgnoreSelected false

* interface (default)

* irq
모든 irq number를 보기위해 아래와 같이 설정하였다.

LoadPlugin irq

IgnoreSelected true

* load (default)

* memory (default)

* mysql
localhost에서만 접근 가능한 monitor 전용 계정을 추가하였다.

LoadPlugin mysql

Host "localhost"
User "monitor"
Password ""
Database ""
# Specifies the path to the UNIX domain socket of the MySQL server. This option only has any effect, if Host is set to localhost (the default).
# Otherwise, use the Port option above. See the documentation for the mysql_real_connect function for details.
Socket "/var/lib/mysql/mysql.sock"
MasterStats true
#SlaveStats true
#SlaveNotifications true

* ntpd

LoadPlugin ntpd

Host "localhost"
Port 123
ReverseLookups false

* rrdtool (enable cache)
default로 collectd는 10초 주기로 rrd file을 update한다. 그런데 rrd file을 update하는데 load가 적잖게 걸린다고 하니 data를 memory에 임시로 저장해놨다가 한꺼번에 rrd files에 update 하는 것이 좋겠다. 단, 그만큼 rrd file이 늦게 update되기 때문에 실시간성이 떨어진다.

LoadPlugin rrdtool

DataDir "/var/lib/collectd/rrd"
# The trade off is that the graphs kind of "drag behind" and that more memory is used
CacheTimeout 120 # interval for rrd files to be updated
# If some RRD-file is not updated anymore for some reason (the computer was shut down, the network is broken, etc.) some values may still be in the cache.
# If CacheFlush is set, then the entire cache is searched for entries older than CacheTimeout seconds and written to disk every Seconds seconds
CacheFlush   900

* tcpconns

LoadPlugin tcpconns

ListeningPorts false
LocalPort "22"
LocalPort "80"
LocalPort "3306"

* uptime
별도 설정 필요 없음

* vmem

LoadPlugin vmem

Verbose false

Pros and Cons

Trouble Shooting

* client가 network를 통해 전송한 data가 rrd file로 기록은 되는데 web page에서 보이지 않는다.
우선 정상적으로 rrd file에 기록이 되는지 확인하기 위해 rrdtool dump 를 이용하여 눈으로 직접 데이터 값을 확인한다. 확인 해야 할 항목 : date, value

Leave a comment

Your email address will not be published. Required fields are marked *