def host_add_batch(request): """ 批量添加主机 """ if request.method == 'POST': multi_hosts = request.POST.get('batch').split('\n') for host in multi_hosts: if host == '': break node_name, eth1, remote_port, idc, host_application, hard_info, comment = host.split( ) hard_info = ast.literal_eval(hard_info) cpu, memory, hard_disk = hard_info[0:3] if Host.objects.filter(eth1=eth1): emg = u'添加失败, 该eth1的IP %s 已存在!' % eth1 return my_render('assets/host_add_batch.html', locals(), request) idc = IDC.objects.get(name=idc) asset = Host(node_name=node_name, eth1=eth1, remote_port=remote_port, idc=idc, host_application=host_application, cpu=cpu, memory=memory, hard_disk=hard_disk, editor=comment) asset.save() smg = u'批量添加成功.' return my_render('assets/host_add_batch.html', locals(), request) return my_render('assets/host_add_batch.html', locals(), request)
def create_asset(self, ignore_errs=False): data = self.request.POST.get('asset_info') if data: try: data = json.loads(data) data_set = { 'hostname': self.clean_data.get('hostname'), 'ipaddress': self.clean_data.get('ipaddress'), 'macaddress': self.clean_data.get('macaddress'), 'os_type': self.clean_data.get('os_type'), 'os_version': self.clean_data.get('os_version'), 'Manufactory': self.clean_data.get('Manufactory'), 'sn': self.clean_data.get('sn'), 'cpu_model': self.clean_data.get('cpu_model'), 'cpu_num': self.clean_data.get('cpu_num'), 'cpu_physical': self.clean_data.get('cpu_physical'), 'memory': self.clean_data.get('memory'), 'disk': self.clean_data.get('disk'), } host = Host(**data_set) host.save() asset_obj = Host.objects.get(sn=self.clean_data.get('sn')) data['asset_id'] = asset_obj.id self.mandatory_check(data) self.clean_data = data if not self.response['error']: return True except Exception as e: self.response_msg('error', 'ObjectCreationException', 'Object [server] {}'.format(str(e)))
def host_add_batch(request): """ 批量添加主机 """ if request.method == 'POST': multi_hosts = request.POST.get('batch').split('\n') for host in multi_hosts: if host == '': break print host print len(host.split("@")) print host.split("@") node_name, cpu, memory, hard_disk, number, brand, eth1, eth2, internal_ip, idc, comment, = host.split( '@') print idc asset = Host(node_name=node_name, number=number, brand=brand, cpu=cpu, memory=memory, hard_disk=hard_disk, eth1=eth1, eth2=eth2, internal_ip=internal_ip, editor=comment) asset.save() smg = u'批量添加成功.' return my_render('assets/host_add_batch.html', locals(), request) return my_render('assets/host_add_batch.html', locals(), request)
def host_add_batch(request): """ 批量添加主机 """ if request.method == 'POST': multi_hosts = request.POST.get('batch').split('\n') for host in multi_hosts: if host == '': break number, brand, hard_info, eth1, eth2, internal_ip, idc, comment = host.split( '!@') hard_info = ast.literal_eval(hard_info) cpu, memory, hard_disk = hard_info[0:3] idc = IDC.objects.get(name=idc) asset = Host(number=number, brand=brand, idc=idc, cpu=cpu, memory=memory, hard_disk=hard_disk, eth1=eth1, eth2=eth2, internal_ip=internal_ip, editor=comment) asset.save() smg = u'批量添加成功.' return my_render('assets/host_add_batch.html', locals(), request) return my_render('assets/host_add_batch.html', locals(), request)
def batadd(request): """ 批量添加debian主机 'idc', 'eth1', 'eth2', 'barnd', 'system', 'system_cpuarch', 'cabinet', 'editor', 'business', 'sort', 'use_for', 'service_line' """ check_auth("bat_add_host") eth1 = request.GET['eth1'] idc = 1 brand = "Other" system = "Debian" system_cpuarch = "x86_64" editor = request.GET['editor'] sort = "cloud" vm = 0 host = Host(node_name=eth1, idc_id=idc, eth1=eth1, brand=brand, system=system, system_cpuarch=system_cpuarch, editor=editor, sort=sort, vm=vm, status=1) test = host.save() if test: content = {"node_name": eth1, "idc": idc, "eth1": eth1, "brand": brand, "system": system, "system_cpuarch": system_cpuarch, "editor": editor, "sort": sort, "vm": vm} else: content = {"node_name": eth1} return HttpResponse(json.dumps(content, ensure_ascii=False, indent=4,))
def host_add_batch_bak(request): """ 批量添加主机 """ if request.method == 'POST': multi_hosts = request.POST.get('batch').split('\n') for host in multi_hosts: if host == '': break ip, hostname, idc, service, brand, comment, pip = host.split() idc = get_object_or_404(IDC, name=idc) services = [] for s in ast.literal_eval(service): services.append(get_object_or_404(Service, name=s.strip())) if Host.objects.filter(eth1=ip): emg = u'添加失败, 该IP%s已存在' % ip return my_render('assets/host_add_batch.html', locals(), request) if pip != '[]': pip = Host.objects.get(eth1=ast.literal_eval(pip)[0]) asset = Host(node_name=hostname, eth1=ip, idc=idc, brand=brand, editor=comment, vm=pip) else: asset = Host(node_name=hostname, eth1=ip, idc=idc, brand=brand, editor=comment) asset.save() asset.service = services asset.save() smg = u'批量添加成功.' return my_render('assets/host_add_batch.html', locals(), request) return my_render('assets/host_add_batch.html', locals(), request)
def CmdbUpdate(request): """ sn编号查询,返回记录IP """ import requests url = "http://192.168.8.80:8000/cmdb/" s = requests.get(url) rst = s.json() data = rst.get("result") idc = IDC.objects.get(pk="9b70bec5660441c2ae10908da1db38d3") for i in data: result = i.get("fields") try: host_data = Host.objects.get(eth1=result.get("eth1")) host_data.cpu = result.get("cpu", "") host_data.memory = result.get("memory", "") host_data.hard_disk = result.get("hard_disk", "") host_data.brand = result.get("brand", "Dell R410") host_data.editor = result.get("editor", "") host_data.number = result.get("number", "") host_data.eth2 = result.get("eth2", "") host_data.save() except: cmdb = Host(node_name=result.get("node_name"), eth1=result.get("eth1"), mac=result.get("mac"), internal_ip=result.get("internal_ip"), brand=result.get("brand"), cpu=result.get("cpu"), idc=idc, system="CentOS", system_cpuarch="X86_64", cabinet=result.get("cabinet"), server_cabinet_id=result.get("server_cabinet_id"), number=result.get("number"), editor=result.get("editor"), status=result.get("status", 1), room_number=result.get("room_number"), hard_disk=result.get("hard_disk"), eth2=result.get("eth2", ""), memory=result.get("memory", ""), server_sn=result.get("server_sn", "")) cmdb.save() return HttpResponse( json.dumps( { "status": 403, "result": u"参数不正确" }, ensure_ascii=False, indent=4, ))
def CmdbUpdate(request): """ sn编号查询,返回记录IP """ import requests url = "http://192.168.8.80:8000/cmdb/" s = requests.get(url) rst = s.json() data = rst.get("result") idc = IDC.objects.get(pk="9b70bec5660441c2ae10908da1db38d3") for i in data: result = i.get("fields") try: host_data = Host.objects.get(eth1=result.get("eth1")) host_data.cpu = result.get("cpu", "") host_data.memory = result.get("memory", "") host_data.hard_disk = result.get("hard_disk", "") host_data.brand = result.get("brand", "Dell R410") host_data.editor = result.get("editor", "") host_data.number = result.get("number", "") host_data.eth2 = result.get("eth2", "") host_data.save() except: cmdb = Host(node_name=result.get("node_name"), eth1=result.get("eth1"), mac=result.get("mac"), internal_ip=result.get("internal_ip"), brand=result.get("brand"), cpu=result.get("cpu"), idc=idc, system="CentOS", system_cpuarch="X86_64", cabinet=result.get("cabinet"), server_cabinet_id=result.get("server_cabinet_id"), number=result.get("number"), editor=result.get("editor"), status=result.get("status", 1), room_number=result.get("room_number"), hard_disk=result.get("hard_disk"), eth2=result.get("eth2", ""), memory=result.get("memory", ""), server_sn=result.get("server_sn", "")) cmdb.save() return HttpResponse(json.dumps({"status": 403, "result": u"参数不正确"}, ensure_ascii=False, indent=4, ))
def host_add_batch(request): """ 批量添加主机 """ if request.method == 'POST': multi_hosts = request.POST.get('batch').split('\n') for host in multi_hosts: if host == '': break number, brand, hard_info, eth1, eth2, internal_ip, idc, comment = host.split('!@') hard_info = ast.literal_eval(hard_info) cpu, memory, hard_disk = hard_info[0:3] idc = IDC.objects.get(name=idc) asset = Host(number=number, brand=brand, idc=idc, cpu=cpu, memory=memory, hard_disk=hard_disk, eth1=eth1, eth2=eth2, internal_ip=internal_ip, editor=comment) asset.save() smg = u'批量添加成功.' return my_render('assets/host_add_batch.html', locals(), request) return my_render('assets/host_add_batch.html', locals(), request)
def host_add_batch(request): """ 批量添加主机 """ if request.method == 'POST': multi_hosts = request.POST.get('batch').split('\n') for host in multi_hosts: if host == '': break print host print len(host.split("@")) print host.split("@") node_name, cpu, memory, hard_disk, number, brand, eth1, eth2, internal_ip, idc, comment, = host.split('@') print idc asset = Host(node_name=node_name, number=number, brand=brand, cpu=cpu, memory=memory, hard_disk=hard_disk, eth1=eth1, eth2=eth2, internal_ip=internal_ip, editor=comment) asset.save() smg = u'批量添加成功.' return my_render('assets/host_add_batch.html', locals(), request) return my_render('assets/host_add_batch.html', locals(), request)
def add(self, name, ip, state=None, *, group, persion): ''' 添加主机,group必须存在,因为多对多原因,group必须是一个queryset对象 ''' _persion_instance = Manager_persion.objects.filter( persion=persion).first() is_have = Host.objects.filter(ip_address=ip).first() if not is_have: new_data = Host(host_name=name, ip_address=ip, running_state=0, manager_persion=_persion_instance) new_data.save() _mongo = MongoBase() _mongo.add(ip) _group_instance = Group.objects.filter(name=group).first() logger_info.info('用户:%s 添加主机:%s' % (request_header.current_user(self.request), name)) Operation_logs(username=request_header.current_user(self.request), operation_msg="添加主机:%s" % name).save() new_data.group_name.add(_group_instance) return True
def xls_select(request): """ http监控列表 """ data = xlrd.open_workbook('/Users/voilet/Documents/cmdb.xls') table = data.sheet_by_name(u'cmdb') nrows = table.nrows s = 0 for i in range(1, nrows): ip = str(table.cell(i, 9).value).strip() # if Host.objects.filter(eth1=ip).count() == 0: # Host.objects.get(eth1=ip) try: rst = Host.objects.get(eth1=ip) if table.cell(i, 6).value and table.cell(i, 7).value: rst.cabinet = str(table.cell(i, 6).value) rst.server_cabinet_id = int(table.cell(i, 7).value) rst.system_cpuarch = u"x86_64" else: print ip rst.brand = u"%s" % str(table.cell(i, 4).value) rst.save() except Host.DoesNotExist: # print str(ip).strip() s += 1 node_conf = str(table.cell(i, 5).value).split("/") ctc = IDC.objects.get(name="亦庄电信") cnc = IDC.objects.get(name="北京联通") ip_search = str(ip).split(".") cnc1 = "%s.%s.%s" % (ip_search[0], ip_search[1], ip_search[2]) # rst = Host(cabinet=str(table.cell(i, 6).value), server_cabinet_id=int(table.cell(i, 7).value)) # print len(node_conf) if len(node_conf) == 3: print ip # print ip_search, type(ip_search) rst = Host( eth1=ip, node_name=ip, internal_ip=ip, memory=node_conf[1], hard_disk=node_conf[-1], cpu=node_conf[0], system="CentOS", cabinet=table.cell(i, 6).value, number=table.cell(i, 1).value, server_sn=table.cell(i, 2).value, Services_Code=table.cell(i, 3).value, server_cabinet_id=table.cell(i, 7).value, editor=table.cell(i, 12).value, brand=str(table.cell(i, 4).value), status=1, ) else: print ip print "*" * 100 rst = Host( eth1=ip, node_name=ip, internal_ip=ip, system="CentOS", cabinet=table.cell(i, 6).value, number=table.cell(i, 1).value, server_sn=table.cell(i, 2).value, Services_Code=table.cell(i, 3).value, editor=table.cell(i, 12).value, brand=str(table.cell(i, 4).value), status=1, ) if str(ip_search[2] ) == "219" or cnc1 == "114.66.198" or cnc1 == "123.125.20": rst.idc = cnc else: rst.idc = ctc if str(table.cell(i, 4).value) == u"虚拟机": rst.type = 0 if table.cell(i, 6).value and table.cell(i, 7).value: rst.cabinet = str(table.cell(i, 6).value) rst.server_cabinet_id = int(table.cell(i, 7).value) rst.system_cpuarch = u"x86_64" rst.save() # print rst # rst.save() print s return HttpResponse( json.dumps({ "retCode": 200, "retMsg": "ok" }, ensure_ascii=False, indent=4))
def Index_add_batch(request): ''' 批量添加资产 ''' status = check_auth(request, "bat_add_host") if not status: return render_to_response('default/error_auth.html', locals(), context_instance=RequestContext(request)) content = {} batch_error = [] batch_ok = [] test = {} server_type = Project.objects.all() content["server_type"] = server_type idc_name = IDC.objects.all() idc_list = [i.name for i in idc_name] if request.method == 'POST': uf = request.POST["add_batch"] for i in uf.split("\r\n"): i = i.split() if len(i) == 6: #判断主机名 node_name = i[0] #检测主机名是否合法 mac = str(i[1]).replace(':', '-') if not verifyDomainNameFormart(node_name, idc_list) \ or not MAC_formart(mac) \ or i[2] not in ["default", "openstack"] \ or i[4] not in ["CentOS", "Debian"] \ or i[3] not in ["DELL", "HP", "Other"] \ or i[5] not in ["x86_64", "i386", "X86_64"]: error_return = "{nodename}:{name_return} {MAC}:{mac_return} {System_usage}:{System_usage_return} {system}:{system_return} {brand}:{brand_return} {system_cpuarch}:{system_cpuarch_return}" \ .format( nodename=i[0], name_return=verifyDomainNameFormart(node_name, idc_list), # eth1=i[1], System_usage=i[2], System_usage_return=i[2] in ["default", "openstack", "cloud"], system=i[4], system_return=i[4] in ["CentOS", "Debian"], brand=i[3], brand_return=i[3] in ["DELL", "HP", "Other"], system_cpuarch=i[5], system_cpuarch_return=i[5] in ["x86_64", "i386", "X86_64"] ) # test[i[0]] = {i[0]: verifyDomainNameFormart(node_name, idc_list), i[1]: MAC_formart(mac), i[2]: i[2] in ["default", "openstack"], i[4]: i[4] in ["CentOS", "Debian"], i[3]: i[3] in ["DELL", "HP", "Other"], i[5]: i[5] in ["x86_64", "i386"]} print error_return batch_error.append(error_return) else: batch_ok.append(i) else: batch_error.append(i) for i in batch_ok: print i[0], str(i[1]).replace(':', '-'), i[2], i[3], i[4], i[5] mac = str(i[1]).replace(':', '-') host = Host(node_name=i[0], eth1=i[1], sort=i[2], brand=i[3], system=i[4], system_cpuarch=i[5]) host.save() # print batch_error content["error"] = batch_error content["batch"] = False content.update(csrf(request)) return render_to_response('assets/add_batch.html', content, context_instance=RequestContext(request)) else: content.update(csrf(request)) content["batch"] = True return render_to_response('assets/add_batch.html', content, context_instance=RequestContext(request))
def hostSyncTask(): try: try: group = HostGroup.objects.get(name='备用组') except HostGroup.DoesNotExist: group = HostGroup(name='备用组') group.save() api = SaltAPI() rsp = api.minions() datas = rsp['return'][0] onlines = set() updateTime = now() for saltId, data in datas.items(): serialNumber = data['serialnumber'] try: host = Host.objects.get(serialNumber=serialNumber) except Host.DoesNotExist: # 新建的机器全部放在备用组 host = Host(serialNumber=serialNumber, hostGroup=group) ips = data['ip_interfaces'] if 'eth0' in ips and len(ips['eth0']) > 0: host.ip = ips['eth0'][0] if 'eth1' in ips and len(ips['eth1']) > 0: host.ipEth1 = ips['eth1'][0] host.kernel = data['kernel'] host.os = data['os'] host.osArch = data['osarch'] host.osRelease = data['osrelease'] host.saltId = saltId host.saltStatus = Host.ONLINE host.updateTime = updateTime host.save() onlines.add(serialNumber) for host in Host.objects.all(): if host.serialNumber not in onlines: host.saltStatus = Host.OFFLINE host.save() return 0 except Exception: return 1
def xls_cdn(request): """ http监控列表 """ data = xlrd.open_workbook('/Users/voilet/Documents/cdn.xls') cdn_data = data.sheet_by_name(u'cdn') cdn_result = cdn_data.nrows for s in range(1, cdn_result): ip = str(cdn_data.cell(s, 9).value).strip() sn = str(cdn_data.cell(s, 4).value).strip() idc = str(cdn_data.cell(s, 14).value).strip() internal_ip = str(cdn_data.cell(s, 10).value).strip() number = str(cdn_data.cell(s, 3).value).strip() server_sn = str(cdn_data.cell(s, 18).value).strip() Services_Code = str(cdn_data.cell(s, 19).value).strip() cpu = str(cdn_data.cell(s, 6).value).strip() memory = str(cdn_data.cell(s, 7).value).strip() hard_disk = str(cdn_data.cell(s, 8).value).strip() cabinet = str(cdn_data.cell(s, 13).value).strip() if len(ip) > 0: print ip, idc, internal_ip, number, server_sn, Services_Code, cpu, memory, hard_disk, cabinet try: idc_data = IDC.objects.get(name=idc) except: uuid = str(cmdb_uuid()).replace("-", "") idc_data = IDC( name=idc, pk=uuid, phone=11111111111, linkman=u"曾阳", address=idc, ) idc_data.save() try: rst = Host.objects.get(eth1=ip) rst.idc = idc_data except Host.DoesNotExist: rst = Host( eth1=ip, idc=idc_data, node_name=ip, internal_ip=internal_ip, system="CentOS", status=1, ) # rst.save() # print error_list # print len(error_list) # s = open("/Users/voilet/test.txt", "r") # rst = s.readlines() # # print rst # result = {} # for i in rst: # i = str(i).split() # if len(i) == 3: # ip = i[-1] # idc = i[0] # sn = i[1] # result[sn] = {"idc": idc, "ip": ip} # # print sn, idc, ip, "ok" # else: # print i, "error" # s.close() # s = 0 # for i in range(1, nrows): # ip = str(table.cell(i, 9).value).strip() # # if Host.objects.filter(eth1=ip).count() == 0: # # Host.objects.get(eth1=ip) # try: # Host.objects.get(eth1=ip) # except Host.DoesNotExist: # # print str(ip).strip() # s += 1 # node_conf = str(table.cell(i, 5).value).split("/") # ctc = IDC.objects.get(name="亦庄电信") # cnc = IDC.objects.get(name="北京联通") # ip_search = str(ip).split(".")[2] # if len(node_conf) != 1: # # # print ip_search, type(ip_search) # # rst = Host(eth1=ip, # node_name=ip, # internal_ip=ip, # memory=node_conf[1], # hard_disk=node_conf[-1], # cpu=node_conf[0], # system="CentOS", # cabinet=table.cell(i, 6).value, # number=table.cell(i, 1).value, # server_sn=table.cell(i, 2).value, # Services_Code=table.cell(i, 3).value, # server_cabinet_id=table.cell(i, 7).value, # editor=table.cell(i, 12).value, # # idc=cnc, # status=1, # ) # # else: # print ip # rst = Host(eth1=ip, # node_name=ip, # internal_ip=ip, # system="CentOS", # cabinet=table.cell(i, 6).value, # number=table.cell(i, 1).value, # server_sn=table.cell(i, 2).value, # Services_Code=table.cell(i, 3).value, # editor=table.cell(i, 12).value, # status=1, # ) # if str(ip_search) == "219": # rst.idc = cnc # else: # rst.idc = ctc # rst.save() # # # print rst # # rst.save() # print s return HttpResponse( json.dumps({ "retCode": 200, "retMsg": "ok" }, ensure_ascii=False, indent=4))
#!/usr/bin/env python #-*- coding: utf-8 -*- #============================================================================= # FileName: # Desc: # Author: 苦咖啡 # Email: [email protected] # HomePage: http://blog.kukafei520.net # Version: 0.0.1 # LastChange: # History: #============================================================================= from assets.models import Host, IDC, Server_System, Cores, System_os, system_arch server_data = open('/Users/voilet/opsautomation/jumei_optools/scripts/server_list.txt', 'r') data = server_data.readlines() server_data.close() for i in data: s = i.split() # print i host_data = Host(node_name=s[0], idc_id=1, eth1=s[0], brand="DELL", internal_ip=s[1], cabinet=s[2], editor=s[3], auto_install=1, sort="default") host_data.save()
def xls_select(request): """ http监控列表 """ data = xlrd.open_workbook("/Users/voilet/Documents/cmdb.xls") table = data.sheet_by_name(u"cmdb") nrows = table.nrows s = 0 for i in range(1, nrows): ip = str(table.cell(i, 9).value).strip() # if Host.objects.filter(eth1=ip).count() == 0: # Host.objects.get(eth1=ip) try: rst = Host.objects.get(eth1=ip) if table.cell(i, 6).value and table.cell(i, 7).value: rst.cabinet = str(table.cell(i, 6).value) rst.server_cabinet_id = int(table.cell(i, 7).value) rst.system_cpuarch = u"x86_64" else: print ip rst.brand = u"%s" % str(table.cell(i, 4).value) rst.save() except Host.DoesNotExist: # print str(ip).strip() s += 1 node_conf = str(table.cell(i, 5).value).split("/") ctc = IDC.objects.get(name="亦庄电信") cnc = IDC.objects.get(name="北京联通") ip_search = str(ip).split(".") cnc1 = "%s.%s.%s" % (ip_search[0], ip_search[1], ip_search[2]) # rst = Host(cabinet=str(table.cell(i, 6).value), server_cabinet_id=int(table.cell(i, 7).value)) # print len(node_conf) if len(node_conf) == 3: print ip # print ip_search, type(ip_search) rst = Host( eth1=ip, node_name=ip, internal_ip=ip, memory=node_conf[1], hard_disk=node_conf[-1], cpu=node_conf[0], system="CentOS", cabinet=table.cell(i, 6).value, number=table.cell(i, 1).value, server_sn=table.cell(i, 2).value, Services_Code=table.cell(i, 3).value, server_cabinet_id=table.cell(i, 7).value, editor=table.cell(i, 12).value, brand=str(table.cell(i, 4).value), status=1, ) else: print ip print "*" * 100 rst = Host( eth1=ip, node_name=ip, internal_ip=ip, system="CentOS", cabinet=table.cell(i, 6).value, number=table.cell(i, 1).value, server_sn=table.cell(i, 2).value, Services_Code=table.cell(i, 3).value, editor=table.cell(i, 12).value, brand=str(table.cell(i, 4).value), status=1, ) if str(ip_search[2]) == "219" or cnc1 == "114.66.198" or cnc1 == "123.125.20": rst.idc = cnc else: rst.idc = ctc if str(table.cell(i, 4).value) == u"虚拟机": rst.type = 0 if table.cell(i, 6).value and table.cell(i, 7).value: rst.cabinet = str(table.cell(i, 6).value) rst.server_cabinet_id = int(table.cell(i, 7).value) rst.system_cpuarch = u"x86_64" rst.save() # print rst # rst.save() print s return HttpResponse(json.dumps({"retCode": 200, "retMsg": "ok"}, ensure_ascii=False, indent=4))