一、原理介绍:
1. SNMP简介
目前网络中用得最广泛的网络管理协议是SNMP(Simple Network Management Protocol,简单网络管理协议)。SNMP是被广泛接受并投入使用的工业标准,用于保证管理信息在网络中任意两点间传送,便于网络管理员在网络上的任何节点检索信息、修改信息、定位故障、完成故障诊断、进行容量规划和生成报告。
SNMP采用轮询机制,只提供最基本的功能集,特别适合在小型、快速和低价格的环境中使用。SNMP的实现基于无连接的传输层协议UDP,因此可以实现和众多产品的无障碍连接。
2. SNMP的工作机制
SNMP分为NMS和Agent两部分:
NMS(Network Management Station,网络管理站)是运行客户端程序的工作站,目前常用的网管平台有QuidView、Sun NetManager和IBM NetView。
Agent是运行在网络设备上的服务器端软件。
NMS可以向Agent发出GetRequest、GetNextRequest和SetRequest报文,Agent接收到NMS的这些请求报文后,根据报文类型进行Read或Write操作,生成Response报文,并将报文返回给NMS。
Agent在设备发生异常情况或状态改变时(如设备重新启动),也会主动向NMS发送Trap报文,向NMS汇报所发生的事件。
3. SNMP的版本
目前,设备中的SNMP Agent支持SNMP v3版本,兼容SNMP v1版本、SNMP v2C版本。
SNMP v3采用用户名和密码认证方式。
SNMP v1、SNMP v2C采用团体名(Community Name)认证,非设备认可团体名的SNMP报文将被丢弃。SNMP团体名用来定义SNMP NMS和SNMP Agent的关系。团体名起到了类似于密码的作用,可以限制SNMP NMS访问设备上的SNMP Agent。用户可以选择指定以下一个或者多个与团体名相关的特性:
1.定义团体名可以访问的MIB视图。
2.设置团体名对MIB对象的访问权限为读写权限(write)或者只读权限(read)。具有只读权限的团体名只能对设备信息进行查询,而具有读写权限的团体名还可以对设备进行配置。
3.设置团体名指定的基本访问控制列表。
4. 设备支持的MIB
在SNMP报文中用管理变量来描述设备中的管理对象。为了唯一标识设备中的管理对象,SNMP用层次结构命名方案来识别管理对象。整个层次结构就像一棵树,树的节点表示管理对象,如下图1-1所示。每一个节点,都可以用从根开始的一条路径唯一地标识。
MIB(Management Information Base,管理信息库)的作用就是用来描述树的层次结构,它是所监控网络设备的标准变量定义的集合。在图1-1中,管理对象B可以用一串数字{1.2.1.1}唯一确定,这串数字是管理对象的对象标识符(Object Identifier)。
系统支持的常见MIB如下表1-1所示。
二、RRDtool安装,分析及使用:
工作原理图:
整个系统有四部分组成:
1、Cacti系统平台用于用户设置与查看图表;Cacti需要LAMP环境的支持。
2、snmp用于采集数据给rrdtool绘制成图,使用161.162端口,用MIB数据库来保持数据信息,根据OID定位数据(类似1.3.6.1.2.1);Linux下使用net-snmp自带的snmpget和snmpwalk等程序,Windows下使用php的snmp功能。
3、RRDTool绘图引擎用于性能数据的存储和绘画图像。
4、Mysql数据库用来存储RRDTool绘图所需要的信息。
Cacti常见的监测对象:
1、服务器资源:CPU、内存、磁盘、进程、连接数等
2、服务器类型:WEB、Mail、FTP、数据库
3、网络接口:流量、转发速度、丢包率
Cacti偏重于网络流量,系统负载方面的监控(网络流量,cpu,硬盘使用率等)
1、安装cacti基于lamp环境(apache+mysql+php+net-snmp+rrdtool)
1
|
|
1
2
3
4
5
|
service httpd start
service mysqld start
chkconfig httpd on
chkconfig mysqld on
mysqladmin -u root password root
#设置mysql登录密码
|
2、安装配置cacti
1)、安装cacti
1
2
3
|
wget http:
//www
.cacti.net
/downloads/cacti-0
.8.8b.
tar
.gz
tar
zxvf cacti-0.8.8b.
tar
.gz
mv
cacti-0.8.8b/*
/var/www/html
|
2)、导入数据库结构
1
2
3
|
mysql>create database cacti;
mysql>use cacti;
mysql>
source
/var/www/html/cacti
.sql;
|
3)、配置cacti连接数据库:
1
2
3
4
5
6
7
8
9
|
vi
/var/www/html/include/config
.php
$database_type =
"mysql"
;
$database_default =
"cactidb"
;
$database_hostname =
"localhost"
;
$database_username =
"root"
;
$database_password =
"root"
;
$database_port =
"3306"
;
$database_ssl =
false
;
$url_path =
"/"
;
#去掉注释,修改路径为根
|
4)、图形安装cacti,输入IP开始安装
默认用户、密码都是admin,输入后会让你修改密码
5)、配置snmp监控本地主机
1
2
3
4
5
|
vi
/etc/snmp/snmpd
.conf
com2sec notConfigUser 127.0.0.1 public
access notConfigGroup
""
any noauth exact all none none
view all included .1 80
service snmpd restart
|
3、cacti其他配置
1)、php设置时区
1
2
3
|
vi
/etc/php
.ini
date
.timezone = Asia
/Shanghai
service httpd restart
|
2)、手动运行一次,生成初始图像文件: (编绎安装路径/usr/local/apache/htdocs/)
1
|
/usr/bin/php
/var/www/html/poller
.php
|
3)、设置计划任务,每五分钟生成一次图表:
1
2
|
crontab
-e
*
/5
* * * *
/usr/bin/php
/var/www/html/poller
.php
|
4)、修改cacti默认设置:console->setting
rrdtool -v #查看rrdtool版本
snmp version :设置snmp版本,一般是version2
snmp community:设置共同体名,默认是public
5)、设置权限:
1
2
3
4
|
chown
-R apache.root
/var/www/html/
chmod
775 -R
/var/www/html
chmod
777
/var/www/html/rra
/var/www/html/log
chown
-R root.root
/var/www/html/rra
/var/www/html/log
|
6)、监控本地主机
解决:rrdtool fetch -r 300 localhost_mem_buffers_3.rrd AVERAGE #使用rrdtool获取抓取的信息,结果都是-nan,现在看最后一个日期是1379679000,当date +%s获取的日期超过最后一个,就该出图了。
4、监控Linux主机
被监控主机配置SNMP:
1
2
3
4
5
6
|
yum
install
-y net-snmp-*
vi
/etc/snmp/snmpd
.conf
com2sec notConfigUser 192.168.0.10 public
#改为允许监控主机的IP
access notConfigGroup
""
any noauth exact all none none
view all included .1 80
service snmpd restart
#重启snmp
|
监控主机添加设备:
Console->Device->Add->create
添加到图形树:
Console->Graph Trees->Add->Tree Item Type->Host->选择主机->create
5、监控Windows主机
首先主机已经添加snmp服务了,再安装snmp-informant-std会在服务里面多出SNMP Trap,不需要配置,然后配置Snmp Service:
开始—运行—services.msc,找到“snmp service”,右键打开“属性”,选择“安全”,在“接受团体名称”处,点“添加”,在“团体名称”处写入你的cacti使用的community,选中“接受来自这些主机的snmp数据包”,默认值为“localhost”,点击“编辑”,将“localhost”改为cacti监控服务器的实际 ip地址即可!
6、不出图或不出数据以下入手解决
1.图像没有生成,运行 #/usr/bin/php /var/www/html/poller.php
2.权限问题,修改权限 #chmod 777 -R /var/www/html/rra /var/www/html/log/
3.snmp配置问题,检测是否有数据输出 #snmpwalk -v2c -c public 127.0.0.1
4.删除rra下所有文件,清除缓存 #system utilities –>Rebuild poller cache
5.系统时间不准确,重新设置时间 date.timezone = PRC
6.数据库表有损坏,修复 #mysqlcheck -ao cacti -auto-repair -uroot -p
7.不显示图片也不显示数据 #service snmpd restart
7.尝试重启snmpd服务 #service snmpd restart
到此安装完毕!!!
7、相关详细配制1 --- 以下为通过RRDtool命令查看数据
[root@localhost ~]# yum install net-snmp
[root@localhost ~]# yum install net-snmp-utils
[root@localhost ~]# snmpwalk -v 2c -c public localhost host
HOST-RESOURCES-MIB::hrSystemUptime.0 = Timeticks: (1618864) 4:29:48.64
HOST-RESOURCES-MIB::hrSystemUptime.0 = No more variables left in this MIB View (It is past the end of the MIB tree)
[root@localhost ~]# snmpwalk -v 2c -c public localhost
SNMPv2-MIB::sysDescr.0 = STRING: Linux localhost.localdomain 2.6.18-164.el5PAE #1 SMP Tue Aug 18 15:59:11 EDT 2009 i686
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (745096) 2:04:10.96
SNMPv2-MIB::sysContact.0 = STRING: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
SNMPv2-MIB::sysName.0 = STRING: localhost.localdomain
SNMPv2-MIB::sysLocation.0 = STRING: Unknown (edit /etc/snmp/snmpd.conf)
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (13) 0:00:00.13
SNMPv2-MIB::sysORID.1 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.2 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.3 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.4 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.5 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORID.6 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID.7 = OID: SNMP-MPD-MIB::snmpMPDCompliance
SNMPv2-MIB::sysORID.8 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORDescr.1 = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr.2 = STRING: The MIB module for ma
[root@localhost ~]# vi /etc/snmp/snmpd.conf
# First, map the community name "public" into a "security name"
# sec.name source community
#com2sec notConfigUser default public
com2sec notConfigUser 127.0.0.1 mypublic
com2sec notConfigUser 10.109.0.0/16 mypublic
# Second, map the security name into a group name:
# groupName securityModel securityName
group notConfigGroup v1 notConfigUser
group notConfigGroup v2c notConfigUser
[root@localhost ~]# service snmpd restart
[root@localhost ~]# snmpwalk -v 2c -c mypublic 10.109.131.198
SNMPv2-MIB::sysDescr.0 = STRING: Linux localhost.localdomain 2.6.18-164.el5PAE #1 SMP Tue Aug 18 15:59:11 EDT 2009 i686
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (15379) 0:02:33.79
SNMPv2-MIB::sysContact.0 = STRING: Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
SNMPv2-MIB::sysName.0 = STRING: localhost.localdomain
SNMPv2-MIB::sysLocation.0 = STRING: Unknown (edit /etc/snmp/snmpd.conf)
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (4) 0:00:00.04
SNMPv2-MIB::sysORID.1 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.2 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.3 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.4 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.5 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORID.6 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
... ...
[root@localhost ~]# chkconfig snmpd on
[root@localhost ~]# chkconfig --list snmpd
snmpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost ~]# yum install ruby
...
Downloading Packages:
(1/2): ruby-libs-1.8.5-5.el5_3.7.i386.rpm | 1.6 MB 00:00
(2/2): ruby-1.8.5-5.el5_3.7.i386.rpm | 274 kB 00:00
...
[root@localhost ~]# rpm -ivh rrdtool-1.2.27-4.el5.i386.rpm
[root@localhost ~]# rpm -ivh rrdtool-perl-1.2.27-4.el5.i386.rpm
[root@localhost ~]# rrdtool create test.rrd --step 5 --start -1 DS:testds:GAUGE:8:0:U RRA:AVERAGE:0.5:1:17280 RRA:AVERAGE:0.5:10:3456 RRA:AVERAGE:0.5:100:1210
[root@localhost ~]# ll -h test.rrd
-rw-r--r-- 1 root root 173K Feb 22 16:14 test.rrd
[root@localhost ~]# rrdtool info test.rrd
filename = "test.rrd"
rrd_version = "0003"
step = 5
last_update = 1487751267
ds[testds].type = "GAUGE"
ds[testds].minimal_heartbeat = 8
ds[testds].min = 0.0000000000e+00
ds[testds].max = NaN
ds[testds].last_ds = "UNKN"
...
[root@localhost ~]# cat insert.sh
#!/bin/bash
#
for I in {1..200000}; do
mysql -e "INSERT INTO testdb.tb1(NAME) VALUES ('stu$I')"
mysql -e "SELECT * FROM testdb.tb1" &> /dev/null
done
[root@localhost ~]# cat getselect.sh
#!/bin/bash
#
while true; do
SELECT=`mysql --batch -e "SHOW GLOBAL STATUS LIKE 'com_select'" | awk '/Com_select/{print $2}'`
rrdtool update mysql.rrd N:$SELECT
sleep 3
done
573 rrdtool fetch -r 5 mysql.rrd AVERAGE
574 rrdtool fetch -r 30 mysql.rrd AVERAGE
575 rrdtool fetch -r 30 mysql.rrd max
576 rrdtool fetch -r 30 mysql.rrd MAX
577 rrdtool fetch -r 30 mysql.rrd LAST
580 rrdtool graph mysql.png -s 1487747370 -t "mysql select" -v "select/3" DEF:select3=mysql.rrd:myselect:AVERAGE:step=3 LINE1:select3#FF0000:"SELECT"
581 rrdtool graph mysql.png -s 1487747370 -t "mysql select" -v "select/30" DEF:select3=mysql.rrd:myselect:AVERAGE:step=30 LINE1:select3#FF0000:"SELECT"
583 rrdtool graph mysql2.png -s 1487747370 -t "mysql select" -v "select/3" DEF:select3=mysql.rrd:myselect:AVERAGE:step=30 DEF:max30=mysql.rrd:myselect:MAX:step=30 DEF:last30=mysql.rrd:myselect:LAST:step=30 LINE1:select3#FF0000:"SELECT"
584 rrdtool graph mysql3.png -s 1487747370 -t "mysql select" -v "select/30" DEF:select3=mysql.rrd:myselect:AVERAGE:step=30 DEF:max30=mysql.rrd:myselect:MAX:step=30 DEF:last30=mysql.rrd:myselect:LAST:step=30 LINE1:select3#FF0000:"SELECT"
589 rrdtool graph mysql4.png -s 1487747370 -t "mysql select" -v "select/30" DEF:select3=mysql.rrd:myselect:AVERAGE:step=30 DEF:max30=mysql.rrd:myselect:MAX:sep=30 DEF:last30=mysql.rrd:myselect:LAST:step=30 LINE1:select3#FF0000:"SELECT" GPRINT:last30:LAST:"CURRENT\: %10.21f" GPRINT:max30:MAX:"MAXIMUM\:%6.21f"
8、相关详细配制2 --- 创建HTTP虚拟主机及手动查看
[root@localhost ~]# vi /etc/httpd/extra/httpd-vhosts.conf
<VirtualHost *:80>
ServerName cacti.jacktest.com
DocumentRoot "/web/vhosts/cacti"
<Directory "/web/vhosts/cacti">
Options Indexes
AllowOverride none
Require all granted
</Directory>
ErrorLog "logs/cacti-error_log"
CustomLog "logs/cacti-access_log" common
</VirtualHost>
[root@localhost ~]# mkdir /web
[root@localhost ~]# mkdir /web/vhosts
[root@localhost ~]# ls /web/vhosts
[root@localhost ~]# tar xf cacti-0.8.8a.tar.gz -C /web/vhosts/
[root@localhost ~]# cd /web/vhosts/
[root@localhost vhosts]# ll
lrwxrwxrwx 1 root root 12 02-23 09:26 cacti -> cacti-0.8.8a
drwxr-xr-x 13 1000 users 4096 2012-04-23 cacti-0.8.8a
[root@localhost vhosts]# service httpd restart
[root@localhost cacti]# mysqladmin create cactidb
[root@localhost cacti]# mysql cactidb < cacti.sql
[root@localhost cacti]# mysql -e "GRANT ALL ON cactidb.* TO cactiuser@localhost IDENTIFIED BY 'cactiuser'"
[root@localhost cacti]# mysqladmin flush-privileges
[root@localhost cacti]# mysql -ucactiuser -p
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| cactidb |
| test |
+--------------------+
3 rows in set (0.01 sec)
[root@localhost cacti]# vi config.php
$database_type = "mysql";
$database_default = "cactidb";
$database_hostname = "localhost";
$database_username = "cactiuser";
$database_password = "cactiuser";
$database_port = "3306";
$database_ssl = false;
/*
Edit this to point to the default URL of your Cacti install
ex: if your cacti install as at http://serverip/cacti/ this
would be set to /cacti/
*/
//$url_path = "/cacti/";
$url_path = "/";
[root@localhost include]# useradd cactiuser
[root@localhost cacti]# pwd
/web/vhosts/cacti
[root@localhost cacti]# chown -R cactiuser:cactiuser rra/ log
poller.php --===>> Spine
[root@localhost cacti]# echo '*/5 * * * * /usr/local/php/bin/php /usr/local/apache/htdocs/cacti/poller.php &>/dev/null' > /var/spool/cron/cactiuser #自动5分钟执行一次poller.php
[root@localhost cacti]# crontab -u cactiuser -e
[root@localhost vhosts]# vi /etc/php.ini
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =
date.timezone = PRC
[root@localhost vhosts]# service httpd restart
[cactiuser@localhost ~]$ /usr/local/php/bin/php /web/vhosts/cacti/poller.php
02/24/2017 09:44:13 AM - SYSTEM STATS: Time:0.1567 Method:cmd.php Processes:1 Threads:N/A Hosts:2 HostsPerProcess:2 DataSources:0 RRDsProcessed:0
[root@localhost log]# vi /usr/local/apache/htdocs/cacti/log/cacti.log
02/24/2017 09:40:01 AM - SYSTEM STATS: Time:0.1457 Method:cmd.php Processes:1 Threads:N/A Hosts:2 HostsPerProcess:2 DataSources:0 RRDsProcessed:0
[root@localhost rra]# ls /usr/local/apache/htdocs/cacti/rra/
localhost_load_1min_5.rrd localhost_mem_swap_4.rrd localhost_users_6.rrd
localhost_mem_buffers_3.rrd localhost_proc_7.rrd
9、相关详细配制3 -- 手动查看监控文档
1. 测试收集命令:
[cactiuser@localhost ~]$ snmpnetstat -v 2c -c public -Can -Cp tcp 10.109.131.144
2. 显示一个结果
[root@localhost htdocs]# vim /usr/local/apache/htdocs/scripts/tcpconn.sh
#!/bin/bash
#
# $1: hostname or ip
# $2: snmp comunity
SNMPNETSTAT=/usr/bin/snmpnetstat
ESTABLISHED=`$SNMPNETSTAT -v 2c -c $2 -Can -Cp tcp $1 | grep -i 'established' |wc -l`
echo -n "established:$ESTABLISHED"
执行结果:established:19
3. 显示多个结果
#!/bin/bash
#
# $1: hostname or ip
# $2: snmp comunity
SNMPNETSTAT=/usr/bin/snmpnetstat
TEMPFILE=`mktemp /tmp/$1_tcpconn.XXXXXXXX`
$SNMPNETSTAT -v 2c -c $2 -Can -Cp tcp $1 > $TEMPFILE
ESTABLISHED=` grep -i '$ESTABLISHED' $TEMPFILE |wc -l `
TIMEWAIT=` grep -i '$TIMEWAIT' $TEMPFILE |wc -l `
SYNRECEIVED=` grep -i '$SYNRECEIVED' $TEMPFILE |wc -l `
echo -n "established:$ESTABLISHED timewait:$TIMEWAIT synreceived:$SYNRECEIVED"
10、相关下载
1) rrdtool 下载:
https://mirrors.tuna.tsinghua.edu.cn/epel//5/i386/
2) cacti-0.8.8a 下载:
http://vdisk.weibo.com/s/ukAFmeXH43dKX
3) cacti-spine-0.8.8a 下载:
http://www.cacti.net/downloads/spine/
4) monitor-v1.3-1.tgz 下载:
http://docs.cacti.net/plugin:monitor
5) Cacti0.8.8a (Spine-Monitor-Setting-thold)安装与优化
http://damondeng.blog.51cto.com/1038075/1226857
6) thold-v0.5.0.tgz 下载: http://docs.cacti.net/plugin:thold#download
settings-v0.71-1.tgz 下载: http://docs.cacti.net/plugin:thold#download
monitor-v1.3-1.tgz 下载: http://docs.cacti.net/plugin:monitor
7) Cacti 使用第三方模版监控windows主机
http://www.cactifans.org/category/cacti%E8%A7%86%E9%A2%91
8) cacti模板大全:
http://csevan.blog.163.com/blog/static/117971266201071292318877/
9) 用Webmin轻松管理linux服务
http://guojiping.blog.51cto.com/5635432/987817
10) Cnyunwei-Cacti+Nagios最新版本 V11发布,安装完成即使用。
http://www.cnyunwei.com/thread-5714-1-1.html
---end---
转载于:https://blog.51cto.com/wangfx/1902202