網站首頁 編程語言 正文
前言
在測試過程中,注意力往往都在功能上,如果功能正常,是基本不會查看日志的,反之會查看日志定位問題。但是表面上的功能正常不能確保日志沒有報錯,不能確保其他功能點沒有問題,這時我們就需要日志的監控,一旦有報錯就觸發報警機制(報警機制可以有郵件報警、釘釘微信發消息報警等),我選擇的是發郵件報警。
實現思路
1、在測試過程中,日志時時在刷,時時監控難度太大
2、轉換思路,每分鐘對日志進行掃描一次,發現報錯即報警
- a.獲取當前時間前一分鐘的日志,并將日志全部寫入一個文件中,每次寫入前會將該文件清空
- b.獲取前一分鐘文件時,方法是獲取前一分鐘日志的第一行的行號和最后一行的行號,然后將這兩個行號間的所有內容輸出到一個文件中(這樣做相比于直接根據時間過濾的好處就是會包含報錯內容,因為java日志中報錯信息前面是沒有時間的,根據時間過濾就會漏掉報錯信息)
- c.在前一分鐘日志中進行java關鍵詞報錯過濾,并將查詢到的報錯信息的前后20行(具體多少行根據實際情況而定)內容都輸出到另一個文件中
- d.發郵件之前判斷上一步生成的文件大小,如果大于0(說明有報錯)就觸發郵件,如果等于0就不觸發郵件報警
3.腳本寫好后,放入crond定時任務中,每分鐘執行一次,即可進行監控
實現代碼
僅供參考
#!/bin/sh
#日志路徑
mall_c_log_path='/data/admin/log/mall-c/1.0.0/qa/base/1/mall-c.log'
mall_mg_log_path='/data/admin/log/mall-mg/1.0.0/qa/base/1/mall-mg.log'
#當前時間前一分鐘,精確到分
curdate=`date -d "1 minute ago" +"%Y-%m-%d %H:%M"`
echo ${curdate}
#獲取mall_c要截取日期日志的開始和結束行號
c_start_line=`cat -n $mall_c_log_path | grep "${curdate}" | head -1 | cut -f1`
c_end_line=`cat -n $mall_c_log_path | grep "${curdate}" | tail -1 | cut -f1`
#獲取mall_mg要截取日期日志的開始和結束行號
mg_start_line=`cat -n $mall_mg_log_path | grep "${curdate}" | head -1 | cut -f1`
mg_end_line=`cat -n $mall_mg_log_path | grep "${curdate}" | tail -1 | cut -f1`
sed -n "$c_start_line,${c_end_line}p" $mall_c_log_path > /data/admin/log_err/mall_c_now.log
sed -n "$mg_start_line,${mg_end_line}p" $mall_mg_log_path > /data/admin/log_err/mall_mg_now.log
#清空錯誤日志文件
> /data/admin/log_err/mall-c_err.txt
> /data/admin/log_err/mall-mg_err.txt
#將報錯信息寫入文件
err_list=(ArithmeticExecption NullPointerException ClassCastException NegativeArrayException ArrayIndexOutOfBoundsException SecturityException EOFException FileNotFoundException NumberFormatException SQLException IOException NoSuchMethodException SocketTimeoutException)
for i in ${err_list[*]}; do
cat /data/admin/log_err/mall_c_now.log | grep -C 20 "${i}" >> /data/admin/log_err/mall-c_err.txt
cat /data/admin/log_err/mall_mg_now.log | grep -C 20 "${i}" >> /data/admin/log_err/mall-mg_err.txt
done
# -*- coding: UTF-8 -*-
import smtplib,os
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
def send_mail_attch():
#發送郵箱
# sender='18706710668@163.com'
sender='815618406@qq.com'
#接收郵箱
receiver='815618406@qq.com'
#發送郵箱服務器
# smtpserver='smtp.163.com'
smtpserver='smtp.qq.com'
#用戶名 口令
# username='18706710668@163.com'
username='815618406@qq.com'
password='vwrfpqwbwgsybdah'
#中文需參數‘utf8',單字節字符不需要
# 發送郵件主題
subject = '互動贏家QA環境日志監控報警'
msg = MIMEMultipart('mixed')
msg['Subject'] = Header(subject, 'utf-8')
#郵件正文
text = "Dear all!\n 附件是后端日志報錯內容,請查收~"
zw = MIMEText(text,'plain','utf-8')
msg.attach(zw)
#郵件附件1
size_mall_c = os.path.getsize('/data/admin/log_err/mall-c_err.txt')
if size_mall_c != 0:
mall_c_log = open('/data/admin/log_err/mall-c_err.txt',"rb")
send_mall_c_log = mall_c_log.read()
mall_c_log.close()
att_1 = MIMEText(send_mall_c_log, 'base64', 'utf-8')
att_1["Content-Type"] = "application/octet-stream"
att_1["Content-Disposition"] = "attachment;filename = 'mall_c_err.txt'"
msg.attach(att_1)
#郵件附件2
size_mall_mg = os.path.getsize('/data/admin/log_err/mall-mg_err.txt')
if size_mall_mg != 0:
mall_mg_log = open('/data/admin/log_err/mall-mg_err.txt',"rb")
send_mall_mg_log = mall_mg_log.read()
mall_mg_log.close()
att_2 = MIMEText(send_mall_mg_log, 'base64', 'utf-8')
att_2["Content-Type"] = "application/octet-stream"
att_2["Content-Disposition"] = "attachment;filename = 'mall_mg_err.txt'"
msg.attach(att_2)
msg['to']='815618406@qq.com'
msg['from']='815618406@qq.com'
smtp=smtplib.SMTP_SSL('smtp.qq.com',port=465)
#smtp=smtplib.SMTP()
#smtp.connect('smtp.qq.com')
#smtp.set_debuglevel(1)
smtp.login(username,password)
smtp.sendmail(sender,receiver,msg.as_string())
smtp.quit()
if __name__ == '__main__':
size_mall_c = os.path.getsize('/data/admin/log_err/mall-c_err.txt')
size_mall_mg = os.path.getsize('/data/admin/log_err/mall-mg_err.txt')
if (size_mall_c != 0) or (size_mall_mg != 0):
send_mail_attch()
#!/bin/sh
#執行收集報錯的腳本
sh /data/admin/log_err/monitor_log_err.sh
sleep 10
#執行發送郵件腳本
/usr/bin/python /data/admin/log_err/send_email.py
原文鏈接:https://www.cnblogs.com/zy0209/p/12769466.html
相關推薦
- 2024-02-16 SpringBoot 事務管理Transactional 數據回滾 數據一致性
- 2022-02-23 圖片返回base64數據渲染為圖片的處理
- 2022-12-12 C語言實現打印星號圖案_C 語言
- 2022-04-21 Ubuntu16.04系統搭建.Net?Core開發環境_實用技巧
- 2023-10-11 在MyBatisPlus中添加分頁插件
- 2022-12-22 關于C++中push_back()函數的用法及代碼實例_C 語言
- 2022-12-24 Go中函數的使用細節與注意事項詳解_Golang
- 2022-05-13 Shell腳本命令結果保存到變量,保留換行符
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支