簡單shell腳本結合curl檢查nginx服務器狀態

本地搭建了虛擬主機,git.comhtml

圖片描述

說一下shell中curl幾個經常使用參數,curl實在太強大。可經過man curl查看詳細信息。nginx

-m 最大傳輸時間
-s slient輸出
-w 格式化輸出
-o 保留到文件,-O能夠保留文件名字
-i i是輸入http頭部和內容
-I -I是隻是輸出HTTP頭部git

其餘curl參數詳解shell

curl -m 5 -s -w %{http_code}-o /home/index.html git.com
    
    HTTP/1.1 200 OK
    Server: nginx/1.4.6 (Ubuntu)
    Date: Tue, 05 Jan 2016 03:22:12 GMT
    Content-Type: text/html
    Content-Length: 24
    Last-Modified: Wed, 30 Dec 2015 06:05:47 GMT
    Connection: keep-alive
    ETag: "5683743b-18"
    Accept-Ranges: bytes


200

root@tb:/home/tb250# curl -m 5 -s -w %{http_code} -i git.comvim

HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 05 Jan 2016 03:23:00 GMT
Content-Type: text/html
Content-Length: 24
Last-Modified: Wed, 30 Dec 2015 06:05:47 GMT
Connection: keep-alive
ETag: "5683743b-18"
Accept-Ranges: bytes

hello git

hello git 10

200

vim check_nginx_server.shbash

#!/bin/bash
NginxServer='git.com'
Check_Nginx_Server()
{

    http_status_code=$(curl -m 5 -s -i  -w %{http_code} -o/home/index.html $NginxServer)
    if [ $http_status_code -eq 000 -o $http_status_code -ge 500 ];then
            echo "check http server error \nhttp_status_code is"  $http_status_code
    else
            http_content=$(curl -s ${NginxServer}) 
            echo "service status ok\n"$http_content
           
    fi

}

Check_Nginx_Server

測試nginx開啓和停用狀態對此腳本影響curl

root@tb:/home/tb250# sh  check_server.sh 
service status ok
hello git hello git 10
root@tb:/home/tb250# service nginx stop
root@tb:/home/tb250# sh  check_server.sh 
check http server error 
http_status_code is 000
root@tb:/home/tb250# service nginx start
root@tb:/home/tb250# sh  check_server.sh 
service status ok
hello git hello git 10
相關文章
相關標籤/搜索