我可以依靠脚本的哪些条件来确定我的服务器是否“启动”?
我通常每周至少处理两打服务器。我认为创建一个脚本来查询它们中的每一个并确定它们是否“启动”是一个好主意。
- 服务器运行 Apache2 或 IIS7。
- 托管提供商各不相同,
- 每个服务器上通常有多个站点,
- 设置不一致,当您直接访问 IP 时,并不总是有默认的 apache“hello world”页面。
- 我在想,站点都是虚拟主机
,确定它们是否正常运行的最佳方法是否只是从每台服务器获取一个站点并发出 http HEAD 请求以确保服务器的响应为 200 OK?显然,如果出现以下情况,这很容易出现“误报”:
- 站点配置/设置在应返回 4xx 错误代码时不正确地返回 200 OK
- 如果单个站点 (
)配置已禁用,或者站点已移至其他服务器。
但在大多数情况下,HEAD 请求和依赖 200 OK 应该是可靠的,对吧?以及确保域的 A 记录与网站移动时列出的记录相匹配。
伪代码:
import http
list = {
'72.0.0.0' : 'domain.com',
'71.0.0.0' : 'blah.com',
}
serverNames = {
'jetty' : '72.0.0.0',
'bob' : '71.0.0.0'
}
for each ( list as server => domain ) {
headRequest = http.head( domain )
if ( headRequest.response == 200 && http.arecord(domain) == server ) {
print serverNames[server] + ' is up ';
} else {
print 'Server is either down or the site attached to the server lookup has moved.';
}
}
我可能会用Python或PHP编写脚本,但这个问题只是讨论逻辑。
I usually deal with at least two dozen servers on a weekly basis. I thought it would be a good idea to create a script that queries each of them and determines whether they're "up" or not.
- The servers run either Apache2 or IIS7.
- The hosting providers vary
- There are usually multiple sites on each server
- The setups are inconsistent, there isn't always a default apache "hello world" page when you access the ip directly.
- Sites are all Virtualhosts
I was thinking, would the best way to determine if they're up be just taking one site from each server and making an http HEAD request to make sure the response from the server is 200 OK? Obviously this would be prone to a "false positive" if:
- The site configuration/setup improperly returns a 200 OK when it should return a 4xx error code
- If an individual site (
<VirtualHost>
)'s configuration is disabled, or if the site has moved to a different server.
But for the most part, a HEAD request and relying on 200 OK should be reliable, right? As well as making sure the domain's A record matches what it's listed as incase of site moves.
Pseudo code:
import http
list = {
'72.0.0.0' : 'domain.com',
'71.0.0.0' : 'blah.com',
}
serverNames = {
'jetty' : '72.0.0.0',
'bob' : '71.0.0.0'
}
for each ( list as server => domain ) {
headRequest = http.head( domain )
if ( headRequest.response == 200 && http.arecord(domain) == server ) {
print serverNames[server] + ' is up ';
} else {
print 'Server is either down or the site attached to the server lookup has moved.';
}
}
I'll probably write the script in Python or PHP, but this question is to discuss the logic only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我觉得逻辑很合理。我建议扩展它以在 apache 服务器上使用 mod_status 模块。完成头部检查后,还尝试获取 /server-status 并使用它来仔细检查您的服务器是否正常。我在想这样的事情:
我过去使用的另一种技术是对我知道应该给出 404 的东西进行检查。这样你就有更好的机会知道服务器是否只是分发 200对于任何询问的人。 (我见过一次,因为配置不好)
Logic seems sound to me. I would suggest extending it to use the mod_status module on your apache servers. After doing your head check, also try to grab /server-status and use that to double check that your server is healthy. I'm thinking something like this:
Another technique I've used in the past is to also do a check for something that I know should give a 404. That way you have a better chance of knowing if the server is just handing out 200's to anyone that asks. (I saw it once due to a bad config)