shell监测Http应用状态

获取IP地址:

# no param
get_ip() {
        #ifconfig -a| awk -F':' '{ if(NR==2){ print $2 } }'| cut -d' ' -f1
        IP=`ifconfig | grep -w inet | grep -v "127.0.0.1" | awk '{ print $2}'| tr -d "addr"`
        echo ${IP#*:} ## 兼容部分服务器ifconfig输出addr带":"问题
}

获取应用端口号:

# param: /home/wy/www/testtomcat.test.com/conf/server.xml
get_port() {
	[ $# -lt 1 -o "X"$1 == "X" ] && echo "" || echo `sudo sed -n '//!p' $1 | grep -E '
}

监测Http服务状态:

# param: /home/wy/www/testtomcat.test.com/conf/server.xml
get_http_status() {
        http_addr="http://$(get_ip):$(get_port $1)"
        status_code=`curl -m 5 -s -o /dev/null -w %{http_code} $http_addr`
        [ $status_code -ne 200 ] && echo "[ERROR]: http connection error: $http_addr -1" || echo "[INFO]: http connection is ok: $http_addr 0"
}

循环监测:

while : ;do
        get_http_status /home/wy/www/testtomcat.test.com/conf/server.xml
        sleep 1
done

查看输出结果:

[dev@192-168-50-143 ~]$ ./checkServerStatus.sh
[INFO]: http connection is ok: http://192.168.50.143:8005 0
[INFO]: http connection is ok: http://192.168.50.143:8005 0
[INFO]: http connection is ok: http://192.168.50.143:8005 0
[INFO]: http connection is ok: http://192.168.50.143:8005 0

你可能感兴趣的:(shell,http,shell,linux)