In the previous Windows Bat Curl implementation Tomcat Daemon, according to the Bat Deamon program written a few years ago, implemented the function of Tomcat Deamon, in the end also mentioned Python to achieve, to be more powerful and more intelligent. Here's a more powerful Tomcat daemon based on Python implementations. Features currently implemented:
1, through more powerful--by modifying config file can be implemented to other Tomcat program Guardian;
2, with log view---polling 30 log files;
3, without the help of curl tools or installation of other environments---with Py2exe can be Python script can be generated EXE file;
The code is as follows:
#!-*-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)//Every day
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 With 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)
The contents of the Config.ini file are as follows:
[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
The above code can be simpler if it is only for a single application, because the configuration file part can be read without the Configparse module. And here the Configparse module is processed (Get function and read_config function) to avoid the error when the key does not exist. The error of the specific errors is shown in the following figure:
Configparser
The resulting log file contents are as follows:
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 with time 111.999988556ms
2016-10-09 15:09:30 INFO httpcode is 200-open URL with time 7.99989700317ms
2016-10-09 15:10:00 INFO httpcode is 200-open URL with time 6.00004096167ms
In addition, can also increase the mail notification function, can refer to: Python Mail send module
Code slightly modified, can also be very convenient through the port detection to confirm the health of the business, port detection script content as follows:
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)