Translate

12/04/2013

linux server management 11

MRTG와 SNMP 활용한 시스템 모니터링
이제 SNMP와 MRTG를 결합하여 보자. SNMP나 쉘 스크립트 등을 통하여 모니터링 자료를 얻고 이 자료를 토대로 하여 MRTG를 이용 웹페이지로 만드는 작업이다. MRTG에서 자료를 처리하기 위해서는 SNMP가 아니라 하더라도 쉘 스크립트를 이용하여 필요한 두 개의 값만 가져오면 된다.
이에 대한 예제가 web_count.sh 와 mysql_count.sh 이다. 현재 서버의 웹프로세스와 동시접속자수, Mysql 프로세스와 동시접속자수를 구하는 스크립트이다. 웹에서 동시접속자수는 tcp-ip 상태가 ESTABLISHED로 되어 있는것만 포함을 했다. SYN_SENT, TIME_WAIT 등 다른 것들은 접속시도 및 접속해제와 관련되어 있기 때문에 뺀 것이다. (**주1)


$ cat web_count.sh
#!/bin/sh
a=`ps auxw | grep http | grep -v grep | wc -l`
b=`netstat -an | grep :80 | grep -v "0.0.0.0" | grep ESTABLISHED | wc -l`


echo $a
echo $b


$ cat mysql_count.sh
#!/bin/sh
a=`ps auxw | grep mysqld | grep -v grep | wc -l`
b=`netstat -an | grep mysql.sock | wc -l`


echo $a
echo $b



첫 장에서 설명을 한 대로 free 메모리를 정확히 파악하려면 Free+Buffer+Cached를 합쳐야 한다.


아래 mrtg.cfg 파일을 참고하여 판단을 하기 바란다. Target에는 MRTG에서 모니터링할 대상을 정해주는데 SNMP를 이용하는 경우 OID는 숫자나 이름 모두 사용할 수 있다. (iso.org.dod.internet.mgm.mib-2.system =1.3.6.1.2.1.1) 참고가 될듯하여 혼합해서 설정을 했다. 위에서 만든 web_count.sh, mysql_cout.sh 스크립트도 target에 지정하면 되는데 MRTG가 실행되면서 해당 스크립트를 실행하여 그 결과값을 가져오는 것이다. 지면 사정상 모든 cfg 파일을 보여주지는 못하고 몇가지만 보여준다.


$ cat mrtg.cfg


## 작업 디렉토리
WorkDir: /home/httpd/html/mrtg


## 한글설정
Language: korean


#
## taejun.pe.kr - network traffic(eth0)
#


### Interface 2 >> Descr: 'eth0' | Name: '' | Ip: '211.111.111.111' | Eth: '00-01-02-26-bd-ce' ###


Target[tunelinux.pe.kr.eth0]: 2:taejun_community@localhost
SetEnv[tunelinux.pe.kr.eth0]: MRTG_INT_IP="211.111.111.111" MRTG_INT_DESCR="eth0"
MaxBytes[tunelinux.pe.kr.eth0]: 10000000
Options[tunelinux.pe.kr.eth0]: bits
Title[tunelinux.pe.kr.eth0]: Traffic Analysis for -- tunelinux.pe.kr
PageTop[tunelinux.pe.kr.eth0]: <H1>Traffic Analysis for -- tunelinux.pe.kr</H1>
<TABLE>
<TR><TD>System:</TD> <TD>tunelinux.pe.kr in kdaily/seoul/korea</TD></TR>
<TR><TD>Maintainer:</TD> <TD>taejun@tunelinux.pe.kr</TD></TR>
<TR><TD>Description:</TD><TD>eth0 </TD></TR>
<TR><TD>ifType:</TD> <TD>ethernetCsmacd (6)</TD></TR>
<TR><TD>ifName:</TD> <TD></TD></TR>
<TR><TD>Max Speed:</TD> <TD>10,000 kbits/s</TD></TR>
<TR><TD>Ip:</TD> <TD>211.168.2.136 ()</TD></TR>
</TABLE>



#
## tunelinux.pe.kr - memory usage
#
Target[tunelinux.pe.kr.mem]: memAvailReal.0&memAvailReal.0:taejun_community@localhost * 1024 + memBuffer.0&memBuffer.0:taejun_community@localhost * 1024 + memCached.0&memCached.0:taejun_community@localhost * 1024
MaxBytes[tunelinux.pe.kr.mem]: 527654912
Title[tunelinux.pe.kr.mem]: tunelinux.pe.kr: MEMORY USAGE
kilo[tunelinux.pe.kr.mem]: 1024
Options[tunelinux.pe.kr.mem]: gauge, nopercent
YLegend[tunelinux.pe.kr.mem]: Free Memory
LegendI[tunelinux.pe.kr.mem]: Free Mem:&nbsp;
LegendO[tunelinux.pe.kr.mem]:
Legend1[tunelinux.pe.kr.mem]: Free Memory
Legend2[tunelinux.pe.kr.mem]:
Legend3[tunelinux.pe.kr.mem]:
Legend4[tunelinux.pe.kr.mem]:
ShortLegend[tunelinux.pe.kr.mem]: B
PageTop[tunelinux.pe.kr.mem]: <H1>tunelinux.pe.kr: Free Memory Usage</H1>
<TABLE>
<TR><TD>Free memory (Total - 512MB) : </TD> <TD> Free + buffers + cached </TD></TR>
</TABLE>



