监控nginx脚本
监控nginx脚本:
本脚本功能:
- 支持nginx服务状态数据显示
- 支持运行的web应用健康检测
#!/bin/bash
#********************************************************************
# File Name: ngx.sh
# Version: V1.0
# Author: dahuangji
# Email:
# Created Time : 2022-02-19 02:13:16
# Description:
#********************************************************************
RED='\E[31;1m'
GREEN='\E[32;1m'
END='\E[0m'
n_port=$1
n_state=$2
ngx_state(){
curl 127.0.0.1:$n_port/ngx_status 2>/dev/null|egrep -io "$n_state: [[:digit:]]*"|cut -d ' ' -f2
}
ngx_accepts(){
curl 127.0.0.1:$n_port/ngx_status 2>/dev/null|awk 'NR==3{print $1}'
}
ngx_handled(){
curl 127.0.0.1:$n_port/ngx_status 2>/dev/null|awk 'NR==3{print $2}'
}
ngx_requests(){
curl 127.0.0.1:$n_port/ngx_status 2>/dev/null|awk 'NR==3{print $3}'
}
ngx_active(){
curl 127.0.0.1/ngx_status 2>/dev/null|awk 'NR==1{print $3}'
}
web_status(){
n_code=`curl 127.0.0.1:$n_port/index.html -I 2>/dev/null|awk 'NR==1{print $2}'`
if [[ $n_code = 20* ]] ;then
echo 1
elif [[ $n_code = 40* ]] ;then
echo $n_code
elif [[ $n_code = 50* ]] ;then
echo $n_code
fi
}
case $2 in
active)
ngx_active ;;
accepts)
ngx_accepts ;;
handled)
ngx_handled ;;
requests)
ngx_requests ;;
reading)
ngx_state ;;
writing)
ngx_state ;;
waiting)
ngx_state ;;
web_status)
web_status ;;
*)
echo -e "$RED必须给定nginx信息(大小写不敏感) $END"
echo "----------------------------------"
echo "用法: $0 端口 状态"
echo "示例: $0 80 requests"
;;
esac