Gentoo安装SNMP & MRTG 本机监控

Posted by Elton's Blog on July 20, 2009

必要组件:

  • [net-analyzer/net-snmp]
  • [media-libs/gd]
  • [net-analyzer/mrtg]
  • [sys-process/vixie-cron]

0) 切换用户

/bin/su -

1) 创建目录

/usr/bin/mkdir /etc/mrtg
/usr/bin/mkdir /etc/cron.mrtg
/usr/bin/mkdir /var/www/localhost/mrtg

2) 创建snmpd.conf文件

vim /etc/snmp/snmpd.conf
com2sec local     127.0.0.1/32    public
com2sec local     10.10.10.0/24   public

group MyROGroup v1         local
group MyROGroup v2c        local
group MyROGroup usm        local

view all    included  .1                               80

access MyROGroup ""      any       noauth    exact  all    none   none

syslocation MyLocation
syscontact Me <[email protected]> 

编辑 /etc/conf.d/snmpd 在SNMPD_FLAGS里面添加

-c /etc/snmp/snmpd.conf

如:SNMPD_FLAGS=”-c /etc/snmp/snmpd.conf”

3) 创建守护进程

/etc/init.d/snmpd start
/sbin/rc-update add snmpd default

4) 配置

Traffic:

/usr/bin/cfgmaker  
--output=/etc/mrtg/traffic.cfg 
--ifdesc=ip 
--ifref=descr 
--global "WorkDir: /var/www/localhost/mrtg" 
--global "Options[_]: bits,growright" 
[email protected]

Cpu:

vim /etc/mrtg/cpu.cfg
WorkDir: /var/www/localhost/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

Mem:

vim /etc/mrtg/mem.cfg
LoadMIBs: /usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[localhost.mem]: .1.3.6.1.4.1.2021.4.11.0&.1.3.6.1.4.1.2021.4.11.0:[email protected]
PageTop[localhost.mem]: 

Free Memory

WorkDir: /var/www/localhost/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

Swap:

vim /etc/mrtg/swap.cfg
LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[localhost.swap]: memAvailSwap.0&memAvailSwap.0:[email protected]
PageTop[localhost.swap]: 

Swap Memory

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

Ping:

vim /etc/mrtg/ping.cfg
WorkDir: /var/www/localhost/mrtg
Title[mithril.ping]: Round Trip Time
PageTop[mithril.ping]: 

Round Trip Time

Target[mithril.ping]: `/etc/mrtg/ping.sh` MaxBytes[mithril.ping]: 2000 Options[mithril.ping]: growright,unknaszero,nopercent,gauge LegendI[mithril.ping]: Pkt loss % LegendO[mithril.ping]: Avg RTT Legend1[mithril.ping]: Maximum Round Trip Time in ms Legend2[mithril.ping]: Minimum Round Trip Time in ms #Legend3[mithril.ping]: Maximal 5 Minute Maximum Round Trip Time in ms #Legend4[mithril.ping]: Maximal 5 Minute Minimum Round Trip Time in ms YLegend[mithril.ping]: RTT (ms)

5) 创建脚本

Traffic:

vim /etc/cron.mrtg/traffic.sh
#!/bin/sh
/usr/bin/mrtg /etc/mrtg/traffic.cfg 

Cpu:

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

Mem:

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

Swap:

vim /etc/cron.mrtg/swap.sh
#!/bin/sh
/usr/bin/mrtg /etc/mrtg/swap.cfg 

Ping:

vim /etc/cron.mrtg/ping.sh
#!/bin/sh
/usr/bin/mrtg /etc/mrtg/ping.cfg 

ping.sh

vim /etc/mrtg/ping.sh
#!/bin/sh
PING="/bin/ping"
# Google, for example
ADDR="google.com"
DATA=`$PING -c10 -s500 $ADDR -q `
LOSS=`echo $DATA | awk '{print $18 }' | tr -d %`
echo $LOSS
if [ $LOSS = 100 ];
then
               echo 0
else
        echo $DATA | awk -F/ '{print $5 }'
fi 

赋予脚本可执行权限

/bin/chmod +x /etc/cron.mrtg/*.sh
/bin/chmod +x /etc/mrtg/ping.sh 

下面的每个脚本执行3次,不必理会warning

/etc/cron.mrtg/traffic.sh
/etc/cron.mrtg/cpu.sh
/etc/cron.mrtg/mem.sh
/etc/cron.mrtg/swap.sh
/etc/cron.mrtg/ping.sh 

6) 创建MRTG首页

/usr/bin/indexmaker --output=/var/www/localhost/mrtg/index.html 
--title="Power Under Control : )" 
--sort=name 
--enumerate 
/etc/mrtg/traffic.cfg 
/etc/mrtg/cpu.cfg 
/etc/mrtg/mem.cfg 
/etc/mrtg/swap.cfg 
/etc/mrtg/ping.cfg 

7) 计划任务

每5分钟执行一次数据更新

usr/bin/crontab -e
*/5 * * * * /bin/run-parts /etc/cron.mrtg 1> /dev/null 

现在就有一个html文件生成好了/var/www/localhost/www/index.html 将你的web服务器设置好,打开流量器就可以看到统计数据了。

参考链接:http://forums.gentoo.org/viewtopic-t-105862.html