多线程批量ping服务器

最近由于工作的需要写了个多线程批量ping服务器的脚本,如果服务器down了,自动发报警邮件(可以添加多收件人)

 

提示:程序网页显示可以能有缩进问题。

multithreading_ping_host.py 主程序使用需要一个参数,(参数:服务器IP地址列表)

send_mail.py 发邮件程序

ip.yml 服务器IP地址列表

[root@nginx1 python]# cat /root/python/ip.yml 
192.168.1.1
192.168.1.103
192.168.1.100
www.360kad.com
192.168.1.245

[root@nginx1 python]# ./multithreading_ping_host.py /root/python/ip.yml 

程序效果:

[root@nginx1 python]# cat multithreading_ping_host.py 
#!/usr/bin/python
# -*- coding: gbk -*-
import multiprocessing
import os,sys
import time

filename=sys.argv[1]

if filename == '':
    sys.exit(0)

result=[]
excution_list=[]

def ip_address(iplist):
        hosts=[]
        IP=[]
        if os.path.exists(filename):
                f=file(filename)
                for line in f.readlines():
                        f_line =line.strip().split()
                        hosts.append(f_line)

                for i in range(int(len(hosts))):
                        IP.append(hosts[i][0])
                return IP
        else:
                print "\033[31mfile:-->%s not found\033[0m" % (filename)
                sys.exit(1)

hosts=ip_address(filename)

 

for line in hosts:

        cmd="ping -c 2 %s > /dev/null" % line
        excution_list.append(cmd)


def run_cmd(cmd):
         if os.system(cmd) == 0:
                 ip=cmd.split()
                 HOST=ip[3]
                 print  "%s is:" % (HOST) + "\033[32m UP\033[0m"
       else:
                 ip=cmd.split()
                 HOST=ip[3]
                 print  "%s is:" % (HOST) + "\033[31m DOWN\033[0m"
                 SEN=HOST+':down'
                 os.system('/root/python/send_mail.py '+SEN +' '+ HOST+':服务器down机了')

                 #如果服务器down了调用send_mail.py发报警邮件

def main():
         work=multiprocessing.Pool(processes=len(excution_list))
         for task in excution_list:
                 result.append(work.apply_async(run_cmd,(task,)))
         work.close()

 

         for print_result in result:
                 print_result.get()

 

if __name__ == '__main__':
 main()

 

[root@nginx1 python]# cat send_mail.py 
#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
import smtplib
from email.mime.text import MIMEText

mailto_list=["[email protected]"] #可定义多个收件人以头号隔开。
mail_host="mail.sina.com"
mail_user="*********"
mail_pass="*********"
mail_postfix="sina.com"

SEN=sys.argv[1]
CON=sys.argv[2]

if SEN == '  ' and CON == '  ':
        sys.exit(0)

def send_mail(to_list,sub,content):
            me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
            msg = MIMEText(content)
            msg['Subject'] = sub
            msg['From'] = me
            msg['To'] = ";".join(to_list)
            try:
                    s = smtplib.SMTP()
                    s.connect(mail_host)
                    s.login(mail_user,mail_pass)
                    s.sendmail(me, to_list, msg.as_string())
                    s.close()
                    return True
            except Exception, e:
                    print str(e)
                    return False


if __name__ == '__main__':

        if send_mail(mailto_list,SEN,CON):

                print "\033[32m邮件通知成功..\033[0m"
        else:

                print "\033[31m邮件通知失败..\033[0m"

---------------------------------------------------------------------------------------------

 

 

 

 

 

 

你可能感兴趣的:(多线程批量ping服务器)