资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

object-python-监控web状态-nagios-plug-创新互联

#! /usr/bin/env python2.6
# 2013-1-28
# written by Tony.yang
#

import os
import sys
import getopt
import urllib

def usage():
    print """
---------------------------------------------------------------------------------------------
| Tony-warn: This nagios receiving return status.(0:OK; 1:WARNING; 2:CRITICAL; 3:UNKNOWN) |
---------------------------------------------------------------------------------------------
Usage: check_web_status [-h|--help] [-u|--url value] [-s|--status url_status]

If url_status is not %s of the url, the script will send critical message. % (url_status)
"""
    sys.exit(3)

# obtain command args
try:
    options, args = getopt.getopt(sys.argv[1:], 'hu:s:', '--help --url= --status=')
except getopt.GetoptError:
    usage()
    sys.exit(3)

#
argUrl = 'http://__init__'
argStatus = '200__init__'

#
for name, value in options:
    if name in ('-h', '--help'):
        usage()
    if name in ('-u', '--url'):
        try:
            argUrl = value
        except Exception:
            print "Unable to converting char URL\n"
    if name in ('-s', '--status'):
        try:
            argStatus = value
        except Exception:
            print "Unable to converting char STATUS\n"
            usage()

try:
    Status_url = urllib.urlopen(argUrl).getcode()
    #print "WEB: %s" % Status_url
except Exception:
    print "GETLOADAVG UNKNOWN: Error while requesting this URL"
    sys.exit(3)

if str(argStatus) == str(Status_url):
    print "WEBURL OK: This %s state is %s" % (argUrl, Status_url)
    print "0"
    sys.exit(0)
else:
    #print "weburl xx: the url is %s" % (argStatus)
    print "WEBURL CRITICAL: This %s state is %s" % (argUrl, Status_url)
    print "2"
    sys.exit(2)

备注:
此脚本使用起来很灵活,可以和nagios使用起来也很方便,废话不多说,看实例:
python2.6 check_web_status.py -u yoururl -s yoururl_status(200, 301, 302, 404, 500 ... 都可以)

[xxx@xxx 10:32:47 ]# /usr/bin/python2.6 check_web_status.py -u http://www.8558.com/ -s 200
WEBURL OK: This http://www.8558.com/ state is 200
0  <----为nagios返回0
[xxx@xxx 10:32:47 ]# /usr/bin/python2.6 check_web_status.py -u http://www.8558.com/index.ph -s 200
WEBURL CRITICAL: This http://www.8558.com/index.ph state is 404
2 <----为nagios返回2

解释:-u 参数后面可以加你要监控的网址。
      -s 参数后面可以加你要监控网址的状态,如何你监控的状态是301,那么除了301状态,其他的会报警给nagios。

创新互联-专业网站定制、快速模板网站建设、高性价比三穗网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式三穗网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖三穗地区。费用合理售后完善,十年实体公司更值得信赖。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网页名称:object-python-监控web状态-nagios-plug-创新互联
网站地址:http://cdkjz.cn/article/cocjsd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220