debian下的mrtg,实现网络流量监控。

apt-get install mrtg snmpd

安装完毕后,自动生成/usr/share/mrtg
ThinkPad:/usr/share/mrtg# pwd
/usr/share/mrtg
并且自动加入计划任务:

art@ThinkPad:/etc$ cat /etc/cron.d/mrtg
*/5 * * * * root if [ -x /usr/bin/mrtg ] && [ -r /etc/mrtg.cfg ]; then env LANG=C /usr/bin/mrtg /etc/mrtg.cfg 2>&1 | tee -a /var/log/mrtg/mrtg.log ; fi

vim /etc/cron.d/mrtg

0-55/5 * * * * root if [ -x /usr/bin/mrtg ] && [ -r /etc/mrtg.cfg ]; then env LANG=C /usr/bin/mrtg /etc/mrtg.cfg >> /var/log/mrtg/mrtg.log 2>&1; fi

查看snmp设置:

vim /etc/snmp/snmpd.conf
# sec.name source community
# com2sec paranoid default public
com2sec readonly default public
#com2sec readwrite default private

注释
com2sec paranoid default public
取消注释
com2sec readonly default public

启动snmpd
/etc/init.d/snmpd restrat

使用cfgmaker生成snmp设备的mrtg配置文件。

ThinkPad:/etc/snmp#

cfgmaker --global 'WorkDir: /usr/share/mrtg' --global 'Options[_]: bits,growright' --output /etc/mrtg.cfg [email protected]
ThinkPad:/etc/snmp#

生成index.html

indexmaker --columns=1 --output=/usr/share/mrtg/index.html --title='Traffic Info' /etc/mrtg.cfg

vim /etc/apache2/conf.d/mrtg.conf

Alias /mrtg /usr/share/mrtg

Options +FollowSymLinks
AllowOverride All
order allow,deny
allow from all

访问:
http://127.0.0.1/mrtg

BTW:

Some of examples how to monitor cpu and memory usage

CPU Usage

/etc/mrtg/cpu.cfg

WorkDir: /var/www/localhost/htdocs/mrtg
LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[localhost.cpu]:ssCpuRawUser.0&ssCpuRawUser.0:[email protected]+ ssCpuRawSystem.0&ssCpuRawSystem.0:[email protected]+
ssCpuRawNice.0&ssCpuRawNice.0:[email protected]
RouterUptime[localhost.cpu]: [email protected]
MaxBytes[localhost.cpu]: 100
Title[localhost.cpu]: CPU Load
PageTop[localhost.cpu]:Active CPU Load %

Unscaled[localhost.cpu]: ymwd
ShortLegend[localhost.cpu]: %
YLegend[localhost.cpu]: CPU Utilization
Legend1[localhost.cpu]: Active CPU in % (Load)
Legend2[localhost.cpu]:
Legend3[localhost.cpu]:
Legend4[localhost.cpu]:
LegendI[localhost.cpu]: Active
LegendO[localhost.cpu]:
Options[localhost.cpu]: growright,nopercent

Memory Usage

/etc/mrtg/mem.cfg

LoadMIBs: /usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[localhost.mem]: .1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.4.1.2021.4.6.0:public@localhost
PageTop[localhost.mem]:Free Memory

WorkDir: /var/www/localhost/htdocs/mrtg
Options[localhost.mem]: nopercent,growright,gauge,noinfo
Title[localhost.mem]: Free Memory
MaxBytes[localhost.mem]: 1000000
kMG[localhost.mem]: k,M,G,T,P,X
YLegend[localhost.mem]: bytes
ShortLegend[localhost.mem]: bytes
LegendI[localhost.mem]: Free Memory:
LegendO[localhost.mem]:
Legend1[localhost.mem]: Free memory, not including swap, in bytes

Creating jobs for the above two examples

CPU

/etc/cron.mrtg/cpu

#!/bin/sh
/usr/bin/mrtg /etc/mrtg/cpu.cfg

Memory

/etc/cron.mrtg/mem

#!/bin/sh
/usr/bin/mrtg /etc/mrtg/mem.cfg

Run each script 3 times (disregard the warnings)

/etc/cron.mrtg/cpu
/etc/cron.mrtg/mem

Make the Index Files

#/usr/bin/indexmaker --output=/var/www/mrtg/index.html \
--title="Memory and CPU Usage :)" \
--sort=name \
--enumerate \
/etc/mrtg/cpu.cfg \
/etc/mrtg/mem.cfg

Make the mrtg.cfg file

#cfgmaker --global "WorkDir: /var/www/mrtg/" \
--global "Options[_]: growright,bits" \
--ifref=ip \
public@localhost > /etc/mrtg/mrtg.conf

Cronjob setup

/bin/cat >> /var/spool/cron/crontabs/root
*/5 * * * * /bin/run-parts /etc/cron.mrtg 1> /dev/null

Now you logon to your web browser and check you should see the graphs