环境介绍
操作系统:Centos 6.* ( 双核4G、硬盘至少20G)
zabbix_agentd版本:3.0.3
zabbix_server版本:3.2.6
memcached版本:1.4.15
zabbix server : 192.168.1.127
zabbix agentd :192.168.1.101
1:zabbix监控memcached所需脚本
A): vim /etc/zabbix/memcached-tool.pl
#!/usr/bin/perl
#
# memcached-tool:
# stats/management tool for memcached.
#
# Author:
# Brad Fitzpatrick <brad@danga.com>
#
# Contributor:
# Andrey Niakhaichyk <andrey@niakhaichyk.org>
#
# License:
# public domain. I give up all rights to this
# tool. modify and copy at will.
#
use strict;
use IO::Socket::INET;
my $addr = shift;
my $mode = shift || "display";
my ($from, $to);
if ($mode eq "display") {
undef $mode if @ARGV;
} elsif ($mode eq "move") {
$from = shift;
$to = shift;
undef $mode if $from < 6 || $from > 17;
undef $mode if $to < 6 || $to > 17;
print STDERR "ERROR: parameters out of range\n\n" unless $mode;
} elsif ($mode eq 'dump') {
;
} elsif ($mode eq 'stats') {
;
} elsif ($mode eq 'settings') {
;
} elsif ($mode eq 'sizes') {
;
} else {
undef $mode;
}
undef $mode if @ARGV;
die
"Usage: memcached-tool <host[:port] | /path/to/socket> [mode]\n
memcached-tool 10.0.0.5:11211 display # shows slabs
memcached-tool 10.0.0.5:11211 # same. (default is display)
memcached-tool 10.0.0.5:11211 stats # shows general stats
memcached-tool 10.0.0.5:11211 settings # shows settings stats
memcached-tool 10.0.0.5:11211 sizes # shows sizes stats
memcached-tool 10.0.0.5:11211 dump # dumps keys and values
WARNING! sizes is a development command.
As of 1.4 it is still the only command which will lock your memcached instance for some time.
If you have many millions of stored items, it can become unresponsive for several minutes.
Run this at your own risk. It is roadmapped to either make this feature optional
or at least speed it up.
" unless $addr && $mode;
my $sock;
if ($addr =~ m:/:) {
$sock = IO::Socket::UNIX->new(
Peer => $addr,
);
}
else {
$addr .= ':11211' unless $addr =~ /:\d+$/;
$sock = IO::Socket::INET->new(
PeerAddr => $addr,
Proto => 'tcp',
);
}
die "Couldn't connect to $addr\n" unless $sock;
if ($mode eq 'dump') {
my %items;
my $totalitems;
print $sock "stats items\r\n";
while (<$sock>) {
last if /^END/;
if (/^STAT items:(\d*):number (\d*)/) {
$items{$1} = $2;
$totalitems += $2;
}
}
print STDERR "Dumping memcache contents\n";
print STDERR " Number of buckets: " . scalar(keys(%items)) . "\n";
print STDERR " Number of items : $totalitems\n";
foreach my $bucket (sort(keys(%items))) {
print STDERR "Dumping bucket $bucket - " . $items{$bucket} . " total items\n";
print $sock "stats cachedump $bucket $items{$bucket}\r\n";
my %keyexp;
while (<$sock>) {
last if /^END/;
# return format looks like this
# ITEM foo [6 b; 1176415152 s]
if (/^ITEM (\S+) \[.* (\d+) s\]/) {
$keyexp{$1} = $2;
}
}
foreach my $k (keys(%keyexp)) {
print $sock "get $k\r\n";
my $response = <$sock>;
if ($response =~ /VALUE (\S+) (\d+) (\d+)/) {
my $flags = $2;
my $len = $3;
my $val;
read $sock, $val, $len;
print "add $k $flags $keyexp{$k} $len\r\n$val\r\n";
# get the END
$_ = <$sock>;
$_ = <$sock>;
}
}
}
exit;
}
if ($mode eq 'stats') {
my %items;
print $sock "stats\r\n";
while (<$sock>) {
last if /^END/;
chomp;
if (/^STAT\s+(\S*)\s+(.*)/) {
$items{$1} = $2;
}
}
printf ("#%-17s %5s %11s\n", $addr, "Field", "Value");
foreach my $name (sort(keys(%items))) {
printf ("%24s %12s\n", $name, $items{$name});
}
exit;
}
if ($mode eq 'settings') {
my %items;
print $sock "stats settings\r\n";
while (<$sock>) {
last if /^END/;
chomp;
if (/^STAT\s+(\S*)\s+(.*)/) {
$items{$1} = $2;
}
}
printf ("#%-17s %5s %11s\n", $addr, "Field", "Value");
foreach my $name (sort(keys(%items))) {
printf ("%24s %12s\n", $name, $items{$name});
}
exit;
}
if ($mode eq 'sizes') {
my %items;
print $sock "stats sizes\r\n";
while (<$sock>) {
last if /^END/;
chomp;
if (/^STAT\s+(\S*)\s+(.*)/) {
$items{$1} = $2;
}
}
printf ("#%-17s %5s %11s\n", $addr, "Size", "Count");
foreach my $name (sort(keys(%items))) {
printf ("%24s %12s\n", $name, $items{$name});
}
exit;
}
# display mode:
my %items; # class -> { number, age, chunk_size, chunks_per_page,
# total_pages, total_chunks, used_chunks,
# free_chunks, free_chunks_end }
print $sock "stats items\r\n";
my $max = 0;
while (<$sock>) {
last if /^END/;
if (/^STAT items:(\d+):(\w+) (\d+)/) {
$items{$1}{$2} = $3;
}
}
print $sock "stats slabs\r\n";
while (<$sock>) {
last if /^END/;
if (/^STAT (\d+):(\w+) (\d+)/) {
$items{$1}{$2} = $3;
$max = $1;
}
}
print " # Item_Size Max_age Pages Count Full? Evicted Evict_Time OOM\n";
foreach my $n (1..$max) {
my $it = $items{$n};
next if (0 == $it->{total_pages});
my $size = $it->{chunk_size} < 1024 ?
"$it->{chunk_size}B" :
sprintf("%.1fK", $it->{chunk_size} / 1024.0);
my $full = $it->{free_chunks_end} == 0 ? "yes" : " no";
printf("%3d %8s %9ds %7d %7d %7s %8d %8d %4d\n",
$n, $size, $it->{age}, $it->{total_pages},
$it->{number}, $full, $it->{evicted},
$it->{evicted_time}, $it->{outofmemory});
}
B):vim /etc/zabbix/memcached.sh
#!/bin/bash
##############
# #
# 数据连接 #
# #
##############
MEMCACHED_HOME="/etc/zabbix/memcached-tool.pl"
HOST="127.0.0.1"
PORT="11211"
#################
# 参数是否正确 #
#################
if [ $# -ne "1" ];then
echo "arg error!"
fi
#################
# #
# 获取数据 #
# #
#################
case $1 in
accepting_conns)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "accepting_conns" | awk '{print $NF}'`
echo $result
;;
auth_cmds)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "auth_cmds" | awk '{print $NF}'`
echo $result
;;
auth_errors)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "auth_errors" | awk '{print $NF}'`
echo $result
;;
bytes)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "bytes" | awk '{print $NF}'`
echo $result
;;
bytes_read)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "bytes_read" | awk '{print $NF}'`
echo $result
;;
bytes_written)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "bytes_written" | awk '{print $NF}'`
echo $result
;;
cas_badval)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "cas_badval" | awk '{print $NF}'`
echo $result
;;
cas_hits)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "cas_hits" | awk '{print $NF}'`
echo $result
;;
cas_misses)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "cas_misses" | awk '{print $NF}'`
echo $result
;;
cmd_flush)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "cmd_flush" | awk '{print $NF}'`
echo $result
;;
cmd_get)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "cmd_get" | awk '{print $NF}'`
echo $result
;;
cmd_set)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "cmd_set" | awk '{print $NF}'`
echo $result
;;
cmd_touch)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "cmd_touch" | awk '{print $NF}'`
echo $result
;;
conn_yields)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "conn_yields" | awk '{print $NF}'`
echo $result
;;
connection_structures)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "connection_structures" | awk '{print $NF}'`
echo $result
;;
curr_connections)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "curr_connections" | awk '{print $NF}'`
echo $result
;;
curr_items)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "curr_items" | awk '{print $NF}'`
echo $result
;;
delete_hits)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "delete_hits" | awk '{print $NF}'`
echo $result
;;
delete_misses)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "delete_misses" | awk '{print $NF}'`
echo $result
;;
decr_hits)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "decr_hits" | awk '{print $NF}'`
echo $result
;;
decr_misses)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "decr_misses" | awk '{print $NF}'`
echo $result
;;
evicted_unfetched)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "evicted_unfetched" | awk '{print $NF}'`
echo $result
;;
evictions)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "evictions" | awk '{print $NF}'`
echo $result
;;
expired_unfetched)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "expired_unfetched" | awk '{print $NF}'`
echo $result
;;
get_hits)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "get_hits" | awk '{print $NF}'`
echo $result
;;
get_misses)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "get_misses" | awk '{print $NF}'`
echo $result
;;
hash_bytes)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "hash_bytes" | awk '{print $NF}'`
echo $result
;;
hash_is_expanding)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "hash_is_expanding" | awk '{print $NF}'`
echo $result
;;
hash_power_level)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "hash_power_level" | awk '{print $NF}'`
echo $result
;;
incr_hits)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "incr_hits" | awk '{print $NF}'`
echo $result
;;
incr_misses)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "incr_misses" | awk '{print $NF}'`
echo $result
;;
libevent)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "libevent" | awk '{print $NF}'`
echo $result
;;
limit_maxbytes)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "limit_maxbytes" | awk '{print $NF}'`
echo $result
;;
listen_disabled_num)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "listen_disabled_num" | awk '{print $NF}'`
echo $result
;;
pid)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "pid" | awk '{print $NF}'`
echo $result
;;
pointer_size)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "pointer_size" | awk '{print $NF}'`
echo $result
;;
reclaimed)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "reclaimed" | awk '{print $NF}'`
echo $result
;;
reserved_fds)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "reserved_fds" | awk '{print $NF}'`
echo $result
;;
rusage_system)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "rusage_system" | awk '{print $NF}'`
echo $result
;;
rusage_user)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "rusage_user" | awk '{print $NF}'`
echo $result
;;
threads)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "threads" | awk '{print $NF}'`
echo $result
;;
time)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "time" | awk '{print $NF}'`
echo $result
;;
total_connections)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "total_connections" | awk '{print $NF}'`
echo $result
;;
total_items)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "total_items" | awk '{print $NF}'`
echo $result
;;
touch_hits)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "touch_hits" | awk '{print $NF}'`
echo $result
;;
touch_misses)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "touch_misses" | awk '{print $NF}'`
echo $result
;;
uptime)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "uptime" | awk '{print $NF}'`
echo $result
;;
version)
result=`${MEMCACHED_HOME} ${HOST}:${PORT} stats| awk '!(NR==1)' | grep -w "version" | awk '{print $NF}'`
echo $result
;;
*)
echo "Usage:$0(accepting_conns|auth_cmds|auth_errors|bytes|bytes_read|bytes_written|cas_badval|cas_hits|cas_misses|cmd_flush|cmd_set|cmd_get|cmd_touch|conn_yields|connection_structures|curr_connections|curr_items|delete_hits|delete_misses| decr_hits|decr_misses|evicted_unfetched|evictions|expired_unfetched|get_hits|get_misses|hash_bytes|hash_is_expanding|hash_power_level|incr_hits|incr_misses|libevent|limit_maxbytes|listen_disabled_rum|pid|pointer_size|reclaimed|reserved_fds|rusage_system|rusage_user|threads|time|total_connections|total_items|touch_hits|touch_misses|uptime|version)"
;;
esac
2:给脚本添加执行权限
chmod +x memcached.sh
chmod +x memcached-tool.pl
3:zabbix agentd端添加配置文件
vim /etc/zabbix/zabbix_agentd.conf
UserParameter=memcached_stats[*],/etc/zabbix/memcached.sh $1
4:重启服务
/etc/init.d/zabbix-agent restart
5:本地获取数据测试
/etc/zabbix/memcached-tool.pl 127.0.0.1:11211 stats| awk '!(NR==1)' | grep -w "accepting_conns" | awk '{print $NF}'
6: server端获取数据
/usr/local/zabbix/bin/zabbix_get -s192.168.1.101 -p10050 -k memcached_stats[uptime]
web端配置
memcached模板下载地址:http://note.youdao.com/noteshare?id=4c118efc7f465fa9ee424d3980197074&sub=7E5E2788A94941C9BC2FEA51DEAD0D18
只需将模板导入并添加到agentd主机即可