使用 curl 对网页进行健康检查

pal*_*lto 50 monitoring curl

我想通过调用特定的 url 来对服务进行健康检查。感觉最简单的解决方案是使用 cron 每分钟左右进行一次检查。如果出现错误,cron 会向我发送电子邮件。

我尝试为此使用 cUrl,但我无法让它仅在出现错误时输出消息。如果我尝试将输出定向到 /dev/null,它会打印出进度报告。

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  5559  100  5559    0     0   100k      0 --:--:-- --:--:-- --:--:--  106k
Run Code Online (Sandbox Code Playgroud)

我尝试查看 curl 选项,但我找不到任何适合您希望它对成功保持沉默但对错误发出噪音的情况的任何内容。

有没有办法让 curl 做我想做的事,或者我应该看看其他一些工具吗?

ahi*_*end 55

怎么样-sSf?从手册页:

  -s/--silent
     Silent or quiet mode. Do not show progress meter or error messages.  
     Makes Curl mute.

  -S/--show-error
     When used with -s it makes curl show an error message if it fails.

  -f/--fail
     (HTTP)  Fail silently (no output at all) on server errors. This is mostly
     done to better enable scripts etc to better deal with failed attempts. In
     normal  cases  when a HTTP server fails to deliver a document, it returns
     an HTML document stating so (which often also describes  why  and  more).
     This flag will prevent curl from outputting that and return error 22.

     This method is not fail-safe and there are occasions where non-successful
     response codes will  slip  through,  especially  when  authentication  is
     involved (response codes 401 and 407).
Run Code Online (Sandbox Code Playgroud)

例如:

curl -sSf http://example.org > /dev/null
Run Code Online (Sandbox Code Playgroud)

  • -sS 由于某种原因没有输出错误信息。我还必须添加 -f 。正确的工作命令似乎是 `curl -fsS http://example.org > /dev/null`。当没有任何问题时,它不会输出任何内容,但它会在错误时打印状态代码,这对我来说很好。 (3认同)
  • 好的,添加了“-f”以供将来参考。 (2认同)

Raz*_*aza 26

我认为对于检查站点是否还活着的最简单方法,您可以使用以下方法:

curl -Is http://www.google.com | head -n 1
Run Code Online (Sandbox Code Playgroud)

这将返回HTTP/1.1 200 OK。如果返回与您的输出不匹配,则呼叫帮助。

  • 检查状态代码可能像其他提案一样更有意义 (2认同)

jof*_*fel 9

您需要-s标志(静默)、-f标志(因错误退出代码而失败)并且可以使用该-o标志来重定向输出:

curl www.websiteToTest.com -s -f -o /dev/null || echo "Website down." | mail -s "Website is down" admin@thesite.com 
Run Code Online (Sandbox Code Playgroud)

这只是一个简单的 cron 脚本的坏例子。通常,如果网站关闭,您只想收到一封邮件。


小智 9

您可以从 curl 捕获网络计时统计信息。请求/响应周期中每个阶段的延迟可用于确定健康状况。

$ URL=https://example.com
$ curl "$URL" -s -o /dev/null -w \
> "response_code: %{http_code}\n
> dns_time: %{time_namelookup}
> connect_time: %{time_connect}
> pretransfer_time: %{time_pretransfer}
> starttransfer_time: %{time_starttransfer}
> total_time: %{time_total}
> "
response_code: 200

dns_time: 0.029
connect_time: 0.046
pretransfer_time: 0.203
starttransfer_time: 0.212
total_time: 0.212
Run Code Online (Sandbox Code Playgroud)


小智 7

Curl 有非常具体的退出状态代码
为什么不直接检查这些代码呢?

#!/bin/bash

##name: site-status.sh

FAIL_CODE=6

check_status(){
    LRED="\033[1;31m" # Light Red
    LGREEN="\033[1;32m" # Light Green
    NC='\033[0m' # No Color


    curl -sf "${1}" > /dev/null

    if [ ! $? = ${FAIL_CODE} ];then
        echo -e "${LGREEN}${1} is online${NC}"
    else
        echo -e "${LRED}${1} is down${NC}"
    fi
}


check_status "${1}"
Run Code Online (Sandbox Code Playgroud)

用法:

$ site-status.sh example.com
Run Code Online (Sandbox Code Playgroud)

结果:

$ example.com is online
Run Code Online (Sandbox Code Playgroud)

笔记:

该脚本仅检查站点是否可以解析。

如果您只关心网站的运行或关闭,则此代码应该可以帮助您。
但是,如果您对 if/else 块进行一些更改,则可以根据需要轻松测试其他状态代码