本文以获取 squid 内存缓存命中率为例。
在 /etc/snmp/snmpd.conf 可以看到这样的样例:
# exec .1.3.6.1.4.1.2021.50 shelltest /bin/sh /tmp/shtest
可以使用自己编写的脚本,比如:
exec .1.3.6.1.4.1.2021.50 squidmemhits /usr/local/bin/squidmemhits.sh
vi /usr/local/bin/squidmemhits.sh
#!/bin/sh
PATH=$PATH:/usr/local/squid/bin
export PATH
squidclient -t 1 -h localhost -p 3128 mgr:info | grep "Memory hits" | cut -d "%" -f 2 | awk '{print $5}'
squidclient -t 1 -h localhost -p 3128 mgr:info | grep "Memory hits" | cut -d "%" -f 3 | awk '{print $3}'
/etc/init.d/snmpd restart
检查一下能否读取到数据:
snmpwalk -v 2c 192.168.0.200 -c public .1.3.6.1.4.1.2021.50
UCD-SNMP-MIB::ucdavis.50.1.1 = INTEGER: 1
UCD-SNMP-MIB::ucdavis.50.2.1 = STRING: "squidmemhits"
UCD-SNMP-MIB::ucdavis.50.3.1 = STRING: "/usr/local/bin/squidmemhits.sh"
UCD-SNMP-MIB::ucdavis.50.100.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.50.101.1 = STRING: "86.0"
UCD-SNMP-MIB::ucdavis.50.101.2 = STRING: "86.6"
UCD-SNMP-MIB::ucdavis.50.102.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.50.103.1 = ""
其中
UCD-SNMP-MIB::ucdavis.50.101.1 = STRING: "86.0"
UCD-SNMP-MIB::ucdavis.50.101.2 = STRING: "86.6"
这两行就是所需要的。
snmpwalk -v 1 192.168.0.200 -c public .1.3.6.1.4.1.2021.50.101.1
UCD-SNMP-MIB::ucdavis.50.101.1 = STRING: "86.0"
snmpwalk -v 1 192.168.0.200 -c public .1.3.6.1.4.1.2021.50.101.2
UCD-SNMP-MIB::ucdavis.50.101.2 = STRING: "86.6"
能采集到数据,就可以配置 Cacti 来接收了。
先创建数据模板:
console -> Templates -> Data Templates -> Add ->
Data Templates Name: ucd/net - Squid - Memory Hits - 5 Minute
Data Source Name: |host_description| - Squid - Memory Hits - 5 Minute
Data Input Method: GET SNMP Data
Associated RRA's: remove Hourly (1 Minute Average)
Internal Data Source Name: squid_memhits_5min
create ->
OID: .1.3.6.1.4.1.2021.50.101.1
Save
同样再创建一个 Squid - Memory Hits - 60 Minute 的数据模板。
再创建图形模板:
console -> Templates -> Graph Templates -> Add ->
Name: ucd/net - Squid Memory Hits
Title: |host_description| - Squid Memory Hits
Vertical Label: percent
create
Graph Template Items -> Add
Data Source: ucd/net - Squid - Memory Hits - 5 Minute - (squid_memhits_5min)
Color: FFC73B
Graph Item Type: AREA
Consolidation Function: AVERAGE
Text Format: 5 Minute
create ->
Graph Template Items -> Add
Graph Item Type: GPRINT
Consolidation Function: LAST
Text Format: Current:
Insert Hard Return
create ->
Graph Template Items -> Add
Data Source: ucd/net - Squid - Memory Hits - 60 Minute - (squid_memhits_60min)
Color: FF0000
Graph Item Type: LINE1
Consolidation Function: AVERAGE
Text Format: 60 Minute
create ->
Graph Template Items -> Add
Graph Item Type: GPRINT
Consolidation Function: LAST
Text Format: Current:
Insert Hard Return
create -> Save
然后就可以选择主机,添加图形了。
转自:http://aaaxiang000.blog.163.com/blog/static/2063491220112711492421/