标签:cmdb python
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import datetime
import urllib2
import json
import warnings
import MySQLdb
warnings.filterwarnings("ignore")
_url = "http://172.16.8.71/api/query/get/"
server_resultColumn = [ "SvrAssetId","SvrOperator","SvrBakOperator","serverLanIP","innerNetIdc","outerNetIdc","BsiPath",‘serverBusi2‘,‘EqsName‘ ]
class CMDBHelperError(Exception): pass
class CMDBHelper(object):
def __init__(self, qdata=None):
self.qdata = {}
self.data_field = []
self.data_field_map = {}
self.data_field_map1 = {}
self.total_count = 0
self.result = []
self.qdata["params"] = {}
self.qdata["params"]["content"] = {}
self.qdata["params"]["content"]["schemeId"] = ""
self.qdata["params"]["content"]["type"] = "Json"
self.qdata["params"]["content"]["version"] = "1.0"
self.qdata["params"]["content"]["dataFormat"] = "dict"
self.qdata["params"]["content"]["requestInfo"] = {}
self.qdata["params"]["content"]["resultColumn"] = {}
self.qdata["params"]["content"]["pagingInfo"] = {}
self.qdata["params"]["content"]["orderBy"] = ""
self.qdata["params"]["content"]["conditionLogical"] = ""
self.qdata["params"]["content"]["searchCondition"] = {}
self.qdata["params"]["content"]["requestInfo"]["systemId"] = "201206250"
self.qdata["params"]["content"]["requestInfo"]["sceneId"] = "1"
self.qdata["params"]["content"]["requestInfo"]["requestModule"] = ""
self.qdata["params"]["content"]["requestInfo"]["operator"] = ""
self.qdata["params"]["content"]["pagingInfo"] = {"startIndex":"0","returnTotalRows":"0"}
def add_schemeId(self, schemeId="Server"):
if schemeId not in ["Server", "Netdevice", "IPResource", "Idc", "modify_timestamp"]:
raise CMDBHelperError("add_schemeId parameter error")
self.qdata["params"]["content"]["schemeId"] = schemeId
def change_requestInfo(self, requestInfo):
if type(requestInfo) is not dict:
raise CMDBHelperError("change_requestInfo parameter error")
elif not requestInfo:
raise CMDBHelperError("change_requestInfo parameter is empty")
for k in requestInfo.keys():
self.qdata["params"]["content"]["requestInfo"][k] = requestInfo[k]
def add_resultColumn(self, resultColumn):
if type(resultColumn) is not list:
raise CMDBHelperError("add_resultColumn parameter error")
elif not resultColumn:
raise CMDBHelperError("add_resultColumn parameter is empty")
for i in resultColumn:
self.qdata["params"]["content"]["resultColumn"][i] = ""
def change_pagingInfo(self, pagingInfo):
if type(pagingInfo) is not dict or \
set(pagingInfo.keys()) != set(["startIndex", "pageSize", "returnTotalRows"]):
raise CMDBHelperError("change_pagingInfo parameter error")
elif not pagingInfo:
raise CMDBHelperError("change_pagingInfo parameter is empty")
for k in pagingInfo.keys():
self.qdata["params"]["content"]["pagingInfo"][k] = pagingInfo[k]
def add_orderBy(self, orderBy):
if type(orderBy) is not str or \
orderBy not in self.qdata["params"]["content"]["resultColumn"].keys():
raise CMDBHelperError("add_orderBy parameter error")
self.qdata["params"]["content"]["orderBy"] = orderBy
def add_conditionLogical(self, conditionLogical):
if type(conditionLogical) is not str:
raise CMDBHelperError("add_conditionLogical parameter error")
self.qdata["params"]["content"]["conditionLogical"] = conditionLogical
def add_searchCondition(self, searchCondition):
if type(searchCondition) is not dict:
raise CMDBHelperError("add_searchCondition parameter error")
for k in searchCondition.keys():
self.qdata["params"]["content"]["searchCondition"][k] = searchCondition[k]
def do_query(self):
try:
fd = urllib2.urlopen(_url, json.dumps(self.qdata), 60)
content = json.load(fd)
try:
if content["dataSet"]["header"]["returnCode"] != 0:
raise CMDBHelperError("CMDB return error, errorCode: %s, errorInfo: %s" %
(content["dataSet"]["header"]["errorCode"],
content["dataSet"]["header"]["errorInfo"]))
self.total_count = content["dataSet"]["header"]["totalRows"]
except KeyError, e:
raise CMDBHelper(u"do_query error, %s" % e)
self.data_field = [i["id"] for i in content["dataSet"]["fieldDef"]]
for i in content["dataSet"]["fieldDef"]:
self.data_field_map[i["id"]] = i["name"]
self.data_field_map1[i["id"]] = (i["name"], i["dataType"])
self.result = content["dataSet"]["data"]
self.total_count = len(content["dataSet"]["data"])
except urllib2.HTTPError, e:
raise CMDBHelperError(u"urllib2.HTTPError, %s" % e)
except urllib2.URLError, e:
raise CMDBHelperError(u"urllib2.URLError, %s" % e)
except Exception, e:
raise CMDBHelperError(u"Unkown Error, %s" % e)
def show_result(self):
if self.result is []:
raise CMDBHelperError("there is no result")
for i in enumerate(self.result):
return i[1]
def asset_storage(self,line):
try:
conn=MySQLdb.connect(host=‘localhost‘,user="root",passwd="",db="Assetdata",charset=‘utf8‘ )
cur=conn.cursor()
dt= datetime.datetime.now().strftime("%Y-%m-%d")
COLstr=‘‘
ColumnStyle=‘ VARCHAR(300)‘
TableName="t_asset_info"
dic=line
dic[‘update_time‘]=dt
for key in dic.keys():
COLstr=COLstr+‘ ‘+key+ColumnStyle+‘,‘
try:
cur.execute("CREATE TABLE IF NOT EXISTS %s (%s)"%(TableName,COLstr[:-1]))
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
for i in enumerate(self.result):
ROWstr=‘‘
data=i[1]
# data[‘update_time‘]=dt
data[‘update_time‘]="2016-10-25"
for key in data.keys():
ROWstr=(ROWstr+‘"%s"‘+‘,‘)%(data[key])
cur.execute("SELECT * FROM %s"%(TableName))
cur.execute("INSERT INTO %s VALUES (%s)"%(TableName,ROWstr[:-1]))
conn.commit()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
finally:
cur.close()
conn.close()
def asset_data(self):
try:
now_data=[]
yes_data=[]
new_add=[]
old_del=[]
now_time=time.strftime(‘%Y-%m-%d‘)
now_times = datetime.datetime.now()
yes_times = now_times + datetime.timedelta(days=-1)
yes_time = yes_times.strftime(‘%Y-%m-%d‘)
sql="select SvrAssetId from t_asset_info where update_time=\"%s\""%now_time
sql_old="select SvrAssetId from t_asset_info where update_time=\"%s\""%yes_time
conn=MySQLdb.connect(host=‘localhost‘,user="root",passwd="",db="Assetdata",charset=‘utf8‘)
cur=conn.cursor()
cur.execute(sql)
result=cur.fetchall()
cur.execute(sql_old)
result_old=cur.fetchall()
for results in result:
results_new = ‘‘.join(results)
now_data.append(results_new)
for results_old in result_old:
result_old_new= ‘‘.join(results_old)
yes_data.append(result_old_new)
new_num=len(now_data)
old_num=len(yes_data)
for new_id in range(0,new_num):
if now_data[new_id] not in yes_data:
new_add.append(now_data[new_id])
for old_id in range(0,old_num):
if yes_data[old_id] not in now_data:
old_del.append(yes_data[old_id])
return new_add,old_del
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
finally:
cur.close()
conn.close()
def asset_table(self,new,old):
try:
conn=MySQLdb.connect(host=‘localhost‘,user="root",passwd="",db="Assetdata",charset=‘utf8‘ )
cur=conn.cursor()
if len(new):
for insert_data in new:
sql_add="select * from t_asset_info where SvrAssetId=\"%s\""%insert_data
cur.execute(sql_add)
res_n=cur.fetchall()
cur.execute("INSERT INTO t_asset_table VALUES (\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\")"%(res_n[0][0],res_n[0][1],res_n[0][2],res_n[0][3],res_n[0][4],+1))
conn.commit()
if len(old):
for insert_data in old:
sql_del="select * from t_asset_info where SvrAssetId=\"%s\""%insert_data
cur.execute(sql_del)
res_o=cur.fetchall()
cur.execute("INSERT INTO t_asset_table VALUES (\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\")"%(res_o[0][0],res_o[0][1],res_o[0][2],res_o[0][3],res_o[0][4],-1))
conn.commit()
except MySQLdb.Error,e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
finally:
cur.close()
conn.close()
def main(schemeId="Server"):
q = CMDBHelper()
q.add_schemeId(schemeId)
q.add_resultColumn(eval("%s_resultColumn" % schemeId.lower()))
q.add_searchCondition({"serverBusi2":["TMP","TSC","GSLB","CMDB","Ticket","内部公共服务"]})
q.do_query()
line=q.show_result()
q.asset_storage(line)
new_result,old_result=q.asset_data()
q.asset_table(new_result,old_result)
if __name__ == "__main__":
print str(datetime.datetime.now()).center(79, "-")
main()
print str(datetime.datetime.now()).center(79, "-")
本文出自 “12758454” 博客,谢绝转载!
标签:cmdb python
原文地址:http://12768454.blog.51cto.com/12758454/1973977