主动模式

主动模式参考:​​https://blog.51cto.com/u_14814545/4869452​

1.memcached

apt-get  install memcached 
systemctl start memcached #端口11211

apt-get install nmap
#命令测试获取监控值
echo -e "stats\nquit" | nc 127.0.0.1 11211
root@ubuntu:~# echo -e "stats\nquit" | nc 127.0.0.1 11211 | grep "total_connections" | awk '{print $3}'

1.1.配置文件修改

root@ubuntu:/etc/zabbix/zabbix_agentd.d# ls
memcached.conf memcached.sh

#在主配置文件打开,默认是打开的
vim /etc/zabbix/zabbix_agentd.conf
------------------------------------------------------
Include=/etc/zabbix/zabbix_agentd.d/*.conf
------------------------------------------------------

vim /etc/zabbix/zabbix_agentd.d/memcached.conf
-------------------------------------------------------------------------------------
UserParameter=memcached_status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/memcached.sh $1 $2 $3
-------------------------------------------------------------------------------------
memcached.sh 这个脚本是加了执行权限的,但是为啥前面要加上/bin/bash,zabbix_get才能获取到值??

1.2.监控脚本

#!/bin/bash
memcached_status(){
M_PORT=$1
M_COMMAND=$2
echo -e "stats\nquit" | nc 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
chmod a+x memcached.sh
#测试脚本
root@ubuntu:/etc/zabbix/zabbix_agentd.d# bash memcached.sh memcached_status 11211 total_connections

#在[192.168.47.104测试]
/apps/zabbix_agent/bin/zabbix_get -s 192.168.47.105 -p 10050 -k "memcached_status[memcached_status,11211,total_connections]"

zabbix-redis/memcache监控_zabbix-redis/memcach

1.3.模板

<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>4.0</version>
<date>2019-04-15T10:15:11Z</date>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<templates>
<template>
<template>memcached</template>
<name>memcached-zabbix4.2</name>
<description/>
<groups>
<group>
<name>Templates</name>
</group>
</groups>
<applications>
<application>
<name>memcached</name>
</application>
</applications>
<items>
<item>
<name>memcached-curr_connections</name>
<type>7</type>
<snmp_community/>
<snmp_oid/>
<key>memcached_status["memcached_status","11211","curr_connections"]</key>
<delay>5s</delay>
<history>30d</history>
<trends>365d</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<params/>
<ipmi_sensor/>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>memcached</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<preprocessing/>
<jmx_endpoint/>
<timeout>3s</timeout>
<url/>
<query_fields/>
<posts/>
<status_codes>200</status_codes>
<follow_redirects>1</follow_redirects>
<post_type>0</post_type>
<http_proxy/>
<headers/>
<retrieve_mode>0</retrieve_mode>
<request_method>0</request_method>
<output_format>0</output_format>
<allow_traps>0</allow_traps>
<ssl_cert_file/>
<ssl_key_file/>
<ssl_key_password/>
<verify_peer>0</verify_peer>
<verify_host>0</verify_host>
<master_item/>
</item>
<item>
<name>memcached_threads</name>
<type>7</type>
<snmp_community/>
<snmp_oid/>
<key>memcached_status["memcached_status","11211","threads"]</key>
<delay>5s</delay>
<history>30d</history>
<trends>365d</trends>
<status>0</status>
<value_type>3</value_type>
<allowed_hosts/>
<units/>
<snmpv3_contextname/>
<snmpv3_securityname/>
<snmpv3_securitylevel>0</snmpv3_securitylevel>
<snmpv3_authprotocol>0</snmpv3_authprotocol>
<snmpv3_authpassphrase/>
<snmpv3_privprotocol>0</snmpv3_privprotocol>
<snmpv3_privpassphrase/>
<params/>
<ipmi_sensor/>
<authtype>0</authtype>
<username/>
<password/>
<publickey/>
<privatekey/>
<port/>
<description/>
<inventory_link>0</inventory_link>
<applications>
<application>
<name>memcached</name>
</application>
</applications>
<valuemap/>
<logtimefmt/>
<preprocessing/>
<jmx_endpoint/>
<timeout>3s</timeout>
<url/>
<query_fields/>
<posts/>
<status_codes>200</status_codes>
<follow_redirects>1</follow_redirects>
<post_type>0</post_type>
<http_proxy/>
<headers/>
<retrieve_mode>0</retrieve_mode>
<request_method>0</request_method>
<output_format>0</output_format>
<allow_traps>0</allow_traps>
<ssl_cert_file/>
<ssl_key_file/>
<ssl_key_password/>
<verify_peer>0</verify_peer>
<verify_host>0</verify_host>
<master_item/>
</item>
</items>
<discovery_rules/>
<httptests/>
<macros/>
<templates/>
<screens/>
</template>
</templates>
<triggers>
<trigger>
<expression>{memcached:memcached_status["memcached_status","11211","curr_connections"].last()}>12</expression>
<recovery_mode>0</recovery_mode>
<recovery_expression/>
<name>memcached-curr_connections</name>
<correlation_mode>0</correlation_mode>
<correlation_tag/>
<url/>
<status>0</status>
<priority>4</priority>
<description/>
<type>0</type>
<manual_close>0</manual_close>
<dependencies/>
<tags/>
</trigger>
<trigger>
<expression>{memcached:memcached_status["memcached_status","11211","threads"].last()}>5</expression>
<recovery_mode>0</recovery_mode>
<recovery_expression/>
<name>memcached_线程数</name>
<correlation_mode>0</correlation_mode>
<correlation_tag/>
<url/>
<status>0</status>
<priority>4</priority>
<description/>
<type>0</type>
<manual_close>0</manual_close>
<dependencies/>
<tags/>
</trigger>
</triggers>
<graphs>
<graph>
<name>memcached-curr_connections</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>1</show_work_period>
<show_triggers>1</show_triggers>
<type>0</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>0</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>0</drawtype>
<color>1A7C11</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>memcached</host>
<key>memcached_status["memcached_status","11211","curr_connections"]</key>
</item>
</graph_item>
</graph_items>
</graph>
<graph>
<name>memcached_threads</name>
<width>900</width>
<height>200</height>
<yaxismin>0.0000</yaxismin>
<yaxismax>100.0000</yaxismax>
<show_work_period>1</show_work_period>
<show_triggers>1</show_triggers>
<type>0</type>
<show_legend>1</show_legend>
<show_3d>0</show_3d>
<percent_left>0.0000</percent_left>
<percent_right>0.0000</percent_right>
<ymin_type_1>0</ymin_type_1>
<ymax_type_1>0</ymax_type_1>
<ymin_item_1>0</ymin_item_1>
<ymax_item_1>0</ymax_item_1>
<graph_items>
<graph_item>
<sortorder>0</sortorder>
<drawtype>0</drawtype>
<color>1A7C11</color>
<yaxisside>0</yaxisside>
<calc_fnc>2</calc_fnc>
<type>0</type>
<item>
<host>memcached</host>
<key>memcached_status["memcached_status","11211","threads"]</key>
</item>
</graph_item>
</graph_items>
</graph>
</graphs>
</zabbix_export>

1.4.把模板导入及查看

zabbix-redis/memcache监控_zabbix-redis/memcach_02zabbix-redis/memcache监控_zabbix-redis/memcach_03zabbix-redis/memcache监控_zabbix-redis/memcach_04zabbix-redis/memcache监控_zabbix-redis/memcach_05

2.redis

2.1.安装

apt-get install redis
systemctl start redis
systemctl enable redis

#获取监控值脚本
root@ubuntu:/etc/zabbix/zabbix_agentd.d# pwd
/etc/zabbix/zabbix_agentd.d

#获取监控值
root@ubuntu:/etc/zabbix/zabbix_agentd.d# echo -e "INFO\n quit" | nc 127.0.0.1 "6379" |grep -w used_cpu_sys |awk -F":" '{print $2}'
0.27

脚本里面用nc命令就一直卡住,换成ncat命令后可以了,后面需要了解下这是为啥

2.2.获取监控值脚本

redis.sh

#!/bin/bash
redis_status(){
R_PORT=$1
R_COMMAND=$2
(echo -en "INFO \r\n";sleep 1;) | ncat 127.0.0.1 "$R_PORT" > /etc/zabbix/zabbix_agentd.d/redis_"$R_PORT".tmp
REDIS_STAT_VALUE=$(grep ""$R_COMMAND":" /etc/zabbix/zabbix_agentd.d/redis_"$R_PORT".tmp| cut -d ':' -f2)
echo $REDIS_STAT_VALUE
}
help(){
echo "${0} + redis_status+ PORT + COMMAND"
}
main(){
case $1 in
redis_status)
redis_status $2 $3
;;
*)
help
;;
esac
}
main $1 $2 $3
chmod +x redis.sh
#测试
bash redis.sh redis_status 6379 used_cpu_sys

zabbix-redis/memcache监控_zabbix-redis/memcach_06

2.3.测试

在[192.168.47.104测试]
chown zabbix.zabbix /etc/zabbix/zabbix_agentd.d/redis_6379.tmp
/apps/zabbix_agent/bin/zabbix_get -s 192.168.47.105 -p 10050 -k "redis_status[redis_status,6379,used_cpu_sys]"
/apps/zabbix_agent/bin/zabbix_get -s 192.168.47.105 -p 10050 -k "redis_status[redis_status,6379,connected_clients]"

在[192.168.47.102测试]
/usr/local/zabbix/bin/zabbix_get -s 192.168.47.105 -p 10050 -k "redis_status[redis_status,6379,used_cpu_sys]"
/usr/local/zabbix/bin/zabbix_get -s 192.168.47.105 -p 10050 -k "redis_status[redis_status,6379,connected_clients]"

zabbix-redis/memcache监控_zabbix-redis/memcach_07

2.4.自定义模板

zabbix-redis/memcache监控_zabbix-redis/memcach_08zabbix-redis/memcache监控_zabbix-redis/memcach_09zabbix-redis/memcache监控_zabbix-redis/memcach_10zabbix-redis/memcache监控_zabbix-redis/memcach_11zabbix-redis/memcache监控_zabbix-redis/memcach_12zabbix-redis/memcache监控_zabbix-redis/memcach_13