def items_add(request): server_list = host_info.objects.all() server_id = request.POST.get('server_id') hostname = request.POST.get('hostname') item_name = request.POST.get('item_name') service = request.POST.get('service') updata = request.POST.get('updata') port = request.POST.get('port') status = request.POST.get('status') group = request.POST.get('group') notes = request.POST.get('notes') if hostname is not None: if len(item_name): if len(service): print hostname,item_name,service info = items_info(hostname=hostname,item_name=item_name,service=service,updata=updata,port=port,status=status,group=group,notes=notes) try: info.save() save_log(request.user,"添加应用信息:" + info.item_name ,"添加成功") return HttpResponseRedirect('/items/info/') except Exception,log: #log = '应用信息添加失败' save_log(request.user,"添加应用信息失败",log) return render(request,"items/items_add.html",locals()) else: log = '应用信息不完整,请填充完整' return render(request,"items/items_add.html",locals()) else: log = '应用信息不完整,请填充完整' return render(request,"items/items_add.html",locals())
def cmdb_info(request): server_list = host_info.objects.all() change = request.GET.get('change') remove = request.GET.get('remove') save = request.GET.get('save') refresh = request.GET.get('refresh') if change: info = host_info.objects.get(server_id=change) return render(request, 'cmdb/host_change.html', locals()) elif save: ip = request.GET.get('save') info = host_info.objects.get(server_id=save) info.hostname = request.POST.get('hostname') info.cpu = request.POST.get('cpu') info.men = request.POST.get('men') info.disk = request.POST.get('disk') info.os = request.POST.get('os') info.position = request.POST.get('position') info.owner = request.POST.get('owner') info.group = request.POST.get('group') info.notes = request.POST.get('notes') try: info.save() print info.ip, 'aaaa' save_log(request.user, "修改主机信息:" + info.hostname, "修改成功") return HttpResponseRedirect('/cmdb/info/') except Exception, log: #log = 'Host info save fail...' save_log(request.user, "修改主机信息:" + info.hostname, log) return render(request, 'cmdb/host_change.html', locals())
def items_info_echo(request): items_list = items_info.objects.all() edit_id = request.GET.get('edit') save_id = request.GET.get('save') start_id = request.GET.get('start') stop_id = request.GET.get('stop') restart_id = request.GET.get('restart') update_id = request.GET.get('update') remove_id = request.GET.get('remove') if edit_id: items = items_info.objects.get(items_id=edit_id) return render(request,"items/items_change.html",locals()) elif save_id: info = items_info.objects.get(items_id=save_id) info.item_name = request.POST.get('item_name') info.service = request.POST.get('service') info.updata = request.POST.get('updata') info.port = request.POST.get('port') info.status = request.POST.get('status') info.group = request.POST.get('group') info.notes = request.POST.get('notes') try: info.save() save_log(request.user,"修改应用信息:" + info.item_name ,"修改成功") return HttpResponseRedirect('/items/info/') except Exception,e: log = "应用信息修改失败" save_log(request.user,"修改应用信息:" + info.item_name ,"修改失败") return render(request,"items/items_info.html",locals())
def change_pass(request): username = request.GET.get('save') username = request.POST.get('user') password = request.POST.get('old_pass') new_pass1 = request.POST.get('new_pass1') new_pass2 = request.POST.get('new_pass2') if username: if new_pass1 == new_pass2: user = auth.authenticate(username=username, password=password) if user and user.is_active: newuser = User.objects.get(username=username) newuser.set_password(new_pass1) try: newuser.save() save_log(request.user, "修改密码", "修改成功") return render(request, 'auth/login.html') except Exception, e: log = "修改失败!!!" save_log(request.user, "修改密码", "修改失败") return render(request, "auth/account_conf.html", locals()) else: log = "请输入正确的密码!!!" return render(request, "auth/account_conf.html", locals()) else: log = "两次输入的密码不一致" return render(request, "auth/account_conf.html", locals())
def account_login(request): if request.method == "POST": username = request.POST.get('username') password = request.POST.get('password') user = auth.authenticate(username=username, password=password) if user and user.is_active: auth.login(request, user) save_log(username, "登陆系统", "登陆成功") return HttpResponseRedirect('/main/home/') else: return render(request, 'auth/login.html', {'login_err': '登陆失败,请确认后重新输入'}) else: return render(request, 'auth/login.html')
def files_upload(request): from django import forms class UploadFileForm(forms.Form): title = forms.CharField(max_length=65536) file = forms.FileField() if request.method == "GET": #data='get' print request.GET return HttpResponseRedirect('/files/info/') if request.method == "POST": if (request.FILES['t_file']): try: f = save_upload_file(request.FILES['t_file']) save_log(request.user, '上传文件:' + f.name, '上传成功') file_list = os.listdir(upload_dir) return render(request, 'files/files_info.html', locals()) except Exception, e: save_log(request.user, '上传文件', e) return HttpResponseRedirect('/mian/log/') else: return HttpResponseRedirect('/files/info/')
def cmdb_ssh(request): ssh_list = ssh_info.objects.all() change = request.GET.get('change') remove = request.GET.get('remove') save = request.GET.get('save') if change: info = ssh_info.objects.get(server_id=change) return render(request, 'cmdb/ssh_change.html', locals()) elif save: ip = request.GET.get('save') info = ssh_info.objects.get(server_id=save) info.hostname = request.POST.get('hostname') info.ip = request.POST.get('ip') info.username = request.POST.get('username') info.password = request.POST.get('password') info.port = request.POST.get('port') info.notes = request.POST.get('notes') try: info.save() save_log(request.user, "修改主机SSH信息:" + save, "修改成功") return HttpResponseRedirect('/cmdb/ssh/') except Exception, log: save_log(request.user, "修改主机SSH信息失败" + save, log) return render(request, 'cmdb/ssh_change.html', locals())
def files_remove(request): del_list = request.POST.getlist('del') if del_list is not None: for name in del_list: del_file = os.path.join(upload_dir, name) if os.path.isfile(del_file): try: os.remove(del_file) save_log(request.user, "删除文件:" + name, "删除成功") return HttpResponseRedirect('/files/info/') except Exception, e: save_log(request.user, "删除文件:" + name, e) return HttpResponseRedirect('/main/log/') elif os.path.isdir(del_file): try: #os.removedirs(del_file) import shutil shutil.rmtree(del_file) save_log(request.user, "删除目录:" + name, "删除成功") return HttpResponseRedirect('/files/info/') except Exception, e: save_log(request.user, "删除目录:" + name, e) return HttpResponseRedirect('/main/log/')
info.notes = request.POST.get('notes') try: info.save() print info.ip, 'aaaa' save_log(request.user, "修改主机信息:" + info.hostname, "修改成功") return HttpResponseRedirect('/cmdb/info/') except Exception, log: #log = 'Host info save fail...' save_log(request.user, "修改主机信息:" + info.hostname, log) return render(request, 'cmdb/host_change.html', locals()) elif remove: del_host = host_info.objects.filter(server_id=remove) del_ssh = ssh_info.objects.filter(server_id=remove) del_host.delete() del_ssh.delete() save_log(request.user, "删除主机信息:" + remove, "删除成功") return HttpResponseRedirect('/cmdb/info/') elif refresh: try: import salt.client client = salt.client.LocalClient() #client.cmd('*','saltutil.sync_grains',[]) #client.cmd('*','grains.item',['virtual']) 是否虚拟机 #client.cmd('*','grains.item',['manufacturer']) 制造商 #client.cmd('*','grains.item',['num_cpus']) CPU核心数 grains_list = [ 'server_id', 'host', 'ipv4', 'mem_total', 'cpu_model', 'cpuarch', 'os', 'oscodename', 'osrelease', 'osarch' ] for host_list in os.listdir('/etc/salt/pki/master/minions'): print 'refresh info:', host_list
def account_logout(request): save_log(request.user, "退出系统", "成功") auth.logout(request) return render(request, "auth/login.html")