码迷,mamicode.com
首页 > 其他好文 > 详细

tomcat监控脚本

时间:2017-03-03 14:55:27      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:rip   cal   utf-8   out   bin   txt   python3   shu   sendmail   

监控tomcat程序是否正常运行的脚本

#!/usr/local/bin/python3.5
#coding:utf-8

import urllib.request
import subprocess, os
from time import strftime, gmtime, sleep
 
#tomcat_pid = (((subprocess.Popen("lsof -i :8080 |grep root |awk ‘{print $2}‘", shell=True, stdout=subprocess.PIPE)).stdout.read()).decode()).strip()
req = urllib.request.Request(‘http://123.57.79.138‘)
try:
    response = urllib.request.urlopen(req, timeout = 30)
except Exception as e:
    with open(‘/var/tomcat/logs/tomcat_down.txt‘, ‘a‘) as f:
        current_time = ‘#================<< Tomcat Down Time : ‘ + strftime("%Y-%m-%d %H:%M:%S", gmtime()) + ‘ >>==============\nMore information about tomcat...\n‘
        f.write(current_time)
        f.write(e)
        tomcat_status = (open(‘/proc/‘ + tomcat_pid + ‘/status‘)).read()
        f.write(tomcat_status + ‘\n‘)
    
        subprocess.Popen("/var/tomcat/bin/shutdown.sh > /dev/null 2>&1", shell=True, stdout=subprocess.PIPE)
        if len(tomcat_pid) == 0:
            pass
        else:
            subprocess.Popen("kill -9 " + tomcat_pid, shell=True, stdout=subprocess.PIPE)
            subprocess.Popen("rm -rf /var/tomcat/temp/*", shell=True, stdout=subprocess.PIPE)
            subprocess.Popen("rm -rf /var/tomcat/work/Catalina/localhost/*", shell=True, stdout=subprocess.PIPE)
            sleep(3)
            if len(os.listdir(‘/var/tomcat/temp‘)) == 0 and len(os.listdir(‘/var/tomcat/work/Catalina/localhost‘)) == 0:
                subprocess.Popen("/var/tomcat/bin/startup.sh", shell=True, stdout=subprocess.PIPE)
                send_mail.SendMail(‘/var/tomcat/logs/tomcat_lastdown.txt‘)
            else:
                with open(‘/var/tomcat/logs/tomcat_down.txt‘, ‘a‘) as f:
                    f.write(‘The cache file not clean yet!‘)

END!

tomcat监控脚本

标签:rip   cal   utf-8   out   bin   txt   python3   shu   sendmail   

原文地址:http://www.cnblogs.com/changbo/p/6496641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!