Zabbix 3.2:Zabbix 监控 Memcached

3.2:Zabbix 监控 Memcached

本次目的是对Memcached关键指标进行详细了解,并通过编写脚本和自定义监控项对Memcached状态进行监测。
对Memcached的监控主要是基于stats状态信息来进行的。

3.2.1:准备 Memcached 主机

使用node111(192.168.1.111)做为被监控主机,安装Memcached。

root@node111:~# apt install memcached

3.2.2:Memcached 状态监测脚本

3.2.2.1:stats 信息

使用nc连接本地的11211端口,与Memcached交互,并通过echo来传递stats命令,并在获取后quit。

因为Memcached的常用功能就是提供缓存服务,所以最常关注的是四个与缓存对象相关的参数:

  • STAT get_hits:缓存命中的次数;
  • STAT get_misses:缓存未命中的次数;
  • STAT curr_items:当前的缓存对象数量;
  • STAT total_items:运行至今存储过的缓存对象的总数。

此外,两个与连接数相关的参数也值得关注一下:

  • STAT curr_connections:当前连接数量;
  • STAT total_connections:运行至今建立过的连接总数。
root@node111:~# echo -e "stats\nquit" | nc 127.0.0.1 11211
STAT pid 2289
STAT uptime 787
STAT time 1618393631
STAT version 1.5.6 Ubuntu
STAT libevent 2.1.8-stable
STAT pointer_size 64
STAT rusage_user 0.169015
STAT rusage_system 0.157747
STAT max_connections 1024
STAT curr_connections 1
STAT total_connections 9
STAT rejected_connections 0
STAT connection_structures 2
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 0
STAT get_misses 0
STAT get_expired 0
STAT get_flushed 0
STAT delete_misses 0
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 103
STAT bytes_written 11409
STAT limit_maxbytes 67108864
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT time_in_listen_disabled_us 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT slab_reassign_rescues 0
STAT slab_reassign_chunk_rescues 0
STAT slab_reassign_evictions_nomem 0
STAT slab_reassign_inline_reclaim 0
STAT slab_reassign_busy_items 0
STAT slab_reassign_busy_deletes 0
STAT slab_reassign_running 0
STAT slabs_moved 0
STAT lru_crawler_running 0
STAT lru_crawler_starts 1275
STAT lru_maintainer_juggles 835
STAT malloc_fails 0
STAT log_worker_dropped 0
STAT log_worker_written 0
STAT log_watcher_skipped 0
STAT log_watcher_sent 0
STAT bytes 0
STAT curr_items 0
STAT total_items 0
STAT slab_global_page_pool 0
STAT expired_unfetched 0
STAT evicted_unfetched 0
STAT evicted_active 0
STAT evictions 0
STAT reclaimed 0
STAT crawler_reclaimed 0
STAT crawler_items_checked 0
STAT lrutail_reflocked 0
STAT moves_to_cold 0
STAT moves_to_warm 0
STAT moves_within_lru 0
STAT direct_reclaims 0
STAT lru_bumps_dropped 0
END

3.2.2.2:编写脚本

前面使用echo -e "stats\nquit" | nc 127.0.0.1 11211已经可以获取到所有的基础数据,通过向脚本传递相关参数,就可以获取到想要的具体数据了。

脚本如下:

#!/bin/bash
# Description: 获取Memcached相关状态数据
# Author: yqc

# Define variables
Memcached_Addr=127.0.0.1
Memcached_Port=11211
Stats_Name=$1

# Define function "Memcached_Stats"
Memcached_Stats (){
	Stats_Num=$(echo -e "stats\nquit" | nc ${Memcached_Addr} ${Memcached_Port} | grep "${Stats_Name}" | cut -d' ' -f3)
	echo ${Stats_Num}
}

# Use function "Memcached_Stats"
Memcached_Stats $1

测试脚本:

root@node111:~# bash Memcached_Stats.sh curr_connections
1

root@node111:~# bash Memcached_Stats.sh total_connections
17

3.2.3:Zabbix Agent 添加 UserParameter

3.2.3.1:主配置文件

root@node111:~# grep '^[a-Z]' /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.1.101
ListenPort=10050
StartAgents=3
ServerActive=192.168.1.101
Hostname=192.168.1.111
Include=/etc/zabbix/zabbix_agentd.d/*.conf

3.2.3.2:添加自定义监控项

拷贝脚本并添加执行权限:

root@node111:~# cp /root/Memcached_Stats.sh /etc/zabbix/zabbix_agentd.d/
root@node111:~# chmod +x /etc/zabbix/zabbix_agentd.d/Memcached_Stats.sh

编辑zabbix agent配置文件,添加自定义监控项:

root@node111:~# vim /etc/zabbix/zabbix_agentd.d/Memcached_Stats.conf
UserParameter=memcached_stats[*],/etc/zabbix/zabbix_agentd.d/Memcached_Stats.sh "$1"

重启zabbix-agent:

root@node111:~# systemctl restart zabbix-agent

3.2.3.3:Zabbix Server 测试手动获取监控数据

root@zabbix-server:~# zabbix_get -s 192.168.1.111 -p 10050 -k "memcached_stats["total_connections"]"
20

3.2.4:Zabbix Server 制作 Memcached 状态监测模板

3.2.4.1:创建模板

Zabbix 3.2:Zabbix 监控 Memcached

3.2.4.2:创建应用集

Zabbix 3.2:Zabbix 监控 Memcached

3.2.4.3:创建监控项

创建缓存命中数get_hits的监控项:

Zabbix 3.2:Zabbix 监控 Memcached

依次克隆并修改,添加其它几个常关注的监控项:

Zabbix 3.2:Zabbix 监控 Memcached

3.2.5:Zabbix Server 添加主机并关联模板

3.2.5.1:创建主机

Zabbix 3.2:Zabbix 监控 Memcached

3.2.5.2:关联模板

Zabbix 3.2:Zabbix 监控 Memcached

3.2.5.3:验证监控数据

最新数据:

Zabbix 3.2:Zabbix 监控 Memcached

上一篇:【DB笔试面试628】Oracle的统计信息包括哪几种类型?


下一篇:Haproxy+Keepalived+MariaDB(Galera集群)