資產管理 cmdb之ansible 獲取服務器硬件、軟件等信息

    cmdb抓取服務信息的方式有不少種,能夠使用自動化工具saltstack、ansible、puppet,或者使用其它模塊直接ssh遠程鏈接抓取服務器信息。這裏記錄一下用ansible的API接口調用setup模塊抓取。前端

實驗使用兩臺機器ajax

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

幾乎能夠獲取到全部想要的信息,直接處理這些字符串很讓人想砸電腦,調用ansible的API接口就方便多了,其實就是用ansible的runner模塊調用setup模塊來獲取結果,返回的是dict字典,處理起來就方便多了。(注意:ansible1.x版本有runner模塊,ansible2.x以上就沒有runner接口模塊,須要看官網)json

將其封裝成函數保存爲test.py,(此處封裝借鑑的一位高手,惋惜忘了博客地址)後端

裏面的參數根據各自的系統可適當修改centos

# -*- coding: UTF-8 -*-

import ansible.runner


def 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 data


if __name__ == '__main__':
    data = get_info('')

    import pprint
    pprint.pprint(data)

在192.168.93.137上執行test.py獲取192.168.93.128的服務器信息服務器

接着就是結合資產管理平臺存入數據庫中了運維

方法、套路有不少:ssh

  1. 寫成腳本用crontab按期執行,腳本讀取IP列表文件將信息存入數據庫(借鑑燕郊大神春哥)函數

  2. 經過運維平臺主動調用腳本寫入數據庫(借鑑老司機戴總[戴如峯])

此處用第二種,點擊「更新」按鈕,獲取到服務器信息後寫入數據庫

運維平臺展現服務器信息

原理:

      點擊更新按鈕以後觸發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>

按鈕的id值最後一位就是192.168.93.128在數據庫表中的id號

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_required
def 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命令,

獲取信息存入數據庫

相關文章
相關標籤/搜索