通过python的paramiko抓取多台服务器信息,并通过html格式发送邮件到群组

1, Paramiko安装依赖于pycrypto、ecdsa模块

#!/bin/env python

import paramiko
import sys
import smtplib
import string
import time
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import *
from email.utils import COMMASPACE,formatdate  
def GetNowTime(s):
        return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(s))

class Df_infor(object):

    def __init__(self,host,mount,total,used,free):
        self.hostname      = host
        self.mount_point   = mount
        self.total_size    = total
        self.used_size     = used
        self.free_percent  = free


information = []
#Filesystem                   Type   Size  Used Avail Use% Mounted on
#masters    = []  ''' /msdp'''
#medias     = []  ''' /media '''
ip_list     = ['127.0.0.1','10.8.36.61']
df          = Df_infor('hostname','mount_point','total_size','used_size','free_percent')
information.append(df)

for ip in ip_list:
    host = ip
    #host=sys.argv[1]
        #cmd_master = 'df -Th /msdp'
        #cmd_media  = 'df -Th /media'i
        cmd        = " df -ThP  | awk -F' '  '{print $1, $2, $3, $4, $5, $6, $7}' "
    hostname   = 'hostname'
        #cmd = 'ls'
        ssh  = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        #ssh.connect(host,username='root',password='Opsa@8))Best>com',allow_agent=True)
        ssh.connect(host,username='root',password='800best',allow_agent=True)
        stdin,stdout,stderr  = ssh.exec_command(hostname)
    hostname             = stdout.read().split('\n')[0]
        stdin,stdout,stderr  = ssh.exec_command(cmd)
        results              = stdout.readlines()
       
    for line in results:
        line         = line.split('\n')[0]
        line_list    = line.split(' ')
        if 'T' in line_list[2]:
            size         = int(line_list[2].split('T')[0])
            if size >= 10:
                free         = 100-int(line_list[5].split('%')[0])
                free_percent = str(free)+'%'
                df           = Df_infor(hostname,line_list[6],line_list[2],line_list[3],free_percent)
                information.append(df)
            else:
                pass
        else:
            continue

 

for infor in information:
    print infor.hostname+ ' ' + infor.mount_point + ' ' + infor.total_size + ' ' + infor.used_size + ' ' + infor.free_percent + '%'


html_head = "

" + " "  +  \
                                                                            " "                +  \
                                        "  "            +  \
                                        "  "             +  \
                                        "  "              +  \
                                        "  "           +  \
                                        " "
html_body = ""
for infor in information:
    if infor.hostname == 'hostname':
        continue
    else:
        html_body = html_body + "" + ''     +   \
                                                                        ''   +  \
                                                ''   +   \
                                        ''    +   \
                                        '' +   \
                                        ''


html_tail = '

Disk used information
hostnamemount_pointtotal_sizeused_sizefree_percent
' + infor.hostname + '' + infor.mount_point +'' + infor.total_size + '' + infor.used_size + '' + infor.free_percent + '
'


html_str = html_head + html_body + html_tail
print html_str


now_time = GetNowTime(time.time())
HOST = "server_host"
subject = 'Netbackup daily report' + '-' + now_time
TO = ['[email protected]','[email protected]']
FROM='[email protected]'
msgtext = MIMEText(html_str,'html','utf-8')
msg = MIMEMultipart('related')  
msg['From'] = FROM
msg['To'] = ','.join(TO)
msg['Subject'] = subject     
msg['Date'] = formatdate(localtime=True)   
msg.attach(msgtext)  
server = smtplib.SMTP()
#server = smtplib.SMTP_SSL(HOST)
server.connect(HOST,'25')
server.sendmail(FROM,TO,msg.as_string())                                                                                                                                                                                                                                  
server.quit()

 

转载于:https://my.oschina.net/u/2313186/blog/853488

你可能感兴趣的:(python,运维,awk)