標籤:系統軟體 伺服器 資訊 硬體 記錄
cmdb抓取服務資訊的方式有很多種,可以使用自動化工具saltstack、ansible、puppet,或者使用其它模組直接ssh遠端連線抓取伺服器資訊。這裡記錄一下用ansible的API介面調用setup模組抓取。
實驗使用兩台機器
| IP |
系統 |
軟體 |
| 192.168.93.137 |
centos6.5_x64 |
ansible 1.9.4 |
| 192.168.93.128 |
centos6.5_x64 |
無 |
兩台機器建立好ssh信任後在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" />
幾乎可以擷取到所有想要的資訊,直接處理這些字串很讓人想砸電腦,調用ansible的API介面就方便多了,其實就是用ansible的runner模組調用setup模組來擷取結果,返回的是dict字典,處理起來就方便多了。(注意:ansible1.x版本有runner模組,ansible2.x以上就沒有runner介面模組,需要看官網)
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" />
將其封裝成函數儲存為test.py,(此處封裝借鑒的一位高手,可惜忘了部落格地址)
裡面的參數根據各自的系統可適當修改
# -*- 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)
在192.168.93.137上執行test.py擷取192.168.93.128的伺服器資訊
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" />
接著就是結合資產管理平台存入資料庫中了
方法、套路有很多:
寫成指令碼用crontab定期執行,指令碼讀取IP列表檔案將資訊存入資料庫(借鑒燕郊大神春哥)
通過營運平台主動呼叫指令碼寫入資料庫(借鑒老司機戴總[戴如峰])
此處用第二種,點擊“更新”按鈕,擷取到伺服器資訊後寫入資料庫
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" />
營運平台展示伺服器資訊
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" />
原理:
點擊更新按鈕之後觸發ajax,然後將伺服器的id號以post方式提交給“/cmdb/postmachineinfo/”介面,此id是伺服器在資料庫表中的id號,
營運平台介面收到請求後拿該id號去資料庫中查詢服務器的ip,再調用封裝有
ansible API介面的指令碼查詢服務器資訊,然後寫入資料庫,最後給前端返回
“成功”的資訊。
前端更新按鈕:
<button type="button" id="server_update_{{server.id}}" class="btn btn-xs btn-primary">更新</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" />
按鈕的id值最後一位就是192.168.93.128在資料庫表中的id號
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號.png" alt="wKiom1fzqPTyEMaIAAA72S7leKU671.png-wh_50" />
ajax提交資訊到“/cmdb/postmachineinfo/”
/**更新機器資訊*/$("table tbody tr td button[id^=‘server_update‘]").click(function(){ var id_val = $(this).attr(‘id‘); //擷取更新按鈕id值 var id = id_val.split(‘_‘)[2]; //擷取id值的最後一位 var data = {"id":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控制項提示資訊 skin: ‘layui-layer-molv‘ //樣式類名 },function(){ if (msg == ‘成功‘){ window.location="/cmdb/server_list/"; } else{ layer.close(index) } });}
後端介面“/cmdb/postmachineinfo/”
@login_requireddef postmachineinfo(request): # 提交伺服器資訊 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‘] 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) # 設定服務連接埠資訊 response.write(json.dumps(u‘成功‘)) return response
192.168.93.128上的服務和連接埠資訊,原理一樣,通過ansible API遠程執行netstat -ntpl命令,
擷取資訊存入資料庫
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" />
本文出自 “屌絲” 部落格,請務必保留此出處http://sex123.blog.51cto.com/4155785/1858751
資產管理 cmdb之ansible 擷取伺服器硬體、軟體等資訊