LINUX服务器批量巡检的PYTHON脚本

系统环境:CENTOS6,CENTOS7,把要检查的IP地址写在ips.txt,并做好ssh认证

主要检查服务器的CPU,内存,磁盘空间的使用百分比等信息,除了检查根目录以外,还检查了/application分区的使用情况,并打印到EXCEL

 

# -*- coding: utf-8 -*-
import os,sys
reload(sys)
sys.setdefaultencoding('utf8')
import paramiko
import xlsxwriter
import time
def sshexeccmd(ip):
    tmplist = {}
    tmplist["ip"]=ip
    tmplist["diskall"]=None
    tmplist["diskuse"]=None
    tmplist["cpuuse"]=None
    tmplist["momeryall"]=None
    tmplist["momeryuse"]=None
    tmplist["appall"]=None
    tmplist["appuse"]=None
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        # pkey = paramiko.RSAKey.from_private_key_file('/home/super/.ssh/id_rsa', password='yourpassword')
        pkey = paramiko.RSAKey.from_private_key_file('/root/.ssh/id_rsa')
        
        ssh.connect(hostname=ip,port=22,username='root',pkey=pkey)
        # stdin, stdout, stderr = ssh.exec_command(cmd)
        stdindiskall, stdoutdiskall, stderrdiskall = ssh.exec_command("df -hP | awk '/\/$/ {print $2}'")
        stdindisk, stdoutdisk, stderrdisk = ssh.exec_command("df -hP | awk '/\/$/ {print $5}' | sed s'/%//'")
        stdindcpu, stdoutcpu, stderrcpu = ssh.exec_command("top -bn1 | awk '/Cpu/{print $2,$3,$4}' | sed 's/[a-z]//g' | sed 's/ //g' | awk -F',' '{print $1+$2+$3}'")
        stdinmomeryall, stdoutmomeryall, stderrmomeryall = ssh.exec_command("free -m | awk '{print $2}' | awk 'NR==2{print}'")
        stdinmomery, stdoutmomery, stderrmomery = ssh.exec_command("free | sed -n '2p' |awk '{printf ($2-$7)/$2*100}'")
        stdinappsall, stdoutappsall, stderrappsall = ssh.exec_command("df -hP /application | awk '{print $2}' | awk 'NR==2{print}'")
        stdinapps, stdoutapps, stderrapps = ssh.exec_command("df -hP /application | awk '{print $5}' | sed s'/%//' | awk 'NR==2{print}'")

        #print ip+ " "+(stdoutdiskall).read().replace("\n", "")+" " +(stdoutdisk.read()).replace("\n", "")+" "+(stdoutcpu.read()).replace("\n", "")+" "+(stdoutmomeryall.read()).replace("\n", "")+" "+(stdoutmomery.read())+\
        #     " " + (stdoutappsall).read().replace("\n", "") + " " + (stdoutapps).read().replace("\n", "")
        # print(stderr.read())

        tmplist["ip"]=ip
        tmplist["diskall"]=(stdoutdiskall).read().replace("\n", "")
        tmplist["diskuse"]=(stdoutdisk.read()).replace("\n", "")
        tmplist["cpuuse"]=(stdoutcpu.read()).replace("\n", "")
        tmplist["momeryall"]=(stdoutmomeryall.read()).replace("\n", "")
        tmplist["momeryuse"]=(stdoutmomery.read())
        tmplist["appall"]=(stdoutappsall).read().replace("\n", "")
        tmplist["appuse"]=(stdoutapps).read().replace("\n", "")
        print tmplist
        return tmplist


        ssh.close()
    except Exception,e:
        print e
        return tmplist



def trywexrestr(lists):
    nowtime=time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))+""
    bookurl="systems"+nowtime+'info.xlsx'
    #filenames=nowtime+'info.xlsx'
    workbook1 = xlsxwriter.Workbook(bookurl)
    worksheet = workbook1.add_worksheet()
    title=[u'IP地址',u'根目录大小',u'根目录使用百分比',u'CPU使用率',u'内存总量',u'内存使用率',u'application大小'\
         ,u'application使用率']
    format=workbook1.add_format()
    worksheet.set_column(0,15,20)
    format.set_bold()
    worksheet.write_row('A1',title,format)
    row=1
    
    for a in lists:
        worksheet.write(row,0,a["ip"])
        worksheet.write(row,1,a["diskall"])
        worksheet.write(row,2,a["diskuse"])
        worksheet.write(row,3,a["cpuuse"])
        worksheet.write(row,4,a["momeryall"])
        worksheet.write(row,5,a["momeryuse"])
        worksheet.write(row,6,a["appall"])
        worksheet.write(row,7,a["appuse"])

        row=row+1
    workbook1.close()
    







def readinfo():
    myfile=open('ips.txt','r')
    listall=[]
    for line in myfile:
        #con=line.split()
        ip=line.strip()
        tmplist=sshexeccmd(ip)
        listall.append(tmplist)
    return listall

listall=readinfo()
trywexrestr(listall)

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页