背景: redis通过shell脚本进行监控,没有问题,应用报警连不上redis,此时需要通过php模拟web环境进行redis的操作来确认web服务器是否能正常和redis通信 1.配置nginx,让监控redis的php脚本能通过url访问到redis # nginx.conf的配置修改 location ~ ^/php { root /data/www/html; location ~ php(.+.php)(.*)$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; expires -1; if ($request_uri ~ "^(.*)(?.*)$") { set $path_info $1; } fastcgi_param PATH_INFO $path_info; set $path_info $request_uri; } index watchphp.php; } 2.编写监控脚本 # mkdir -p /data/www/html/php # vim /data/www/html/php/redis_monitor.php <?php /* redis_monitor.php author:reblue520 通过类似 http://127.0.0.1/php/eus_redis_monitor.php?func_name='store_redis' php web的访问来判断redis的可用性 */ //通过获取/data/www/machine_no/.machine_no获取机房进行redis的判断 //$machine_no = trim(file_get_contents("/data/www/machine_no/.machine_no")); //empty($machineNo) && $machine_no = 'ws'; //echo "machineno=$machine_no"; // 获取机房编号 /* $file_path = "/data/www/machine_no/.machine_no"; if(file_exists($file_path)) { $machine_no = file_get_contents($file_path); $machine_no= str_replace(" ","<br />",$machine_no); if (empty($machine_no)){ $machine_no = 'ws'; } } */ function conn_redis($redis_domain, $redis_port, $redis_pass){ //创建 Redis 对象 $redis = new Redis(); // global $redis; $redis->connect($redis_domain, $redis_port); $redis->auth($redis_pass); //设置 redis 字符串数据,并返回该设置的字符串 $redis->set("tutorial-name", "Redis-tutorial"); $result = $redis->get("tutorial-name"); return $result; } function eus_store_redis() { global $machine_no; $res = conn_redis('store-cart.chinasoft.com', 6379, 'pass'); if ($res == "Redis-tutorial") { echo "store_success"; }else { echo "store_fail"; } } function ws_store_redis() { global $machine_no; $res = conn_redis('cart.chinasoft.com', 6379, 'pass'); if ($res == "Redis-tutorial") { echo "store_success"; }else { echo "store_fail"; } } function platform_redis() { $res = conn_redis('cbs_out.chinasoft.com', 9376, 'pass'); if ($res == "Redis-tutorial") { echo "platform_success"; }else { echo "platform_fail"; } } function cbs_redis() { $res = conn_redis('cbs_out.chinasoft.com', 9376, 'pass'); if ($res == "Redis-tutorial") { echo "cbs_success"; }else { echo "cbs_fail"; } } // 通过get参数获取不同的应用redis $func_name = $_GET['func_name']; if ($func_name == "store_redis") { eus_store_redis(); }elseif($func_name == "platform_redis"){ platform_redis(); }elseif($func_name == "cbs_redis"){ cbs_redis(); } ?> 3.监控的配置 [root@zabbix:/data/www/html/php]# cat /usr/local/zabbix_agents_3.2.0/conf/zabbix_agentd/redis_monitor.conf UserParameter=redis.store_redis,/usr/bin/curl -s http://127.0.0.1/php/eus_redis_monitor.php?func_name='store_redis' UserParameter=redis.cbs_redis,/usr/bin/curl -s http://127.0.0.1/php/eus_redis_monitor.php?func_name='cbs_redis' UserParameter=redis.platform_redis,/usr/bin/curl -s http://127.0.0.1/php/eus_redis_monitor.php?func_name='platform_redis' 4.模板
模板
<?xml version="1.0" encoding="UTF-8"?> <zabbix_export> <version>3.2</version> <date>2019-01-30T01:43:08Z</date> <groups> <group> <name>Templates</name> </group> </groups> <templates> <template> <template>redis_php_monitor</template> <name>redis_php_monitor</name> <description/> <groups> <group> <name>Templates</name> </group> </groups> <applications/> <items> <item> <name>cbs_redis</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>redis.cbs_redis</key> <delay>180</delay> <history>90</history> <trends>0</trends> <status>0</status> <value_type>1</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex>120/1-7,00:00-24:00</delay_flex> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications/> <valuemap/> <logtimefmt/> </item> <item> <name>platform_redis</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>redis.platform_redis</key> <delay>180</delay> <history>90</history> <trends>0</trends> <status>0</status> <value_type>1</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex>120/1-7,00:00-24:00</delay_flex> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications/> <valuemap/> <logtimefmt/> </item> <item> <name>store_redis</name> <type>0</type> <snmp_community/> <multiplier>0</multiplier> <snmp_oid/> <key>redis.store_redis</key> <delay>180</delay> <history>90</history> <trends>0</trends> <status>0</status> <value_type>1</value_type> <allowed_hosts/> <units/> <delta>0</delta> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <formula>1</formula> <delay_flex>120/1-7,00:00-24:00</delay_flex> <params/> <ipmi_sensor/> <data_type>0</data_type> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications/> <valuemap/> <logtimefmt/> </item> </items> <discovery_rules/> <httptests/> <macros/> <templates/> <screens/> </template> </templates> <triggers> <trigger> <expression>{redis_php_monitor:redis.cbs_redis.str(cbs_success)}=0</expression> <recovery_mode>0</recovery_mode> <recovery_expression/> <name>redis.cbs connection error</name> <correlation_mode>0</correlation_mode> <correlation_tag/> <url/> <status>0</status> <priority>0</priority> <description/> <type>0</type> <manual_close>0</manual_close> <dependencies/> <tags/> </trigger> <trigger> <expression>{redis_php_monitor:redis.platform_redis.str(platform_success)}=0</expression> <recovery_mode>0</recovery_mode> <recovery_expression/> <name>redis.platform connection error</name> <correlation_mode>0</correlation_mode> <correlation_tag/> <url/> <status>0</status> <priority>0</priority> <description/> <type>0</type> <manual_close>0</manual_close> <dependencies/> <tags/> </trigger> <trigger> <expression>{redis_php_monitor:redis.store_redis.str(store_success)}=0</expression> <recovery_mode>0</recovery_mode> <recovery_expression/> <name>redis.store connection error</name> <correlation_mode>0</correlation_mode> <correlation_tag/> <url/> <status>0</status> <priority>0</priority> <description/> <type>0</type> <manual_close>0</manual_close> <dependencies/> <tags/> </trigger> </triggers> </zabbix_export>