玖叶教程网

前端编程开发入门

python利用paramiko模块ssh进linux服务器,统计内存信息

我的文件列表是这样的:

{'id': '135.251.205.62', 'name': 'FNSHB062', 'part': 'bba', 'owner': 'engineer', 'system': 'centos7'}

所以程序里面多转了几次,总算能ssh进去就可以了,没有进一步简化。

统计linux内存,利用基本的cat /proc/meminfo进行取值。

def get_mem_used():
    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(',')
            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('cat /proc/meminfo')
            str_out = stdout.read().decode()
            str_err = stderr.read().decode()
            mem_values = re.findall("(\d+)\ kB",str_out)
            MemTotal = float(mem_values[0])
            MemFree = float(mem_values[1])#系统尚未使用的内存
#           MemAvail = float(mem_values[2])# 应用程序可用内存数,它与MemFree的关键区别点在于,MemFree是说的系统层面,MemAvailable是说的应用程序层面。MemAvailable≈MemFree+Buffers+Cached,
            Buffers = float(mem_values[3])
            Cached = float(mem_values[4])
            SwapCached = float(mem_values[5])
            SwapTotal = float(mem_values[14])
            SwapFree = float(mem_values[15])
            Free_Mem = int(MemFree) + int(Buffers) + int(Cached)
            print('******************************内存监控,对应主机[name:%s]*********************************' % hostip)
            print("*******************时间:", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "******************")
            print("总内存:%.2f" %(MemTotal/1024/1024)+ " GB")
            print("可用内存:%.2f" %(Free_Mem/1024/1024)+ " GB")
#            print("系统层面空闲内存:%.2f" %(MemFree/1024/1024)+ " GB")
#            print("应用层面空闲内存:%.2f" %( MemAvail/1024/1024)+ " GB")
            print("缓冲区内存数:%.2f" % (Buffers / 1024 / 1024) + " GB")
            print("缓存区内存数:%.2f" %( Cached/1024/1024)+ " GB")
            print("被高速缓冲存储用的交换空间大小:%.2f" %(SwapCached/1024/1024)+ " GB")
            if int(SwapTotal) == 0:
                print(u"交换内存总共为:0")
            else:
                Rate_Swap = 100 - 100 * int(SwapFree) / float(SwapTotal)
                print(u"交换内存利用率:", Rate_Swap)
            Used_Mem = int(MemTotal) - Free_Mem
            Rate_Mem = 100 * Used_Mem / float(MemTotal)
            print(u"内存利用率:", str("%.2f" % Rate_Mem), "%")
            ssh.close()

运行结果如下:

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言