MRTG 安装配置

本文档详细介绍了MRTG的安装步骤,包括yum安装依赖包,修改SNMP配置,生成并配置mrtg.cfg文件,设置定时任务以及监控内存、CPU和HTTP的脚本。同时,还解决了页面乱码问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.安装MRTG

yum -y install zlib libpng freetype libjpeg libxml2 gd-devel

yum -y install net-snmp net-snmp-devel net-snmp-utils mrtg

2.配置文件修改
vi /etc/snmp/snmp.conf   //修改snmp配置文件,允许mrtg读取网络接口的流量数据

view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
修改为:
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.2
view systemview included .1.3.6.1.2.1.25.1.1

把下面的#号去掉
#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

/生成mrtg配置文件/etc/mrtg/mrtg.cfg ,其中public@后接的是你的主机名
cfgmaker --global "WorkDir:/var/www/mrtg" --global "Language:chinese" --global "Options[_]: growright,bits" --output /etc/mrtg/mrtg.cfg public@192.168.20.42

编辑 /etc/cron.d/mrtg,使mrtg每隔2分钟自动运行来生成统计信息
vi /etc/cron.d/mrtg
输入:
*/2 * * * * 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

产生网页索引文件
indexmaker /etc/mrtg/mrtg.cfg --output /var/www/mrtg/index.html --title="MRTG监控"

启动mrtg
env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg


修改 /etc/httpd/conf.d/mrtg.conf ,允许任何人访问

Alias /mrtg /var/www/mrtg
<Location /mrtg>
Order deny,allow
# Deny from all
Allow from all
# Allow from .example.com
</Location>


MRTG生成的web页面是静态的,为了能让其不断的刷新,需要将以上命令添加进crontab 。
#crontab -e
添加如下一行
*/2 * * * * env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg
注:此行表示每2分钟刷新一次,可以根据自己的需要修改刷新时间间隔。
若监控多台服务器,需要创建多个不同的mrtg.cfg,并添加进crontab 中。

查看端口的开启状况
# netstat -tunlp |grep snmp

 


新建一个存放脚本的文件夹 /var/www/mrtgsh
===========================内存监控===========================
建立脚本文件 mrtg.memory
#!/bin/bash
# run this script to check the mem usage.
totalmem=`/usr/bin/free |grep Mem |awk '{print $2}'`
usedmem=`/usr/bin/free |grep Mem |awk '{print $3}'`
UPtime=`/usr/bin/uptime | awk '{print $3""$4""$5}'`
echo $totalmem
echo $usedmem
echo $UPtime

===========================CPU监控===========================
使用yum,如何安装iostat
sysstat 使用yum安装
# yum install sysstat

sysstat的安装包是:sysstat-5.0.5-1.i386.rpm,装完了sysstat-5.0.5-1.i386.rpm
后就会有iostat、mpstat、sar、sa的功能,sysstat-5.0.5-1.i386.rpm

启动sysstat
# /etc/init.d/sysstat start

设置sysstat自启动
# /sbin/chkconfig sysstat on

建立脚本文件mrtg.cpu
#!/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
echo localhost


===========================http监控===========================
建立脚本文件mrtg.http
#!/usr/bin/perl
$hostname=`hostname`;
$hostname=~s/\s+$//;
$apache_all_conn=`netstat -an|grep :80|wc -l`;
$apache_conn=`netstat -an|grep :80|grep ESTABLISHED|wc -l`; 
$apache_all_conn=~s/\n$//;
$apache_conn=~s/\n$//;
$apache_all_conn=~s/^\s+|\s+$//;
$apache_conn=~s/^\s+|\s+$//;
$gettime=`uptime|awk '{print \$1" "\$3" "\$4}'`;
$gettime=~s/\,|\n$//g;
print("$apache_all_conn\n");
print("$apache_conn\n");
print("$gettime\n");
print("$hostname\n");


===========================编辑mrtg.cfg===========================

