安装MRTG检测网络、CPU、内存、Nginx、Apache使用情况

准备工作
# 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


安装iostat
# yum install sysstat


安装 snmp
方法一:编译安装 snmp
# wget http://www.vixual.net/download/source/mrtg/ucd-snmp-4.2.5.tar.gz
# mv ucd-snmp-4.2.5.tar.gz /usr/local/src
# cd /usr/local/src
# tar -zxvpf ucd-snmp-4.2.5.tar.gz
# cd ucd-snmp-4.2.5
# ./configure --prefix=/usr/local/snmp


#過程中會有幾個要注意的地方
-Press return to continue- (這裡按 enter 吧)
System Contact Information (root@):root@tsai.adsldns.org  (這裡輸入你的 e-mail)
System Location (Unknown):CentOS 5.6 (這裡可以隨便輸入,不正確也沒關係)
Location to write logfile (/var/log/snmpd.log): (按 enter 即可)
Location to write persistent information (/var/ucd-snmp): (按 enter 即可)
[root@localhost ucd-snmp-4.2.5]# make 
[root@localhost ucd-snmp-4.2.5]# make install
#啟動 ucd-snmp
[root@localhost ~]# /usr/local/snmp/sbin/snmpd
[root@localhost ~]# chkconfig snmpd on    #可以自動開機時啟動
[root@localhost ~]# vim /etc/snmp/snmpd.conf
#註解下列三行
# 41
#com2sec notConfigUser  default       public
# 47, 48
#group   notConfigGroup v1           notConfigUser
#group   notConfigGroup v2c           notConfigUser


#能查詢的功能,格式為
# 57
#view    <VIEW_NAME>   included   .1.x
view    systemview    included   .1.3.6.1.2.1.2       #設定可以抓取網卡資料
view    all           included   .1                80 #提供所有的 SNMP 資訊


#定義可以抓取資料的來源 建議未避免被其他第三方抓取可以將 public改為其他名稱


# 74, 75
#com2sec <USERNAME> <NETWORK_SOURCE> <COMM_STRING>
com2sec local     localhost       public
com2sec mynetwork 10.40.0.0/16    public 


#設定使用者群組,格式為
#78 79 80 81
#group <GROUPNAME> <SNMP_VERSION> <USERNAME>
group MyRWGroup  v1      local
group MyRWGroup  v2c     local
group MyROGroup  v1      mynetwork
group MyROGroup  v2c     mynetwork


#存取權限設定,讓 local 可讀可寫,讓 mynetwork 只能讀


#94 95
#access <GROUPNAME> <CONTEXT> <SEC_MODEL> <SEC_LEVEL> <PREFIX> <READ> <WRITE> <NOTIF>
access MyROGroup ""      any        noauth      exact     all    none   none    #network只能讀
access MyRWGroup ""      any        noauth      exact     all    all    all     #local可讀可寫


測試SNMP是否正常


[root@localhost ~]# snmpd -f –Le #查看當前的SNMP有沒有出什麼錯
[root@localhost ~]#/etc/init.d/snmpd start #啟動SNMP
[root@localhost ~]# snmpwalk -c public -v 2c localhost #可測試看SNMP是否正常


方法二:yum安装snmp


1.请确保你的系统安装了以下软件包
net-snmp-5.0.6-17
net-snmp-devel-5.0.6-17
net-snmp-utils-5.0.6-17


# yum -y install net-snmp*


2.修改/etc/snmp/snmpd.conf

找到如下行:

# sec.name source community
com2sec notConfigUser default public

把public 改为其他名字,为了防止黑客的攻击,我改的是ferdinand

去掉如下一行的注释
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生成供浏览图像需要httpd服务的支持,同时也需要gd、libpng和zlib三个软件包的支持,而gd的正常运行也需要其它的几个软件,下面一并安装它们


1.安装zlib-1.2.3
#tar zxvf zlib-1.2.3.tar.gz
#cd zlib-1.2.3
#./configure --prefix=/usr/local/zlib
#make
#make install


2.安装httpd-2.2.4
#tar zxvf httpd-2.2.4.tar.gz
#cd httpd-2.2.4
#./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-track-vars --enable-rewrite --with-z-dir=/usr/local/zlib 
#make 
#make install
启动httpd
/usr/local/apache/bin/apachectl -k start


3.安装libpng-1.2.14 
#tar zxvf libpng-1.2.14.tar.gz   
# cd libpng-1.2.14 
# cp scripts/makefile.linux makefile 
# make (若是提示找不到zlib库文件或者头文件,多半是makefile文件里zlib的默认路径有误。可编辑makefile文件,找到zlib项并重新指定路径到/usr/local/zlib/lib和/usr/local/zlib/include)。
# make install


4.安装freetype-2.1.10 
# tar -zvxf freetype-2.1.10.tar.gz 
# cd freetype-2.1.10
# mkdir -p /usr/local/freetype 
# ./configure --prefix=/usr/local/freetype 
# make;make install  


5.安装jpegsrc.v6b
jpeg默认不会自建目录,因此需手动建立目录: 
# mkdir -pv /usr/local/jpeg6/{,bin,lib,include,man/{,man1},man1}
安装
#tar zxvf jpegsrc.v6b.tar.gz    
# ./configure --prefix=/usr/local/jpeg6/ --enable-shared --enable-static 
# make
# make install  
# make install-lib


6.安装libxml2-2.6.19
# tar -zxf libxml2-2.6.19.tar.gz
# cd libxml2-2.6.19
# mkdir -p /usr/local/libxml2
# ./configure --prefix=/usr/local/libxml2
# make; make install
#cp xml2-config /usr/bin


7.安装GD-2.0.33库 
# tar -zvxf gd-2.0.33.tar.gz 
# mkdir -p /usr/local/gd2 
# cd gd-2.0.33 
# ./configure --prefix=/usr/local/gd2 --with-jpeg=/usr/local/jpeg6/ --with-png=/usr/local/lib/ --with-zlib=/usr/local/zlib/ --with-freetype=/usr/local/freetype/ 
# make
# make install 


三、安装配置MRTG


1.下载mrtg,目前最新版本为mrtg-2.17.4
# 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 public@localhost > /etc/mrtg.cfg


编辑/etc/mrtg.cfg

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


去掉如下行的注释
# Options[_]: growright, bits


添加如下行,实现网页中的中文字符显示
Language:gb2312


生成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
# crontab -e 
添加如下一行
*/3 * * * * 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使用监视

使用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


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

3.监控http状态
然后把以下perl代码创建为 mrtg.http 存于 mrtg 根目录中并赐予可执行权限(chmod +x mrtg.http)

# vim /home/htdocs/monitor/mrtg/sh/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");

# chmod +x /home/htdocs/monitor/mrtg/sh/mrtg.http


4.监控 Nginx 状态
# vim /home/htdocs/monitor/mrtg/sh/nginx_status
# chmod +x /home/htdocs/monitor/mrtg/sh/nginx_status
# /home/htdocs/monitor/mrtg/sh/nginx_status


curl http://www.bobaow.com/status |grep Active |awk '{print $3 }' > /home/htdocs/monitor/mrtg/ngx.active
curl http://www.bobaow.com/status |grep Waiting |awk '{print $6 }' > /home/htdocs/monitor/mrtg/ngx.waiting


MRTG Nginx
# vim /home/htdocs/monitor/mrtg/sh/mrtg.nginx
# chmod +x /home/htdocs/monitor/mrtg/sh/mrtg.nginx
# /home/htdocs/monitor/mrtg/sh/mrtg.nginx


#!/usr/bin/perl -w
`/home/htdocs/monitor/mrtg/sh/nginx_status`; #调用刚才的shell脚本从 NginxStatus 中获取数值到文本。
$hostname=`hostname`;
$hostname=~s/\s+$//;
$apache_all_conn=`tail /home/htdocs/monitor/mrtg/ngx.active`; #数值文本路径按照不同配置自行修改
$apache_conn=`tail /home/htdocs/monitor/mrtg/ngx.waiting`; #数值文本路径按照不同配置自行修改
$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配置文件
vim /etc/mrtg.cfg


Options[net]: growright, bits
Target[net]: 2:public@localhost:
SetEnv[net]: MRTG_INT_IP="127.0.0.1" MRTG_INT_DESCR="eth0"
MaxBytes[net]: 12500000
Title[net]: Traffic Analysis
PageTop[net]: <h1>Traffic Analysis : bobaow-com-11</h1>
  <div id="sysdetails">
    <table>
      <tr>
        <td>System:</td>
        <td>bobaow-com-11 in Unknown (edit /etc/snmp/snmpd.conf)</td>
      </tr>
      <tr>
        <td>Maintainer:</td>
        <td>Root &lt;root@localhost&gt; (configure /etc/snmp/snmp.local.conf)</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>127.0.0.1 (No DNS name)</td>
      </tr>
    </table>
  </div>


#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]: &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]: `/home/htdocs/monitor/mrtg/sh/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]: `/home/htdocs/monitor/mrtg/sh/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>


#nginx
Target[apache_conn]: `/home/htdocs/monitor/mrtg/sh/mrtg.nginx`
Options[apache_conn]: gauge,nopercent,growright
Directory[apache_conn]: nginx_conn
MaxBytes[apache_conn]: 4000
YLegend[apache_conn]: nginx_conn
ShortLegend[apache_conn]:
LegendI[apache_conn]: Active connections:
LegendO[apache_conn]: Waiting:
Title[apache_conn]: Nginx

PageTop[apache_conn]:<H1>Nginx Usages</H1>



以上代码就是一个负责获取系统中80端口访问数的perl小软件,加入后MRTG就会为其在HTML目录底下生成一个 apache_conn 的目录以及html跟统计图表供浏览器访问。


纪念一下:





然后把以下perl代码创建为 mrtg.http 存于 mrtg 根目录中并赐予可执行权限(chmod +x mrtg.http)

# vim /home/htdocs/monitor/mrtg/sh/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");


# chmod +x /home/htdocs/monitor/mrtg/sh/mrtg.http

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值