Zabbix对Memcached的监控的主要思路是Memcached自身提供了查询状态的命令stats,通过脚本调用此命令并进行数据抽取完成对Memcached的实时监控。 基本步骤: 自定义监控项 → 通过脚本采集监
Zabbix对Memcached的监控的主要思路是Memcached自身提供了查询状态的命令stats,通过脚本调用此命令并进行数据抽取完成对Memcached的实时监控。
基本步骤: 自定义监控项 → 通过脚本采集监控项数据 → zabbix agent获取监控项数据 → 自定义模板和图形及触发器 → 验证数据
1. 准备Memcached服务器
安装Memcached,并配置好Memcached;安装zabbix agent及nmap基本工具包。
Nmap(也就是Network Mapper,最早是Linux下的网络扫描和嗅探工具包)是一个网络连接端扫描软件,用来扫描网上电脑开放的网络连接端。
#### Memcached服务器必须配置memcached服务,还需要安装zabbix agent## 配置zabbix 的yum源并安装zabbix agent
[root@CentOS84-IP88 ]#rpm -Uvh https://repo.zabbix.com/zabbix/6.0/rhel/8/x86_64/zabbix-release-6.0-1.el8.noarch.rpm
[root@CentOS84-IP88 ]#dnf clean all
43 files removed
[root@CentOS84-IP88 ]#dnf -y install zabbix-agent
########################################################################
## 安装 memcached及抽取数据用的 nmap
[root@CentOS84-IP88 ]#yum -y install memcached nmap
## 配置 memcached
[root@CentOS84-IP88 ]#ll /etc/sysconfig/memcached
-rw-r--r-- 1 root root 87 Jun 18 2020 /etc/sysconfig/memcached
[root@CentOS84-IP88 ]#vim /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
#OPTIONS="-l 127.0.0.1,::1"
## 启动memcached并验证
[root@CentOS84-IP88 ]#systemctl enable --now memcached
Created symlink /etc/systemd/system/multi-user.target.wants/memcached.service → /usr/lib/systemd/system/memcached.service.
[root@CentOS84-IP88 ]#ss -tln
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 1024 0.0.0.0:11211 0.0.0.0:*
LISTEN 0 1024 [::]:11211 [::]:*
[root@CentOS84-IP88 ]#
2. 准备监控脚本
########################################################################## 手动命令方式查看memcached的运行状态
[root@CentOS84-IP88 ]#telnet IP 11211 【此处IP地址是本机地址】
Trying 192.168.250.88...
Connected to 192.168.250.88.
Escape character is '^]'.
stats
STAT pid 165546
STAT uptime 4614
STAT time 1652964513
STAT version 1.5.22
STAT libevent 2.1.8-stable
STAT pointer_size 64
STAT rusage_user 0.676326
STAT rusage_system 0.310024
STAT max_connections 1024
STAT curr_connections 2
STAT total_connections 6
STAT rejected_connections 0
STAT connection_structures 3
STAT reserved_fds 20
STAT cmd_get 0
STAT cmd_set 0
STAT cmd_flush 0
STAT cmd_touch 0
STAT cmd_meta 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 29
STAT bytes_written 3813
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 3060
STAT lru_maintainer_juggles 4662
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
## 命令行方式先完成需要抽取数据的验证工作
[root@CentOS84-IP88 ]# echo -e "stats\nquit" | ncat 127.0.0.1 11211 | grep "STAT curr_connections"
STAT curr_connections 2
[root@CentOS84-IP88 ]#
[root@CentOS84-IP88 ]# echo -e "stats\nquit" | ncat 127.0.0.1 11211 | grep "STAT curr_connections" | awk '{print $3}'
2
[root@CentOS84-IP88 ]#
########################################################################
## 按照说明的命令行思路完成脚本的编写
[root@CentOS84-IP88 ]#vim memcache_monitor.sh
memcached_status(){
M_PORT=$1
M_COMMAND=$2
echo -e "stats\nquit" | ncat 127.0.0.1 "$M_PORT" | grep "STAT $M_COMMAND" | awk '{print $3}'
}
main(){
case $1 in
memcached_status)
memcached_status $2 $3
;;
esac
}
main $1 $2 $3
## 授权并测试运行
[root@CentOS84-IP88 ]#chmod a+x memcache_monitor.sh
[root@CentOS84-IP88 ]#bash memcache_monitor.sh memcached_status 11211 curr_connections
2
[root@CentOS84-IP88 ]#
3. 添加zabbix agent自定义监控项
#### 前面已经安装好zabbix_agent,修改并配置好zabbix_agent## 抽取出的yum方式默认安装zabbix_agent的配置行
[root@CentOS84-IP88 ]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=127.0.0.1
ServerActive=127.0.0.1
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@CentOS84-IP88 ]#vim /etc/zabbix/zabbix_agentd.conf
## 修改 zabbix_agentd.conf,并抽取出修改后的配置行
[root@CentOS84-IP88 ]#vim /etc/zabbix/zabbix_agentd.conf
[root@CentOS84-IP88 ]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf
PidFile=/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.250.18
ListenPort=10050
ServerActive=127.0.0.1
Hostname=192.168.250.88
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UserParameter=memcache_status[*],/etc/zabbix/zabbix_agentd.d/memcache_monitor.sh "$1" "$2" "$3"
[root@CentOS84-IP88 ]#
## 验证配置
[root@CentOS84-IP88 ]#ss -tln
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 1024 0.0.0.0:11211 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 1024 [::]:11211 [::]:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
[root@CentOS84-IP88 ]#systemctl restart zabbix-agent.service
[root@CentOS84-IP88 ]#systemctl restart zabbix-agent
[root@CentOS84-IP88 ]#ss -tln
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 1024 0.0.0.0:11211 0.0.0.0:*
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 128 0.0.0.0:10050 0.0.0.0:*
LISTEN 0 1024 [::]:11211 [::]:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 128 [::]:10050 [::]:*
[root@CentOS84-IP88 ]#
4. zabbix server测试监控项数据
## 安装 zabbix-get 工具[root@CentOS84-IP18 ]#yum install zabbix-get.x86_64
[root@CentOS84-IP18 ]#find / -name zabbix_get
/usr/bin/zabbix_get
[root@CentOS84-IP18 ]#
[root@CentOS84-IP18 ]#/usr/bin/zabbix_get -s 192.168.250.88 -p 10050 -k "memcache_status["memcached_status","11211","curr_connections"]"
2
[root@CentOS84-IP18 ]#
5. 编制Memcached模板
编制zabbix的模板分四个大的步骤:创建模板 → 在模板内创建监控项 → 创建触发器 → 创建监控图形
5.1 创建模板
登录zabbix server,配置 → 模板 → 创建模板
5.2 创建监控项
配置 → 模板 → shone-memcached → 监控项 → 创建监控项:
5.3 创建触发器
配置 → 模板 → shone-memcached → 触发器 → 创建触发器
5.4 创建图形
配置 → 模板 → shone-memcached → 图形 → 创建图形
6. 添加主机及关联模板
配置 → 主机 → 创建主机 → 关联模板