#监控内外网卡
###nterface 2 >> Descr: 'eth0' | Name: 'eth0' | Ip: '117.79.151.152' | Eth: '00-50-56-8B-0B-56' ###
Target[117.79.151.152_2]: 2:public@117.79.151.152:
SetEnv[117.79.151.152_2]: MRTG_INT_IP="117.79.151.152" MRTG_INT_DESCR="eth0"
MaxBytes[117.79.151.152_2]: 12500000
Title[117.79.151.152_2]: Traffic Analysis for 117.79.151.152
PageTop[117.79.151.152_2]: <h1>DB_117.79.151.152</h1>
                <div id="sysdetails">
                        <table>
                                <tr>
                                        <td>System:</td>
                                        <td>test in Right here, right now.</td>
                                </tr>
                                <tr>
                                        <td>Maintainer:</td>
                                        <td>Me</td>
                                </tr>
                                <tr>
                                        <td>Description:</td>
                                        <td>eth0  </td>
                                </tr>
                                <tr>
                                        <td>ifType:</td>
                                        <td>ethernetCsmacd (6)</td>
                                </tr>
                                <tr>
                                        <td>ifName:</td>
                                        <td>eth0</td>
                                </tr>
                                <tr>
                                        <td>Max Speed:</td>
                                        <td>12.5 MBytes/s</td>
                                </tr>
                                <tr>
                                        <td>Ip:</td>
                                        <td>117.79.151.152 ()</td>
                                </tr>
                        </table>
                </div>


### Interface 3 >> Descr: 'eth1' | Name: 'eth1' | Ip: '192.168.32.152' | Eth: '00-50-56-8B-5F-A8' ###
Target[192.168.32.152_3]: 3:public@192.168.32.152:
SetEnv[192.168.32.152_3]: MRTG_INT_IP="192.168.32.152" MRTG_INT_DESCR="eth1"
MaxBytes[192.168.32.152_3]: 125000000
Title[192.168.32.152_3]: Traffic Analysis for 192.168.32.152
PageTop[192.168.32.152_3]: <h1>DB_192.168.32.152</h1>
                <div id="sysdetails">
                        <table>
                                <tr>
                                        <td>System:</td>
                                        <td>test in Right here, right now.</td>
                                </tr>
                                <tr>
                                        <td>Maintainer:</td>
                                        <td>Me</td>
                                </tr>
                                <tr>
                                        <td>Description:</td>
                                        <td>eth1  </td>
                                </tr>
                                <tr>
                                        <td>ifType:</td>
                                        <td>ethernetCsmacd (6)</td>
                                </tr>
                                <tr>
                                        <td>ifName:</td>
                                        <td>eth1</td>
                                </tr>
                                <tr>
                                        <td>Max Speed:</td>
                                        <td>125.0 MBytes/s</td>
                                </tr>
                                <tr>
                                        <td>Ip:</td>
                                        <td>192.168.32.152 (localhost)</td>
                                </tr>
                        </table>
                </div>


#Memory
Target[memory]: `/etc/mrtgsh/mrtg.memory`
MaxBytes[memory]: 4096000
Title[memory]: Memory Usages
ShortLegend[memory]: &
kmg[memory]: kB,MB
kilo[memory]: 1024
YLegend[memory]: &nbsp; Memory Usage :
Legend1[memory]: &nbsp; Total Memory :
Legend2[memory]: &nbsp; Used Memory :
LegendI[memory]: &nbsp; Total Memory :
LegendO[memory]: &nbsp; Used Memory :
Options[memory]: growright,gauge,nopercent
PageTop[memory]: <H1>Memory Usages</H1>


#CPU
Target[cpu]: `/etc/mrtgsh/mrtg.cpu`
MaxBytes[cpu]: 100
Options[cpu]: gauge, nopercent, growright
YLegend[cpu]: CPU loading (%)
ShortLegend[cpu]: %
LegendO[cpu]: &nbsp; CPU 使用负载;
LegendI[cpu]: &nbsp; CPU 系统负载;
Title[cpu]: CPU Usages
PageTop[cpu]: <H1>CPU Usages</H1>


#http
Target[apache_conn]: `/etc/mrtgsh/mrtg.http`
Options[apache_conn]: gauge,nopercent,growright
MaxBytes[apache_conn]: 4000
YLegend[apache_conn]: apache_conn
ShortLegend[apache_conn]:
LegendI[apache_conn]: apache all coonn:
LegendO[apache_conn]: apache now conn
Title[apache_conn]: apache conn
PageTop[apache_conn]:<H1>http conn</H1>

 

重新生成主页面文件
indexmaker /etc/mrtg/mrtg.cfg --output /var/www/mrtg/index.html --title="MRTG Montior"

env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg


打开浏览器时发现文字为乱码
解决方案:
vi /etc/httpd/conf/httpd.conf 
在740行左右(若是不懂显示行号,可以在打开后命令行输入set nu)将
AddDefaultCharset UTF-8 
修改为
AddDefaultCharset GB2312

vi /etc/mrtg/mrtg.cfg 里添加 Language:chinese

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值