gcp虚拟机信息抓取
拉取gcp虚拟机信息 需要调用到基础接口脚本里的数据库读写和google.auth库
from google.auth import compute_engine
from google.oauth2 import service_account
from googleapiclient.discovery import build
from google.cloud import resourcemanager_v3
from BasicInterfaces import executeMysqlData
import json,requests,time
'''拉取 gcp虚拟机信息'''
#没加入拉取的项目写在这条注释后面
serviceAccount = '<xxx.json>'
credentials = service_account.Credentials.from_service_account_file(serviceAccount)
compute = build('compute', 'v1',credentials= credentials)
project_ids = {"显示名称":"实际项目名称"}
for project_id in project_ids:
request = compute.instances().aggregatedList(project= project_ids[project_id])
# print (request)
while request is not None:
try:
response = request.execute()
except:
print (f" get project {project _ id} information failed! skip... ")
break
for name, instances_scoped_list in response['items'].items():
for instance in instances_scoped_list.get('instances',[]):
hostname = instance['name']
if 'canIpForward' in instance:
#是否有nat
if instance['canIpForward'] == 'false':
forward = θ
else:
forward = 1
else:
forward = θ
zone = instance['zone'].split(ˈ/ˈ)[-1]
for networkInfo in instance[ˈnetworkInterfacesˈ]:
if 'accessConfigs' in networkInfo:
#查询是否有公网ip
for accessConfigInfo in networkInfo['accessConfigs']:
if 'natIP' in accessConfigInfo:
extip = accessConfigInfo['natIP']
else:
extip = 'None'
else:
extip = 'None'
intip = networkInfo['networkIP']
if 'labels' in instance:
labels = json. dumps(str(instance[' labels']).replace("'",'"'))
else:
labels = """+ json. dumps({})+ """
if 'items' in instance['tags']:
tagList = instance['tags']['items']
tag = '.'. join(tagList)
else:
tag = ' None'
id = instance['id']
machineType = instance['machineType'].split('/')[-1]
# print (f' project:{project _ id}, name:{hostname}, forward:{forward}, zone:{zone}, extip:{extip},intip:{intip}, labels:{labels}, tags={tag}, id={id}')
sqlForCheckDup = f""" select * from gcpinfo where project={"'" + project_id +"'"} and name={"'" + hostname + "'"}"""
result = executeMysqlData(1,'read',sqlForCheckDup)
if len(result) == θ:
print (f'插入新数据{extip}')
sql= f"""insert into gcpinfo(project, name, extip, intip, forward, label, tags, machinetype,cloud) VALUES({"'" + project_id + "'"},{"'" + hostname + "'"},{"'" + extip + "'"},{"'"+ intip + "'"},{forward},{labels},{"'" + tag + "'"},{"'" + machineType + "'"},'gcp')"""
# print (sql)
executeMysqlData(1,' write', sql)
else:
print (f'更新新数据{extip}:{labels}')
sqlForUpdate = f""" update gcpinfo set extip={"'" + extip + "'"},intip={"'" + intip+ "'"},label={labels},tags={"'" + tags + "'"},machineType={"'" + machineType + "'"} WHERE project = {"'" + project_id + "'"} and name={"'" + hostname + "'"}"""
executeMysqlData(1,'write',sqlForUpdate)
request = compute.instances().aggregatedList_next(previous_request=request,previous_response=response)
