提交阈值

This commit is contained in:
halliday 2023-11-27 14:41:33 +08:00
parent f0d57397af
commit c2cbbd6567
3 changed files with 86 additions and 42 deletions

2
.vscode/launch.json vendored
View File

@ -9,7 +9,7 @@
"type": "python", "type": "python",
"request": "launch", "request": "launch",
"program": "${file}", "program": "${file}",
"args": ["--send-mail=true"], //"args": ["--send-mail=true"],
"console": "integratedTerminal", "console": "integratedTerminal",
"justMyCode": true "justMyCode": true
} }

View File

@ -33,7 +33,11 @@ with open(config_file_patch,"r",encoding='utf-8') as fy:
config = yaml.safe_load(fy) config = yaml.safe_load(fy)
# print(config) # print(config)
need_sendmail = False
# 发邮件 # 发邮件
def send_mail(): def send_mail():
# 发件人 # 发件人
sender = 'mh@unamail.com' sender = 'mh@unamail.com'
@ -44,7 +48,8 @@ def send_mail():
# 正文 # 正文
with open(str(file_today_path), "r", encoding='utf-8') as f: # 打开文本 with open(str(file_today_path), "r", encoding='utf-8') as f: # 打开文本
msgdata = f.read() msgdata = f.read()
message = "{0}周,{1}资源使用情况检测已完成,本次检测内容如下所示,本周的所有信息请查看附件!\n{2}".format(week,get_IP(),msgdata) message = "{0}周,{1}资源使用情况检测已完成,本次检测内容如下所示,本周的所有信息请查看附件!\n{2}".format(
week, get_IP(), msgdata)
msg.attach(MIMEText(message, 'plain', _charset="utf-8")) msg.attach(MIMEText(message, 'plain', _charset="utf-8"))
# 发送者 # 发送者
msg['From'] = Header(sender, 'utf-8') msg['From'] = Header(sender, 'utf-8')
@ -56,7 +61,8 @@ def send_mail():
# 附件信息 # 附件信息
att = MIMEText(open(str(file_week_path), 'rb').read(), 'base64', 'utf-8') att = MIMEText(open(str(file_week_path), 'rb').read(), 'base64', 'utf-8')
att["Content-Type"] = 'application/octet-stream' att["Content-Type"] = 'application/octet-stream'
att["Content-Disposition"] = 'attachment; filename="{}"'.format(file_name_week) att["Content-Disposition"] = 'attachment; filename="{}"'.format(
file_name_week)
msg.attach(att) msg.attach(att)
try: try:
@ -73,11 +79,15 @@ def save_txt(datas):
[file_txt.write(str(item)+'\n') for item in datas] [file_txt.write(str(item)+'\n') for item in datas]
# 合并文件 # 合并文件
def save_all_tex(): def save_all_tex():
with open(file_today_path, 'rb') as f1, open(file_week_path, 'ab+') as f2: with open(file_today_path, 'rb') as f1, open(file_week_path, 'ab+') as f2:
f2.write(f1.read()) f2.write(f1.read())
# 获取本机磁盘使用率和剩余空间G信息 # 获取本机磁盘使用率和剩余空间G信息
def get_disk_info(): def get_disk_info():
# 循环磁盘分区 # 循环磁盘分区
content = [] content = []
@ -91,14 +101,24 @@ def get_disk_info():
free_disk_size = disk_info.free//1024//1024//1024 free_disk_size = disk_info.free//1024//1024//1024
# 磁盘总空间单位G # 磁盘总空间单位G
total_disk_size = disk_info.total//1024//1024//1024 total_disk_size = disk_info.total//1024//1024//1024
# 检测是否超过阈值
error_msg = ""
if disk_info.percent > config["Disk_MAX"]:
error_msg = "\n\t警告: {0}使用率超过{1},请及时处理!".format(
disk_name, str(config["Disk_MAX"]))
global need_sendmail
need_sendmail = True
# 当前磁盘使用率、剩余空间G和磁盘总空间信息 # 当前磁盘使用率、剩余空间G和磁盘总空间信息
info = "\t{0}\t使用率:{1}% 剩余空间:{2}G 总大小:{3}G".format(disk_name, str(disk_info.percent),free_disk_size,total_disk_size) info = "\t{0}\t使用率:{1}% 剩余空间:{2}G 总大小:{3}G {4}".format(
disk_name, str(disk_info.percent), free_disk_size, total_disk_size, error_msg)
# print(info) # print(info)
# 拼接多个磁盘的信息 # 拼接多个磁盘的信息
content.append(info) content.append(info)
print(content) print(content)
save_txt(content) save_txt(content)
# 获取某个目录的大小 # 获取某个目录的大小
def get_dir_size(path): def get_dir_size(path):
list1 = [] list1 = []
@ -122,18 +142,32 @@ def get_dir_size(path):
# cpu信息 # cpu信息
def get_cpu_info(): def get_cpu_info():
cpu_percent = psutil.cpu_percent(interval=1) cpu_percent = psutil.cpu_percent(interval=1)
cpu_info = ["","CPU使用率{0}%".format(cpu_percent),""] # 检测是否超过阈值
error_msg = ""
if cpu_percent > config["CPU_MAX"]:
error_msg = "\n\t警告: CPU使用率超过{0},请及时处理!".format(str(config["CPU_MAX"]))
global need_sendmail
need_sendmail = True
cpu_info = ["", "CPU使用率{0}% {1}".format(cpu_percent, error_msg), ""]
print(cpu_info) print(cpu_info)
# return cpu_info # return cpu_info
save_txt(cpu_info) save_txt(cpu_info)
# 内存信息 # 内存信息
def get_memory_info(): def get_memory_info():
virtual_memory = psutil.virtual_memory() virtual_memory = psutil.virtual_memory()
used_memory = virtual_memory.used/1024/1024/1024 used_memory = virtual_memory.used/1024/1024/1024
free_memory = virtual_memory.free/1024/1024/1024 free_memory = virtual_memory.free/1024/1024/1024
memory_percent = virtual_memory.percent memory_percent = virtual_memory.percent
memory_info = ["内存使用:{0:0.2f}G使用率{1:0.1f}%,剩余内存:{2:0.2f}G".format(used_memory, memory_percent, free_memory),""] # 检测是否超过阈值
error_msg = ""
if memory_percent > config["Memory_MAX"]:
error_msg = "\n\t警告: 内存使用率超过{0},请及时处理!".format(str(config["Memory_MAX"]))
global need_sendmail
need_sendmail = True
memory_info = ["内存使用:{0:0.2f}G使用率{1:0.1f}%,剩余内存:{2:0.2f}G {3}".format(
used_memory, memory_percent, free_memory,error_msg), ""]
print(memory_info) print(memory_info)
# return memory_info # return memory_info
save_txt(memory_info) save_txt(memory_info)
@ -157,6 +191,7 @@ def get_IP():
print(ipv4_list) print(ipv4_list)
return (ipv4_list[0]) return (ipv4_list[0])
def main(): def main():
# 删除之前的文件 # 删除之前的文件
if file_today_path.exists(): if file_today_path.exists():
@ -183,7 +218,6 @@ def main():
save_txt(['----------------------END----------------------------']) save_txt(['----------------------END----------------------------'])
if __name__ == '__main__': if __name__ == '__main__':
main() main()
save_all_tex() save_all_tex()
@ -191,5 +225,6 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser(description='send email') parser = argparse.ArgumentParser(description='send email')
parser.add_argument("--send-mail", type=bool, default=False) parser.add_argument("--send-mail", type=bool, default=False)
args = parser.parse_args() args = parser.parse_args()
if args.send_mail: # 超过阈值的和需要发邮件的
if (args.send_mail or need_sendmail):
send_mail() send_mail()

View File

@ -1,8 +1,17 @@
#邮箱收件人,支持多个 #邮箱收件人,支持多个
receivers: receivers:
- "mh@unamail.com" - "mh@unamail.com"
- "ypy@unamail.com" # - "ypy@unamail.com"
#agent安装路径,windows路径要用双斜线Linux不需要 #agent安装路径,windows路径要用双斜线Linux不需要
agent_dir : "D:\\agent\\ubackup\\uagent\\logs" agent_dir : "D:\\agent\\ubackup\\uagent\\logs"
#资源阈值,超过这个值的会发告警
#CPU使用率最大值
CPU_MAX: 80
#内存使用率最大值
Memory_MAX: 80
#磁盘使用率最大值
Disk_MAX: 90