Linux安装MRTG检测CPU、网络、内存的使用情况

准备工作
# yum -y install gd-devel
# yum -y install libmcrypt
# yum -y install libmcrypt-devel
# yum -y install freetype
# yum -y install freetype-devel
# yum -y install libtool-ltdl
# yum -y install libtool-ltdl-devel
# yum -y install libjpeg-devel
# yum -y install libpng-devel
# yum -y install libmcrypt
# yum -y install libmcrypt-devel
# yum -y install zlib-1.2.3


一、yum安装snmp

1.

# yum -y install net-snmp*
2.修改/etc/snmp/snmpd.conf

# vim /etc/snmp/snmpd.conf
找到如下行:
# sec.name source community 
com2sec notConfigUser default public
把public 改为其他名字,为了防止黑客的攻击,我改的是jdeyes

去掉如下一行的注释

#view mib2   included  .iso.org.dod.internet.mgmt.mib-2 fc
在第55行处添加如下一行
view    systemview    included   .1.3.6.1.2.1.2
把如下行
access  notConfigGroup ""      any       noauth    exact  systemview none none
改作:
access  notConfigGroup ""      any       noauth    exact  mib2 none none
3.启用snmpd服务
# service snmpd start
# chkconfig snmpd on
4.查看端口的开启状况

# netstat -tunlp |grep snmp
tcp        0      0 0.0.0.0:199             0.0.0.0:*               LISTEN      4973/snmpd          
udp        0      0 0.0.0.0:161             0.0.0.0:*                           4973/snmpd    

二、安装配置MRTG

1.下载mrtg

# wget http://oss.oetiker.ch/mrtg/pub/mrtg-2.17.4.tar.gz
2.安装mrtg-2.17.4
# tar zxvf mrtg-2.17.4.tar.gz
# cd mrtg-2.17.4 
# ./configure --prefix=/usr/local/mrtg --sysconfdir=/etc/mrtg --with-gd=/usr/local/gd2/include --with-gd-lib=/usr/local/gd2/lib --with-gd-inc=/usr/local/gd2/include --with-png=/usr/local/include --with-png-lib=/usr/local/lib --with-png-inc=/usr/local/include --with-zlib=/usr/local/zlib/include --with-zlib-lib=/usr/local/zlib/include --with-zlib-inc=/usr/local/zlib/include
# make
# make install
3.基本配置

生成主配置文件

# /usr/local/mrtg/bin/cfgmaker jdeyes@localhost > /etc/mrtg.cfg
编辑/etc/mrtg.cfg
# vim /etc/mrtg.cfg

# WorkDir:/home/http/mrtg
去掉注释并改为
WorkDir: /home/htdocs/monitor/mrtg   (此处是你的httpd默认的主目录)

去掉如下行的注释

# Options[_]: growright, bits
生成MRTG网页主页面文件
# /usr/local/mrtg/bin/indexmaker /etc/mrtg.cfg --output=/home/htdocs/monitor/mrtg/index.html --title="MRTG"
4.启动MRTG
# env LANG=C /usr/local/mrtg/bin/mrtg /etc/mrtg.cfg
这个命令会输出一些错误信息,一般可以安全忽略,连续执行三次此命令即可。

5.MRTG生成的web页面是静态的,为了能让其不断的刷新,需要将以上命令添加进crontab

# vim /etc/crontab 
添加如下一行
*/5 * * * * root env LANG=C /usr/local/mrtg/bin/mrtg /etc/mrtg.cfg
注:此行表示每三分钟刷新一次,你可以根据自己的需要修改刷新时间间隔。

重启SNMP服务:

# sudo /etc/init.d/snmpd restart
可以通过命令:
# snmpwalk -v 2c -c ferdinand localhost system
来检测是否安装成功

三、一个示例:内存使用监视

1.新建一个存放脚本的文件夹

# mkdir -pv /home/htdocs/monitor/mrtg/sh
2.建立脚本文件
# vim /home/htdocs/monitor/mrtg/sh/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
echo localhost
3.让其具有运行权限
# chmod +x /home/htdocs/monitor/mrtg/sh/mrtg.memory
四、一个示例:CPU使用监视

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
1.建立脚本文件
# vim /home/htdocs/monitor/mrtg/sh/mrtg.cpu
添加如下脚本:
#!/bin/bash 
# 这个程式是用来侦测 CPU 的小外挂程式! 
# 1. 开始使用 sar 来监测 CPU 的 user 及 System 负载率 
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
2.让其具有运行权限
# chmod +x /home/htdocs/monitor/mrtg/sh/mrtg.cpu
编辑mrtg配置文件
# vim /etc/mrtg.cfg
在其最后添加
#Memory
Target[memory]: `/home/htdocs/monitor/mrtg/sh/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]: `/home/htdocs/monitor/mrtg/sh/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>
到此,MRTG已经安装配置完成,最后只需要在nginx或者apache上面配置一个访问地址就行了!
最后效果如下:







发布了47 篇原创文章 · 获赞 16 · 访问量 18万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章