예제 #1
0
def assets_facts(request,args=None):
    if request.method == "POST" and request.user.has_perm('OpsManage.change_server_assets'):
        server_id = request.POST.get('server_id')
        genre = request.POST.get('type')
        if genre == 'setup':
            try:
                server_assets = Server_Assets.objects.get(id=request.POST.get('server_id'))
                if server_assets.keyfile == 1:resource = [{"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username}] 
                else:resource = [{"hostname": server_assets.ip, "port": server_assets.port,"username": server_assets.username, "password": server_assets.passwd}]
            except Exception,ex:
                logger.error(msg="更新资产失败: {ex}".format(ex=str(ex)))
                return  JsonResponse({'msg':"数据更新失败-查询不到该主机资料~","code":502})
            ANS = ANSRunner(resource)
            ANS.run_model(host_list=[server_assets.ip],module_name='setup',module_args="")
            data = ANS.handle_cmdb_data(ANS.get_model_result())
            if data:
                for ds in data:
                    status = ds.get('status')
                    if status == 0:
                        try:
                            assets = Assets.objects.get(id=server_assets.assets_id) 
                            Assets.objects.filter(id=server_assets.assets_id).update(sn=ds.get('serial'),model=ds.get('model'),
                                                                                     manufacturer=ds.get('manufacturer'))
                        except Exception, ex:
                            logger.error(msg="获取服务器信息失败: {ex}".format(ex=str(ex)))
                            return  JsonResponse({'msg':"数据更新失败-查询不到该主机的资产信息","code":403})
                        try:
                            Server_Assets.objects.filter(id=server_id).update(cpu_number=ds.get('cpu_number'),kernel=ds.get('kernel'),
                                                                                  selinux=ds.get('selinux'),hostname=ds.get('hostname'),
                                                                                  system=ds.get('system'),cpu=ds.get('cpu'),
                                                                                  disk_total=ds.get('disk_total'),cpu_core=ds.get('cpu_core'),
                                                                                  swap=ds.get('swap'),ram_total=ds.get('ram_total'),
                                                                                  vcpu_number=ds.get('vcpu_number')
                                                                                  )
                            recordAssets.delay(user=str(request.user),content="修改服务器资产:{ip}".format(ip=server_assets.ip),type="server",id=server_assets.id)
                        except Exception, ex:
                            logger.error(msg="更新服务器信息失败: {ex}".format(ex=str(ex)))
                            return JsonResponse({'msg':"数据更新失败-写入数据失败","code":400})
                        for nk in ds.get('nks'):
                            macaddress = nk.get('macaddress')
                            count = NetworkCard_Assets.objects.filter(assets=assets,macaddress=macaddress).count()
                            if count > 0:
                                try:
                                    NetworkCard_Assets.objects.filter(assets=assets,macaddress=macaddress).update(assets=assets,device=nk.get('device'),
                                                                                                                       ip=nk.get('address'),module=nk.get('module'),
                                                                                                                       mtu=nk.get('mtu'),active=nk.get('active'))
                                except Exception, ex:
                                    logger.warn(msg="更新服务器网卡信息失败: {ex}".format(ex=str(ex)))
                            else:
                                try:
                                    NetworkCard_Assets.objects.create(assets=assets,device=nk.get('device'),
                                                                  macaddress=nk.get('macaddress'),
                                                                  ip=nk.get('address'),module=nk.get('module'),
                                                                  mtu=nk.get('mtu'),active=nk.get('active'))
                                except Exception, ex:
                                    logger.warn(msg="写入服务器网卡信息失败: {ex}".format(ex=str(ex)))
예제 #2
0
def assets_facts(request,args=None):
    if request.method == "POST" and request.user.has_perm('OpsManage.change_server_assets'):
        server_id = request.POST.get('server_id')
        genre = request.POST.get('type')
        if genre == 'setup':
            try:
                server_assets = Server_Assets.objects.get(id=request.POST.get('server_id'))
                if server_assets.keyfile == 1:resource = [{"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username}] 
                else:resource = [{"hostname": server_assets.ip, "port": server_assets.port,"username": server_assets.username, "password": server_assets.passwd}]
            except Exception,e:
                return  JsonResponse({'msg':"数据更新失败-查询不到该主机资料~","code":502})
            ANS = ANSRunner(resource)
            ANS.run_model(host_list=[server_assets.ip],module_name='setup',module_args="")
            data = ANS.handle_cmdb_data(ANS.get_model_result())
            if data:
                for ds in data:
                    status = ds.get('status')
                    if status == 0:
                        try:
                            assets = Assets.objects.get(id=server_assets.assets_id) 
                            Assets.objects.filter(id=server_assets.assets_id).update(sn=ds.get('serial'),model=ds.get('model'),
                                                                                     manufacturer=ds.get('manufacturer'))
                        except Exception,e:
                            return  JsonResponse({'msg':"数据更新失败-查询不到该主机的资产信息","code":403})
                        try:
                            Server_Assets.objects.filter(id=server_id).update(cpu_number=ds.get('cpu_number'),kernel=ds.get('kernel'),
                                                                                  selinux=ds.get('selinux'),hostname=ds.get('hostname'),
                                                                                  system=ds.get('system'),cpu=ds.get('cpu'),
                                                                                  disk_total=ds.get('disk_total'),cpu_core=ds.get('cpu_core'),
                                                                                  swap=ds.get('swap'),ram_total=ds.get('ram_total'),
                                                                                  vcpu_number=ds.get('vcpu_number')
                                                                                  )
                            recordAssets.delay(user=str(request.user),content="修改服务器资产:{ip}".format(ip=server_assets.ip),type="server",id=server_assets.id)
                        except Exception,e:
                            print e
                            return JsonResponse({'msg':"数据更新失败-写入数据失败","code":400})
                        for nk in ds.get('nks'):
                            macaddress = nk.get('macaddress')
                            count = NetworkCard_Assets.objects.filter(assets=assets,macaddress=macaddress).count()
                            if count > 0:
                                try:
                                    NetworkCard_Assets.objects.filter(assets=assets,macaddress=macaddress).update(assets=assets,device=nk.get('device'),
                                                                                                                       ip=nk.get('address'),module=nk.get('module'),
                                                                                                                       mtu=nk.get('mtu'),active=nk.get('active'))
                                except Exception, ex:
                                    print ex
                            else:
                                try:
                                    NetworkCard_Assets.objects.create(assets=assets,device=nk.get('device'),
                                                                  macaddress=nk.get('macaddress'),
                                                                  ip=nk.get('address'),module=nk.get('module'),
                                                                  mtu=nk.get('mtu'),active=nk.get('active'))
                                except Exception, ex:
                                    print ex
예제 #3
0
파일: assets.py 프로젝트: sunmmi/OpsManage
def assets_batch(request):
    if request.method == "POST":
        fList = []
        sList = []
        resource = []
        serList = []
        if request.POST.get('model') == 'update':
            for ast in request.POST.getlist('assetsIds[]'):
                try:
                    assets = Assets.objects.get(id=int(ast))
                except Exception, ex:
                    print ex
                    continue
                if assets.assets_type in ['vmser','server']:
                    try:
                        server_assets = Server_Assets.objects.get(assets=assets)

                    except Exception, ex:
                        fList.append(assets.management_ip)
                        continue
                    serList.append(server_assets.ip)
                    if server_assets.keyfile == 1:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username})
                    else:resource.append({"hostname": server_assets.ip, "port": server_assets.port,"username": server_assets.username, "password": server_assets.passwd})                    
            ANS = ANSRunner(resource)
            ANS.run_model(host_list=serList,module_name='setup',module_args="")
            data = ANS.handle_cmdb_data(ANS.get_model_result())    
            if data:
                for ds in data:
                    status = ds.get('status')
                    if status == 0:
                        try:
                            Server_Assets.objects.filter(ip=ds.get('ip')).update(cpu_number=ds.get('cpu_number'),kernel=ds.get('kernel'),
                                                                                  selinux=ds.get('selinux'),hostname=ds.get('hostname'),
                                                                                  system=ds.get('system'),cpu=ds.get('cpu'),
                                                                                  disk_total=ds.get('disk_total'),cpu_core=ds.get('cpu_core'),
                                                                                  swap=ds.get('swap'),ram_total=ds.get('ram_total'),
                                                                                  vcpu_number=ds.get('vcpu_number')
                                                                                  )
                            sList.append(server_assets.ip)
                        except Exception:
                            fList.append(server_assets.ip)
                    else:fList.append(server_assets.ip)                                  
            if sList:
                return JsonResponse({'msg':"数据更新成功","code":200,'data':{"success":sList,"failed":fList}}) 
            else:return JsonResponse({'msg':"数据更新失败","code":500,'data':{"success":sList,"failed":fList}}) 
예제 #4
0
파일: assets.py 프로젝트: sunmmi/OpsManage
def assets_facts(request,args=None):
    if request.method == "POST" and request.user.has_perm('OpsManage.change_server_assets'):
        server_id = request.POST.get('server_id')
        genre = request.POST.get('type')
        if genre == 'setup':
            try:
                server_assets = Server_Assets.objects.get(id=request.POST.get('server_id'))
                if server_assets.keyfile == 1:resource = [{"hostname": server_assets.ip, "port": int(server_assets.port)}] 
                else:resource = [{"hostname": server_assets.ip, "port": server_assets.port,"username": server_assets.username, "password": server_assets.passwd}]
            except Exception,e:
                return  JsonResponse({'msg':"数据更新失败-查询不到该主机资料~","code":502})
            ANS = ANSRunner(resource)
            ANS.run_model(host_list=[server_assets.ip],module_name='setup',module_args="")
            data = ANS.handle_cmdb_data(ANS.get_model_result())
            if data:
                for ds in data:
                    status = ds.get('status')
                    if status == 0:
                        try:
                            Assets.objects.filter(id=server_assets.assets_id).update(sn=ds.get('serial'),model=ds.get('model'),
                                                                                     manufacturer=ds.get('manufacturer'))
                        except Exception,e:
                            return  JsonResponse({'msg':"数据更新失败-查询不到该主机的资产信息","code":403})
                        try:
                            Server_Assets.objects.filter(id=server_id).update(cpu_number=ds.get('cpu_number'),kernel=ds.get('kernel'),
                                                                                  selinux=ds.get('selinux'),hostname=ds.get('hostname'),
                                                                                  system=ds.get('system'),cpu=ds.get('cpu'),
                                                                                  disk_total=ds.get('disk_total'),cpu_core=ds.get('cpu_core'),
                                                                                  swap=ds.get('swap'),ram_total=ds.get('ram_total'),
                                                                                  vcpu_number=ds.get('vcpu_number')
                                                                                  )
                            recordAssets.delay(user=str(request.user),content="修改服务器资产:{ip}".format(ip=server_assets.ip),type="server",id=server_assets.id)
                        except Exception,e:
                            print e
                            return JsonResponse({'msg':"数据更新失败-写入数据失败","code":400})
                    else:
                        return JsonResponse({'msg':"数据更新失败-无法链接主机~","code":502})                    
                return JsonResponse({'msg':"数据更新成功","code":200})
예제 #5
0
def assets_batch(request):
    if request.method == "POST":
        fList = []
        sList = []
        resource = []
        serList = []
        if request.POST.get('model') == 'update':
            for ast in request.POST.getlist('assetsIds[]'):
                try:
                    assets = Assets.objects.get(id=int(ast))
                except Exception, ex:
                    print ex
                    continue
                if assets.assets_type in ['vmser', 'server']:
                    try:
                        server_assets = Server_Assets.objects.get(
                            assets=assets)

                    except Exception, ex:
                        fList.append(assets.management_ip)
                        continue
                    serList.append(server_assets.ip)
                    if server_assets.keyfile == 1:
                        resource.append({
                            "hostname": server_assets.ip,
                            "port": int(server_assets.port),
                            "username": server_assets.username
                        })
                    else:
                        resource.append({
                            "hostname": server_assets.ip,
                            "port": server_assets.port,
                            "username": server_assets.username,
                            "password": server_assets.passwd
                        })
            ANS = ANSRunner(resource)
            ANS.run_model(host_list=serList,
                          module_name='setup',
                          module_args="")
            data = ANS.handle_cmdb_data(ANS.get_model_result())
            if data:
                for ds in data:
                    status = ds.get('status')
                    if status == 0:
                        assets = Server_Assets.objects.get(
                            ip=ds.get('ip')).assets
                        try:
                            Server_Assets.objects.filter(
                                ip=ds.get('ip')).update(
                                    cpu_number=ds.get('cpu_number'),
                                    kernel=ds.get('kernel'),
                                    selinux=ds.get('selinux'),
                                    hostname=ds.get('hostname'),
                                    system=ds.get('system'),
                                    cpu=ds.get('cpu'),
                                    disk_total=ds.get('disk_total'),
                                    cpu_core=ds.get('cpu_core'),
                                    swap=ds.get('swap'),
                                    ram_total=ds.get('ram_total'),
                                    vcpu_number=ds.get('vcpu_number'))
                            sList.append(server_assets.ip)
                        except Exception:
                            fList.append(server_assets.ip)
                        for nk in ds.get('nks'):
                            macaddress = nk.get('macaddress')
                            count = NetworkCard_Assets.objects.filter(
                                assets=assets, macaddress=macaddress).count()
                            if count > 0:
                                try:
                                    NetworkCard_Assets.objects.filter(
                                        assets=assets,
                                        macaddress=macaddress).update(
                                            assets=assets,
                                            device=nk.get('device'),
                                            ip=nk.get('address'),
                                            module=nk.get('module'),
                                            mtu=nk.get('mtu'),
                                            active=nk.get('active'))
                                except Exception, ex:
                                    print ex
                            else:
                                try:
                                    NetworkCard_Assets.objects.create(
                                        assets=assets,
                                        device=nk.get('device'),
                                        macaddress=nk.get('macaddress'),
                                        ip=nk.get('address'),
                                        module=nk.get('module'),
                                        mtu=nk.get('mtu'),
                                        active=nk.get('active'))
                                except Exception, ex:
                                    print ex
                    else:
                        fList.append(server_assets.ip)
예제 #6
0
파일: sched.py 프로젝트: laowa/CMDB-02
def updateAssets():
    sList = []
    resource = []    
    for assets in Assets.objects.all():
        if assets.assets_type in ['vmser','server']:
            try:
                server_assets = Server_Assets.objects.get(assets=assets)
            except Exception, ex:
                print ex
                continue
            sList.append(server_assets.ip)
            if server_assets.keyfile == 1:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username})
            else:resource.append({"hostname": server_assets.ip, "port": server_assets.port,"username": server_assets.username, "password": server_assets.passwd})  
    ANS = ANSRunner(resource)
    ANS.run_model(host_list=sList,module_name='setup',module_args="")
    data = ANS.handle_cmdb_data(ANS.get_model_result())    
    if data:
        for ds in data:
            status = ds.get('status')
            if status == 0:
                try:
                    assets = Server_Assets.objects.get(ip=ds.get('ip')).assets
                    Server_Assets.objects.filter(ip=ds.get('ip')).update(cpu_number=ds.get('cpu_number'),kernel=ds.get('kernel'),
                                                                          selinux=ds.get('selinux'),hostname=ds.get('hostname'),
                                                                          system=ds.get('system'),cpu=ds.get('cpu'),
                                                                          disk_total=ds.get('disk_total'),cpu_core=ds.get('cpu_core'),
                                                                          swap=ds.get('swap'),ram_total=ds.get('ram_total'),
                                                                          vcpu_number=ds.get('vcpu_number')
                                                                          )
                    sList.append(server_assets.ip)
                except Exception, ex:
예제 #7
0
def assets_batch(request):
    if request.method == "POST":
        fList = []
        sList = []
        resource = []
        serList = []
        if request.POST.get('model') == 'update':
            for ast in request.POST.getlist('assetsIds[]'):
                try:
                    assets = Assets.objects.get(id=int(ast))
                except Exception, ex:
                    print ex
                    continue
                if assets.assets_type in ['vmser','server']:
                    try:
                        server_assets = Server_Assets.objects.get(assets=assets)

                    except Exception, ex:
                        fList.append(assets.management_ip)
                        continue
                    serList.append(server_assets.ip)
                    if server_assets.keyfile == 1:resource.append({"hostname": server_assets.ip, "port": int(server_assets.port),"username": server_assets.username})
                    else:resource.append({"hostname": server_assets.ip, "port": server_assets.port,"username": server_assets.username, "password": server_assets.passwd})                    
            ANS = ANSRunner(resource)
            ANS.run_model(host_list=serList,module_name='setup',module_args="")
            data = ANS.handle_cmdb_data(ANS.get_model_result())    
            if data:
                for ds in data:
                    status = ds.get('status')
                    if status == 0:
                        assets = Server_Assets.objects.get(ip=ds.get('ip')).assets
                        try:
                            Server_Assets.objects.filter(ip=ds.get('ip')).update(cpu_number=ds.get('cpu_number'),kernel=ds.get('kernel'),
                                                                                  selinux=ds.get('selinux'),hostname=ds.get('hostname'),
                                                                                  system=ds.get('system'),cpu=ds.get('cpu'),
                                                                                  disk_total=ds.get('disk_total'),cpu_core=ds.get('cpu_core'),
                                                                                  swap=ds.get('swap'),ram_total=ds.get('ram_total'),
                                                                                  vcpu_number=ds.get('vcpu_number')
                                                                                  )
                            sList.append(server_assets.ip)
                        except Exception:
                            fList.append(server_assets.ip)
                        for nk in ds.get('nks'):
                            macaddress = nk.get('macaddress')
                            count = NetworkCard_Assets.objects.filter(assets=assets,macaddress=macaddress).count()
                            if count > 0:
                                try:
                                    NetworkCard_Assets.objects.filter(assets=assets,macaddress=macaddress).update(assets=assets,device=nk.get('device'),
                                                                                                                       ip=nk.get('address'),module=nk.get('module'),
                                                                                                                       mtu=nk.get('mtu'),active=nk.get('active'))
                                except Exception, ex:
                                    print ex
                            else:
                                try:
                                    NetworkCard_Assets.objects.create(assets=assets,device=nk.get('device'),
                                                                  macaddress=nk.get('macaddress'),
                                                                  ip=nk.get('address'),module=nk.get('module'),
                                                                  mtu=nk.get('mtu'),active=nk.get('active'))
                                except Exception, ex:
                                    print ex                            
                    else:fList.append(server_assets.ip)