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 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 /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 /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]: Memory Usage :
Legend1[memory]: Total Memory :
Legend2[memory]: Used Memory :
LegendI[memory]: Total Memory :
LegendO[memory]: 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]: CPU 使用负载;
LegendI[cpu]: 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 /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
---------------------
作者:yong_sun
来源:CSDN
原文:https://blog.csdn.net/yong_sun/article/details/8440015
版权声明:本文为博主原创文章,转载请附上博文链接!
本文链接:https://www.kinber.cn/post/463.html 转载需授权!
推荐本站淘宝优惠价购买喜欢的宝贝: