“丝路通:监控报警程序”的版本间的差异

来自CloudWiki
跳转至: 导航搜索
第1行: 第1行:
 
'''1.系统运行状态报警<br>'''
 
'''1.系统运行状态报警<br>'''
 
我们这里通过检测程序的端口号,进程号来判断程序是否正常运行,如果检测不到端口就发送邮电警告,告诉运维人员及时修复,采用每分钟一次扫描机制,同时运行状态通过ES实时发送到网页展示<br>
 
我们这里通过检测程序的端口号,进程号来判断程序是否正常运行,如果检测不到端口就发送邮电警告,告诉运维人员及时修复,采用每分钟一次扫描机制,同时运行状态通过ES实时发送到网页展示<br>
 +
 +
配置邮电警告服务器<br>
 +
 +
[root@master YXJG]# cat S_mail.py
 +
#coding:utf-8
 +
import smtplib
 +
from email.mime.text import MIMEText
 +
class SendEMail(object):
 +
    # 定义第三方 SMTP 服务
 +
    def __init__(self):
 +
        self.mail_host = "smtp.163.com"  # SMTP服务器
 +
        self.mail_user = "15610347662@163.com"  # 用户名
 +
        self.mail_pass = "APLTTDBTLKVHYKSU"  # 密码
 +
        self.sender = '15610347662@163.com'  # 发件人邮箱
 +
        self.smtpObj = smtplib.SMTP_SSL(self.mail_host, 465)
 +
        self.smtpObj.login(self.mail_user,self.mail_pass)  # 登录验证
 +
    def sendmail(self,receivers,title,content):
 +
        message = MIMEText(content,'plain','utf-8')  # 内容, 格式, 编码
 +
        message['From'] = "{}".format(self.sender)
 +
        message['To'] = ",".join(receivers)
 +
        message['Subject'] = title
 +
        try:
 +
            self.smtpObj.sendmail(self.sender, message['To'].split(','), message.as_string())  # 发送
 +
            print("mail has been send successfully.")
 +
        except smtplib.SMTPException as e:
 +
            print(e)
 +
if __name__ == '__main__':
 +
    sm = SendEMail()
 +
    sm.sendmail(['15610347662@163.com'], '主题', '正文')
 +
  
 
代码展示:<br>
 
代码展示:<br>
第147行: 第177行:
 
             print(line)
 
             print(line)
  
邮电报警页面展示:
+
邮电报警页面展示:<br>
 
[[文件:2020-09-24 231138.png]]
 
[[文件:2020-09-24 231138.png]]

2020年9月24日 (四) 15:16的版本

1.系统运行状态报警
我们这里通过检测程序的端口号,进程号来判断程序是否正常运行,如果检测不到端口就发送邮电警告,告诉运维人员及时修复,采用每分钟一次扫描机制,同时运行状态通过ES实时发送到网页展示

配置邮电警告服务器

[root@master YXJG]# cat S_mail.py 
#coding:utf-8
import smtplib
from email.mime.text import MIMEText
class SendEMail(object):
   # 定义第三方 SMTP 服务
   def __init__(self):
       self.mail_host = "smtp.163.com"  # SMTP服务器
       self.mail_user = "15610347662@163.com"  # 用户名
       self.mail_pass = "APLTTDBTLKVHYKSU"  # 密码
       self.sender = '15610347662@163.com'  # 发件人邮箱
       self.smtpObj = smtplib.SMTP_SSL(self.mail_host, 465)
       self.smtpObj.login(self.mail_user,self.mail_pass)  # 登录验证
   def sendmail(self,receivers,title,content):
       message = MIMEText(content,'plain','utf-8')  # 内容, 格式, 编码
       message['From'] = "{}".format(self.sender)
       message['To'] = ",".join(receivers)
       message['Subject'] = title
       try:
           self.smtpObj.sendmail(self.sender, message['To'].split(','), message.as_string())  # 发送
           print("mail has been send successfully.")
       except smtplib.SMTPException as e:
           print(e)
if __name__ == '__main__':
   sm = SendEMail()
   sm.sendmail(['15610347662@163.com'], '主题', '正文')


代码展示:

[root@master YXZT]# cat check.py 
# coding:utf-8
import socket
from S_mail import SendEMail   #导入邮件类
import psutil
# 实例化邮件类
f = open("yxzt.txt","a+")
sm = SendEMail()
## 定义收件人
receivers = ['15610347662@163.com','2977023867@qq.com']  # 接收人邮箱
# 定义进程名
P_name="mysqld"
#定义检测进程函数
def checkprocess(processname):
   pl = psutil.pids()
   for pid in pl:
       if psutil.Process(pid).name() == processname:
           return pid
if isinstance(checkprocess(P_name),int):
   print("{0}进程存在".format(P_name),file=f)
   pass   # 进程存在
else:
   print("{0}进程不存在,发送邮件警告".format(P_name),file=f)
   sm.sendmail(receivers,"{0}进程down掉了,请及时修复".format(P_name),"{0}进程down掉了,请检测原因....".format(P_name))
