def get_server_asset_info(request): ''' 获取服务器资产信息 ''' idc = [i['idc_name'] for i in IdcAsset.objects.values('idc_name')] if request.method == 'GET': ret = '' all_server = ServerAsset.objects.all() if request.GET.has_key('aid'): aid = request.get_full_path().split('=')[1] server_detail = ServerAsset.objects.filter(id=aid) return render(request, 'asset_server_detail.html', {'server_detail': server_detail}) if request.GET.has_key('get_idc'): return HttpResponse(json.dumps(idc)) if request.GET.has_key('action'): action = request.get_full_path().split('=')[1] if action == 'flush': q = SaltHost.objects.filter(alive=True) tgt_list = [] [tgt_list.append(i.hostname) for i in q] ret = MultipleCollect(tgt_list) for i in ret: try: server_asset = get_object_or_404(ServerAsset, nodename=i['nodename']) for j in i: if i[j] != 'Nan': setattr(server_asset, j, i[j]) server_asset.save() except: server_asset = ServerAsset() for j in i: setattr(server_asset, j, i[j]) server_asset.save() return redirect('server_info') if request.GET.has_key('export'): response = HttpResponse(content_type='application/vnd.ms-excel') response['Content-Disposition'] = 'attachment;filename=服务器资产信息.xls' wb = xlwt.Workbook(encoding = 'utf-8') sheet = wb.add_sheet(u'服务器资产信息') alignment = xlwt.Alignment() alignment.horz = xlwt.Alignment.HORZ_LEFT alignment.vert = xlwt.Alignment.VERT_CENTER style = xlwt.XFStyle() style.alignment = alignment style.alignment.wrap = 1 #1st line row0 = [u'主机名',u'操作系统',u'内核',u'Salt版本',u'ZeroMQ版本','Shell','Locale','SELinux',u'CPU型号',u'CPU线程',u'内存',u'硬盘分区',u'网络接口',u'平台',u'序列号',u'厂商型号',u'IDC机房'] for i in range(0, len(row0)): sheet.write(0,i,row0[i]) sheet.col(i).width = 6666 export = request.GET.get('export') id_list = request.GET.getlist('id') row = 1 if export == 'check': for id in id_list: serverasset = get_object_or_404(ServerAsset, pk=id) SheetWrite(sheet, row, serverasset, style) row = row + 1 elif export == 'check_all': for serverasset in ServerAsset.objects.all(): SheetWrite(sheet, row, serverasset, style) row = row + 1 output = StringIO.StringIO() wb.save(output) output.seek(0) response.write(output.getvalue()) return response return render(request, 'asset_server_list.html', {'all_server': all_server}) if request.method == 'POST': field = request.POST.get('field') value = request.POST.get('value') if field == 'idc': value = idc[int(value)] value = str(value) id = request.POST.get('id') ServerAsset.objects.filter(id=id).update(**{field:value}) return HttpResponse(value)
def get_server_asset_info(request): ''' 获取服务器资产信息 ''' idc = [i['idc_name'] for i in IdcAsset.objects.values('idc_name')] if request.method == 'GET': if request.user.has_perm('asset.view_asset'): ret = '' all_server = ServerAsset.objects.all() if request.GET.has_key('aid'): aid = request.get_full_path().split('=')[1] server_detail = ServerAsset.objects.filter(id=aid) return render(request, 'asset_server_detail.html', {'server_detail': server_detail}) if request.GET.has_key('get_idc'): return HttpResponse(json.dumps(idc)) if request.GET.has_key('action'): if request.user.has_perm('asset.edit_asset'): action = request.get_full_path().split('=')[1] if action == 'flush': q = SaltHost.objects.filter(alive=True) tgt_list = [i.hostname for i in q] info = UpdateHostInfo.objects.all() if info: ret = [] for i in info: ret.append({ 'nodename': i.nodename, 'hostname': i.hostname, 'manufacturer': i.manufacturer, 'productname': i.productname, 'sn': i.sn, 'cpu_model': i.cpu_model, 'cpu_nums': i.cpu_nums, 'memory': i.memory, 'disk': i.disk, 'network': i.network, 'os': i.os, 'virtual': i.virtual, 'kernel': i.kernel, 'shell': i.shell, 'zmqversion': i.zmqversion, 'saltversion': i.saltversion, 'locale': i.locale, 'selinux': i.selinux, 'idc': i.idc, 'networkarea': i.networkarea, }) else: ret = MultipleCollect(tgt_list) # ret = MultipleCollect(tgt_list) for i in ret: try: server_asset = get_object_or_404( ServerAsset, nodename=i['nodename']) for j in i: if i[j] != 'Nan': setattr(server_asset, j, i[j]) server_asset.save() except: server_asset = ServerAsset() for j in i: setattr(server_asset, j, i[j]) server_asset.save() return redirect('server_info') else: raise Http404 if request.GET.has_key('export'): response = HttpResponse( content_type='application/vnd.ms-excel') response[ 'Content-Disposition'] = 'attachment;filename=服务器资产信息.xls' wb = xlwt.Workbook(encoding='utf-8') sheet = wb.add_sheet(u'服务器资产信息') alignment = xlwt.Alignment() alignment.horz = xlwt.Alignment.HORZ_LEFT alignment.vert = xlwt.Alignment.VERT_CENTER style = xlwt.XFStyle() style.alignment = alignment style.alignment.wrap = 1 #1st line row0 = [ u'主机名', u'操作系统', u'内核', u'Salt版本', u'ZeroMQ版本', 'Shell', 'Locale', 'SELinux', u'CPU型号', u'CPU线程', u'内存', u'硬盘容量', u'网络地址', u'平台', u'序列号', u'厂商型号', u'网络区域', u'IDC机房' ] for i in range(0, len(row0)): sheet.write(0, i, row0[i]) sheet.col(i).width = 6666 export = request.GET.get('export') id_list = request.GET.getlist('id') row = 1 if export == 'check': for id in id_list: serverasset = get_object_or_404(ServerAsset, pk=id) SheetWrite(sheet, row, serverasset, style) row = row + 1 elif export == 'check_all': for serverasset in ServerAsset.objects.all(): SheetWrite(sheet, row, serverasset, style) row = row + 1 output = StringIO.StringIO() wb.save(output) output.seek(0) response.write(output.getvalue()) return response else: raise Http404 return render(request, 'asset_server_list.html', {'all_server': all_server}) if request.method == 'POST': if request.user.has_perm('asset.edit_asset'): field = request.POST.get('field') value = request.POST.get('value') if field == 'idc': value = idc[int(value)] value = str(value) id = request.POST.get('id') ServerAsset.objects.filter(id=id).update(**{field: value}) return HttpResponse(value) else: raise Http404