def get_server_info(request): ''' @note: 通过saltapi获取所有minion主机的服务器信息,填入写入数据库中 ''' # 获取所有server的hostname if request.method == "POST": client = SaltApi() params = {'client': 'local', 'fun': 'test.ping', 'tgt': '*'} json_data = client.get_allhostname(params) data = dict(json.loads(json_data)['return'][0]) hostname_list = [] [ hostname_list.append(i) for i in data.keys() ] # 获取所有server的hostname for host in hostname_list: all_host_info = dict(client.get_minions(host).items()) try: host_record = Hostinfo( hostname=all_host_info['hostname'], private_ip=all_host_info['private_ip'], public_ip=all_host_info['public_ip'], mem_total=all_host_info['mem_total'], cpu_type=all_host_info['cpu_type'], num_cpus=all_host_info['num_cpus'], os_release=all_host_info['os_release'], kernelrelease=all_host_info['kernelrelease'] ) host_record.save() print u'导入主机 %s 成功!' % all_host_info['hostname'] except Exception,e: print e
def get_server_info(request): ''' @note: 通过saltapi获取所有minion主机的服务器信息,填入写入数据库中 ''' # 获取所有server的hostname if request.method == "POST": if len(ApiMg.objects.filter(app_name='saltstack')) == 0: result = {"result": False, "message": u'请确保api信息已录入!'} return render_json(result) else: try: client = SaltApi('saltstack') params = {'client': 'local', 'fun': 'test.ping', 'tgt': '*'} json_data = client.get_allhostname(params) data = dict(json.loads(json_data)['return'][0]) hostname_list = [] [hostname_list.append(i) for i in data.keys()] for host in hostname_list: if not Hostinfo.objects.filter(hostname=host): all_host_info = dict(client.get_minions(host).items()) host_record = Hostinfo( hostname=all_host_info['hostname'], private_ip=all_host_info['private_ip'], public_ip=all_host_info['public_ip'], mem_total=all_host_info['mem_total'], cpu_type=all_host_info['cpu_type'], num_cpus=all_host_info['num_cpus'], os_release=all_host_info['os_release'], kernelrelease=all_host_info['kernelrelease']) host_record.save() result = {"result": True, "message": u'刷新完毕!'} return render_json(result) except Exception, e: result = {"result": False, "message": u'刷新出错!'} return render_json(result)