当前位置:Linux教程 - Linux文化 - MRTG配置完整手册

MRTG配置完整手册


1. 安装. (1)需要的RPM RHEL 3.0/4.0的安装CD带有MRTG的RPM安装包. 为了使用MRTG的一些脚本,需要安装其他的一些安装包. Sysstat.rpm 系统状况监视安装包.(cpu.sh) Procps.rpm 内存,Swap 使用情况工具等(cpu.sh,mem.sh,swap.sh) Coreutils.rpm (df.pl,disk.sh) gd-1.8.4-4 gd-devel-1.8.4-4 perl-5.6.0-17 perl-CGI mod_perl-1.24_01-3 libpng-1.0.12-2 libpng-devel-1.0.12-2 gcc-2.96-98 gcc-g77-2.96-98 gcc-c++-2.96-98 zlib-1.1.3-24 zlib-devel-1.1.3-24 httpd(apache) net-snmp mrtg 检查装有没有上面rpm包. (2)安装MRTG Rpm –ivh mrtg -2.9.29-4.ent.i386.rpm

2. 配置SNMP 让他可以配合MRTG工作 编辑/etc/snmp/snmpd.conf 把下面的#号去掉 #view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc #在89行 把下面的语句#在62行 access notConfigGroup "" any noauth exact systemview none none 改为: access notConfigGroup "" any noauth exact mib2 none none 改完重启一下snmp Service snmpd restat

3. 配置MRTG (1) mkdir /var/www/html/mrtg (2) mkdir /opt/mrtg (3) 从下面的贴子复制以下几个script 到/opt/mrtg Cpu.sh , mem.sh , swap.sh, diskperf.sh, disk.pl (4) cp /var/www/mrtg/* /var/www/html/mrtg (5) /usr/bin/cfgmaker –output=/var/www/html/mrtg/mrtg.cfg [email protected] 用来配置一个mrtg配置文件. (6) 编辑MRTG的配置文件 vi /var/www/html/mrtg/mrtg.cfg (1)把WorkDir: /var/www/html/mrtg 加入到配置文件的前面. (2)把Options[_]: growright, bits 前面的#删掉. (3)把要监测的几个ITEM加入到配置文件里面去. 1) CPU ################CPU###################### Target[cpu]: `/opt/mrtg/cpu.sh` MaxBytes[cpu]: 12500000 Title[cpu]: CPU -- XMNLINUXTEST02 Options[cpu]: gauge,nopercent,growright YLegend[cpu]: CPU loading (%) ShortLegend[cpu]:% LegendO[cpu]: & CPU USER; LegendI[cpu]: & CPU SYSTEM; PageTop[cpu]: ;CPU -- XMNLINUXTEST02; 2)NIC 用配置文件自动配好的. 3)MEM 这个图最大设为2G,如果服务器有更大的内存,可以修改Maxbytes. ############### MEM 2G ################# Target[managemem]:`/opt/mrtg/mem.sh` Unscaled[managemem]: dwym MaxBytes[managemem]: 2048000 Title[managemem]:Memory ShortLegend[managemem]: & kmg[managemem]:kB,MB kilo[managemem]:1024 YLegend[managemem]: Memory Usage Legend1[managemem]: Total Memory Legend2[managemem]: Used Memory LegendI[managemem]: Total Memory LegendO[managemem]: Used Memory Options[managemem]: growright,gauge,nopercent PageTop[managemem]:;Memory -------XMNLINUXTEST2; 4)Swap 也是设为2G,根据实际情况改MaxBytes. ################### Swap 2G####################### Target[swap]:`/opt/mrtg/swap.sh` Unscaled[swap]: dwym MaxBytes[swap]: 2048000 Title[swap]:Memory State of Server ShortLegend[swap]: & kmg[swap]:kB,MB kilo[swap]:1024 YLegend[swap]: Swap Usage Legend1[swap]: Total Swap Legend2[swap]: Used Swap LegendI[swap]: Total Swap LegendO[swap]: Used Swap Options[swap]: growright,gauge,nopercent PageTop[swap]:;Swap-------XMNLINUXTEST02;

5)设置磁盘使用量 最大为480G,可以根据实际情况修改MaxBytes ################## Disk Space 480G ######################### Target[disk]: `/opt/mrtg/df.pl` Title[disk]: Disk Space ----XMNLINUXTEST02 Unscaled[disk]: dwym MaxBytes[disk]: 465247550 kmg[disk]: KB,MB,GB LegendI[disk]: Total Disk Space LegendO[disk]: Used Disk Space Legend1[disk]: Total Disk Space Legend2[disk]: Used Disk Space YLegend[disk]: Megabytes ShortLegend[disk]: & Options[disk]: growright,gauge,nopercent PageTop[disk]: ;Disk Space --------XMNLINUXTEST02; 6)磁盘I/O的检测,如果需要的检,更多的分区,修改并复制这个部分,并修改diskperf.sh ################## Disk I/O 100K/sec /DEV/SDA ########## # If you want monitor more disk, please copy below create # now monitor job, and copy & modify the diskperf.sh to setting disk. Target[diskIO]: `/opt/mrtg/diskperf.sh` Title[diskIO]: Disk HDA I/O Utilization Report Unscaled[diskIO]: dwym MaxBytes[diskIO]: 100 PageTop[diskIO]: ;Disk I/O Utilization Report; kmg[diskIO]: KB,MB,GB LegendI[diskIO]: Disk I/O KBread/sec LegendO[diskIO]: Disk I/O KBwrite/sec Legend1[diskIO]: Disk I/O KBread/sec Legend2[diskIO]: Disk I/O KBwrite/sec YLegend[diskIO]: Megabytes ShortLegend[diskIO]: & Options[diskIO]: growright,gauge,nopercent

4. 编译配置文件 先在/etc/profile 加入两个参数 LC_ALL=C LANGUAGE=on

# env LANG=C /usr/bin/mrtg /var/www/html/mrtg/mrtg.cfg 前面两次会出错,多运行几次就没有问题. 5. 生成html文件. /usr/bin/indexmaker –output=/var/www/html/mrtg/index.html –title=主机名 /var/www/html/mrtg/mrtg.cfg 6. 配置apache 的设置. #vi /etc/httpd/conf.d/mrtg.cfg 把Alias /mrtg /var/www/mrtg 改为Alias /mrtg /var/www/html/mrtg 把Allow from localhost 改为Allow from all #service httpd restart ,重启apache. 7. 配置,自运行脚本,每五分钟自动运行一次. 在root 下启动 crontab –e 添加计划 */5 * * * * env LANG=C /usr/bin/mrtg /var/www/html/mrtg/mrtg.cfg 8. 确认service http&crontab的运进级别. 可以用下面两个命令查看这个服务的进行状况, #chkconfig –list |grep httpd #chkconfig –list |grep crontab 或用设定运行级别 chkconfig --level 35 httpd on chkconfig --level 35 crontab on

5. 常见问题 (1)问:实际内存或硬盘跟的图里面的值相差太多,要吗线超出图表,要吗,在图表的下面. 如何更改图表的比例? 如内存实际上只有512M,但表里面内存显示最大为2G. 答:修改/var/www/html/mrtg/mrtg.cfg 找到相关的部分, 修改MaxBytes[managemem]: 2048000 中的2048000为512000 (2)问:我有几个硬盘,如果要监测其他的硬盘IO? 答:首先复制一份diskperf.sh 成diskperf2.sh ,再修改diskperf2.sh里面把hd=xxx改为你要监测的硬盘,再修改/var/www/html/mrtg/mrtg.cfg 复制sda的那一部门,并修改相关的部分. 如Target[diskIO]:`/opt/mrtg/diskperf.sh` 改成Target[diskIO]: `/opt/mrtg/diskperf2.sh` 这一部份的[diskIO]改为disk2IO (3)问:修改了配置文件/var/www/html/mrtg/mrtg.cfg 后,网页没有生效 答:修改完配置文件应该要重新用下面命令生成新的网页. /usr/bin/indexmaker –output=/var/www/html/mrtg/index.html –title=主机名 /var/www/html/mrtg/mrtg.cfg (4)问:如何看我的MRTG图. 答:用http://你的IP/mrtg/ 就可以看到了. 附:1: mrtg.cfg 里面几个参数的意思. Target:是要执行的脚本 Xsize:生成图表的横向宽度(最大600) Ysize:生成图表的纵向高度(最大200) Title:标题 kMG: Change the default multiplier prefixes Ytics:纵向划分为几个块(格子) MaxBytes:图表纵向数值的最大上限 PageTop:页面上面的提示 kilo:一般是写1024,如果需要的话,是1000在计算机里的单位 LegendI:从SHELL返回的数据中的第一个 LegendO:从SHELL返回的数据中的第二个 Options: growright,表示图表向右延展 附2:各个script (1) cat /opt/mrtg/cpu.sh #!/bin/bash cpuusr=`/usr/bin/sar -u 1 3 |grep Average |awk '{print $3}'` cpusys=`/usr/bin/sar -u 1 3 |grep Average |awk '{print $5}'` UPtime=`/usr/bin/uptime |awk '{print $3""$4""$5}'` echo $cpuusr echo $cpusys echo $UPtime hostname (2) Disk usage cat /opt/mrtg/df.pl #!/usr/bin/perl # output(df -kl) looks like this: # Filesystem 1k-blocks Used Available Use% Mounted on # /dev/md0 95645100 30401312 64272080 33% / # /dev/hde1 14119 1159 12231 9% /boot # # In which case, this script returns : # 95659219 # 30402503 # when run. foreach $filesystem (`df -kl | grep -v "Filesystem"`) { @df = split(/\s+/,$filesystem); $total += $df[1]; $usage += $df[2]; } print "$total\n"; print "$usage\n"; hostname (3) DISK IO # cat /opt/mrtg/diskperf.sh

#!/bin/bash # This script will monitor the KBread/sec &KBwriten/sec of Disk. # Creater: CCC IT loren ext:2288 2005/8/3 # As sda ,sdb,sdc,sdd,hda.

# disk=sda hd=sda disk=/dev/$hd KBread_sec=`iostat -x $disk|grep $hd |awk '{print $8}'` KBwrite_sec=`iostat -x $disk|grep $hd |awk '{print $9}'` echo "$KBread_sec" echo "$KBwrite_sec" hostname (4)MEMORY cat /opt/mrtg/mem.sh #!/bin/bash # This script to monitor the mem usage. totalmem=`/usr/bin/free |grep Mem |awk '{print $2}'` usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'` echo "$totalmem" echo "$usedmem" (5)SWAP cat /opt/mrtg/swap.sh #!/bin/bash # This script to monitor the swap usage. totalswap=`/usr/bin/free |grep Swap |awk '{print $2}'` usedswap=`/usr/bin/free |grep Swap |awk '{print $3}'` echo "$totalswap" echo "$usedswap"