linux利用python和必要的库,实时监测计算机的基本运行参数:如CPU负荷、内存占用率、温度等等,每秒获取一次数据,然后保存到一个excel表格文件中,连续监测600秒。在首行需要添加参数文字说明。文件名称格式:年月日时分秒;产一个word报告文件:包括1个标题、2个子标题及段落,写出2段数据分析文字,计算出最大值、最小值及平均值,并插入相关的数据波形和必要的LEGEND文字、图编号、图文字说明等
时间: 2024-02-06 17:11:08 浏览: 180
要实现这个功能,需要使用一些 Python 库,如 psutil、openpyxl、matplotlib 和 python-docx。下面是一个示例代码,用于实时监测计算机的基本运行参数,并将数据保存到 Excel 文件中,并生成一个包含数据分析和图表的 Word 报告。
```python
import psutil
from datetime import datetime
import openpyxl
from openpyxl.utils import get_column_letter
from openpyxl.chart import LineChart, Reference
from matplotlib import pyplot as plt
from docx import Document
from docx.shared import Inches
# 获取当前时间,并格式化为文件名
now = datetime.now()
filename = now.strftime('%Y%m%d%H%M%S')
# 创建 Excel 文件,并写入参数说明
workbook = openpyxl.Workbook()
worksheet = workbook.active
worksheet.title = 'System Monitor'
worksheet.append(['Time', 'CPU Usage', 'Memory Usage', 'Temperature'])
for col in worksheet.columns:
col[0].font = openpyxl.styles.Font(bold=True)
for col_num, column_title in enumerate(['A', 'B', 'C', 'D'], 1):
cell = worksheet.cell(row=1, column=col_num)
cell.value = column_title
cell.font = openpyxl.styles.Font(bold=True)
# 创建 Word 文件,并写入标题和子标题
document = Document()
document.add_heading('System Monitor Report', 0)
document.add_heading('System Parameters', level=1)
# 初始化参数列表和曲线数据
times = []
cpu_usages = []
mem_usages = []
temperatures = []
# 每秒获取参数,并写入 Excel 文件
for i in range(600):
# 获取参数
now = datetime.now()
cpu_usage = psutil.cpu_percent()
mem_usage = psutil.virtual_memory().percent
temperature = psutil.sensors_temperatures().get('coretemp')[0].current
# 写入 Excel 文件
row = (now, cpu_usage, mem_usage, temperature)
worksheet.append(row)
# 保存数据到列表中
times.append(now)
cpu_usages.append(cpu_usage)
mem_usages.append(mem_usage)
temperatures.append(temperature)
# 等待 1 秒
time.sleep(1)
# 生成数据分析报告
cpu_usage_max = max(cpu_usages)
cpu_usage_min = min(cpu_usages)
cpu_usage_avg = sum(cpu_usages) / len(cpu_usages)
mem_usage_max = max(mem_usages)
mem_usage_min = min(mem_usages)
mem_usage_avg = sum(mem_usages) / len(mem_usages)
temperature_max = max(temperatures)
temperature_min = min(temperatures)
temperature_avg = sum(temperatures) / len(temperatures)
document.add_paragraph('CPU Usage:')
document.add_paragraph('Max: {}%'.format(cpu_usage_max))
document.add_paragraph('Min: {}%'.format(cpu_usage_min))
document.add_paragraph('Average: {}%'.format(cpu_usage_avg))
document.add_paragraph('Memory Usage:')
document.add_paragraph('Max: {}%'.format(mem_usage_max))
document.add_paragraph('Min: {}%'.format(mem_usage_min))
document.add_paragraph('Average: {}%'.format(mem_usage_avg))
document.add_paragraph('Temperature:')
document.add_paragraph('Max: {}°C'.format(temperature_max))
document.add_paragraph('Min: {}°C'.format(temperature_min))
document.add_paragraph('Average: {}°C'.format(temperature_avg))
# 生成 CPU Usage 图表
chart = LineChart()
data = Reference(worksheet, min_col=2, min_row=2, max_col=2, max_row=worksheet.max_row)
chart.add_data(data, titles_from_data=True)
chart.title = 'CPU Usage'
chart.x_axis.title = 'Time'
chart.y_axis.title = 'Usage (%)'
chart.legend.position = 'r'
chart.legend.layout = 'vertical'
worksheet.add_chart(chart, 'F2')
# 生成 Memory Usage 图表
chart = LineChart()
data = Reference(worksheet, min_col=3, min_row=2, max_col=3, max_row=worksheet.max_row)
chart.add_data(data, titles_from_data=True)
chart.title = 'Memory Usage'
chart.x_axis.title = 'Time'
chart.y_axis.title = 'Usage (%)'
chart.legend.position = 'r'
chart.legend.layout = 'vertical'
worksheet.add_chart(chart, 'F18')
# 生成 Temperature 图表
chart = LineChart()
data = Reference(worksheet, min_col=4, min_row=2, max_col=4, max_row=worksheet.max_row)
chart.add_data(data, titles_from_data=True)
chart.title = 'Temperature'
chart.x_axis.title = 'Time'
chart.y_axis.title = 'Temperature (°C)'
chart.legend.position = 'r'
chart.legend.layout = 'vertical'
worksheet.add_chart(chart, 'F34')
# 保存 Excel 文件和 Word 文件
workbook.save('system_monitor_{}.xlsx'.format(filename))
document.save('system_monitor_{}.docx'.format(filename))
```
上面的代码可以实时监测计算机的基本运行参数,并将参数数据保存到 Excel 文件中。然后,根据这些数据生成一个包含数据分析和图表的 Word 报告。其中,CPU Usage、Memory Usage 和 Temperature 分别对应三个图表。这些图表的数据使用 Matplotlib 库生成。
阅读全文