龙空技术网

python利用paramiko模块批量统计服务器的磁盘使用百分比

叽叽佩佩 192

前言:

今天我们对“python 百分比输出”大体比较讲究,咱们都需要了解一些“python 百分比输出”的相关资讯。那么小编同时在网络上汇集了一些有关“python 百分比输出””的相关文章,希望同学们能喜欢,大家快快来学习一下吧!

最近总是有工程师抱怨磁盘空间满了,帮他们解决问题以后,就想着扫描一下所有机器的磁盘使用情况,提前把快满的给清理一下。下面是一般输出结果:

我的函数实现就是把输出结果进行简单的统计整理:

def get_disk():    with open('server.txt', 'r', encoding='utf-8') as rfile:        servers = rfile.readlines()  # 按行读取        for item in servers:  # 遍历列表,            d = dict(eval(item))  # 列表中的第一个元素,编写成字典            d.update(username="xxx", password="xxx", port='22')  # 第一个元素更新            value_list = []            value_list.append(d['id'])            value_list.append(d['username'])            value_list.append(d['password'])            value_list.append(d['port'])            hostinfo = value_list[0] + ',' + value_list[1] + ',' + value_list[2] + ',' + value_list[3]            hostinfo = hostinfo.strip()            hostip, username, password, port = hostinfo.split(',')            print(hostip)            ssh = paramiko.SSHClient()            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())            ssh.connect(hostname=hostip, port=port, username=username, password=password)            stdin, stdout, stderr = ssh.exec_command('df -h')            result = stdout.read().decode('utf-8')            ##arr = result.split('\n')            a=analyze(result)            for row in a:                path = row[5]  # 路径                use = row[4]  # 使用占比                use_number = int(use.replace('%', ''))                if (use_number > 90):                    print('磁盘{}路径使用超过{}'.format(path, use))                if  (path == '/'):                    print('磁盘/root使用率为{}'.format( use))                if  (path == '/repo'):                    print('磁盘{}使用率为{}'.format(path, use))                if  (path == '/repo-1'):                    print('磁盘{}使用率为{}'.format(path, use))            ssh.close()

输出格式如下:

标签: #python 百分比输出