zabbix监控memcached服务
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.安装并配置memcached服务
1>.使用yum方式安装memcached

[root@node201.yinzhengjie.org.cn ~]# yum -y install memcached Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.aliyun.com * updates: mirrors.tuna.tsinghua.edu.cn base | 3.6 kB 00:00:00 docker-ce-stable | 3.5 kB 00:00:00 extras | 2.9 kB 00:00:00 kubernetes | 1.4 kB 00:00:00 updates | 2.9 kB 00:00:00 zabbix | 2.9 kB 00:00:00 zabbix-non-supported | 951 B 00:00:00 Resolving Dependencies --> Running transaction check ---> Package memcached.x86_64 0:1.4.15-10.el7_3.1 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================================================================================================================== Package Arch Version Repository Size ============================================================================================================================================================================================================================================================================== Installing: memcached x86_64 1.4.15-10.el7_3.1 base 85 k Transaction Summary ============================================================================================================================================================================================================================================================================== Install 1 Package Total download size: 85 k Installed size: 176 k Downloading packages: memcached-1.4.15-10.el7_3.1.x86_64.rpm | 85 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : memcached-1.4.15-10.el7_3.1.x86_64 1/1 Verifying : memcached-1.4.15-10.el7_3.1.x86_64 1/1 Installed: memcached.x86_64 0:1.4.15-10.el7_3.1 Complete! [root@node201.yinzhengjie.org.cn ~]#
2>.查看memcached的配置文件
[root@node201.yinzhengjie.org.cn ~]# cat /etc/sysconfig/memcached PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS="" [root@node201.yinzhengjie.org.cn ~]#
3>.启动memcached服务
[root@node201.yinzhengjie.org.cn ~]# systemctl start memcached.service [root@node201.yinzhengjie.org.cn ~]# [root@node201.yinzhengjie.org.cn ~]# systemctl enable memcached.service Created symlink from /etc/systemd/system/multi-user.target.wants/memcached.service to /usr/lib/systemd/system/memcached.service. [root@node201.yinzhengjie.org.cn ~]# [root@node201.yinzhengjie.org.cn ~]# ss -ntl | grep 11211 LISTEN 0 1024 *:11211 *:* LISTEN 0 1024 :::11211 :::* [root@node201.yinzhengjie.org.cn ~]# [root@node201.yinzhengjie.org.cn ~]#
二.zabbix agent端配置监控memcached的脚本
1>.在命令行中查看数据memcached的监控指标

