Python统计服务器的负载情况

假如公司有几百台服务器,现在统计每一台服务器的负载情况,并将数据导入到excel中?
思路:
首先将每一台服务器的负载统计出来,并导入到文件中 –> 对文件进行处理,截取我们想要的内容 –> 利用python对文件进行操作,将其内容放到一个字典中或者列表中 –> 运用python的xlsxwriter模块,将文件里的内容导入到excel中

一、统计所有服务器的负载情况(下面我就以四台为例)

#!/usr/bin/env python
#-*- coding:utf-8 -*-
# scripts: service_uptime.py
import threading,paramiko
url_list = ['192.168.88.130',
            '192.168.88.131',
            '192.168.88.132',
            '192.168.88.133',
            ]
def run(ssh_ip):
    private_key = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(hostname=ssh_ip,port=22,username='root',password=private_key)
    stdin,stdout,stderr = ssh.exec_command('uptime')
    res,err = stdout.read(),stderr.read()
    result = res if res else err
    print(ssh_ip,result.decode())
    ssh.close()
if __name__ == '__main__':
    for ip in url_list:
        t = threading.Thread(target=run,args=(ip,))
        t.start()

上述代码中是采用密钥对的方式对服务器进行连接,当然也可以通过密码的方式来连接,代码如下:

import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='192.168.88.130',port=22,username='root',password='123456')
stdin,stdout,stderr = ssh.exec_command('uptime')
res,err = stdout.read(),stderr.read()
result = res if res else err
print(result.decode())
ssh.close()

将统计出来的服务器负载导入到文件中 python service_uptime.py > uptime.txt

二、对负载文件进行处理,截取我们想要的内容

awk -F',' '{print $1","$(NF-2)","$(NF-1)","$(NF)}' uptime.txt | sed -e \
's#(##g' -e 's#)##g' -e 's#\\n##g' -e 's#load average:##g' -e "s#'##g" > new.txt

Python统计服务器的负载情况_第1张图片

三、利用python对文件的操作和模块将文件内容放到excel表格中

#!/usr/bin/env python
#-*- coding:utf-8 -*-

uptime_dic = {}
with open('new.txt','r') as f:
    for line in f:
        ip, uptime1, uptime5, uptime15 = line.strip('\n').split(',')
        uptime_dic[ip.strip()] = [ip.strip(),uptime1.strip(),uptime5.strip(),uptime15.strip()]

import xlsxwriter
workbook = xlsxwriter.Workbook('uptime.xls') # 建立文件
worksheet = workbook.add_worksheet() # 建立sheet, 可以work.add_worksheet('employee')来指定sheet名,但中文名会报UnicodeDecodeErro的错误

# Start from the first cell. Rows and columns are zero indexed. 按标号写入是从0开始的,按绝对位置'A1'写入是从1开始的
row = 1
col = 0
worksheet.write('A1','ip')
worksheet.write('B1','1分钟负载')
worksheet.write('C1','5分钟负载')
worksheet.write('D1','15分钟负载')
for ip, uptime1,uptime5,uptime15 in uptime_dic.values():
    worksheet.write(row, col, ip)
    worksheet.write(row, col + 1, uptime1)
    worksheet.write(row, col + 2, uptime5)
    worksheet.write(row, col + 3, uptime15)
    row += 1

workbook.close()

Python统计服务器的负载情况_第2张图片

备注:
1、如果统计内存,磁盘使用量等内容都是一样的
2、我写的可能比较low,因为我都是自学python,可能并没有那么好,直接写成一个python文件就把上面的操作全部给统计出来


扩展

源码安装paramiko

#安装依赖包
yum -y install zlib-devel readline-devel libffi-devel python-devel openssl-devel  gcc git

#下载pycrypto和paramiko包
wget http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz
git clone https://github.com/paramiko/paramiko.git 

#编译安装
tar -xf pycrypto-2.6.tar.gz
cd pycrypto-2.6
python setup.py install

cd paramiko
python setup.py build
python setup.py install

你可能感兴趣的:(python,自动化,python,uptime,大数据,自动化,paramiko)