欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python腳本監(jiān)控Tomcat服務(wù)器的方法

 更新時(shí)間:2018年07月06日 09:55:52   作者:朱培  
這篇文章主要介紹了利用python腳本監(jiān)控Tomcat服務(wù)器的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

文章出處:https://blog.csdn.net/sdksdk0/article/details/80933444

作者:朱培      ID:sdksdk0    
--------------------------------------------------------------------------------------------

對(duì)于最近的開發(fā)環(huán)境,偶爾會(huì)有掛掉的現(xiàn)象發(fā)生,然而并沒有及時(shí)發(fā)現(xiàn),下載需要添加一個(gè)監(jiān)控功能,當(dāng)服務(wù)掛掉的時(shí)候需要有郵件提醒,同時(shí)我們的系統(tǒng)每天晚上會(huì)跑定時(shí)任務(wù),想知道有沒有異常發(fā)生,所以添加了兩個(gè)python監(jiān)本監(jiān)控,因?yàn)楸旧硐到y(tǒng)不大,所以沒必要去配置kafka+storm這種日志監(jiān)控了,只用了很簡(jiǎn)單的方式來處理了。

1、監(jiān)控tomcat是否掛掉

from smtplib import SMTP_SSL 
from email.mime.text import MIMEText 
from email.header import Header 
from os.path import getsize 
from sys import exit 
from re import compile, IGNORECASE 
import sys, time 
import os 
#定義主機(jī) 帳號(hào) 密碼 收件人 郵件主題 
#定義主機(jī) 帳號(hào) 密碼 收件人 郵件主題 
mail_info = { 
 "from": "info@sogoucloud.cn", 
 "to": "zhupei@sogoucloud.cn", 
 "hostname": "smtp.exmail.qq.com", 
 "username": "info@sogoucloud.cn", 
 "password": "123456", 
 "mail_subject": "qybd服務(wù)器異常", 
 "mail_text": "hello, tomcat服務(wù)器出現(xiàn)異常了!,請(qǐng)及時(shí)處理", 
 "mail_encoding": "utf-8" 
} 
#發(fā)送郵件函數(shù) 
def send_mail(error): 
 #定義郵件的頭部信息 
 #連接SMTP服務(wù)器,然后發(fā)送信息 
 smtp = SMTP_SSL(mail_info["hostname"]) 
 smtp.set_debuglevel(1) 
 smtp.ehlo(mail_info["hostname"]) 
 smtp.login(mail_info["username"], mail_info["password"]) 
 msg = MIMEText(error, "plain", mail_info["mail_encoding"]) 
 msg["Subject"] = Header(mail_info["mail_subject"], mail_info["mail_encoding"]) 
 msg["from"] = mail_info["from"] 
 msg["to"] = mail_info["to"] 
 smtp.sendmail(mail_info["from"], mail_info["to"], msg.as_string()) 
 smtp.quit() 
def isRunning(process_name): 
 try: 
  process = len(os.popen('ps aux | grep "' + process_name + '" | grep -v grep').readlines()) 
  if process >= 1: 
   return True 
  else: 
   return False 
 except: 
  print("Check process ERROR!!!") 
  return False 
#調(diào)用發(fā)送郵件函數(shù)發(fā)送郵件 
if __name__ == '__main__': 
 process_name = "qybd" 
 isrunning = isRunning(process_name) 
 print(isrunning) 
 if isrunning == False: 
  send_mail("老鐵!qybd服務(wù)器掛了!") 

2、添加crontab定時(shí)任務(wù):

*/3 * * * * python /usr/tools/qybd/cmd/sendEmail.py >> /usr/tools/qybd/cmd/tomcatlife.py.log 2>&1

3、使用crontab -u root -l 命令查看當(dāng)前運(yùn)行的定時(shí)任務(wù)

4、監(jiān)控日志的腳本

from smtplib import SMTP_SSL 
from email.mime.text import MIMEText 
from email.header import Header 
from os.path import getsize 
from sys import exit 
from re import compile, IGNORECASE 
#定義主機(jī) 帳號(hào) 密碼 收件人 郵件主題 
#定義主機(jī) 帳號(hào) 密碼 收件人 郵件主題 
mail_info = { 
 "from": "info@sogoucloud.cn", 
 "to": "zhupei@sogoucloud.cn", 
 "hostname": "smtp.exmail.qq.com", 
 "username": "info@sogoucloud.cn", 
 "password": "123456", 
 "mail_subject": "qybd服務(wù)器異常", 
 "mail_text": "hello, tomcat服務(wù)器出現(xiàn)異常了!,請(qǐng)及時(shí)處理", 
 "mail_encoding": "utf-8" 
} 
#定義tomcat日志文件位置 
tomcat_log = '/usr/tools/qybd/tomcat/logs/catalina.out' 
#該文件是用于記錄上次讀取日志文件的位置,執(zhí)行腳本的用戶要有創(chuàng)建該文件的權(quán)限 
last_position_logfile = '/usr/tools/qybd/tomcat/logs/last_position.txt' 
#匹配的錯(cuò)誤信息關(guān)鍵字的正則表達(dá)式 
pattern = compile(r'Exception|^\t+\bat\b',IGNORECASE) 
#發(fā)送郵件函數(shù) 
def send_mail(error): 
 #定義郵件的頭部信息 
 #連接SMTP服務(wù)器,然后發(fā)送信息 
 smtp = SMTP_SSL(mail_info["hostname"]) 
 smtp.set_debuglevel(1) 
 smtp.ehlo(mail_info["hostname"]) 
 smtp.login(mail_info["username"], mail_info["password"]) 
 msg = MIMEText(error, "plain", mail_info["mail_encoding"]) 
 msg["Subject"] = Header(mail_info["mail_subject"], mail_info["mail_encoding"]) 
 msg["from"] = mail_info["from"] 
 msg["to"] = mail_info["to"] 
 smtp.sendmail(mail_info["from"], mail_info["to"], msg.as_string()) 
 smtp.quit() 