[root@node201.yinzhengjie.org.cn ~]# yum -y install nc Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.huaweicloud.com * extras: mirrors.aliyun.com * updates: mirrors.tuna.tsinghua.edu.cn Resolving Dependencies --> Running transaction check ---> Package nmap-ncat.x86_64 2:6.40-19.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ============================================================================================================================================================================================================================================================================== Package Arch Version Repository Size ============================================================================================================================================================================================================================================================================== Installing: nmap-ncat x86_64 2:6.40-19.el7 base 206 k Transaction Summary ============================================================================================================================================================================================================================================================================== Install 1 Package Total download size: 206 k Installed size: 423 k Downloading packages: nmap-ncat-6.40-19.el7.x86_64.rpm | 206 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : 2:nmap-ncat-6.40-19.el7.x86_64 1/1 Verifying : 2:nmap-ncat-6.40-19.el7.x86_64 1/1 Installed: nmap-ncat.x86_64 2:6.40-19.el7 Complete! [root@node201.yinzhengjie.org.cn ~]#
[root@node201.yinzhengjie.org.cn ~]# echo -e "stats quit" | nc 127.0.0.1 "11211" STAT pid 88756 STAT uptime 261 STAT time 1582849154 STAT version 1.4.15 STAT libevent 2.0.21-stable STAT pointer_size 64 STAT rusage_user 0.014680 STAT rusage_system 0.014680 STAT curr_connections 10 STAT total_connections 11 STAT connection_structures 11 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 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 11 STAT bytes_written 0 STAT limit_maxbytes 67108864 STAT accepting_conns 1 STAT listen_disabled_num 0 STAT threads 4 STAT conn_yields 0 STAT hash_power_level 16 STAT hash_bytes 524288 STAT hash_is_expanding 0 STAT bytes 0 STAT curr_items 0 STAT total_items 0 STAT expired_unfetched 0 STAT evicted_unfetched 0 STAT evictions 0 STAT reclaimed 0 END [root@node201.yinzhengjie.org.cn ~]#
2>.编写脚本获取memcached的监控项数据
[root@node201.yinzhengjie.org.cn ~]# cat /etc/zabbix/zabbix_agentd.d/memcached_monitoring.sh #!/bin/bash # #******************************************************************** #Author: yinzhengjie #QQ: 1053419035 #Date: 2019-11-23 #FileName: shell/menu.sh #URL: http://www.cnblogs.com/yinzhengjie #Description: The test script #Copyright notice: original works, no reprint! Otherwise, legal liability will be investigated. #******************************************************************** function memcached_status(){ M_PORT=$1 M_COMMAND=$2 echo -e "stats quit" | nc 127.0.0.1 "$M_PORT" | grep "STAT $M_COMMAND " | awk '{print $3}' } function main(){ case $1 in memcached_status) memcached_status $2 $3 ;; esac } main $1 $2 $3 [root@node201.yinzhengjie.org.cn ~]# [root@node201.yinzhengjie.org.cn ~]# bash /etc/zabbix/zabbix_agentd.d/memcached_monitoring.sh memcached_status 11211 curr_connections 10 [root@node201.yinzhengjie.org.cn ~]# [root@node201.yinzhengjie.org.cn ~]# bash /etc/zabbix/zabbix_agentd.d/memcached_monitoring.sh memcached_status 11211 reserved_fds 20 [root@node201.yinzhengjie.org.cn ~]#
3>.给自定义的脚本添加可执行权限
[root@node201.yinzhengjie.org.cn ~]# ll /etc/zabbix/zabbix_agentd.d/memcached_monitoring.sh -rw-r--r-- 1 root root 760 Feb 28 08:25 /etc/zabbix/zabbix_agentd.d/memcached_monitoring.sh [root@node201.yinzhengjie.org.cn ~]# [root@node201.yinzhengjie.org.cn ~]# chmod +x /etc/zabbix/zabbix_agentd.d/memcached_monitoring.sh [root@node201.yinzhengjie.org.cn ~]# [root@node201.yinzhengjie.org.cn ~]# ll /etc/zabbix/zabbix_agentd.d/memcached_monitoring.sh -rwxr-xr-x 1 root root 760 Feb 28 08:25 /etc/zabbix/zabbix_agentd.d/memcached_monitoring.sh [root@node201.yinzhengjie.org.cn ~]# [root@node201.yinzhengjie.org.cn ~]#
4>.修改zaibbix agent的配置文件
[root@node201.yinzhengjie.org.cn ~]# grep ^Include /etc/zabbix/zabbix_agentd.conf Include=/etc/zabbix/zabbix_agentd.d/*.conf [root@node201.yinzhengjie.org.cn ~]# [root@node201.yinzhengjie.org.cn ~]# cat /etc/zabbix/zabbix_agentd.d/yinzhengjie_monitoring.conf UserParameter=tcp_status_monitoring[*],/etc/zabbix/zabbix_agentd.d/tcp_status_monitoring.sh $1 $2 UserParameter=memcached_monitoring[*],/etc/zabbix/zabbix_agentd.d/memcached_monitoring.sh $1 $2 $3 [root@node201.yinzhengjie.org.cn ~]#
5>.重启zabbix agent服务使得配置生效
[root@node201.yinzhengjie.org.cn ~]# systemctl restart zabbix-agent.service
三.zabbix WebUI配置
1>.在zabbix server测试是否可以获取memcached的监控数据
[root@zabbix203.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/zabbix/bin/zabbix_get -s node201.yinzhengjie.org.cn -p 10050 -k "memcached_monitoring[memcached_status 11211 curr_connections]" 10 [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]# /yinzhengjie/softwares/zabbix/bin/zabbix_get -s node201.yinzhengjie.org.cn -p 10050 -k "memcached_monitoring[memcached_status 11211 reserved_fds]" 20 [root@zabbix203.yinzhengjie.org.cn ~]#
2>.自定义memcached的模板
3>.自定义memecached模板监控项图形
4>.自定义触发器
5>.将自定义的memcached模板和对应的zabbix agent主机进行关联
6>.查看图形