資產管理 cmdb之ansible 擷取伺服器硬體、軟體等資訊

來源:互聯網
上載者:User

標籤:系統軟體   伺服器   資訊   硬體   記錄   

    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" />

接著就是結合資產管理平台存入資料庫中了

方法、套路有很多:

  1. 寫成指令碼用crontab定期執行,指令碼讀取IP列表檔案將資訊存入資料庫(借鑒燕郊大神春哥)

  2. 通過營運平台主動呼叫指令碼寫入資料庫(借鑒老司機戴總[戴如峰])

此處用第二種,點擊“更新”按鈕,擷取到伺服器資訊後寫入資料庫

650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/88/5F/wKioL1f0RYSDefYMAAD1acJXFHk073.png-wh_500x0-wm_3-wmp_4-s_650018166.png" title="update.png" alt="wKioL1f0RYSDefYMAAD1acJXFHk073.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/1858777

資產管理 cmdb之ansible 擷取伺服器硬體、軟體等資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.