背景: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 ",$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.confUserParameter=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.模板
模板
3.2 2019-01-30T01:43:08Z Templates redis_php_monitor redis_php_monitor Templates cbs_redis 0 0 redis.cbs_redis 180 90 0 0 1 0 0 0 0 1 120/1-7,00:00-24:00 0 0 0 platform_redis 0 0 redis.platform_redis 180 90 0 0 1 0 0 0 0 1 120/1-7,00:00-24:00 0 0 0 store_redis 0 0 redis.store_redis 180 90 0 0 1 0 0 0 0 1 120/1-7,00:00-24:00 0 0 0 {redis_php_monitor:redis.cbs_redis.str(cbs_success)}=0 0 redis.cbs connection error 0 0 0 0 0 {redis_php_monitor:redis.platform_redis.str(platform_success)}=0 0 redis.platform connection error 0 0 0 0 0 {redis_php_monitor:redis.store_redis.str(store_success)}=0 0 redis.store connection error 0 0 0 0 0