鍩轰簬python瀹炵幇鐨勫姛鑳芥洿寮哄ぇ鐨則omcat 瀹堟姢杩涚▼銆傜洰鍓嶅疄鐜扮殑鍔熻兘锛�
1銆侀�氳繃鎬ф洿寮� 锛嶏紞锛嶉�氳繃淇敼config鏂囦欢鍗冲彲瀹炵幇瀵瑰叾浠杢omcat绋嬪簭鐨勫畧鎶わ紱
2銆佸叿鏈夋棩蹇楁煡鐪� 锛嶏紞锛� 杞30涓棩蹇楁枃浠讹紱
3銆佹棤闇�鍊熷姪curl 绛夊伐鍏锋垨瀹夎鍏朵粬鐜 锛嶏紞锛嶅�熷姪py2exe鍙互灏唒ython鑴氭湰鍙互鐢熸垚exe鏂囦欢锛�
浠g爜濡備笅锛�
#!-*- encoding: utf-8 -*-
# code by yangbk
# mysite: www.361way.com
import urllib2
import logging
import os
import time
from ConfigParser import ConfigParser
from logging.handlers import TimedRotatingFileHandler
LOG_FILE = "./logs/output.log"
logger = logging.getLogger()
logger.setLevel(logging.INFO)
fh = TimedRotatingFileHandler(LOG_FILE,when='midnight',interval=1,backupCount=7)聽聽 //姣忓ぉ
datefmt = '%Y-%m-%d %H:%M:%S'
format_str = '%(asctime)s %(levelname)s %(message)s '
formatter = logging.Formatter(format_str, datefmt)
fh.setFormatter(formatter)
fh.suffix = "%Y%m%d%H%M"
logger.addHandler(fh)
def getUrlcode(url):
try:
start = time.time()
response = urllib2.urlopen(url,timeout=10)
msg = 'httpcode is : ' + str(response.getcode()) + ' - open use time :' + str((time.time()-start)*1000) + 'ms'
logging.info(msg)
return response.getcode()
except urllib2.URLError as e:
msg = 'open url error ,reason is:' + str(e.reason)
logging.info(msg)
def get(field, key):
result = ""
try:
result = cf.get(field, key)
except:
result = ""
return result
def read_config(config_file_path, field, key):
cf = ConfigParser()
try:
cf.read(config_file_path)
result = cf.get(field, key)
except:
sys.exit(1)
return result
CONFIGFILE='./cfg/config.ini'
os.environ["JAVA_HOME"] = read_config(CONFIGFILE,'MonitorProgram','JAVA_HOME')
os.environ["CATALINA_HOME"] = read_config(CONFIGFILE,'MonitorProgram','CATALINA_HOME')
ProgramPath = read_config(CONFIGFILE,'MonitorProgram','StartPath')
ProcessName = read_config(CONFIGFILE,'MonitorProcessName','ProcessName')
url = read_config(CONFIGFILE,'MonitorUrl','Url')
#url = "http://dh.361way.com/"
while True:
HttpCode = getUrlcode(url)
if HttpCode is not 200:
command = 'taskkill /F /FI "WINDOWSTITLE eq ' + ProcessName + '"'
os.system(command)
os.system(ProgramPath)
time.sleep(30)
config.ini鏂囦欢鐨勫唴瀹瑰涓嬶細
[MonitorProgram]
StartPath: C:/tomcat/bin/startup.bat
CATALINA_HOME: C:\\tomcat\\
JAVA_HOME: C:\\Program Files\\Java\\jdk1.8.0_31
[MonitorProcessName]
ProcessName: tomcat_8080
[MonitorUrl]
Url: http://127.0.0.1:8080
浠ヤ笂浠g爜濡傛灉浠呬粎鍙拡瀵瑰崟涓簲鐢紝浠g爜鍙互鏇寸畝娲侊紝鍥犱负閰嶇疆鏂囦欢閮ㄥ垎鍙互涓嶉�氳繃configparse妯″潡杩涜璇诲彇 銆傝�屼笖杩欓噷瀵筩onfigparse妯″潡杩涜浜嗛噸鏂板鐞嗭紙get鍑芥暟涓巖ead_config鍑芥暟锛夛紝閬垮厤鑾峰彇鐨刱ey涓嶅瓨鍦ㄦ椂锛屽嚭鐜扮殑鎶ラ敊鎯呭喌銆傚叿浣撳嚭閿欐椂鐨勬姤閿欒涓嬪浘锛�
configparser
浜х敓鐨勬棩蹇楁枃浠跺唴瀹瑰涓嬶細
2016-10-09 15:08:30 INFO open url error ,reason is:[Errno 10061]
2016-10-09 15:09:00 INFO httpcode is 200 - open url use time 111.999988556ms
2016-10-09 15:09:30 INFO httpcode is 200 - open url use time 7.99989700317ms
2016-10-09 15:10:00 INFO httpcode is 200 - open url use time 6.00004096167ms
闄ゆ涔嬪锛岃繕鍙互澧炲姞閭欢閫氱煡鍔熻兘锛屽叿浣撳彲浠ュ弬鑰冿細python閭欢鍙戦�佹ā鍧�
浠g爜绋嶄綔淇敼锛屼篃鍙互寰堟柟渚跨殑閫氳繃绔彛妫�娴嬬‘璁や笟鍔℃槸鍚﹀仴搴凤紝绔彛妫�娴嬬殑鑴氭湰鍐呭濡備笅锛�
import os
import socket
def IsOpen(ip,port):
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect((ip,int(port)))
s.shutdown(2)
print '%d is open' % port
return True
except:
print '%d is down' % port
return False
if __name__ == '__main__':
IsOpen('127.0.0.1',800)