1.安装
./configure --prefix=/usr/local/mrtg
make
make install
2.设置
vi /etc/snmp/snmpd.conf
将
#view systemview included mib2的内容修改为:
view mib2 included .iso.org.dod.internet.mgmt.mib-2 fc
然后将
access notConfigGroup "" any noauth exact systemview none none
修改为:
access notConfigGroup "" any noauth exact mib2 none none
再重新启动snmpd:
/etc/rc.d/init.d/snmpd restart
3.修改配置文件,实现对cpu、内存、网络、硬盘的监控
vi mrtg.cfg
######################################################################
# System: public
# Description: linux public 2.6.9-1.667smp #1 SMP Tue Nov 2 14:59:52 EST 2004 i686
# Contact: Root
# Location: Unknown (edit /etc/snmp/snmpd.conf)
######################################################################
WorkDir: /home/mrtg
Options[_]: growright, bits
Language:GB2312
EnableIPv6: no
Target[network]: 2:public@localhost:
SetEnv[network]: MRTG_INT_IP="localhost" MRTG_INT_DESCR="eth0"
MaxBytes[network]: 12500000
Title[network]:网络流量监控
PageTop[network]:
网络流量监控
#Target[cpu]: 100 - .1.3.6.1.4.1.2021.11.11.0&.1.3.6.1.4.1.2021.11.11.0:public@localhost
#Target[cpu]:.1.3.6.1.4.1.2021.11.9.0&.1.3.6.1.4.1.2021.11.10.0:public@localhost
Target[cpu]:`/home/mrtg/conf/cpu.pl`
MaxBytes[cpu]: 100
Title[cpu]: CPU负载分析
PageTop[cpu]:
CPU负载分析
Options[cpu]: nopercent,growright,gauge,transparent
YLegend[cpu]: CPU Loading
ShortLegend[cpu]: %
LegendI[cpu]: User
LegendO[cpu]: System
Legend1[cpu]: User CPU负载(%)
Legend2[cpu]: System CPU负载(%)
Target[memory]: `/home/mrtg/conf/memory.pl`
MaxBytes[memory]: 100
Options[memory]: nopercent,growright,gauge
YLegend[memory]: Memory Using(%)
ShortLegend[memory]: %
LegendI[memory]: 物理内存负载
LegendO[memory]: 虚拟内存负载
Legend1[memory]: 物理内存负载(%)
Legend2[memory]: 虚拟内存负载(%)
Title[memory]: 内存负载分析
PageTop[memory]:
内存负载分析
Target[load]: `/home/mrtg/conf/load.sh`
Xsize[load]: 600
Ysize[load]: 200
Ytics[load]: 10
MaxBytes[load]: 3000
PageTop[load]:
服务器 Load Average
Options[load]: growright, gauge, nopercent
YLegend[load]: Load average (10E-2)
ShortLegend[load]: (10E-2)&
Legend1[load]: Load over 5 minutes&
Legend2[load]: Load over last 15 minutes&
Legend3[load]: Average over last 5 minutes
Legend4[load]: Average over last 15 minutes
LegendO[load]: 15 minute stagger&&
Title[load]: System load
Target[disk]: `/home/mrtg/conf/df.pl`
Xsize[disk]: 600
Ysize[disk]: 200
Ytics[disk]: 10
Title[disk]: 服务器硬盘使用情况
Unscaled[disk]: dwym
MaxBytes[disk]: 76524755
PageTop[disk]:
服务器硬盘使用情况
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
4.调用以下程序(cpu.pl、memory.pl、load.sh、df.pl
#cpu.pl
#!/usr/bin/perl
$machine="211.167.240.54";
$getcpu=`/usr/bin/sar -u 1 3|grep Average`;
$getuptime=`/usr/bin/uptime`;
$getcpu=~/^Average:\s+(\w+)\s+(\d+).\d+\s+(\d+).\d+\s+(\d+)\.(\d+)\s+(\d+)\.(\d+)/;
$getcpuusr=$2;
$getcpusys=$3;
print $getcpuusr."\n";
print $getcpusys."\n";
#print "2\n1\n";
$getuptime=~/.+up\s+(.+)(\,\s+\d+\s+user)(.+)/;
print $1."\n";
print $machine."\n";
#df.pl
#!/usr/bin/perl
# This script was written on Debian 3.0, it assumes that the command
# output(df -kl) looks like this:
# Filesystem 1k-blocksUsed Available Use% Mounted on
# /dev/md095645100304013126427208033% /
# /dev/hde1141191159 12231 9% /boot
#
# In which case, this script returns :
# 95659219
# 30402503
# when run.
foreach $filesystem (`df -hl | grep -v "Filesystem"`)
{
@df = split(/\s+/,$filesystem);
$total += $df[1];
$usage += $df[2];
}
print "$total\n";
print "$usage\n";
uname -a
#memory.pl
#!/usr/bin/perl
$machine="localhost";
$memuse=`sar -r 1 5|grep Average`;
$memuse=~/Average:\s+\d+\s+\d+\s+(\d+).\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+).\d+\D+/;
$memused=$1;
$swpused=$2;
print $memused."\n";
print $swpused."\n";
#print "36.45\n2.50\n";
$times=`/usr/bin/uptime`;
$times=~/.+up\s+(.+)(\,\s+\d+\s+user)(.+)/;
$time=$1;
print $time."\n";
print $machine."\n";
#load.sh
#!/bin/sh
# first the load 5 and 15 min avg
# multiply * 100 to avoid floats
# it helps if mrtg "period" is a multiple of 5 mins
uptime | sed -e 's/^.*average.*: \(.*\)$/\1/' -e 's/ //g' |
awk -F, '{ printf("%.0f\n",$2*100); printf("%.0f\n",$3*100) }'
# the uptime
uptime | sed 's:^.* up \(.*\), [0-9][0-9]* users.*$:\1:'
# my name
uname -n
5.运行生成图表
/usr/loca/mrtg/bin/mrtg /home/mrtg/conf/mrtg.cfg
6.制作index.html文件
/usr/local/mrtg/bin/indexmaker /home/mrtg/conf/mrtf.cnf --output=/home/mrtg/index.html\
