#!/usr/bin/env python import sys sys.path.append("/scripts/libvirt_py") import Xen import MySQLdb
def run_sql (conn,sql,status): #print sql if status==1: cursor=conn.cursor() try: cursor.execute(sql) row=cursor.fetchall() except: pass cursor.close() return row else: cursor=conn.cursor() try: cursor.execute(sql) except: pass cursor.close()
def info(mysql_conn,ip,user,password): x=Xen.Info() conn=x.conn(ip,user,password) #print "#"*80 #print "# Physical Matchine Info #" (conn_stat,hostname,uri,freemem)=x.get_phy_info(conn) #print "\tconn_stat:%s hostname:%s uri:%s freemem:%sM" %(conn_stat,hostname,uri,freemem/1024/1024) sql="SELECT ID FROM PHYSICALMATCHINE WHERE IP='%s'" %(ip) row=run_sql(mysql_conn,sql,1) id=''
if row: id=row[0][0] sql="UPDATE PHYSICALMATCHINE SET CONN_STAT=%s,HOSTNAME='%s',URI='%s',FREEMEM=%d,UPDATETIME=now() WHERE ID=%s" %(conn_stat,hostname,uri,freemem/1024,id) else: sql="INSERT INTO PHYSICALMATCHINE (IP,CONN_STAT,HOSTNAME,URI,FREEMEM,UPDATETIME) VALUES('%s','%s','%s','%s','%d',now())" %(ip,conn_stat,hostname,uri,freemem/1024) run_sql(mysql_conn,sql,0)
interfaces=x.get_phy_interface(conn)
sql="DELETE FROM PHYSICALMACS WHERE PHYSICALMATCHINE_ID=%s" %(id) run_sql(mysql_conn,sql,0) sql="DELETE FROM DOMAININFO WHERE PHYSICALMATCHINE_ID=%s" %(id) run_sql(mysql_conn,sql,0) sql="DELETE FROM DOMAIN_FILEINFO WHERE PHYSICALMATCHINE_ID=%s" %(id) run_sql(mysql_conn,sql,0) sql="DELETE FROM DOMAIN_BLOCKINFO WHERE PHYSICALMATCHINE_ID=%s" %(id) run_sql(mysql_conn,sql,0) sql="DELETE FROM DOMAIN_NETWORKINFO WHERE PHYSICALMATCHINE_ID=%s" %(id) run_sql(mysql_conn,sql,0)
for i in interfaces: #print "\tinterface:%s" %(i) j = "%s" % (i) mac= "%s:%s:%s:%s:%s:%s" % (i[4:6],i[7:9],i[10:12],i[13:15],i[16:18],i[19:21]) sql="INSERT INTO PHYSICALMACS (PHYSICALMATCHINE_ID,MAC,UPDATETIME) VALUES(%s,'%s',now())" %(id,mac) run_sql(mysql_conn,sql,0)
#print "# All Domains #" doms=x.get_domains(conn) for domain_name in doms.keys(): #print "\tdomain_name:%s domain_id:%s" %(domain_name,doms[domain_name]) (state,maxmem,usedmem,vcpus,files,blocks,devices,devices_mac)=x.get_domain_info(conn,domain_name) #print "\t\tstate:%s maxmem:%s usedmem:%s vcpus:%s" % (state,maxmem,usedmem,vcpus) sql="INSERT INTO DOMAININFO (PHYSICALMATCHINE_ID,DOMAINNAME,DOMAIN_ID,STATE,MAXMEM,USEDMEM,VCPUS,UPDATETIME) VALUES(%s,'%s','%s','%s','%s','%s','%s',now())" %(id,domain_name,doms[domain_name],state,maxmem/1024,usedmem/1024,vcpus) run_sql(mysql_conn,sql,0)
#print "\t#files#" for f in files.keys(): #print "\t\t%s %s" %(f,files[f]) sql="INSERT INTO DOMAIN_FILEINFO (PHYSICALMATCHINE_ID,DOMAINNAME,FILENAME,DEVICETYPE,UPDATETIME) VALUES(%s,'%s','%s','%s',now())" %(id,domain_name,f,files[f]) run_sql(mysql_conn,sql,0)
#print "\t#blocks#" for b in blocks.keys(): #print "\t\t%s %s" %(b,blocks[b]) sql="INSERT INTO DOMAIN_BLOCKINFO (PHYSICALMATCHINE_ID,DOMAINNAME,FILENAME,DEVICETYPE,UPDATETIME) VALUES(%s,'%s','%s','%s',now())" %(id,domain_name,b,blocks[b]) run_sql(mysql_conn,sql,0)
#print "\t#devices#" for d in devices.keys(): #print "\t\t%s %s" %(d,devices[d]) sql="INSERT INTO DOMAIN_NETWORKINFO (PHYSICALMATCHINE_ID,DOMAINNAME,DEVICENAME,MAC,SOURCE,TARGET,UPDATETIME) VALUES(%s,'%s','%s','%s','%s','%s',now())" %(id,domain_name,devices[d],devices_mac[devices[d]],d,devices[d]) run_sql(mysql_conn,sql,0)
#print "\t#devices_mac#" #for d_m in devices_mac.keys(): #print "\t\t%s %s" %(d_m,devices_mac[d_m]) #print "\t"+"#"*50 #print "#"*80
mysql_conn=MySQLdb.connect(host="localhost",user="root",passwd="",db="XEN") user='xen' password='xen' sql="SELECT IP FROM XENMATCHINE" ips=run_sql(mysql_conn,sql,1) for i in ips: ip=i[0] #print ip try: info(mysql_conn,ip,user,password) except: pass mysql_conn.close()
|