一、前言
写本脚本的初衷是因手动查看统计已分配的PVE
资源过于耗时,因此写一个脚本一劳永逸,具体实现方法:利用Python的paramiko
模块进行远程命令查看、统计PVE
平台各节点已分配的cpu、内存、磁盘空间。
二、步骤
1.构建shell脚本
1.1 统计对象取值(cpu、mem、disk)
获取虚拟机ID
qm list | awk -F" " '{print $1}'|grep -v VMID > VMID
获取cpu核数
# 总核 = 插槽数量 * 核心数量
qm config VMID | grep cores | awk -F" " '{print $2}'
获取mem数据
qm config VMID | grep memory | awk -F" " '{print $2}'
获取disk数据
# 存在多块磁盘现象
qm config VMID | grep -e ^scsi* | grep -v scsihw | awk -F'[,=G]' '{print $3 }'
1.2 sum_cpu.sh
#bin/bash
#计算单节点已分配cpu核数
qm list | awk -F" " '{print $1}'|grep -v VMID > VMIDi=0
vmids=`cat VMID`
for vmid in $vmidsdoc_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`
done
echo "$(hostname)节点Cpu已分配:$i 核"
1.3 sum_mem.sh
#bin/bash
#计算单节点已分配mem
qm list | awk -F" " '{print $1}'|grep -v VMID > VMIDi=0
vmids=`cat VMID`
for vmid in $vmidsdom_number=`qm config $vmid | grep memory | awk -F" " '{print $2}'`sum=`echo "$m_number / 1024 "| bc`i=`echo "$i + $sum" | bc`
done
echo "$(hostname)节点内存已分配:$i G"
rm -rf VMID
1.4 sum_disk.sh
#bin/bash
#计算单节点已分配disk
qm list | awk -F" " '{print $1}'|grep -v VMID > VMIDi=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`donei=`echo "$i + $b" | bc`
done
echo "$(hostname)节点磁盘已分配:$i G"
2.构建python脚本
2.1 Cpu_Query.py
# -*- coding: utf-8 -*-
import paramiko# 连接信息:
# host = '192.168.100.103'
port = 22 # 默认的SSH端口
userName = 'root'
password = '******'def SafeCheck():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);done &&echo "$(hostname)节点Cpu已分配:$i 核""""# 执行bash命令stdin, stdout, stderr = ssh.exec_command(bash_cmd)# 读取并打印结果result = stdout.read().decode('utf-8')err_result = stderr.read().decode('utf-8')print("执行结果:", result, "执行ip:", i)# 关闭SSH连接ssh.close()except paramiko.SSHException as sshException:print("SSH异常:", sshException)except TimeoutError:print("执行结果: b'Failed\\n'", "超时端口:", i)except KeyboardInterrupt:print("Ctrl + c 中断运行!")breakif __name__ == '__main__':SafeCheck()
执行结果:
2.2 Disk_Query.py
# -*- coding: utf-8 -*-
import paramiko# 连接信息:
# host = '192.168.100.103'
port = 22 # 默认的SSH端口
userName = 'root'
password = 'adfmaoijo1869#!@%'def SafeCheck():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 > VMIDi=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`donei=`echo "$i + $b" | bc`doneecho "$(hostname)节点磁盘已分配:$i G""""# 执行bash命令stdin, stdout, stderr = ssh.exec_command(bash_cmd)# 读取并打印结果result = stdout.read().decode('utf-8')err_result = stderr.read().decode('utf-8')print("执行结果:", result, "执行ip:", i)# 关闭SSH连接ssh.close()except paramiko.SSHException as sshException:print("SSH异常:", sshException)except TimeoutError:print("执行结果: b'Failed\\n'", "超时端口:", i)except KeyboardInterrupt:print("Ctrl + c 中断运行!")breakif __name__ == '__main__':SafeCheck()
执行结果:
2.3 Mem_Query.py
# -*- coding: utf-8 -*-
import paramiko# 连接信息:
# host = '192.168.100.103'
port = 22 # 默认的SSH端口
userName = 'root'
password = 'adfmaoijo1869#!@%'def SafeCheck():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 $vmidsdom_number=`qm config $vmid | grep memory | awk -F" " '{print $2}'`sum=`echo "$m_number / 1024 "| bc`i=`echo "$i + $sum" | bc`doneecho "$(hostname)节点内存已分配:$i G"rm -rf VMID"""# 执行bash命令stdin, stdout, stderr = ssh.exec_command(bash_cmd)# 读取并打印结果result = stdout.read().decode('utf-8')err_result = stderr.read().decode('utf-8')print("执行结果:", result, "执行ip:", i)# 关闭SSH连接ssh.close()except paramiko.SSHException as sshException:print("SSH异常:", sshException)except TimeoutError:print("执行结果: b'Failed\\n'", "超时端口:", i)except KeyboardInterrupt:print("Ctrl + c 中断运行!")breakif __name__ == '__main__':SafeCheck()
执行结果:
三、结束语
到此Python
统计PVE(Proxmox VE)云平台节点资源讲解就完毕啦,如果有其它问题欢迎大家评论留言,如果觉得还可以,那么给作者点赞、收藏、关注吧!