使用curl来监控动态页面的可用性
curl是一个命令行下的http下载工具,类似wget。与wget相似,它也可以通过发送指定的http header到服务器来判断服务的状态。
这里介绍一个使用curl监控页面可用性的方法。
可以使用下面的命令,来采集页面的状态码。如果这条命令返回结果为200,说明服务正常。如果返回的是其他的页面,说明异常。
curl -o /dev/null -s -w %{http_code} http://zys.8800.org/
-o 参数,是把下载的所有内容都重定向到/dev/null,-s命令,是屏蔽了curl本身的输出,而-w参数,是根据我们自己的需要,自定义了curl的输出格式。
使用这条命令,再配合邮件和短信,就可以实现对页面的可用性监控。将这个程序部署在全国各地的机器上,就可以对cdn网络进行可用性监控。
扶凯的blog上还介绍了一个使用curl来测试页面打开时间的方法,基本思路和这个基本一致。只是write-out的参数不同。可以供参考
PS: 这里附上所有的write-out参数。网上的文档大多直接介绍使用-w参数,并没有介绍-w参数都可以采集哪些方面的数据。
The variables available at this point are:
url_effective The URL that was fetched last. This is most mean- ingful if you've told curl to follow location: headers.
http_code The numerical response code that was found in the last retrieved HTTP(S) or FTP(s) transfer. In 7.18.2 the alias response_code was added to show the same info.
http_connect The numerical code that was found in the last response (from a proxy) to a curl CONNECT request. (Added in 7.12.4)
time_total The total time, in seconds, that the full opera- tion lasted. The time will be displayed with mil- lisecond resolution.
time_namelookup The time, in seconds, it took from the start until the name resolving was completed. time_connect The time, in seconds, it took from the start until the TCP connect to the remote host (or proxy) was completed.
time_appconnect The time, in seconds, it took from the start until the SSL/SSH/etc connect/handshake to the remote host was completed. (Added in 7.19.0) time_pretransfer The time, in seconds, it took from the start until the file transfer was just about to begin. This includes all pre-transfer commands and nego- tiations that are specific to the particular pro- tocol(s) involved. time_redirect The time, in seconds, it took for all redirection steps include name lookup, connect, pretransfer and transfer before the final transaction was started. time_redirect shows the complete execu- tion time for multiple redirections. (Added in 7.12.3)
time_starttransfer The time, in seconds, it took from the start until the first byte was just about to be trans- ferred. This includes time_pretransfer and also the time the server needed to calculate the result. size_download The total amount of bytes that were downloaded.
size_upload The total amount of bytes that were uploaded.
size_header The total amount of bytes of the downloaded head- ers.
size_request The total amount of bytes that were sent in the HTTP request.
speed_download The average download speed that curl measured for the complete download.
speed_upload The average upload speed that curl measured for the complete upload.
content_type The Content-Type of the requested document, if there was any.
num_connects Number of new connects made in the recent trans- fer. (Added in 7.12.3)
num_redirects Number of redirects that were followed in the request. (Added in 7.12.3)
redirect_url When a HTTP request was made without -L to follow redirects, this variable will show the actual URL a redirect would take you to. (Added in 7.18.2)
ftp_entry_path The initial path libcurl ended up in when logging on to the remote FTP server. (Added in 7.15.4)
ssl_verify_result The result of the SSL peer certificate verifica- tion that was requested. 0 means the verification was successful. (Added in 7.19.0)
Popularity: unranked [?]
