python 脚本实现cpu监控
时间: 2023-09-12 08:07:50 浏览: 121
以下是一个示例 Python 脚本,用于实现 CPU 监控:
```python
import psutil
import time
# 循环获取 CPU 使用率
while True:
cpu_percent = psutil.cpu_percent()
print("CPU 使用率:{}%".format(cpu_percent))
time.sleep(1)
```
psutil 库可以用来获取系统资源使用情况,包括 CPU、内存、磁盘等。在上述示例中,我们使用 `psutil.cpu_percent()` 方法来获取当前 CPU 使用率,并且通过 `time.sleep(1)` 方法来使程序每秒钟检测一次 CPU 使用率。你可以通过修改 `time.sleep()` 的参数来调整监控的频率。
相关问题
Python编写监控脚本
编写一个监控脚本可以分为以下几个步骤:
1. 确定需要监控的指标:这可以是服务器的 CPU 使用率、内存使用率、磁盘空间、网络流量等等。
2. 选择合适的模块:Python有很多第三方模块可以用来监控指标,例如psutil、pydf、speedtest-cli等等。
3. 编写代码:根据选择的模块,编写代码来获取并处理指标数据。可以将数据写入日志文件,或者通过邮件、短信等方式将监控结果发送给管理员。
以下是一个简单的示例代码:
```
import psutil
import logging
logging.basicConfig(filename='monitor.log', level=logging.INFO)
while True:
cpu_usage = psutil.cpu_percent()
mem_usage = psutil.virtual_memory().percent
disk_usage = psutil.disk_usage('/').percent
logging.info(f'CPU usage: {cpu_usage}%, Memory usage: {mem_usage}%, Disk usage: {disk_usage}%')
# 这里可以将监控结果通过邮件、短信等方式发送给管理员
time.sleep(60) # 每隔60秒进行一次监控
```
在这个示例中,我们使用了psutil模块来获取CPU使用率、内存使用率和磁盘使用率,并将结果写入日志文件。在while循环中,我们每隔60秒进行一次监控。管理员可以通过查看日志文件来了解服务器的运行情况,或者通过邮件、短信等方式接收监控结果。
python3 运维监控脚本
下面是一个简单的 Python3 运维监控脚本示例,可以用于监控服务器的 CPU、内存、磁盘等指标:
```python
import psutil
def check_cpu_usage():
cpu_usage = psutil.cpu_percent(interval=1)
if cpu_usage > 80:
print("CPU usage is too high: %d%%" % cpu_usage)
def check_memory_usage():
virtual_memory = psutil.virtual_memory()
swap_memory = psutil.swap_memory()
if virtual_memory.percent > 80:
print("Virtual memory usage is too high: %d%%" % virtual_memory.percent)
if swap_memory.percent > 80:
print("Swap memory usage is too high: %d%%" % swap_memory.percent)
def check_disk_usage():
partitions = psutil.disk_partitions()
for partition in partitions:
usage = psutil.disk_usage(partition.mountpoint)
if usage.percent > 80:
print("Disk usage is too high: %d%%" % usage.percent)
if __name__ == '__main__':
check_cpu_usage()
check_memory_usage()
check_disk_usage()
```
这个示例使用了 Python 的 psutil 库来获取系统指标信息。其中,check_cpu_usage() 函数检查 CPU 使用率是否过高,check_memory_usage() 函数检查虚拟内存和交换内存使用率是否过高,check_disk_usage() 函数检查磁盘使用率是否过高。
你可以根据自己的需求添加或修改这些函数,例如加入网络流量、进程状态等检查。此外,你还可以使用定时任务工具(如 crontab 或 Windows 计划任务)来定时运行这个脚本,实现自动化监控。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)