- #Auth:tyk
- #Date:2018-11-9
- #Function:RAID info and NIC port check
- #############################################################
- #--coding:utf-8--
- import os,sys,commands
- import MySQLdb
- import datetime,time
- import socket
- import re
- class Mysql_connect(object):
- def init(self,agre):
- self.port=agre[0]
- self.host=agre[1]
- self.passwd=agre[2]
- self.username=agre[3]
- self.result=agre[4]
- def Mysql_deal(self):
- try:
- conn = MySQLdb.connect(user=self.username,passwd=self.passwd,db="cloudboot",host=self.host,port=int(self.port),charset="utf8")
- dt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
- cursor=conn.cursor()
- HW_sn=self.result['SN']
- HW_raid=",".join(self.result['RAID'])
- HW_port=",".join(self.result['ETH']).strip()
- SWITCHE_tor_1=self.result['TOR']['tor_1']
- SWITCHE_tor_2=self.result['TOR']['tor_2']
- SWITCHE_vlan_1=self.result['VLAN']['vlan_1']
- SWITCHE_vlan_2=self.result['VLAN']['vlan_2']
- HW_MAC=self.result['MAC']
- SWITCHE_port_1=self.result['TOR_SWITH']['tor_switch_1']
- SWITCHE_port_2=self.result['TOR_SWITH']['tor_switch_2']
- sql="""insert into hw_info(HW_sn,HW_raid,HW_port,SWITCHE_tor_1,SWITCHE_tor_2,SWITCHE_vlan_1,SWITCHE_vlan_2,HW_MAC,SWITCHE_port_1,SWITCHE_port_2,create_time)values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
- vaule=(HW_sn,HW_raid,HW_port,SWITCHE_tor_1,SWITCHE_tor_2,SWITCHE_vlan_1,SWITCHE_vlan_2,HW_MAC,SWITCHE_port_1,SWITCHE_port_2,dt)
- cursor.execute(sql,vaule)
- cursor.close()
- conn.commit()
- conn.close()
- except MySQLdb.Error,e:
- print "ERROR %d: %s" % (e.args[0],e.args[1])
- sys.exit(1)
- def file_Deal():
- eth_value=[]
- with open ('/tmp/nic_port.txt') as file:
- content=file.readlines()
- for status in content:
- if 'UP' in status.split(" "):
- eth=status.split(":")[1]
- eth_value.append(eth)
- return eth_value
- def raid_Deal():
- raid_info=[]
- Raid_info=[]
- with open ('/tmp/raid_info.txt') as file:
- raidinfo=file.readlines()
- for raid in raidinfo:
- if 'RAID' in raid:
- raid_one=raid.split(" ")
- if raid_one[3]:
- raid_info.append(raid_one[3])
- raid_info=list(set(raid_info))
- for raid_n in raid_info:
- info=raid_n.strip('\n')
- if re.match("^RAID\d",info) != None:
- Raid_info.append(info)
- return Raid_info
- def data_Deal(eth_port):
- eth_value=eth_port
- port=[]
- data=['Ifname','PVID','Tor','MAC:']
- for eth_port in eth_value[0]:
- cmd='lldptool -n -t -i %s>> /tmp/nic_tor.txt'%eth_port
- os.system(cmd)
- for nic_info in data:
- info_cmd="awk /%s/'{print $2}'/tmp/nic_tor.txt"%(nic_info)
- output = commands.getstatusoutput(info_cmd)
- port.append(output)
- info_name="grep -A 1'System Name TLV'/tmp/nic_tor.txt|grep -v'System Name TLV'|sed's/--//g'|awk'{if (length($0))print $0}'"
- output1 = commands.getstatusoutput(info_name)
- return port,output1[1]
- def create_value(eth_port,value1,value_port,value_raid):
- data={}
- vlan={}
- tor={}
- tor_switch={}
- sn_cmd="dmidecode -s system-serial-number 2>/dev/null | awk'/^[^#]/ { print $1 }'"
- sn = commands.getstatusoutput(sn_cmd)
- tor_name=value1[0][1].split()
- vlan_data=value1[1][1].split()
- vlan['vlan_1']=vlan_data[0]
- vlan['vlan_2']=vlan_data[2]
- tor['tor_1']=tor_name[0]
- tor['tor_2']=tor_name[1]
- data['RAID']=value_raid
- data['TOR']=tor
- data['ETH']=eth_port
- data['VLAN']=vlan
- data['SN']=sn[1]
- data['MAC']=",".join(value1[3][1].split())
- data['tor_port']=value_port[1].split('-')
- tor_switch['tor_switch_1']=value_port.split('\n')[0].strip()
- tor_switch['tor_switch_2']=value_port.split('\n')[1].strip()
- data['TOR_SWITH']=tor_switch
- return data
- def main():
- os.system('ip addr> /tmp/nic_port.txt')
- os.system('/opt/MegaRAID/storcli/storcli64 /c0 show> /tmp/raid_info.txt')
- eth_port=file_Deal()
- value_raid=raid_Deal()
- value1,value_port=data_Deal(eth_port)
- data=create_value(eth_port,value1,value_port,value_raid)
- agre = ["3306","192.168.255.135","****","root",data]
- inser = Mysql_connect(agre)
- inser.Mysql_deal()
- if name == 'main':
- try:
- main()
- os.system('rm -fr /tmp/*.txt')
- except Exception as e:
- print e
来源: http://www.bubuko.com/infodetail-2967621.html