# 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 none3.启用snmpd服务
# service snmpd start # chkconfig snmpd on4.查看端口的开启状况
# 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
1.下载mrtg
# wget http://oss.oetiker.ch/mrtg/pub/mrtg-2.17.4.tar.gz2.安装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 install3.基本配置
生成主配置文件
# /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/sh2.建立脚本文件
# 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 localhost3.让其具有运行权限
# chmod +x /home/htdocs/monitor/mrtg/sh/mrtg.memory四、一个示例:CPU使用监视
sysstat 使用yum安装
# yum install sysstatsysstat的安装包是:sysstat-5.0.5-1.i386.rpm,装完了sysstat-5.0.5-1.i386.rpm
启动sysstat
# /etc/init.d/sysstat start设置sysstat自启动
# /sbin/chkconfig sysstat on1.建立脚本文件
# 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 localhost2.让其具有运行权限
# 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上面配置一个访问地址就行了!