#
## tunelinux.pe.kr - load average
#
Target[tunelinux.pe.kr.load]: .1.3.6.1.4.1.2021.10.1.3.2&.1.3.6.1.4.1.2021.10.1.3.3:taejun_community@localhost * 100
WithPeak[tunelinux.pe.kr.load]: my
MaxBytes[tunelinux.pe.kr.load]: 10000
Title[tunelinux.pe.kr.load]: tunelinux.pe.kr: LOAD AVG.
Options[tunelinux.pe.kr.load]: gauge,integer,nopercent
YLegend[tunelinux.pe.kr.load]: Load Average
LegendI[tunelinux.pe.kr.load]: 5 min:
LegendO[tunelinux.pe.kr.load]: 15 min:
Legend1[tunelinux.pe.kr.load]: Load average for 5 Min
Legend2[tunelinux.pe.kr.load]: Load average for 15 Min
Legend3[tunelinux.pe.kr.load]: Max load average for 5 Min
Legend4[tunelinux.pe.kr.load]: Max Load average for 15 Min
YTicsFactor[tunelinux.pe.kr.load]: 0.01
PageTop[tunelinux.pe.kr.load]: <H1>tunelinux.pe.kr: LOAD AVERAGE</H1>



#
# httpd daemon & httpd session
#


Target[tunelinux.pe.kr.web]: `/usr/local/mrtg/web_count.sh`
WithPeak[tunelinux.pe.kr.web]: my
MaxBytes[tunelinux.pe.kr.web]: 100
Title[tunelinux.pe.kr.web]: tunelinux.pe.kr: httpd daemon & httpd session
Options[tunelinux.pe.kr.web]: gauge,integer,nopercent
YLegend[tunelinux.pe.kr.web]: Number?LegendI[tunelinux.pe.kr.web]: httpd daemon:
LegendO[tunelinux.pe.kr.web]: httpd session:
Legend1[tunelinux.pe.kr.web]: Number of httpd
Legend2[tunelinux.pe.kr.web]: Number of session
Legend3[tunelinux.pe.kr.web]: 5 Min Max Number of httpd
Legend4[tunelinux.pe.kr.web]: 5 Min Max Number of httpd session
ShortLegend[tunelinux.pe.kr.web]: 개
PageTop[tunelinux.pe.kr.web]: <H1>tunelinux.pe.kr: httpd daemon & session</H1>



Target[tunelinux.pe.kr.mysql]: `/usr/local/mrtg/mysql_count.sh`
WithPeak[tunelinux.pe.kr.mysql]: my
MaxBytes[tunelinux.pe.kr.mysql]: 100
Title[tunelinux.pe.kr.mysql]: tunelinux.pe.kr: mysql daemon & mysql session
Options[tunelinux.pe.kr.mysql]: gauge,integer,nopercent
YLegend[tunelinux.pe.kr.mysql]: Number?LegendI[tunelinux.pe.kr.mysql]: mysql daemon:
LegendO[tunelinux.pe.kr.mysql]: mysql session:
Legend1[tunelinux.pe.kr.mysql]: Number of mysql
Legend2[tunelinux.pe.kr.mysql]: Number of session
Legend3[tunelinux.pe.kr.mysql]: 5 Min Max Number of mysql
Legend4[tunelinux.pe.kr.mysql]: 5 Min Max Number of mysql session
ShortLegend[tunelinux.pe.kr.mysql]: 개
PageTop[tunelinux.pe.kr.mysql]: <H1>tunelinux.pe.kr: mysql daemon & session</H1>



#
## tunelinux.pe.kr - disk usage
#
Target[tunelinux.pe.kr.disk]: .1.3.6.1.4.1.2021.9.1.6.1&.1.3.6.1.4.1.2021.9.1.8.1:taejun_community@localhost
Title[tunelinux.pe.kr.disk]: tunelinux.pe.kr: DISK USAGE
kilo[tunelinux.pe.kr.disk]: 1024
Options[tunelinux.pe.kr.disk]: gauge
LegendI[tunelinux.pe.kr.disk]: /home total:&nbsp;
LegendO[tunelinux.pe.kr.disk]: /home use:
Legend1[tunelinux.pe.kr.disk]: /home Disk usage
Legend2[tunelinux.pe.kr.disk]: use
MaxBytes[tunelinux.pe.kr.disk]: 10080520
ShortLegend[tunelinux.pe.kr.disk]: B
kMG[tunelinux.pe.kr.disk]: K,M,G,T,P
PageTop[tunelinux.pe.kr.disk]: <H1>tunelinux.pe.kr: DISK USAGE</H1>