from urllib import request,parse
from http import cookiejar
import json
import time
import os
# 创建cookiejar的实例
cookie = cookiejar.CookieJar()
# 常见cookie的管理器
cookie_handler = request.HTTPCookieProcessor(cookie)
# 创建http请求的管理器
http_handler = request.HTTPHandler()
# 生成https管理器
https_handler = request.HTTPSHandler()
# 创建请求管理器
opener = request.build_opener(http_handler,https_handler,cookie_handler)
# 登录函数,cookie在opener里
def login():
try:
# 负责首次登录,输入用户名和密码,用来获取cookie
url = 'url'
userName = 'userName'
password = 'password'
param ={
'username':userName,
'password':password
}
# 把数据进行编码
data = parse.urlencode(param)
# 创建一个请求对象
req = request.Request(url,data=data.encode('utf-8'))
# 使用opener发起请求
rsp = opener.open(req)
return json.loads(rsp.read())['success']
except:
return False
#获取虚拟机信息 post方法
def getFatherHost():
url = 'url'
param ={
'page':1,
'rows':99999
}
# 如果已经执行login函数,则opener自动已经包含cookie
data = parse.urlencode(param)
req = request.Request(url,data=data.encode('utf-8'))
rsp = opener.open(req)
return json.loads(rsp.read())['data']['rows']
#获取宿主机监控信息 get方法
def getHostState(hostId,vendorId):
url = 'url'
param = {
't':round(time.time()*1000),
'params':{"time":"REAL","vendorId":vendorId,"name":hostId}
}
data = parse.urlencode(param)
req = request.Request(url = '%s%s%s'%(url,'?',data))
#print('%s%s%s'%(url,'?',data))
rsp = opener.open(req)
return json.loads(rsp.read())['data']
##获取虚拟机信息 post方法
def getChildHost(hostId,vendorId):
param ={
'page':1,
'rows':99999,
'params':[{"param":{"hostId":hostId,"vendorId":vendorId,"isTemplate":'false'},"sign":"EQ"}]
}
url = 'url'
data = parse.urlencode(param)
req = request.Request(url,data=data.encode('utf-8'))
rsp = opener.open(req)
return json.loads(rsp.read())['data']['rows']
##获取上次信息时间和若无文件则创建
def getLastTime(filePath):
if not os.path.exists(filePath):
f=open(filePath,'w')
f.write(filePath+'已创建 \n')
f.close()
return ''
with open(filePath,'r') as f:
lines = f.readlines()
if len(lines)>0:
last_line = lines[-1]
return last_line[0:19]
return ''
if __name__ == '__main__':
##防止文件过大,每天一个新文件
filePath1 = '宿主机信息'+time.strftime("%Y-%m-%d",time.localtime())+'.txt'
##防止文件过大,每月一个新文件
filePath2 = '主机对应虚拟机信息'+time.strftime("%Y-%m",time.localtime())+'.txt'
##当登录成功时
if (login()):
print('登录成功')
f1 = open(filePath1,'a')
f2 = open(filePath2,'a')
##宿主机列表
FatherHosts = getFatherHost()
#timestr2=getLastTime(filePath2)
##获取上次时间戳
timestr1=getLastTime(filePath1)
timelast=''
for host in FatherHosts:
##虚拟机列表信息
childsHosts = getChildHost(host['id'],host['vendorId'])
for childsHost in childsHosts:
f2.write('主机名称:'+host['name']+' 主机IP:'+host['manageIp']+' 虚机名称:'+childsHost['name']+' 虚机IP:'+childsHost['managerIp']+' 虚机状态:'+childsHost['status']+' 数据存储区:'+childsHost['datastores']+' 快照数量:'+str(childsHost['snapshotCount'])+' 磁盘/GB:'+str(childsHost['disk'])+'\n')
##宿主机信息
hostState = getHostState(host['name'],host['vendorId'])
sorts = ['SYSTEM','MEM','CPU','POWER','NET','DISK']
timelast = hostState[sorts[0]]['keys'][-1]
for sort in sorts:
for i in range(len(hostState[sort]['keys'])):
if hostState[sort]['keys'][i]>timestr1:
for j in range(len(hostState[sort]['values'])):
f1.write(hostState[sort]['keys'][i]+' 主机名称:'+host['name']+' 主机IP:'+host['manageIp']+' '+sort+':'+hostState[sort]['values'][j]['name']+':'+hostState[sort]['values'][j]['data'][i]+'\n')
if timelast!='':
f1.write(timelast+'\n')
else:
f1.write("00-00-00 00:00:00\n")
f1.close()
f2.close()
print('done')
else:
print('登录失败')