提交py代码
This commit is contained in:
parent
6561b021bb
commit
99f080a45b
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
// 使用 IntelliSense 了解相关属性。
|
||||||
|
// 悬停以查看现有属性的描述。
|
||||||
|
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
|
||||||
|
{
|
||||||
|
"name": "Python 调试程序: 当前文件",
|
||||||
|
"type": "debugpy",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${file}",
|
||||||
|
"console": "integratedTerminal"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -0,0 +1,143 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import smtplib
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
from email.mime.text import MIMEText
|
||||||
|
from email.header import Header
|
||||||
|
from email.mime.multipart import MIMEMultipart
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
"""
|
||||||
|
检查作业成功率,并将结果发送邮件
|
||||||
|
|
||||||
|
"""
|
||||||
|
#公共的
|
||||||
|
now=os.path.dirname(os.path.abspath(__file__))
|
||||||
|
file_path=os.path.join(now,"check_report.txt")
|
||||||
|
week = str(datetime.now().isocalendar()[1])
|
||||||
|
|
||||||
|
# 发送邮件
|
||||||
|
def send_mail():
|
||||||
|
# 发件人
|
||||||
|
sender = 'mh@una-mail.com'
|
||||||
|
# 接收邮件,可以发给多人
|
||||||
|
receivers = ['mh@una-mail.com','dhx@una-mail.com']
|
||||||
|
# 邮件主体
|
||||||
|
msg = MIMEMultipart()
|
||||||
|
# 正文取文本中的内容
|
||||||
|
with open(file_path, "r") as f:
|
||||||
|
msgdata = f.read()
|
||||||
|
message = "第{0}周,长期任务的作业成功率如下:\n{1}".format(
|
||||||
|
week, msgdata)
|
||||||
|
msg.attach(MIMEText(message, 'plain', _charset="utf-8"))
|
||||||
|
# 发送者
|
||||||
|
msg['From'] = Header(sender, 'utf-8')
|
||||||
|
# 接收者
|
||||||
|
msg['To'] = Header(receivers[0], 'utf-8')
|
||||||
|
# 主题
|
||||||
|
subject = '【长期任务】第W{0}周作业成功率情况'.format(week)
|
||||||
|
msg['Subject'] = Header(subject, 'utf-8')
|
||||||
|
# 附件信息
|
||||||
|
#att = MIMEText(open(file_week_path, 'rb').read(), 'base64', 'utf-8')
|
||||||
|
#att["Content-Type"] = 'application/octet-stream'
|
||||||
|
#att["Content-Disposition"] = 'attachment; filename="{}"'.format(
|
||||||
|
# file_name_week)
|
||||||
|
#msg.attach(att)
|
||||||
|
try:
|
||||||
|
smtpObj = smtplib.SMTP('10.10.110.102')
|
||||||
|
#身份认证
|
||||||
|
smtpObj.login(sender,"111111")
|
||||||
|
smtpObj.sendmail(sender, receivers, msg.as_string())
|
||||||
|
print("邮件发送成功")
|
||||||
|
except smtplib.SMTPException:
|
||||||
|
print("Error: 无法发送邮件")
|
||||||
|
|
||||||
|
def get_need_tables():
|
||||||
|
# 先查询出所有的tbl_jobhistory表
|
||||||
|
SQL_TABLES="SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='unary' AND TABLE_NAME regexp 'tbl_jobhistory(_2)|^tbl_jobhistory$'"
|
||||||
|
# 触发查询
|
||||||
|
#RESULT_TABLES=`mysql -u$MYSQLUSER -p$MYSQLPWD -e "$SQL_TABLES" 2>/dev/null|awk 'NR>1'`
|
||||||
|
RESULT_TABLES=None
|
||||||
|
commdline = ["mysql", "-uunadba", "-p1223Bc@2008", "-e", SQL_TABLES]
|
||||||
|
result=subprocess.run(commdline,capture_output=True,text=True)
|
||||||
|
tables_list = result.stdout.split('\n')[1:-1]
|
||||||
|
return tables_list
|
||||||
|
|
||||||
|
|
||||||
|
def get_policy_tasks(id,name):
|
||||||
|
#拼接所有的表
|
||||||
|
ALLJOBHISTORY = []
|
||||||
|
for table in check_tables:
|
||||||
|
JOBHISTORY = "select operdesc,count(*) as n2 from "
|
||||||
|
JOBHISTORY += table
|
||||||
|
JOBHISTORY += " where policyname=\'"
|
||||||
|
JOBHISTORY += name
|
||||||
|
JOBHISTORY += "\' and compid=\'"
|
||||||
|
JOBHISTORY += id
|
||||||
|
JOBHISTORY += "\' group by operdesc"
|
||||||
|
ALLJOBHISTORY.append(JOBHISTORY)
|
||||||
|
ALLJOBHISTORY_SQL = " UNION ALL ".join(ALLJOBHISTORY)
|
||||||
|
#最终查询的语句
|
||||||
|
check_sql = "select operdesc,sum(n2) as num from ("+ ALLJOBHISTORY_SQL + ") b group by b.operdesc"
|
||||||
|
print(check_sql)
|
||||||
|
commdline = ["mysql", "-uunadba", "unary","-p1223Bc@2008", "-e", check_sql]
|
||||||
|
result=subprocess.run(commdline,capture_output=True,text=True)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def get_angent_sucessful(bkinfo):
|
||||||
|
# 获取策略的结果
|
||||||
|
policy_datas=[]
|
||||||
|
for name in bkinfo['policyName']:
|
||||||
|
policy_result=get_policy_tasks(bkinfo['agentID'],name)
|
||||||
|
policy_datas.append(policy_result)
|
||||||
|
# 合并处理
|
||||||
|
check_result=""
|
||||||
|
|
||||||
|
# 返回结果
|
||||||
|
return check_result
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
# 需要查询的数据
|
||||||
|
# data={
|
||||||
|
# "mysql物理":{"agentID":"2a0df1771995da56fc60663293bd92da","policyName":["长期任务-mysql物理多通道"]},
|
||||||
|
# "Oracle物理":{"agentID":"2752836e63174d405e3e9cb5cf493d4a","policyName":["长期Oracle多通道"]},
|
||||||
|
# "zstack":{"agentID":"48CB24F42D8048FCA9DA1D6498CE66FC","policyName":["bk1"]},
|
||||||
|
# "MySQLCDM":{"agentID":"be98d51cd9658ed982a3c3e411633418","policyName":["长期"]},
|
||||||
|
# "OracleCDM":{"agentID":"d0003e0c3da5730a7252fe973aec538b","policyName":["长期"]},
|
||||||
|
# "sanforscp":{"agentID":"45CE47DCD8D14C79BA9A4CD8BF3160CA","policyName":["长期"]},
|
||||||
|
# "OracleCLP":{"agentID":"2737975f-15c3-4716-968a-fc8ab2e9f1fc","policyName":["长期clp"]},
|
||||||
|
# "文件定时":{"agentID":"dc62d38f7e8ad92d2bdc77e0760173e6","policyName":["多通道","普通"]},
|
||||||
|
# "PG物理":{"agentID":"289063a564255daff1f211fb7e1db3b0","policyName":["pg物理挂载备份长期"]},
|
||||||
|
# "VMwareCDM":{"agentID":"3D661E09C4F0456B97FBFBA5386C70D8","policyName":["长期"]},
|
||||||
|
# }
|
||||||
|
data={
|
||||||
|
"vmware":{"agentID":"22CDE0BCDE104B0C8596009D236383AA","policyName":["复现2"]},
|
||||||
|
"vmware2":{"agentID":"22CDE0BCDE104B0C8596009D236383AA","policyName":["复现2"]},
|
||||||
|
}
|
||||||
|
# 获取所需要查询的表
|
||||||
|
global check_tables
|
||||||
|
check_tables=get_need_tables()
|
||||||
|
# 以此遍历查询
|
||||||
|
sucessfuldata={}
|
||||||
|
for bktype,bkinfo in data.items():
|
||||||
|
# 获取组件的作业成功率
|
||||||
|
ag_sucessful=get_angent_sucessful(bkinfo)
|
||||||
|
sucessfuldata[bktype]=ag_sucessful
|
||||||
|
|
||||||
|
# 保存输出到文件
|
||||||
|
# save_data(sucessfuldata)
|
||||||
|
# 发邮件
|
||||||
|
#send_mail()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
|
@ -0,0 +1,2 @@
|
||||||
|
@echo off
|
||||||
|
start "" code .
|
Loading…
Reference in New Issue