资产管理 cmdb之ansible 获取服务器硬件、软件等信息

时间:2024-04-16 17:17:44

    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

几乎可以获取到所有想要的信息,直接处理这些字符串很让人想砸电脑,调用ansible的API接口就方便多了,其实就是用ansible的runner模块调用setup模块来获取结果,返回的是dict字典,处理起来就方便多了。(注意:ansible1.x版本有runner模块,ansible2.x以上就没有runner接口模块,需要看官网)

将其封装成函数保存为test.py,(此处封装借鉴的一位高手,可惜忘了博客地址)

里面的参数根据各自的系统可适当修改

# -*- 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的服务器信息

接着就是结合资产管理平台存入数据库中了

方法、套路有很多:

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

获取信息存入数据库