将本次的结果作为正文内容,一周的作为附件
This commit is contained in:
parent
7c00bac8e1
commit
8924880162
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3file_today_path
|
||||||
import _load
|
import _load
|
||||||
import psutil
|
import psutil
|
||||||
import smtplib
|
import smtplib
|
||||||
|
@ -20,8 +20,11 @@ if not report_dir.exists():
|
||||||
report_dir.mkdir()
|
report_dir.mkdir()
|
||||||
#报告文件
|
#报告文件
|
||||||
week=str(datetime.now().isocalendar()[1])
|
week=str(datetime.now().isocalendar()[1])
|
||||||
file_name = "ComputerInfos-week-" + week + ".txt"
|
file_name_week = "ComputerInfos-week-" + week + ".txt"
|
||||||
file_path = report_dir/file_name
|
file_name_today="ComputerInfos-today-.txt"
|
||||||
|
|
||||||
|
file_today_path = report_dir/file_name_today
|
||||||
|
file_week_path = report_dir/file_name_week
|
||||||
|
|
||||||
#发邮件
|
#发邮件
|
||||||
def send_mail():
|
def send_mail():
|
||||||
|
@ -32,7 +35,9 @@ def send_mail():
|
||||||
# 邮件主体
|
# 邮件主体
|
||||||
msg = MIMEMultipart()
|
msg = MIMEMultipart()
|
||||||
#正文
|
#正文
|
||||||
message = "第{0}周,{1}运行信息检测已完成,详情请查看附件!".format(week,get_IP())
|
with open(str(file_today_path), "r", encoding='utf-8') as f: #打开文本
|
||||||
|
msgdata = f.read()
|
||||||
|
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')
|
||||||
|
@ -42,9 +47,9 @@ def send_mail():
|
||||||
subject = '【长期任务】第{0}周{1}系统运行信息'.format(week,get_IP())
|
subject = '【长期任务】第{0}周{1}系统运行信息'.format(week,get_IP())
|
||||||
msg['Subject'] = Header(subject, 'utf-8')
|
msg['Subject'] = Header(subject, 'utf-8')
|
||||||
#附件信息
|
#附件信息
|
||||||
att = MIMEText(open(str(file_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)
|
att["Content-Disposition"] = 'attachment; filename="{}"'.format(file_name_week)
|
||||||
msg.attach(att)
|
msg.attach(att)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -57,9 +62,13 @@ def send_mail():
|
||||||
|
|
||||||
#写入文件
|
#写入文件
|
||||||
def save_txt(datas):
|
def save_txt(datas):
|
||||||
with open(str(file_path),'a+',encoding='utf-8',newline='') as file_txt:
|
with open(str(file_today_path),'a+',encoding='utf-8',newline='') as file_txt:
|
||||||
[file_txt.write(str(item)+'\n') for item in datas]
|
[file_txt.write(str(item)+'\n') for item in datas]
|
||||||
|
|
||||||
|
#合并文件
|
||||||
|
def save_all_tex():
|
||||||
|
with open(file_today_path, 'rb') as f1, open(file_week_path, 'ab+') as f2:
|
||||||
|
f2.write(f1.read())
|
||||||
|
|
||||||
# 获取本机磁盘使用率和剩余空间G信息
|
# 获取本机磁盘使用率和剩余空间G信息
|
||||||
def get_disk_info():
|
def get_disk_info():
|
||||||
|
@ -76,7 +85,7 @@ def get_disk_info():
|
||||||
# 磁盘总空间,单位G
|
# 磁盘总空间,单位G
|
||||||
total_disk_size = disk_info.total//1024//1024//1024
|
total_disk_size = disk_info.total//1024//1024//1024
|
||||||
# 当前磁盘使用率、剩余空间G和磁盘总空间信息
|
# 当前磁盘使用率、剩余空间G和磁盘总空间信息
|
||||||
info = "\t{0}盘使用率:{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".format(disk_name, str(disk_info.percent),free_disk_size,total_disk_size)
|
||||||
# print(info)
|
# print(info)
|
||||||
# 拼接多个磁盘的信息
|
# 拼接多个磁盘的信息
|
||||||
content.append(info)
|
content.append(info)
|
||||||
|
@ -88,7 +97,7 @@ def get_dir_size(path):
|
||||||
list1 = []
|
list1 = []
|
||||||
for item in path.iterdir():
|
for item in path.iterdir():
|
||||||
if item.is_file():
|
if item.is_file():
|
||||||
file_size = file_path.stat().st_size
|
file_size = file_today_path.stat().st_size
|
||||||
list1.append(file_size)
|
list1.append(file_size)
|
||||||
str_tex=f"\t{item}的大小是{file_size}字节"
|
str_tex=f"\t{item}的大小是{file_size}字节"
|
||||||
print(str_tex)
|
print(str_tex)
|
||||||
|
@ -142,8 +151,12 @@ def get_IP():
|
||||||
return(ipv4_list[0])
|
return(ipv4_list[0])
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
#删除之前的文件
|
||||||
|
if file_today_path.exists():
|
||||||
|
file_today_path.unlink()
|
||||||
|
#公共信息
|
||||||
commoninfo = []
|
commoninfo = []
|
||||||
if not file_path.exists():
|
if not file_week_path.exists():
|
||||||
t1 = "本周是第{0}周".format(week)
|
t1 = "本周是第{0}周".format(week)
|
||||||
commoninfo.append(t1)
|
commoninfo.append(t1)
|
||||||
commoninfo.append("")
|
commoninfo.append("")
|
||||||
|
@ -166,6 +179,7 @@ def main():
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
save_all_tex()
|
||||||
#默认不发邮件,通过参数控制
|
#默认不发邮件,通过参数控制
|
||||||
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)
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
@echo off
|
||||||
|
echo 开始获取本机信息,请稍等......
|
||||||
|
|
||||||
|
rem 代理的logs路径
|
||||||
|
set agentlog="D:\agent\ubackup\uagent\logs"
|
||||||
|
|
||||||
|
rem 定义输出的文件
|
||||||
|
for /f "tokens=*" %%i in ('powershell -c "Get-Date -UFormat %%V"') do (
|
||||||
|
set vars=%%i
|
||||||
|
)
|
||||||
|
|
||||||
|
set filename=C:\ComputerInfos_week_%vars%.txt
|
||||||
|
|
||||||
|
echo 本周是第:%vars%周 >>%filename%
|
||||||
|
|
||||||
|
|
||||||
|
SET CurrentTime=%date:~0,10% %time:~0,5%
|
||||||
|
echo 本次运行时间:%CurrentTime% >>%filename%
|
||||||
|
|
||||||
|
|
||||||
|
rem 查看内存使用情况
|
||||||
|
echo 1.内存使用情况:>>%filename%
|
||||||
|
wmic OS get FreePhysicalMemory| findstr /r /v "^$" | more >>%filename%
|
||||||
|
wmic ComputerSystem get TotalPhysicalMemory| findstr /r /v "^$" |more >>%filename%
|
||||||
|
|
||||||
|
rem 查看磁盘使用情况:
|
||||||
|
echo 2.磁盘使用情况:>>%filename%
|
||||||
|
wmic logicaldisk get caption,freespace,size| findstr /r /v "^$"|more >>%filename%
|
||||||
|
|
||||||
|
rem 查看CPU使用情况:
|
||||||
|
echo 3.CPU使用率:>>%filename%
|
||||||
|
wmic cpu get loadpercentage| findstr /r /v "^$"|more >>%filename%
|
||||||
|
|
||||||
|
|
||||||
|
rem 查看日志文件大小:
|
||||||
|
echo 4.日志文件路径:>>%filename%
|
||||||
|
|
||||||
|
dir %agentlog% >>%filename%
|
||||||
|
|
||||||
|
echo "===================%CurrentTime%已经获取系统信息================ " >>%filename%
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
#!/bin/bash
|
||||||
|
#set -x
|
||||||
|
#uagent路径地址
|
||||||
|
uagent_dir=/agent/ubackup/uagent/logs/
|
||||||
|
|
||||||
|
|
||||||
|
#创建存放的目录
|
||||||
|
report_dir="/opt/sys-info/"
|
||||||
|
mkdir -p $report_dir
|
||||||
|
|
||||||
|
IP=`hostname -I|awk '{print $1}'`
|
||||||
|
|
||||||
|
#按周创建文件,一周一个
|
||||||
|
week_number=`date +%V`
|
||||||
|
filename=${IP}-ComputerInfos-week-${week_number}.txt
|
||||||
|
file_path=$report_dir/$filename
|
||||||
|
|
||||||
|
#输出需要的信息
|
||||||
|
now=$(date +"%Y-%m-%d %H:%M:%S")
|
||||||
|
echo "-------------------------------------------" >> $file_path
|
||||||
|
echo "当前时间:$now" >> $file_path
|
||||||
|
echo "当前地址:$IP" >> $file_path
|
||||||
|
|
||||||
|
# CPU
|
||||||
|
echo "" >> $file_path
|
||||||
|
echo "内存信息:">> $file_path
|
||||||
|
free -h >> $file_path
|
||||||
|
|
||||||
|
# uagent_dir的日志大小
|
||||||
|
echo "" >> $file_path
|
||||||
|
echo "uagent日志大小:">> $file_path
|
||||||
|
du -h $uagent_dir >> $file_path
|
||||||
|
|
||||||
|
# 磁盘空间使用情况
|
||||||
|
echo "" >> $file_path
|
||||||
|
echo "磁盘空间使用情况:">> $file_path
|
||||||
|
df -h >> $file_path
|
||||||
|
|
||||||
|
echo "" >> $file_path
|
||||||
|
echo "-------------------------------------------" >> $file_path
|
||||||
|
|
Loading…
Reference in New Issue