zabbix3.2利用自动发现功能对fastcgi模式的php状态进行集中监控
前端nginx虚拟主机引用后端多个php接口,为了方便监控,将后端服务器集中配置在nginx中,具体配置如下:[root@eus_nginx:]# cat /usr/local/nginx/config/nginx.confuser apache users;worker_processes 4;worker_cpu_affinity 0001 0010 0100 1000; error_log /data/www/logs/nginx_log/error/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;pid logs/nginx.pid;events { use epoll; worker_connections 10240;}http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - - [$time_local] - - "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_time '; log_format error '$remote_addr - - [$time_local] - - "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $request_time '; log_format test '[$fastcgi_script_name] [$time_local] $document_root $remote_addr $status $request_time $body_bytes_sent "$request" "$http_referer" $upstream_addr $http_x_real_ip $http_x_forwarded_for $http_user_agent '; log_format timed_combined '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_x_real_ip" "$http_x_forwarded_for" "$http_user_agent" ' '$request_time $upstream_response_time $pipe'; #access_log /data/www/logs/nginx_log/access/access.log main; access_log off; sendfile on; #tcp_nopush on; tcp_nodelay on; keepalive_timeout 70; #----for upload file client_max_body_size 8M; client_body_buffer_size 2M; #--- for resolve 400 error client_header_buffer_size 64k; large_client_header_buffers 4 64k; proxy_connect_timeout 60s; proxy_read_timeout 60s; #60s内后端服务器需要返回成功 proxy_send_timeout 60s; proxy_buffer_size 16k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $http_x_forwarded_for; gzip on; gzip_vary off; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 3; gzip_disable "MSIE [1-6]\."; gzip_types text/plain text/css text/javascript application/javascript application/x-javascript text/xml application/xml; fastcgi_intercept_errors on; ssi on; ssi_silent_errors on; #expires 30d; server_names_hash_bucket_size 20480; # -------------- firewall ---------- include blackip.conf; geo $whiteiplist { default 1; include whiteip.conf; } map $whiteiplist $limit { 1 $binary_remote_addr; 0 ""; } limit_req_zone $binary_remote_addr zone=perip:10m rate=10r/s; limit_req_zone $server_name zone=perserver:1024m rate=1000r/s; limit_req_log_level notice ; server { listen 80; server_name localhost; location / { root /data/www/html; index index.html index.htm; } location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } # 集中后端php的状态 location ~ ^/(back02_status|back02_ping)$ { include fastcgi_params; fastcgi_pass 172.20.103.198:9000; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; } location ~ ^/(back03_status|back03_ping)$ { include fastcgi_params; fastcgi_pass 172.20.103.196:9000; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; } location ~ ^/(back04_status|back04_ping)$ { include fastcgi_params; fastcgi_pass 172.20.103.194:9000; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; } location ~ ^/(back05_status|back05_ping)$ { include fastcgi_params; fastcgi_pass 172.20.103.195:9000; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; } location ~ ^/(back06_status|back06_ping)$ { include fastcgi_params; fastcgi_pass 172.20.103.192:9000; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; } location ~ ^/(back07_status|back07_ping)$ { include fastcgi_params; fastcgi_pass 172.20.103.193:9000; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; } #error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } include vhost.d/*; }
1.编写自动发现脚本
[root@eus_nginx:/usr/local/zabbix_agents_3.2.0/scripts]# cat back_php_status.sh #!/bin/bash # function:monitor php connect status from zabbix web_ip_discovery () { WEB_IP=($(cat /usr/local/zabbix_agents_3.2.0/scripts/php_backs.txt|grep -v "^#")) printf '{\n' printf '\t"data":[\n' for((i=0;i<${#WEB_IP[@]};++i)) { num=$(echo $((${#WEB_IP[@]}-1))) if [ "$i" != ${num} ]; then printf "\t\t{ \n" printf "\t\t\t\"{#SITENAME}\":\"${WEB_IP[$i]}\"},\n" else printf "\t\t{ \n" printf "\t\t\t\"{#SITENAME}\":\"${WEB_IP[$num]}\"}]}\n" fi } } web_site_code () {ip=`echo $1|awk -F ':' '{print $1}'`#echo $ipcurl -s -k --connect-timeout 10 -m 4 -o /dev/null -w %{http_code} "http://${ip}"} case "$1" inweb_ip_discovery) web_ip_discovery ;;web_site_code) web_site_code $2;; *) echo "Usage:$0 {web_ip_discovery|web_site_code [URL]}" ;; esac
2.需要监控的php连接
[root@eus_nginx:/usr/local/zabbix_agents_3.2.0/scripts]# cat php_backs.txt 127.0.0.1/back02_status:eus_chinasoft_back02127.0.0.1/back03_status:eus_chinasoft_back03127.0.0.1/back04_status:eus_chinasoft_back04127.0.0.1/back05_status:eus_chinasoft_back05127.0.0.1/back06_status:eus_chinasoft_back06127.0.0.1/back07_status:eus_chinasoft_back07172.20.73.207/back09_status:eus_chinasoft_back09172.20.73.207/back10_status:eus_chinasoft_back10172.20.73.207/back11_status:eus_chinasoft_back11172.20.73.207/back12_status:eus_chinasoft_back12172.20.73.207/back13_status:eus_chinasoft_back13172.20.73.207/back14_status:eus_chinasoft_back14 3.编写配置文件# vim /usr/local/zabbix_agents_3.2.0/conf/zabbix_agentd/php_status_ip_discovery.confUserParameter=web.ip.discovery,/usr/local/zabbix_agents_3.2.0/scripts/back_php_status.sh web_ip_discovery
UserParameter=web.ip.code[*],/usr/local/zabbix_agents_3.2.0/scripts/back_php_status.sh web_site_code $14.zabbix服务端验证
[root@aliyun-american-guigu-zabbix:~]# zabbix_get -s 1.1.1.1 -k 'web.ip.discovery'{ "data":[ { "{#SITENAME}":"127.0.0.1/back02_status:eus_chinasoft_back02"}, { "{#SITENAME}":"127.0.0.1/back03_status:eus_chinasoft_back03"}, { "{#SITENAME}":"127.0.0.1/back04_status:eus_chinasoft_back04"}, { "{#SITENAME}":"127.0.0.1/back05_status:eus_chinasoft_back05"}, { "{#SITENAME}":"127.0.0.1/back06_status:eus_chinasoft_back06"}, { "{#SITENAME}":"127.0.0.1/back07_status:eus_chinasoft_back07"}, { "{#SITENAME}":"172.20.73.207/back09_status:eus_chinasoft_back09"}, { "{#SITENAME}":"172.20.73.207/back10_status:eus_chinasoft_back10"}, { "{#SITENAME}":"172.20.73.207/back11_status:eus_chinasoft_back11"}, { "{#SITENAME}":"172.20.73.207/back12_status:eus_chinasoft_back12"}, { "{#SITENAME}":"172.20.73.207/back13_status:eus_chinasoft_back13"}, { "{#SITENAME}":"172.20.73.207/back14_status:eus_chinasoft_back14"}]}[root@aliyun-american-guigu-zabbix:~]# zabbix_get -s 1.1.1.1 -k 'web.ip.code[172.20.73.207/back14_status:eus_chinasoft_back14]'
200
服务端web的配置:
{monitor_back_php_status:web.ip.code[{#SITENAME},].max(#5)}<>200