#! /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)
備注:
此腳本使用起來(lái)很靈活,可以和nagios使用起來(lái)也很方便,廢話不多說(shuō),看實(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
解釋?zhuān)?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。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。