## 定义收件人
# 定义进程名
P2_name="nginx"
#定义检测进程函数
def checkprocess(processname):
   pl = psutil.pids()
   for pid in pl:
       if psutil.Process(pid).name() == processname:
           return pid
if isinstance(checkprocess(P2_name),int):
   print("{0}进程存在".format(P2_name),file=f)
   pass   # 进程存在
else:
   print("{0}进程不存在,发送邮件警告".format(P2_name),file=f)
   sm.sendmail(receivers,"{0}进程down掉了,请及时修复".format(P2_name),"{0}进程down掉了,请检测原因....".format(P2_name))
# 定义进程名
P2_name="filebeat"
#定义检测进程函数
def checkprocess(processname):
   pl = psutil.pids()
   for pid in pl:
       if psutil.Process(pid).name() == processname:
           return pid
if isinstance(checkprocess(P2_name),int):
   print("{0}进程存在".format(P2_name),file=f)
   pass   # 进程存在
else:
   print("{0}进程不存在,发送邮件警告".format(P2_name),file=f)
   sm.sendmail(receivers,"{0}进程down掉了,请及时修复".format(P2_name),"{0}进程down掉了,请检测原因....".format(P2_name))
hosts = ['39.101.189.249:3000']
hosts1 = ['39.101.189.249:1314']
socket.setdefaulttimeout(5)
for host in hosts:
   ip = host.split(':')[0]
port = host.split(':')[1]
server = socket.socket()
res = server.connect_ex((ip, int(port)))# 返回值为0代表ok,
print("检测8080端口,网页状态0为正常:")
print(res)
if res == 0:
   print("网页状态正常",file=f)
   pass
else :
   print("网页无法访问,发送邮件警告",file=f)
   sm.sendmail(receivers,"网页无法访问,请及时修复","网页无法访问,请检测原因....")
socket.setdefaulttimeout(5)
for host1 in hosts1:
   ip = host1.split(':')[0]
port = host1.split(':')[1]
server = socket.socket()
res = server.connect_ex((ip, int(port)))# 返回值为0代表ok,
print("检测端口9200,elasticsearch状态0为正常:")
print(res)
if res == 0:
   print("elasticsearch状态正常",file=f)
   pass
else :
   print("elasticsearch状态异常","发送邮件警告",file=f)
   sm.sendmail(receivers,"elasticsearch状态异常,请及时修复","elasticsearch状态异常,请检测原因....")

2.运行日志报警 我们检测日志文件,如果发现日志文件中有Waring,Error,CRITICAL等关键词,就发送邮电警告,告诉运维人员及时修复,以免影响正常运行 代码展示(Mysql为例):

[root@master YXJG]# cat jkv2mysql.py 
#coding:utf-8
import time
from S_mail import SendEMail
file = open("/var/log/mariadb/mariadb.log")      #mysql监控
f = open("yxzt.txt","a+")
key_word="WARNING"
key_word2="ERROR"
key_word3="CRITICAL"
key_word4="error"
key_word5="Error"
key_word6="Warning"
key_word7="warning"
while True:
   where = file.tell()
   line = file.readline()
   if not line:
       time.sleep(1)
       print("无异常mysql")
       file.seek(where)
   else:
       if line.find(key_word) >=0:
           print("Mysql检测到日志出现Waring异常,发送邮箱",file=f)
           sm = SendEMail()
           sm.sendmail(['2977023867@qq.com'],'主题','Mysql有异常,发送警告'.format(key_word))
           print(line)
       if line.find(key_word2) >=0:
           print("Mysql检测到日志出现Error异常,发送邮箱",file=f)
           sm = SendEMail()
           sm.sendmail(['2977023867@qq.com'],'主题','Mysql有异常,发送警告'.format(key_word2))
           print(line)
       if line.find(key_word3) >=0:
           print("Mysql检测到日志出现CRITICAL异常,发送邮箱",file=f)
           sm = SendEMail()
           sm.sendmail(['2977023867@qq.com'],'主题','Mysql有异常,发送警告'.format(key_word3))
           print(line)
       if line.find(key_word4) >=0:
           print("Mysql检测到日志出现Error异常,发送邮箱",file=f)
           sm = SendEMail()
           sm.sendmail(['2977023867@qq.com'],'主题','Mysql有异常,发送警告'.format(key_word4))
           print(line)
       if line.find(key_word5) >=0:
           print("Mysql检测到日志出现Error异常,发送邮箱",file=f)
           sm = SendEMail()
           sm.sendmail(['2977023867@qq.com'],'主题','Mysql有异常,发送警告'.format(key_word5))
           print(line)
       if line.find(key_word6) >=0:
           print("Mysql检测到日志出现Waring异常,发送邮箱",file=f)
           sm = SendEMail()
           sm.sendmail(['2977023867@qq.com'],'主题','Mysql有异常,发送警告'.format(key_word6))
           print(line)
       if line.find(key_word7) >=0:
           print("Mysql检测到日志出现Waring异常,发送邮箱",file=f)
           sm = SendEMail()
           sm.sendmail(['2977023867@qq.com'],'主题','Mysql有异常,发送警告'.format(key_word7))
           print(line)

邮电报警页面展示:
2020-09-24 231138.png