RHEL6(CENTOS)配置MRTG服务

安装snmpd和mrtg

#yum install mrtg net-snmp net-snmp-utils


配置SNMP,配置完成后重启snmpd (因为网络接口转而采用了脚本采集,所以snmp就不配置了)
[color=gray]
com2sec notConfigUser localhost public
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
access notConfigGroup "" any noauth exact all all none
view all included .1 80
syslocation Unknown (edit /etc/snmp/snmpd.conf)
syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
[/color]

因为功能需求,我们再编写三个插件脚本,分别监视eth0、cpu和内存。
监视eth接口脚本内容

#!/bin/bash
# (c) 2002 Denis Kolisnichenko
# Usage: /usr/bin/count iface
/bin/grep "$1" /proc/net/dev | /bin/awk -F ":" '{ print $2 }' | /bin/awk '{ print $1 "\n" $9 }'
UPTIME=`/usr/bin/uptime | /bin/awk -F " " '{ print $3 }'`
echo $UPTIME
echo $1

监视CPU脚本内容

#!/usr/bin/perl
system ("/usr/bin/sar -u 1 3|grep Average >cpu_info_file");
open (CPUINFO,"cpu_info_file");
@cpuinfo=<CPUINFO>;
close (CPUINFO);
foreach $line(@cpuinfo) {
@cpustatus=split(/ +/,$line);
}
$cpuused=$cpustatus[2]+$cpustatus[4];
$cpuidle=$cpustatus[5];
print "$cpuused\n";
print "$cpuidle";
system ("uptime");
system ("uname -n");

监视MEM脚本内容

#!/usr/bin/perl
system ("/usr/bin/free -m | grep Mem >mem_info_file");
open (MEMINFO,"mem_info_file");
@meminfo=<MEMINFO>;
close (MEMINFO);
foreach $line(@meminfo) {
@memstatus=split(/ +/,$line);
}
$memused=$memstatus[2];
$memtotal=$memstatus[1];
print "$memused\n";
print "$memtotal\n";
system ("uptime");
system ("uname -n");


配置/etc/mrtg/mrtg.cfg

EnableIPv6: no
WorkDir: /var/www/mrtg/
# eth0 monitor
Target[eth0]: `/etc/mrtg/bin/count.sh eth0`
Options[eth0]: nopercent,growright,bits
Title[eth0]: Server eth0 Traffic
PageTop[eth0]: <h1>Eth0 - interface</h1>
MaxBytes[eth0]: 99999999
kilo[eth0]: 1024
YLegend[eth0]: bits per second
ShortLegend[eth0]: bit/s
LegendO[eth0]:   out:
LegendI[eth0]:   in :
# CPU monitor
Target[localhost_cpu]: `/etc/mrtg/bin/mrtg.cpu`
Ytics[localhost_cpu]: 10
YticsFactor[localhost_cpu]: 10
MaxBytes[localhost_cpu]:100
Title[localhost_cpu]:CPU State
PageTop[localhost_cpu]:<H1>Server CPU </H1>
ShortLegend[localhost_cpu]: %
YLegend[localhost_cpu]: CPU (%)
Legend1[localhost_cpu]: Used
Legend2[localhost_cpu]: Total
LegendI[localhost_cpu]: CPU Used
LegendO[localhost_cpu]: CPU IDEL
Options[localhost_cpu]: growright,gauge,nopercent
# Mem monitor
Target[localhost_mem]: `/etc/mrtg/bin/mrtg.mem`
Ytics[localhost_mem]:5
YticsFactor[localhost_mem]: 1
MaxBytes[localhost_mem]: 1000
Title[localhost_mem]:Memory State of TestServer
PageTop[localhost_mem]:<H1>Server Mem </H1>
ShortLegend[localhost_mem]: B
kmg[localhost_mem]: M
YLegend[localhost_mem]: Memory Usage
Legend1[localhost_mem]: Used
Legend2[localhost_mem]: Total
LegendI[localhost_mem]: Used
LegendO[localhost_mem]: Total
Options[localhost_mem]: growright,gauge,nopercent


生成index.html
[code]
#indexmaker --output=/var/www/mrtg/index.html /etc/mrtg/mrtg.cfg
[/code]

添加crontab计划

#cat /etc/cron.d/mrtg

*/5 * * * * root LANG=C LC_ALL=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --lock-file /var/lock/mrtg/mrtg_l --confcache-file /var/lib/mrtg/mrtg.ok


注:可以先运行一遍该命令,立即生成数据

/usr/bin/mrtg /etc/mrtg/mrtg.cfg --lock-file /var/lock/mrtg/mrtg_l --confcache-file /var/lib/mrtg/mrtg.ok


修改httpd的配置中mrtg的网址Allow from 部分

访问MRTG

http://youripaddres/mrtg/



[quote]
参考资料:
http://pm4u.narod.ru/en/mrtg.htm
http://www.php-oa.com/2009/11/01/centos-rhel-mrtg.html
http://oss.oetiker.ch/mrtg/doc/mrtg-reference.en.html
http://hi.baidu.com/wangli19880718/blog/item/b0cd9c514c47dd190cf3e3df.html
[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值