The CMDB crawls service information in many ways, using automated tools Saltstack, Ansible, puppet, or using other modules to directly SSH remote connections to fetch server information. Here is a record of using the Ansible API interface to invoke the Setup module fetch.
Experiment using two machines
Ip |
System |
Software |
192.168.93.137 |
Centos6.5_x64 |
Ansible 1.9.4 |
192.168.93.128 |
Centos6.5_x64 |
No |
Two machines build SSH trust and execute on 192.168.93.137
Ansible 192.168.93.128-m Setup
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/88/60/wKiom1fzlY-i3YIgAAB0pjyK7Ck267.png-wh_500x0-wm_3 -wmp_4-s_701138283.png "title=" Ansible-setup.png "alt=" Wkiom1fzly-i3yigaab0pjyk7ck267.png-wh_50 "/>
Almost get all the information you want, the direct processing of these strings is so people want to smash the computer, call Ansible API interface is more convenient, in fact, with Ansible Runner module calls the Setup module to obtain the results, the return is dict dictionary, processing is more convenient. (Note: ansible1.x version has runner module, ansible2.x above there is no runner interface module, need to crossing network)
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/88/60/wKiom1fzmlnT_sSIAABhJ0U9ByI932.png-wh_500x0-wm_3 -wmp_4-s_3873109549.png "title=" Runner.png "alt=" Wkiom1fzmlnt_ssiaabhj0u9byi932.png-wh_50 "/>
It is encapsulated into a function to save as test.py, (here encapsulates a master, unfortunately forget the blog address)
The parameters inside are appropriately modified according to their respective systems.
# -*- coding: utf-8 -*-import ansible.runnerdef get_info (IP): data = {} runner = ansible.runner.runner (module_name= ' setup ', module_args= ", pattern= ' all ', forks=2) datastructure = Runner.run () sn = datastructure[' contacted '][ip][' ansible_facts ' [' Ansible_ Product_serial '] host_name = datastructure[' contacted '][ip][' ansible_facts ' [ ' Ansible_hostname '] description = datastructure[' contacted '][ip][' Ansible_ Facts ' [' ansible_lsb '] [' description '] ansible_machine = datastructure[' Contacted '][ip][' ansible_facts ' [' ansible_machine '] sysinfo = '%s %s ' % (description, ansible_machine) os_kernel = datastructure[' Contacted '][ip][' ansible_facts '[' Ansible_kernel '] cpu = datastructure[' contacted '][ip][' ansible_facts ' [' Ansible_processor '][1] cpu_count = datastructure[' contacted '][ip][' Ansible_ Facts ' [' ansible_processor_count '] cpu_cores = datastructure[' contacted '][ip] [' Ansible_facts '] [' Ansible_processor_cores '] mem = datastructure[' contacted '][ip][' Ansible_ Facts ' [' ansible_memtotal_mb '] ipadd_in = datastructure[' contacted '][ip][' Ansible_facts ' [' ansible_all_ipv4_addresses '][0] disk = datastructure[' Contacted '][ip][' ansible_facts ' [' ansible_devices '] [' SDA '] [' Size '] # print sysinfo data[' sn '] = sn data[' sysinfo '] = sysinfo data[' CPU '] = cpu data[' Cpu_count '] = cpu_count data[' cpu_cores '] = cpu_cores data[' mem '] = mem data[' disk '] = disk data[' ipadd_in '] = ipadd_in data[' Os_kernel '] = os_kernel data[' host_name '] = host_name return dataif __name__ == ' __main__ ': data = get_info (") import pprint pprint.pprint (data)
Perform test.py on 192.168.93.137 to get 192.168.93.128 server information
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/88/5D/wKioL1fznkLA45BKAAA4xsoJMaM208.png-wh_500x0-wm_3 -wmp_4-s_170046621.png "title=" Core-api.png "alt=" Wkiol1fznkla45bkaaa4xsojmam208.png-wh_50 "/>
Then it's a combination of the asset management platform into the database.
There are many methods and routines:
Written in a script with crontab regular execution, script read IP list file to the database (for reference to the Yan Jiao Chun brother)
Write to database via active call script of operation and maintenance platform (refer to old driver wearing total [Dai Feng])
In the second case, click on the "Update" button to get to the server information and write to the database
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M00/88/5D/wKioL1fzozejjPruAAD1acJXFHk662.png-wh_500x0-wm_3 -wmp_4-s_508736147.png "title=" Update.png "alt=" Wkiol1fzozejjpruaad1acjxfhk662.png-wh_50 "/>
Shipping dimensional plane Platform display server information
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/88/5D/wKioL1fzpIryZF3vAAD2kX2k05s961.png-wh_500x0-wm_3 -wmp_4-s_413367383.png "title=" Res.png "alt=" Wkiol1fzpiryzf3vaad2kx2k05s961.png-wh_50 "/>
Principle:
Clicking the Update button triggers Ajax and then submits the server's ID number to the "/cmdb/postmachineinfo/" interface, which is the ID number of the server in the database table.
Operation and maintenance platform interface after receiving the request, take the ID number to query the server's IP in the database, and then call the encapsulated
The Ansible API interface script queries the server information, then writes to the database, and finally returns to the front end
The "Success" message.
Front-End Update button:
<button type= "button" Id= "Server_update_{{server.id}}" class= "btn btn-xs btn-primary" > Update </button>
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M01/88/61/wKiom1fzqMWSHSAbAADIRnUjCIo431.png-wh_500x0-wm_3 -wmp_4-s_1022710686.png "title=" Update1.png "alt=" Wkiom1fzqmwshsabaadirnujcio431.png-wh_50 "/>
The ID value of the button the last one is 192.168.93.128 ID number in the database table
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/88/61/wKiom1fzqPTyEMaIAAA72S7leKU671.png-wh_500x0-wm_3 -wmp_4-s_4149424211.png "title=" ID number. png "alt=" wkiom1fzqptyemaiaaa72s7leku671.png-wh_50 "/>
Ajax submits information to "/cmdb/postmachineinfo/"
/** Update machine Information */$ ("table tbody tr td button[id^= ' server_update ']"). Click (function () { var id_val = $ (This). attr (' id '); //gets the Update button ID value var id = id_val.split ('_') [2]; //gets the last var data = {"id" of the ID value: ID }; data = json.stringify (data); var url = "/cmdb/postmachineinfo/"; $.get (Url,{data:data},function (msg) { ajax_callback (msg); }); **ajax get callback*/function ajax_callback (msg) { var msg = $.parsejson (msg); var index = layer.alert (msg, { / /layer Control Tip Info skin: ' layui-layer-molv ' //style class name },function () { if (msg == ' success ') { window.location= "/cmdb/server_list/"; } else{ layer.close (index) } });
Back-end Interface "/cmdb/postmachineinfo/"
@login_requireddef postmachineinfo (Request): # Submit Server information response = httpresponse () data = json.loads (request. Get.get (' Data ', ")) id = int (data[' id ')) print ' Update---> ' server = server.objects.get (pk=id) data = get_info (server.in_ip) server.os_version = data[' SysInfo '] server.host_name = data[' host_name '] server.os_kernel = data[' Os_kernel '] server.cpu_model = data[' CPU '] server.cpu_count = data[' Cpu_count '] server.cpu_cores = data[' cpu_cores '] server.mem = data[' mem '] server.disk = data[' Disk ']&NBsp; server.status = true server.max_open_files = Get_ulimit (SERVER.IN_IP) server.uptime = get_uptime (SERVER.IN_IP) server.save () # set_service_port (server) # set up service port information response.write (Json.dumps (U ' success ')) return response
The service and port information on the 192.168.93.128, like the principle, remotely executes the NETSTAT-NTPL command via the Ansible API,
Get information into a database
650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/88/61/wKiom1fzrR2j4hgSAAF959b3Tts203.png-wh_500x0-wm_3 -wmp_4-s_2370687672.png "title=" Port.png "alt=" Wkiom1fzrr2j4hgsaaf959b3tts203.png-wh_50 "/>
This article is from the "Dick Silk" blog, please make sure to keep this source http://sex123.blog.51cto.com/4155785/1858751
Ansible of the CMDB of asset management get server hardware, software and other information