一、SNMP
安装并启动SNMP:
yum install net-snmp net-snmp-utils -y service snmpd start
测试SNMP:
netstat -unlp # 查看SNMP端口号:161 snmpwalk -v 2c -c public localhost # 查看本机SNMP信息 ls /usr/share/snmp/mibs/ # 查看MIB库文件 less SNMPv2-MIB.txt # 查看标准MIB库文件 cat /etc/snmp/snmpd.conf # 查看SNMP配置文件 snmpget -v 2c -c public localhost HOST-RESOURCES-MIB::hrSystemUptime.0 # 查看本机SNMP信息 snmpget -v 2c -c public localhost HOST-RESOURCES-MIB::hrSystemUptime.1 less TCP-MIB.txt # 查看TCP库文件 vi /etc/snmp/snmpd.conf # 在配置文件中添加TCP库文件定义 cat /etc/snmp/snmpd.conf ... view systemview included .1.3.6.1.2.1.6 ... service snmpd restart snmpwalk -v 2c -c public localhost tcp # 可以查看到TCP相关信息了 snmpwalk -v 2c -c public localhost tcp|grep established|wc -l # 查看已建立的连接数 vi /etc/snmp/snmpd.conf # 修改密码,从public到mypublic cat /etc/snmp/snmpd.conf ... com2sec notConfigUser 127.0.0.1 mypublic com2sec notConfigUser 192.168.2.0/24 mypublic ... vi /etc/snmp/snmpd.conf # 重启服务 snmpwalk -v 2c -c public 192.168.2.52 tcp # 原来的连接已不可用 snmpwalk -v 2c -c mypublic 192.168.2.52 tcp # 使用更改后的密码建立连接 snmpnetstat -v 2c -c mypublic -Ca -Cp tcp localhost # 使用snmpnetstat snmpnetstat -v 2c -c mypublic -Can -Cp tcp localhost
SNMP开机自启:
chkconfig snmpd on chkconfig --list|grep snmpd
二、rrdtool:
安装rrdtool:
wget http://packages.express.org/rrdtool/rrdtool-1.4.7-1.slc6.wrl.x86_64.rpm wget http://packages.express.org/rrdtool/rrdtool-devel-1.4.7-1.slc6.wrl.x86_64.rpm wget http://packages.express.org/rrdtool/rrdtool-perl-1.4.7-1.slc6.wrl.x86_64.rpm yum -y --nogpgcheck install rrdtool-1.4.7-1.slc6.wrl.x86_64.rpm rrdtool-perl-1.4.7-1.slc6.wrl.x86_64.rpm
使用rrdtool测试随机数分布率:
cd /tmp/ rrdtool create test.rrd --step 5 DS:testds:GAUGE:8:0:U RRA:AVERAGE:0.5:1:17280 RRA:AVERAGE:0.5:10:3456 RRA:AVERAGE:0.5:100:1210 ll -h # 查看所建的RRD文件属性 rrdtool info test.rrd # 查看RRD文件内容 rrdtool update test.rrd N:$RANDOM # 向RRD文件不停生成随机数 rrdtool fetch test.rrd AVERAGE # 查看匹配项 vi genval.sh # 生成批处理脚本文件,不停向RRD生成随机数 [root@SNMP tmp]# cat genval.sh # #!/bin/bash # while true; do rrdtool update test.rrd N:$RANDOM sleep 5 done bash -x genval.sh # DEBUG模式编译执行 rrdtool fetch -r 5 test.rrd AVERAGE # 每5s生成一次数据 rrdtool fetch -r 10 test.rrd AVERAGE # 每10s生成一次数据 rrdtool fetch -r 50 test.rrd AVERAGE # 每50s生成一次数据 rrdtool graph a.png --step 5 -s 1405335030 DEF:vartest=test.rrd:testds:AVERAGE LINE1:vartest#FF0000:"testline" # 绘制每5s图 rrdtool graph a.png --step 50 -s 1405335030 DEF:vartest=test.rrd:testds:AVERAGE LINE1:vartest#FF0000:"testline" # 绘制每50s图 rrdtool graph a.png -s 1405335030 DEF:vartest=test.rrd:testds:AVERAGE:step=5 DEF:vartest2=test.rrd:testds:AVERAGE:step=50 LINE1:vartest#FF0000:"testline" LINE2:vartest2#00FF00:"testline2" # 同时绘制5s及50s图
使用rrdtool测试数据库查询次数:
yum install mysql-server -y service mysqld start mysqladmin -u root password 'zhoufwind' [root@SNMP ~]# mysql -u root -p mysql> SHOW GLOBAL STATUS LIKE 'com_select'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Com_select | 3 | +---------------+-------+ 1 row in set (0.00 sec) rrdtool create mysql.rrd --step 3 DS:myselect:COUNTER:5:0:U RRA:AVERAGE:0.5:1:28800 RRA:AVERAGE:0.5:10:2880 RRA:MAX:0.5:10:2880 RRA:LAST:0.5:10:2880 # 创建rrd文件 [root@SNMP tmp]# mysql -uroot -pzhoufwind -e "SHOW GLOBAL STATUS LIKE 'com_select'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | Com_select | 4 | +---------------+-------+ [root@SNMP tmp]# mysql --batch -uroot -pzhoufwind -e "SHOW GLOBAL STATUS LIKE 'com_select'" Variable_name Value Com_select 5 [root@SNMP tmp]# mysql --batch -uroot -pzhoufwind -e "SHOW GLOBAL STATUS LIKE 'com_select'"|awk '/Com_select/{pri}[root@SNMP tmp]# mysql --batch -uroot -pzhoufwind -e "SHOW GLOBAL STATUS LIKE 'com_select'"|awk '/Com_select/{print $2}' 6 [root@SNMP tmp]# mysql --batch -uroot -pzhoufwind -e "SHOW GLOBAL STATUS LIKE 'com_select'"|awk '/Com_select/{print $2}' 7 # 简化查询次数结果 [root@SNMP tmp]# vi getselect.sh # 创建脚本,不断监测查询数 "getselect.sh" [New] 6L, 148C written [root@SNMP tmp]# cat getselect.sh #!/bin/bash # while true; do SELECT=`mysql --batch -uroot -pzhoufwind -e "SHOW GLOBAL STATUS LIKE 'com_select'"|awk '/Com_select/{print $2}'` rrdtool update mysql.rrd N:$SELECT sleep 3 done bash -x getselect.sh # 编译执行 vi insert.sh # 创建脚本不断的对数据库进行插入及查询操作 "insert.sh" [New] 6L, 190C written [root@SNMP tmp]# cat insert.sh #!/bin/bash # for I in {1..200000}; do mysql -uroot -pzhoufwind -e "INSERT INTO testdb.tb1(NAME) VALUES ('stu$I')" mysql -uroot -pzhoufwind -e "SELECT * FROM testdb.tb1" &> /dev/null done [root@SNMP tmp]# bash -x insert.sh + for I in '{1..200000}' + mysql -uroot -pzhoufwind -e 'INSERT INTO testdb.tb1(NAME) VALUES ('\''stu1'\'')' + mysql -uroot -pzhoufwind -e 'SELECT * FROM testdb.tb1' + for I in '{1..200000}' + mysql -uroot -pzhoufwind -e 'INSERT INTO testdb.tb1(NAME) VALUES ('\''stu2'\'')' + mysql -uroot -pzhoufwind -e 'SELECT * FROM testdb.tb1' + for I in '{1..200000}' ... rrdtool fetch -r 5 mysql.rrd AVERAGE # 每5s生成一次平均值数据 rrdtool fetch -r 30 mysql.rrd AVERAGE # 每30s生成一次平均值数据 rrdtool fetch -r 30 mysql.rrd MAX # 每30s生成一次最大值数据 rrdtool fetch -r 30 mysql.rrd LAST # 每30s生成一次上次执行数据 rrdtool graph mysql.png -s 1405339620 -t "mysql select" -v "selects/3" DEF:select3=mysql.rrd:myselect:AVERAGE:step=3 LINE1:select3#FF0000:"SELECT" # 绘制每3秒查询数图 rrdtool graph mysql.png -s 1405339620 -t "mysql select" -v "selects/30" DEF:select30=mysql.rrd:myselect:AVERAGE:step=30 DEF:max30=mysql.rrd:myselect:MAX:step=30 LINE1:select30#FF0000:"SELECT" GPRINT:max30:MAX:"MAXIMUM\: %6.2lf" # 绘制每30秒查询平均值/最大值图 rrdtool graph mysql.png -s 1405339620 -t "mysql select" -v "selects/30" DEF:select30=mysql.rrd:myselect:AVERAGE:step=30 DEF:max30=mysql.rrd:myselect:MAX:step=30 DEF:last30=mysql.rrd:myselect:LAST:step=30 LINE1:select30#FF0000:"SELECT" GPRINT:max30:MAX:"MAXIMUM\: %6.2lf" GPRINT:last30:LAST:"CURRENT\: %10.2lf" # 绘制每30秒查询平均值/最大值/当前值图
rrdtool绘制图样例:
三、参考文章: