本文共 5332 字,大约阅读时间需要 17 分钟。
openfalcon 监控 nginx 状态
主要使用 通过 agent push 数据至 server
由于我这边有两套nginx 需要计算综合
在默认主机里面加上location或者你希望能访问到的主机里面。
server { listen *:80 default_server; server_name _; location /ngx_status { stub_status on; access_log off; #allow 127.0.0.1; #deny all; }}
curl http://127.0.0.1/ngx_statusActive connections: 11921server accepts handled requests 11989 11989 11991Reading: 0 Writing: 7 Waiting: 42
#!/bin/bashnginx_17=${ 1}nginx_18=${ 2}nginx_status_name=${ 3}nginx_active_connections(){ active_connections_17=`curl -s http://${ nginx_17}:18190/nginx_status | grep connections | awk '{print $3}'` active_connections_18=`curl -s http://${ nginx_18}:18190/nginx_status | grep connections | awk '{print $3}'` let active_connections=($active_connections_17+$active_connections_18) echo $active_connections}nginx_reading(){ reading_17=`curl -s http://${ nginx_17}:18190/nginx_status | grep Reading | awk '{print $2}'` reading_18=`curl -s http://${ nginx_18}:18190/nginx_status | grep Reading | awk '{print $2}'` let reading=($reading_17+$reading_18) echo $reading}nginx_writing(){ writing_17=`curl -s http://${ nginx_17}:18190/nginx_status | grep Reading | awk '{print $4}'` writing_18=`curl -s http://${ nginx_18}:18190/nginx_status | grep Reading | awk '{print $4}'` let writing=($writing_17+$writing_18) echo $writing}nginx_waiting(){ waiting_17=`curl -s http://${ nginx_17}:18190/nginx_status | grep Reading | awk '{print $6}'` waiting_18=`curl -s http://${ nginx_18}:18190/nginx_status | grep Reading | awk '{print $6}'` let waiting=($waiting_17+$waiting_18) echo $waiting}case "$nginx_status_name" innginx_active_connections)nginx_active_connections;;nginx_reading)nginx_reading;;nginx_writing)nginx_writing;;nginx_waiting)nginx_waiting;;*)printf 'Usage: %s {nginx_active_connections|nginx_reading|nginx_writing|nginx_waiting}n' "$prog"exit 1;;esac
需要注意 运行python及shell 需要在同一台服务器
同时需要能访问nginx_status 端口权限
#!-*- coding:utf8 -*-import osimport requestsimport timeimport jsonimport socket## 环境变量hostname = socket.gethostname()nginx_17_ip = "IP" ## nginx服务器IPnginx_18_ip = "IP"def nginx_active_connections(nginx17_ip,nginx_18_ip): os.environ['nginx_17_ip']=str(nginx_17_ip) os.environ['nginx_18_ip']=str(nginx_18_ip) connections_tmp =os.popen('/root/scripts/nginx_status.sh $nginx_17_ip $nginx_18_ip nginx_active_connections') connections = connections_tmp.read() #print(nginx_active_connections) return connectionsdef nginx_reading(nginx17_ip,nginx_18_ip): os.environ['nginx_17_ip']=str(nginx_17_ip) os.environ['nginx_18_ip']=str(nginx_18_ip) nginx_reading_tmp = os.popen('/root/scripts/nginx_status.sh $nginx_17_ip $nginx_18_ip nginx_reading') nginx_reading = nginx_reading_tmp.read() return nginx_readingdef nginx_writing(nginx17_ip,nginx_18_ip): os.environ['nginx_17_ip']=str(nginx_17_ip) os.environ['nginx_18_ip']=str(nginx_18_ip) nginx_writing_tmp = os.popen('/root/scripts/nginx_status.sh $nginx_17_ip $nginx_18_ip nginx_writing') nginx_writing = nginx_writing_tmp.read() return nginx_writingdef nginx_waiting(nginx17_ip,nginx_18_ip): os.environ['nginx_17_ip']=str(nginx_17_ip) os.environ['nginx_18_ip']=str(nginx_18_ip) nginx_waiting_tmp = os.popen('/root/scripts/nginx_status.sh $nginx_17_ip $nginx_18_ip nginx_waiting') nginx_waiting = nginx_waiting_tmp.read() return nginx_waitingnginx_active_count = int(nginx_active_connections(nginx_17_ip,nginx_18_ip))nginx_reading_count = int(nginx_reading(nginx_17_ip,nginx_18_ip))nginx_writing_count = int(nginx_writing(nginx_17_ip,nginx_18_ip))nginx_waiting_count = int(nginx_waiting(nginx_17_ip,nginx_18_ip))nginx_waiting_count = int(nginx_waiting(nginx_17_ip,nginx_18_ip))ts = int(time.time())payload = [ { "endpoint": hostname, "metric": "nginx.active.connections", "timestamp": ts, "step": 60, "value": nginx_active_count, "counterType": "GAUGE", "tags": "", }, { "endpoint": hostname, "metric": "nginx.reading", "timestamp": ts, "step": 60, "value": nginx_reading_count, "counterType": "GAUGE", "tags": "", }, { "endpoint": hostname, "metric": "nginx.writing", "timestamp": ts, "step": 60, "value": nginx_writing_count, "counterType": "GAUGE", "tags": "", }, { "endpoint": hostname, "metric": "nginx.waiting", "timestamp": ts, "step": 60, "value": nginx_waiting_count, "counterType": "GAUGE", "tags": "", },]r = requests.post("http://127.0.0.1:1988/v1/push", data=json.dumps(payload))print r.text
通过dashboard 查询相关数据
Screen
Grafana
转载地址:http://uuevo.baihongyu.com/