最新消息:

kvm 镜像管理

kvm admin 2878浏览 0评论

# -*- coding: utf-8 -*-

import Check_Disk
import ConfigParser
import time
import sys

if __name__==’__main__’:

#时间格式
format=”%Y%m%d-%H%M%S”

#输出查找的ip
son_ip=raw_input(“Please enter vm ip:”)
#    son_ip=”192.168.3.69″

#取配置文件的值
cf=ConfigParser.ConfigParser()
cf.read(“e:snap.conf.txt”)
port=(cf.getint(“snapshot”,”port”))
username=cf.get(“snapshot”,”username”)
pkey=cf.get(“snapshot”,”pkey”)
cmd=cf.get(“snapshot”,”cmd”)
hostname=(cf.get(“snapshot”,”hostname”)).split(“,”)

#查找ip,查看快照
while True:
for i in hostname:
result_list=Check_Disk.ssh2(i,port,username,pkey,cmd)
for j in range(0,len(result_list.split(‘n’))):
if son_ip in result_list.split(‘n’)[j]:
print result_list.split(‘n’)[j]+” in “+i
#取镜像实例名
count=0
new_dict={}
a=result_list.split(‘n’)[j].split(‘.xml’)[0]
cmd2=”virsh snapshot-list”+” “+(result_list.split(‘n’)[j]).split(‘.xml’)[0]
www=Check_Disk.ssh2(i,port,username,pkey,cmd2)
fo=open(“e:yanchao.txt”,”w”)
fo.writelines(www.strip())
fo.close()
fo=open(“e:yanchao.txt”,”r+”)
b=fo.readlines()
for j in range(2,len(b)):
count+=1
c=b[j]
d=c.split()
print “[“,count,”]”,d
new_dict[count]=d[0]
fo.truncate()
fo.close()
#进行快照还是恢复快照
choice=raw_input(“1.Restore or 2.Snapshot or 3.Quit:”)
if choice == “1”:
print new_dict
choice1=int(raw_input(“please enter which one to be restored?”))
choice2=raw_input(“Are you ture? yes/no”)
if choice2==”yes”:
cmd3=”virsh snapshot-revert “+a+” “+new_dict[choice1]+” –force”+” && “+”virsh destroy “+a+” && “+”virsh start “+a
Check_Disk.ssh2(i,port,username,pkey,cmd3)
continue
else:
continue
elif choice == “2”:
choice2=raw_input(“Are you ture? yes/no”)
if choice2==”yes”:
cmd3=”virsh snapshot-create-as –domain “+a+” –name “+time.strftime(format,time.localtime(time.time()))
Check_Disk.ssh2(i,port,username,pkey,cmd3)
continue
else:
continue
elif choice == “3”:
sys.exit()

snap.conf.txt

[snapshot]
port=22
username=root
pkey=e:id_rsa
cmd=ls -l /etc/libvirt/qemu |awk ‘{print $NF}’| grep 192.168
hostname=192.168.2.49,192.168.2.50,192.168.2.51
[wanjun]
yaochao=2

Check_Disk.py

# -*- coding: gb2312 -*-

import paramiko
import os

def ssh2(hostname,port,username,pkey,cmd):
key=paramiko.RSAKey.from_private_key_file(pkey)
s=paramiko.SSHClient()
s.load_system_host_keys()
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
s.connect(hostname,port,username,pkey=key)
stdin,stdout,stderr=s.exec_command(cmd)

return stdout.read()

if __name__==’__main__’:
cmd1=r’dmidecode | grep -i dell|wc -l’
cmd2=r’dmidecode | grep -i hp|wc -l’
cmd3=r’/opt/MegaRAID/MegaCli/MegaCli64 -PDList -aALL | grep “Device Id|Error”‘
cmd4=r’/usr/sbin/hpacucli ctrl slot=0 pd all show’
hostname=[]
ip=raw_input(“请输入ip地址,多个ip地址请用逗号隔开:”)
hostname=ip.split(‘,’)
port=22
username=’root’
pkey=’e:id_rsa’
for i in hostname:
if int(ssh2(i,port,username,pkey,cmd1)) >0:
print i+” is DELL”
print ssh2(i,port,username,pkey,cmd3)
else:
print i+” is HP”
print  ssh2(i,port,username,pkey,cmd4)

os.system(“pause”)

转载请注明:爱开源 » kvm 镜像管理

您必须 登录 才能发表评论!