监控WEB服务器 - 炫Q香蕉派

监控WEB服务器

很不幸的消息,mikuq.com又挂了,504 Gateway Time-out
不明原因,不过考虑做一个脚本来监控,大概步骤是这样的,

1、每隔一段时间访问一下mikuq.com
2、如果不正常则重起nginx
3、记录日志

听起来应该不太难,计划用python来写,测试代码

#!/usr/bin/env python
import httplib
import sys
import os
def reboot_nginx():
  os.system('nginx -s reload')
def check_webserver(address, port, resource):
  try:
    conn = httplib.HTTPConnection(address, port)
    print 'HTTP connection created successfully'
    req = conn.request('GET', resource)
    print 'request for %s successful' % resource
    response = conn.getresponse()
    print 'response status: %s' % response.status
  except httplib.error, e:
    print "HTTP connection failed: %s" % e
    return False
  finally:
    conn.close()
    print 'HTTP connection closed successfully'
  if response.status in [200, 301]:
    return True
  else:
    return False
check=check_webserver('mikuq.com', 80, '/')
print 'check_webserver returned %s' % check

还没有增加日志和定时器,待续......

标签: none

添加新评论