flask获取远程服务器的硬件信息

运维经常在维护服务器的时候会有人问服务器的配置是怎么样子的,有些cmdb做得比较好的,能在系统中直接查看,但目前公司还没做这样子的系统,经常要手动登录服务器进行查询,因此,用flask写了个能在系统中直接查询服务器配置。效果如下:

224851_qGSA_1588616.png

前端代码如下,用户可输入多个ip,然后获取的信息有服务器ip,主机名,cpu内存信息,ulimit设置,系统版本,selinux的状态,可以自己再进行添加:

<form role="form" class="form-inline" method="post" action="/display_server_info">
    <textarea class="form-control" rows="1" name="searchip" placeholder="请输入ip"></textarea>
    <button type="submit" class="btn btn-default">提交</button>
</form>
<table class="table table-bordered">
    <thead>
    <tr>
        <th>服务器ip</th>
        <th>主机名</th>
        <th>cpu内存</th>
        <th>负载</th>
        <th>资源数限制</th>
        <th>系统版本</th>
        <th>selinux状态</th>
    </tr>
    </thead>
    <tbody>
    {% for my_ip in ip %}
        <tr>
            <td>{{ my_ip }}</td>
            <td>{{ info[loop.index0]['hostname'] }}</td>
            <td>{{ info[loop.index0]['cpuinfo'] }}核{{ info[loop.index0]['meminfo'] }}M</td>
            <td>{{ info[loop.index0]['loadavg'] }}</td>
            <td>{{ info[loop.index0]['ulimit'] }}</td>
            <td>{{ info[loop.index0]['release'] }}</td>
            <td>{{ info[loop.index0]['getenforce'] }}</td>
        </tr>
    {% endfor %}
    </tbody>
</table>

 

view的代码如下,前端提交到后台进行处理,其中/etc/server.ini为默认去查询的ip,如果有前端提交过来,则用前端提交过来的ip去进行查询。查出来的信息放入info中,info为列表,每个元素中含有字典。

#查看服务器信息
@app.route('/display_server_info', methods=['GET', 'POST'])
def display_server_info():
    ip = []
    p = re.compile(r'(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)')
    if request.method == 'POST'and p.findall(request.form['searchip']):
        ip = p.findall(request.form['searchip'])
    else:
        for line in open('/etc/server.ini'):
            temp = line.replace('\n', '')
            ip.append(temp)
    info = []
    for i in range(0, len(ip)):
        info.append(collect_info(ip[i], 'root', 'xxxxx', 22))
    return render_template('display_server_info.html',info = info, ip = ip)

 

其中使用的collect_info方法如下,使用到了paramiko去远程服务器采集信息,这个方法使用的密钥登录到远程主机进行信息的查询(密钥的路径写好),然后以字典的形式返回查询到的信息(这里应该改成一个类,目前对python还不是很熟悉):

#获取远程服务器的信息,主机名等
def collect_info(ip, user, pkey, port):
    info = {}
    ip = ip
    user = user
    pkey = pkey
    port = port

    key = paramiko.RSAKey.from_private_key_file(pkey)
    ssh = paramiko.SSHClient()
    ssh.load_system_host_keys()

    ssh.connect(ip, port, user, pkey=key)
    (stdin, stdout, stderr) = ssh.exec_command('hostname')
    hostname = stdout.read()
    (stdin, stdout, stderr) = ssh.exec_command('cat /proc/cpuinfo |grep "processor"|wc -l')
    cpuinfo = stdout.read()
    (stdin, stdout, stderr) = ssh.exec_command("free -m|grep 'Mem:'|awk '{print $2}'")
    meminfo = stdout.read()
    (stdin, stdout, stderr) = ssh.exec_command("uptime |awk -F':' '{print $NF}'")
    loadavg = stdout.read()
    (stdin, stdout, stderr) = ssh.exec_command("getenforce")
    getenforce = stdout.read()
    (stdin, stdout, stderr) = ssh.exec_command("ulimit -n")
    ulimit = stdout.read()
    (stdin, stdout, stderr) = ssh.exec_command("cat /etc/redhat-release")
    release = stdout.read()
    ssh.close()
    info['hostname'] = hostname
    info['cpuinfo'] = cpuinfo
    info['meminfo'] = meminfo
    info['getenforce'] = getenforce
    info['ulimit'] = ulimit
    info['loadavg'] = loadavg
    info['release'] = release
    return info

 

相关的信息可以进行收集并录入mysql数据库中,界面中可再提供录入数据库、更新数据库的功能。功能还会再进行完善,

 

最后附上本人的网络课堂地址,如有兴趣请点击: 实践哥

转载于:https://my.oschina.net/zhuangweihong/blog/780913

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值