#讀取上一次日志文件的讀取位置 
def get_last_position(file): 
 try: 
  data = open(file,'r') 
  last_position = data.readline() 
  if last_position: 
   last_position = int(last_position) 
  else: 
   last_position = 0 
 except: 
  last_position = 0 
 return last_position 
#寫入本次日志文件的本次位置 
def write_this_position(file,last_positon): 
 try: 
  data = open(file,'w') 
  data.write(str(last_positon)) 
  data.write('\n' + "Don't Delete This File,It is Very important for Looking Tomcat Error Log !! \n") 
  data.close() 
 except: 
  print "Can't Create File !" + file 
  exit() 
#分析文件找出異常的行 
def analysis_log(file): 
 error_list = []           #定義一個(gè)列表,用于存放錯(cuò)誤信息. 
 try: 
  data = open(file,'r') 
 except: 
  exit() 
 last_position = get_last_position(last_position_logfile) #得到上一次文件指針在日志文件中的位置 
 this_postion = getsize(tomcat_log)      #得到現(xiàn)在文件的大小,相當(dāng)于得到了文件指針在末尾的位置 
 if this_postion < last_position:      #如果這次的位置 小于 上次的位置說明 日志文件輪換過了,那么就從頭開始 
  data.seek(0) 
 elif this_postion == last_position:      #如果這次的位置 等于 上次的位置 說明 還沒有新的日志產(chǎn)生 
  exit() 
 elif this_postion > last_position:      #如果是大于上一次的位置,就移動(dòng)文件指針到上次的位置 
  data.seek(last_position) 
 for line in data: 
  if pattern.search(line): 
   error_list.append(line) 
 write_this_position(last_position_logfile,data.tell()) #寫入本次讀取的位置 
 data.close() 
 return ''.join(error_list)        #形成一個(gè)字符串 
#調(diào)用發(fā)送郵件函數(shù)發(fā)送郵件 
error_info = analysis_log(tomcat_log) 
if error_info: 
 send_mail(error_info) 

5、添加crontab定時(shí)任務(wù):

*/10 * * * * python /usr/tools/qybd/cmd/tomcat_log_error_analysis.py >> /usr/tools/qybd/cmd/crontest.py.log 2>&1

效果如下:

總結(jié)

以上所述是小編給大家介紹的python腳本監(jiān)控Tomcat服務(wù)器的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 在Python中處理列表之reverse()方法的使用教程

    在Python中處理列表之reverse()方法的使用教程

    這篇文章主要介紹了在Python中處理列表之reverse()方法的使用教程,是Python入門中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • 終于明白tf.reduce_sum()函數(shù)和tf.reduce_mean()函數(shù)用法

    終于明白tf.reduce_sum()函數(shù)和tf.reduce_mean()函數(shù)用法

    這篇文章主要介紹了終于明白tf.reduce_sum()函數(shù)和tf.reduce_mean()函數(shù)用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python中Scrapy+adbapi提高數(shù)據(jù)庫(kù)寫入效率實(shí)現(xiàn)

    Python中Scrapy+adbapi提高數(shù)據(jù)庫(kù)寫入效率實(shí)現(xiàn)

    本文主要介紹了Python中Scrapy+adbapi提高數(shù)據(jù)庫(kù)寫入效率實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • 利用python如何實(shí)現(xiàn)貓捉老鼠小游戲

    利用python如何實(shí)現(xiàn)貓捉老鼠小游戲

    這篇文章主要給大家介紹了關(guān)于利用python如何實(shí)現(xiàn)貓捉老鼠小游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • python類方法和靜態(tài)方法詳解

    python類方法和靜態(tài)方法詳解

    這篇文章主要為大家介紹了python類方法和靜態(tài)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • minconda安裝pytorch的詳細(xì)方法

    minconda安裝pytorch的詳細(xì)方法

    這篇文章主要介紹了minconda安裝pytorch的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • Python讀寫/追加excel文件Demo分享

    Python讀寫/追加excel文件Demo分享

    今天小編就為大家分享一篇Python讀寫/追加excel文件Demo,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • python 實(shí)時(shí)調(diào)取攝像頭的示例代碼

    python 實(shí)時(shí)調(diào)取攝像頭的示例代碼

    這篇文章主要介紹了python 實(shí)時(shí)調(diào)取攝像頭的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-11-11
  • Python可視化最頻繁使用的10大工具總結(jié)

    Python可視化最頻繁使用的10大工具總結(jié)

    數(shù)據(jù)可視化是數(shù)據(jù)科學(xué)中不可缺少的一部分,下面這篇文章主要給大家介紹了關(guān)于Python可視化最頻繁使用的10大工具,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-03-03
  • 用Python寫個(gè)新年賀卡生成器

    用Python寫個(gè)新年賀卡生成器

    大家好,本篇文章主要講的是用Python寫個(gè)新年賀卡生成器,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-01-01

最新評(píng)論