cacti 监控linux TCP端口连接数

转载、服务器

原文出处:http://blog.chinaunix.net/uid-24499216-id-2602964.html

个人心得:

我的cacti版本是 0.8.8a按照原文的说法要改hash的值才能将模板导入成功,其实我没有改 也成功改入了。

脚本webconn.sh 中的ip地址和端口可以改成自己的,别忘记了

#########################原文内容如下######################

今天动手做了个监控nginx连接数的模板:

修改被监控端snmpd.conf,在最后增加一行:

[root@localhost ~]# vim /etc/snmp/snmpd.conf

extend .1.3.6.1.4.1.2021.21 webconn /bin/bash /etc/snmp/webconn.sh

再在/etc/snmp下创建获取端口连接数的脚本webconn.sh

#!/bin/sh  
#If more than one IP or port Please '|' separated    
ip="58.251.1.2|119.145.1.2"  #我们服务器是双线接入,所以有2个IP    
port="80|443"                #我这里是监控80和443端口    
webconn=`netstat -nt |grep ESTABLISHED |awk '{print $4}'|awk '/('$ip'):('$port')/'| wc -l`    
websyn=`netstat -nt |grep SYN_RECV |awk '{print $4}'|awk '/('$ip'):('$port')/'| wc -l`    
weback=`netstat -nt |grep LAST_ACK |awk '{print $4}'|awk '/('$ip'):('$port')/'| wc -l`    
webwait=`netstat -nt |grep TIME_WAI |awk '{print $4}'|awk '/('$ip'):('$port')/'| wc -l`    
echo $webconn    
echo $websyn    
echo $weback    
echo $webwait    
再赋予脚本可执行权限

[root@localhost ~]# chmod a+x /etc/snmp/webconn.sh

[root@localhost ~]# service snmpd restart

接下来在cacti上验证看能否通过snmp抓取到客户端的数据:

[root@monitor ~]# snmpwalk -v 2c -c public 172.16.88.10 .1.3.6.1.4.1.2021.21  
UCD-SNMP-MIB::ucdavis.21.1.0 = INTEGER: 1    
UCD-SNMP-MIB::ucdavis.21.2.1.2.7.119.101.98.99.111.110.110 = STRING: "/bin/bash"    
UCD-SNMP-MIB::ucdavis.21.2.1.3.7.119.101.98.99.111.110.110 = STRING: "/etc/snmp/webconn.sh"    
UCD-SNMP-MIB::ucdavis.21.2.1.4.7.119.101.98.99.111.110.110 = ""    
UCD-SNMP-MIB::ucdavis.21.2.1.5.7.119.101.98.99.111.110.110 = INTEGER: 5    
UCD-SNMP-MIB::ucdavis.21.2.1.6.7.119.101.98.99.111.110.110 = INTEGER: 1    
UCD-SNMP-MIB::ucdavis.21.2.1.7.7.119.101.98.99.111.110.110 = INTEGER: 1    
UCD-SNMP-MIB::ucdavis.21.2.1.20.7.119.101.98.99.111.110.110 = INTEGER: 4    
UCD-SNMP-MIB::ucdavis.21.2.1.21.7.119.101.98.99.111.110.110 = INTEGER: 1    
UCD-SNMP-MIB::ucdavis.21.3.1.1.7.119.101.98.99.111.110.110 = STRING: "221"    
UCD-SNMP-MIB::ucdavis.21.3.1.2.7.119.101.98.99.111.110.110 = STRING: "221    
16    
1    
19"    
UCD-SNMP-MIB::ucdavis.21.3.1.3.7.119.101.98.99.111.110.110 = INTEGER: 4    
UCD-SNMP-MIB::ucdavis.21.3.1.4.7.119.101.98.99.111.110.110 = INTEGER: 0    
UCD-SNMP-MIB::ucdavis.21.4.1.2.7.119.101.98.99.111.110.110.1 = STRING: "221"    
UCD-SNMP-MIB::ucdavis.21.4.1.2.7.119.101.98.99.111.110.110.2 = STRING: "16"    
UCD-SNMP-MIB::ucdavis.21.4.1.2.7.119.101.98.99.111.110.110.3 = STRING: "1"    
UCD-SNMP-MIB::ucdavis.21.4.1.2.7.119.101.98.99.111.110.110.4 = STRING: "19"

若有上面的输出则说明成功了,接下来就只要建cacti的模板了,这里就不讲建立模板的过程了,我已经把模板导出来,有需要可下载附件包含模板和脚本

另外我的cacti是0.8.7f的,不同版本模板的hash值不一样,所以非0.8.7f版本的cacti导入此模板会报错,不过可将模板里的0020全部替换成你所用版本的 hash值就可以解决,下面是每个版本对应的hash值:  
   1. "0.8.4"  => "0000",  
   2. "0.8.5"  => "0001",  
   3. "0.8.5a" => "0002",  
   4. "0.8.6"  => "0003",  
   5. "0.8.6a" => "0004",  
   6. "0.8.6b" => "0005",  
   7. "0.8.6c" => "0006",  
   8. "0.8.6d" => "0007",  
   9. "0.8.6e" => "0008",  
  10. "0.8.6f" => "0009",  
  11. "0.8.6g" => "0010",  
  12. "0.8.6h" => "0011",  
  13. "0.8.6i" => "0012",  
  14. "0.8.6j" => "0013",  
  15. "0.8.7"  => "0014",  
  16. "0.8.7a" => "0015",  
  17. "0.8.7b" => "0016",  
  18. "0.8.7c" => "0017",  
  19. "0.8.7d" => "0018" 
  20. "0.8.7e" => "0019"    
  21. "0.8.7f" => "0020"1

########################原文结束######################

你可能感兴趣的:(cacti,监控linux,TCP端口连接数)