Ubuntu 10.04
安装snmpd(server端) 和 snmp(工具如snmpwalk)
编辑 vim /etc/default/snmpd
SNMPDOPTS='-Lsd -Lf /dev/null -u snmp -g snmp -I -smux -p /var/run/snmpd.pid 127.0.0.1'
改成
SNMPDOPTS='-Lsd -Lf /dev/null -u snmp -g snmp -I -smux -p /var/run/snmpd.pid'
这样就可以提供给外部接口调用,Ubuntu默认把snmp服务只对内服务
编辑vim /etc/snmp/snmpd.conf(最好先备份好原来的snmpd.conf文件)
把默认的项全部注释掉(按照它默认的不知道为什么搞不成功)
#1
com2sec notConfigUser default public
#2
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
#3
view systemview included .1.3.6.1.2.1.1
view systemview included .1.3.6.1.2.1.25.1.1
view all included .1
#4
access notConfigGroup "" any noauth exact all none none
#下面这两个可以保留没影响
syslocation Unknown(mj) (configure /etc/snmp/snmpd.local.conf)
syscontact Root(mj) <root@localhost> (configure /etc/snmp/snmpd.local.conf)
/etc/init.d/snmpd restart 重启snmp服务
因为我们只关系cpu,内存等负载,所以不需要我们自己写脚本,snmp本身已经有这些功能,看后面常用OID
snmpwalk -v 1 -c public IP OID
例子:
snmpwalk -v 1 -c public 192.168.22.210 .1.3.6.1.4.1.2021.4.6.0
安装mrtg
创建/var/www/mrtg网页目录
编辑vim /etc/mrtg.cfg
mrtg.cfg可以使用cfgmaker生成
cfgmaker --global "WorkDir: /var/www/mrtg"
--global "Options[_]: growright,bits"
--ifref=ip
--output /etc/mrtg.cfg
[email protected]
cfgmaker --global "WorkDir: /var/www/mrtg"
--global "Options[_]: growright,bits"
--ifref=ip
[email protected]
我们把--output /etc/mrtg.cfg这个选项去掉,把结果直接输出到屏幕,如下
**************************************************************
# Created by
# /usr/bin/cfgmaker --ifref=ip [email protected]
### Global Config Options
# for UNIX
# WorkDir: /home/http/mrtg
# for Debian
WorkDir: /var/www/mrtg
# or for NT
# WorkDir: c:/mrtgdata
### Global Defaults
# to get bits instead of bytes and graphs growing to the right
# Options[_]: growright, bits
EnableIPv6: no
######################################################################
# System: mj-desktop
# Description: Linux mj-desktop 2.6.32-22-generic #36-Ubuntu SMP Thu Jun 3 22:02:19 UTC 2010 i686
# Contact: Root(mj) <root@localhost> (configure /etc/snmp/snmpd.local.conf)
# Location: Unknown(mj) (configure /etc/snmp/snmpd.local.conf)
######################################################################
### Interface 1 >> Descr: 'lo' | Name: 'lo' | Ip: '127.0.0.1' | Eth: '' ###
### The following interface is commented out because:
### * it is a Software Loopback interface
#
# Target[192.168.22.210_127.0.0.1]: /127.0.0.1:[email protected]:
# SetEnv[192.168.22.210_127.0.0.1]: MRTG_INT_IP="127.0.0.1" MRTG_INT_DESCR="lo"
# MaxBytes[192.168.22.210_127.0.0.1]: 1250000
# Title[192.168.22.210_127.0.0.1]: Traffic Analysis for 127.0.0.1 -- mj-desktop
# PageTop[192.168.22.210_127.0.0.1]: <h1>Traffic Analysis for 127.0.0.1 -- mj-desktop</h1>
# <div id="sysdetails">
# <table>
# <tr>
# <td>System:</td>
# <td>mj-desktop in Unknown(mj) (configure /etc/snmp/snmpd.local.conf)</td>
# </tr>
# <tr>
# <td>Maintainer:</td>
# <td>Root(mj) <root@localhost> (configure /etc/snmp/snmpd.local.conf)</td>
# </tr>
# <tr>
# <td>Description:</td>
# <td>lo </td>
# </tr>
# <tr>
# <td>ifType:</td>
# <td>softwareLoopback (24)</td>
# </tr>
# <tr>
# <td>ifName:</td>
# <td>lo</td>
# </tr>
# <tr>
# <td>Max Speed:</td>
# <td>1250.0 kBytes/s</td>
# </tr>
# <tr>
# <td>Ip:</td>
# <td>127.0.0.1 (localhost)</td>
# </tr>
# </table>
# </div>
### Interface 2 >> Descr: 'eth0' | Name: 'eth0' | Ip: '192.168.22.210' | Eth: '08-00-27-f9-de-39' ###
Target[192.168.22.210_192.168.22.210]: /192.168.22.210:[email protected]:
SetEnv[192.168.22.210_192.168.22.210]: MRTG_INT_IP="192.168.22.210" MRTG_INT_DESCR="eth0"
MaxBytes[192.168.22.210_192.168.22.210]: 1250000
Title[192.168.22.210_192.168.22.210]: Traffic Analysis for 192.168.22.210 -- mj-desktop
PageTop[192.168.22.210_192.168.22.210]: <h1>Traffic Analysis for 192.168.22.210 -- mj-desktop</h1>
<div id="sysdetails">
<table>
<tr>
<td>System:</td>
<td>mj-desktop in Unknown(mj) (configure /etc/snmp/snmpd.local.conf)</td>
</tr>
<tr>
<td>Maintainer:</td>
<td>Root(mj) <root@localhost> (configure /etc/snmp/snmpd.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>1250.0 kBytes/s</td>
</tr>
<tr>
<td>Ip:</td>
<td>192.168.22.210 (mj-desktop.local)</td>
</tr>
</table>
</div>
**************************************************************
上面产生的是针对192.168.22.210一台机的模板,我们把它保存成/etc/mrtg.conf,以此为模板慢慢建立起来
<div id="sysdetails">这些网页代码前面要有tab,最好按照输出copy进去不要修改
#Options[_]: growright, bits 把注释去掉
默认是只监控目标机的eth0网卡,loopback本地网卡监控是注释掉的,这个不用修改
执行生成页面
indexmaker -output=/var/www/mrtg/index.html /etc/mrtg.cfg
env LANG=C /usr/bin/mrtg /etc/mrtg.cfg
可以把env LANG=C /usr/bin/mrtg /etc/mrtg.cfg加入到crontab里面定时执行(每个5分钟执行一次)
*/5 * * * * env LANG=C /usr/bin/mrtg /etc/mrtg.cfg
最后访问http://localhost/mrtg/
上面只是针对一台目标机器,我们可以继续上面的cfgmaker来产生再多一台目标机器,把结果输出到屏幕,然后在把对应的项添加到原有的mrtg.conf文件里面
我们在监控网络流量的基础上添加其它应用
(自己写脚本,数据不准确,仅供学习)
#建立一个内存使用情况
/home/mj/mem.sh
#!/bin/sh
total=`snmpwalk -v 1 -c public 192.168.22.210 .1.3.6.1.4.1.2021.4.5.0 | awk '{printf("%d/n",$4/1024);}'`
free=`snmpwalk -v 1 -c public 192.168.22.210 .1.3.6.1.4.1.2021.4.6.0 | awk '{printf("%d/n",$4/1024);}'`
echo $(($total-$free))
echo 0
Target[mem]: ` /home/mj/mem.sh `
MaxBytes[mem]: 1024
Title[mem]: Memory State of WY1 IP 192.168.22.210 Server
PageTop[mem]: <H1>Memory State of WY1 IP 192.168.22.210 Server</H1>
ShortLegend[mem]: MB
kmg[mem]: MB
kilo[mem]: 1024
YLegend[mem]: Memory Usage
Options[mem]: growright,gauge,nopercent
#建立一个cpu使用情况
Target[cpu]: `snmpwalk -v 1 -c public 192.168.22.210 .1.3.6.1.4.1.2021.11.11.0 | awk '{printf("%d/n%d/n",100-$4,0);}'`
MaxBytes[cpu]: 100
Title[cpu]: CPU State of WY1 IP 192.168.22.210 Server
PageTop[cpu]:<H1>CPU State of WY1 IP 192.168.13.103 Server</H1>
kmg[cpu]: %
YLegend[cpu]: CPU Usage
Options[cpu]: growright,gauge,nopercent
这里有个地方要注意是,就是target里面的值必须是两行的,可以加上一个print 0上去,否则执行
env LANG=C /usr/bin/mrtg /etc/mrtg.cfg会返回
Expected a Number for 'out' but nothing'错误
原因是
Your script must return at least two values on separate lines, just add
'echo "0"' or something to the end of the script.
http://oss.oetiker.ch/mrtg/nable-mrtg.en.html#nabble-td831392
(下面是mrtg提供的sample)
#cpu
LoadMIBs: /usr/share/snmp/mibs/UCD-SNMP-MIB.txt
Target[185cpu]:ssCpuRawUser.0&ssCpuRawUser.0:[email protected] + ssCpuRawSystem.0&ssCpuRawSystem.0:[email protected] + ssCpuRawNice.0&ssCpuRawNice.0:[email protected]
RouterUptime[185cpu]: [email protected]
MaxBytes[185cpu]: 100
Title[185cpu]: CPU Load
PageTop[185cpu]: <H1>Active CPU Load %</H1>
Unscaled[185cpu]: ymwd
ShortLegend[185cpu]: %
YLegend[185cpu]: CPU Utilization
Legend1[185cpu]: Active CPU in % (Load)
Legend2[185cpu]:
Legend3[185cpu]:
Legend4[185cpu]:
LegendI[185cpu]: Active
LegendO[185cpu]:
Options[185cpu]: growright,nopercent
target是各个值加起来,要把它写成一行
#mem
LoadMIBs: /usr/share/snmp/mibs/HOST-RESOURCES-MIB.txt
Target[185mem]:(memTotalReal.0&memTotalReal.0:[email protected] - memAvailReal.0&memAvailReal.0:[email protected])*100/(memTotalReal.0&memTotalReal.0:[email protected])
Title[185mem]: Percentage Used Memory
PageTop[185mem]: <H1>Percentage Used Memory</H1>
options[185mem]: growright,gauge,transparent,nopercent
Unscaled[185mem]: ymwd
MaxBytes[185mem]: 100
YLegend[185mem]: Memory %
ShortLegend[185mem]: Percent
LegendI[185mem]: Used
LegendO[185mem]: Used
Legend1[185mem]: Percentage Used Memory
Legend2[185mem]: Percentage Used Memory
最好上网查找mrtg的现成例子,如net,cpu,mem负载,tcp连接数等等,还有就是记得target里面的空格之类的多余符号可能会导致生成页面失败