python 监测内存和cpu的使用率实例
作者:ajiong314 时间:2022-07-03 23:49:49
我就废话不多说了,直接上代码吧!
import paramiko
import pymysql
import time
linux = ['192.168.0.179']
def connectHost(ip, uname='shenyuming', passwd='ajiongqqq'):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=uname, password=passwd,port=22)
return ssh
def MainCheck():
try:
while True:
time.sleep(1)
for a in range(len(linux)):
ssh = connectHost(linux[a])
# 查询主机名称
cmd = 'hostname'
stdin, stdout, stderr = ssh.exec_command(cmd)
host_name = stdout.readlines()
host_name = host_name[0]
# 查看当前时间
csj = 'date +%T'
stdin, stdout, stderr = ssh.exec_command(csj)
curr_time = stdout.readlines()
curr_time = curr_time[0]
# 查看cpu使用率,并将信息写入到数据库中(取三次平均值)
cpu = "vmstat 1 3|sed '1d'|sed '1d'|awk '{print $15}'"
stdin, stdout, stderr = ssh.exec_command(cpu)
cpu = stdout.readlines()
cpu_usage = str(round((100 - (int(cpu[0]) + int(cpu[1]) + int(cpu[2])) / 3), 2)) + '%'
# 查看内存使用率,并将信息写入到数据库中
mem = "cat /proc/meminfo|sed -n '1,4p'|awk '{print $2}'"
stdin, stdout, stderr = ssh.exec_command(mem)
mem = stdout.readlines()
mem_total = round(int(mem[0]) / 1024)
mem_total_free = round(int(mem[1]) / 1024) + round(int(mem[2]) / 1024) + round(int(mem[3]) / 1024)
mem_usage = str(round(((mem_total - mem_total_free) / mem_total) * 100, 2)) + "%"
sql = "insert into memory_and_cpu values('%s','%s','%s','%s')" % (
host_name, curr_time, cpu_usage, mem_usage)
db = connectDB()
sqlDML(sql, db)
except:
print("连接服务器 %s 异常" % (linux[a]))
def connectDB(dbname='test11'):
if dbname == 'test11':
db = pymysql.connect("localhost", "root", "shen123", "test11")
return db
def sqlDML(sql, db):
cr = db.cursor()
cr.execute(sql)
db.commit()
cr.close()
#
if __name__ == '__main__':
MainCheck()
来源:https://blog.csdn.net/weixin_41896508/article/details/80859128
标签:python,内存,cpu,使用率
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
通过定位控制信息列表下往上的增加
2008-06-30 14:27:00
PyCharm永久激活方式(推荐)
2023-06-12 00:53:33
![](https://img.aspxhome.com/file/2023/7/59467_0s.png)
python 中文件输入输出及os模块对文件系统的操作方法
2023-04-10 18:34:05
Python读取mat文件,并转为csv文件的实例
2023-01-01 11:03:54
text-overflow实现标题省略号功能
2009-05-19 12:13:00
详谈Python基础之内置函数和递归
2021-12-19 21:55:40
![](https://img.aspxhome.com/file/2023/2/75952_0s.png)
python批量翻译excel表格中的英文
2022-11-16 08:09:07
JS 中如何判断 null
2007-12-13 20:28:00
30个最常用css选择器解析
2011-06-16 20:36:37
Python实现求解一元二次方程的方法示例
2023-09-15 03:29:41
设计MySQL数据库的技巧
2009-09-06 11:56:00
![](https://img.aspxhome.com/file/UploadPic/20099/6/20099612300822s.jpg)
使用Dreamweaver代码片断提高css开发效率
2007-10-28 15:46:00
![](https://img.aspxhome.com/file/UploadPic/200710/28/20071028154950883s.jpg)
python调用百度语音识别实现大音频文件语音识别功能
2023-11-29 00:59:53
全屏窗无提示关闭父窗口
2013-06-30 05:41:36
PHP投票系统防刷票判断流程分析
2023-09-27 13:34:03
![](https://img.aspxhome.com/file/2023/3/55523_0s.gif)
python操作toml文件的示例代码
2023-06-12 03:58:33
JavaScript变量类型以及变量作用域详解
2023-08-12 08:50:17
![](https://img.aspxhome.com/file/2023/5/56005_0s.png)
python中的集合及集合常用的使用方法
2023-05-04 11:14:11
![](https://img.aspxhome.com/file/2023/2/77832_0s.png)
关于pycharm中pip版本10.0无法使用的解决办法
2021-08-27 15:30:47
![](https://img.aspxhome.com/file/2023/9/85019_0s.jpg)
javascript 版 Bad Apple 字符动画
2010-01-28 12:19:00
![](https://img.aspxhome.com/file/UploadPic/20101/28/201012812221315s.jpg)