溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

object-python-監(jiān)控web狀態(tài)-nagios-plug

發(fā)布時(shí)間:2020-08-10 22:56:20 來源:網(wǎng)絡(luò) 閱讀:452 作者:123yph321 欄目:移動(dòng)開發(fā)

#! /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使用起來也很方便,廢話不多說,看實(shí)例:
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 參數(shù)后面可以加你要監(jiān)控的網(wǎng)址。
            -s  參數(shù)后面可以加你要監(jiān)控網(wǎng)址的狀態(tài),如何你監(jiān)控的狀態(tài)是301,那么除了301狀態(tài),其他的會(huì)報(bào)警給nagios。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI