本文主要是介绍Python管理PVE(Proxmox VE)云平台--节点资源统计2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、前言
承接上文:Python管理PVE(Proxmox VE)云平台–节点资源统计 将cpu
、内存
、磁盘
融入到一个脚本中,新增总资源占用计算模块,更直观的看出剩余未分配资源。
二、步骤
1.代码
# -*- coding: utf-8 -*-
import os
import re
import paramiko# 连接信息:
# host = '192.168.100.103'
port = 22 # 默认的SSH端口
userName = 'root'def BrainTrust_1_Check():password = 'xxxxxxx'for i in range(101, 105):try:# 创建SSHClient 实例对象ssh = paramiko.SSHClient()# 调用方法,表示没有存储远程机器的公钥,允许访问ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 连接远程机器ssh.connect(hostname=f"192.168.100.{i}", port=port, username=userName, password=password)# 组合bash命令为一个字符串bash_cmd = """qm list | awk -F" " '{print $1}' | grep -v VMID > VMID &&i=0 &&vmids=$(cat VMID) &&for vmid in $vmids; doc_number=$(qm config $vmid | grep cores | awk -F" " '{print $2}') &&socket=$(qm config $vmid | grep sockets | awk -F" " '{print $2}') &&sum=$(echo "$c_number * $socket" | bc) &&i=$(echo "$i + $sum" | bc);donej=0 &&vmids=`cat VMID` &&for vmid in $vmidsdom_number=`qm config $vmid | grep memory | awk -F" " '{print $2}'`sum=`echo "$m_number / 1024 "| bc`j=`echo "$j + $sum" | bc`donek=0vmids=`cat VMID` for vmid in $vmidsdob=0qm config $vmid | grep -e ^scsi* | grep -v scsihw | awk -F'[,=G]' '{print $3 }' > DISKqm config $vmid | grep -e ^ide* | grep -v ide2 | awk -F'[,=G]' '{print $3 }' >> DISKdisks=`cat DISK`for disk in $disksdob=`echo "$b + $disk" | bc`donek=`echo "$k + $b" | bc`doneecho "$(hostname)节点Cpu已分配:$i 核、内存已分配:$j G、磁盘已分配:$k G""""# 执行bash命令stdin, stdout, stderr = ssh.exec_command(bash_cmd)# 读取并打印结果result = stdout.read().decode('utf-8')err_result = stderr.read().decode('utf-8')with open('output.txt', 'a') as f:print("执行结果:", result, file=f, end="")# 关闭SSH连接ssh.close()except paramiko.SSHException as sshException:print("SSH异常:", sshException)except TimeoutError:print("执行结果: b'Failed\\n'", "超时端口:", i)except KeyboardInterrupt:print("Ctrl + c 中断运行!")break
def BrainTrust_2_Check():password = 'xxxxxx'for i in range(105, 109):try:# 创建SSHClient 实例对象ssh = paramiko.SSHClient()# 调用方法,表示没有存储远程机器的公钥,允许访问ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 连接远程机器ssh.connect(hostname=f"192.168.100.{i}", port=port, username=userName, password=password)# 组合bash命令为一个字符串bash_cmd = """qm list | awk -F" " '{print $1}' | grep -v VMID > VMID &&i=0 &&vmids=$(cat VMID) &&for vmid in $vmids; doc_number=$(qm config $vmid | grep cores | awk -F" " '{print $2}') &&socket=$(qm config $vmid | grep sockets | awk -F" " '{print $2}') &&sum=$(echo "$c_number * $socket" | bc) &&i=$(echo "$i + $sum" | bc);donej=0 &&vmids=`cat VMID` &&for vmid in $vmidsdom_number=`qm config $vmid | grep memory | awk -F" " '{print $2}'`sum=`echo "$m_number / 1024 "| bc`j=`echo "$j + $sum" | bc`donek=0vmids=`cat VMID` for vmid in $vmidsdob=0qm config $vmid | grep -e ^scsi* | grep -v scsihw | awk -F'[,=G]' '{print $3 }' > DISKqm config $vmid | grep -e ^ide* | grep -v ide2 | awk -F'[,=G]' '{print $3 }' >> DISKdisks=`cat DISK`for disk in $disksdob=`echo "$b + $disk" | bc`donek=`echo "$k + $b" | bc`doneecho "$(hostname)节点Cpu已分配:$i 核、内存已分配:$j G、磁盘已分配:$k G""""# 执行bash命令stdin, stdout, stderr = ssh.exec_command(bash_cmd)# 读取并打印结果result = stdout.read().decode('utf-8')err_result = stderr.read().decode('utf-8')with open('output.txt', 'a') as f:print("执行结果:", result, file=f, end="")# 关闭SSH连接ssh.close()except paramiko.SSHException as sshException:print("SSH异常:", sshException)except TimeoutError:print("执行结果: b'Failed\\n'", "超时端口:", i)except KeyboardInterrupt:print("Ctrl + c 中断运行!")breakdef Operation():# 读取文本文件内容with open('output.txt', 'r') as file:data = file.read()# 提取数值cpu_values = []memory_values = []disk_values = []# 正则表达式匹配Cpu、内存和磁盘的数值pattern = re.compile(r'Cpu已分配:(\d+) 核、内存已分配:(\d+) G、磁盘已分配:(\d+) G')for match in pattern.findall(data):cpu_values.append(int(match[0]))memory_values.append(int(match[1]))disk_values.append(int(match[2]))# 计算每列数值的和total_cpu = sum(cpu_values)total_memory = sum(memory_values)total_disk = sum(disk_values)print(f"Cpu总和: {total_cpu}/256 C")print(f"内存总和: {total_memory}/502 G")print(f"磁盘总和: {total_disk}/13199 G")def DelFile():# 检查文件是否存在file_path = 'output.txt'if os.path.exists(file_path):os.remove(file_path)# print(f"文件 {file_path} 已删除")else:print(f"文件 {file_path} 不存在")if __name__ == '__main__':BrainTrust_1_Check()print("***脑库1资源占用情况***")Operation()DelFile()BrainTrust_2_Check()print("***脑库2资源占用情况***")Operation()DelFile()
2.执行结果
三、结束语
到此Python统计PVE(Proxmox VE)云平台节点资源讲解就完毕啦,如果有其它问题欢迎大家评论留言,如果觉得还可以,那么给作者点赞
、收藏
、关注
吧!
这篇关于Python管理PVE(Proxmox VE)云平台--节点资源统计2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!