zoukankan      html  css  js  c++  java
  • python监控端口脚本[jkport2.0.py]

    #!/usr/bin/env python
    #!coding=utf-8
    import os
    import time
    import sys
    import smtplib
    from email.mime.text import MIMEText
    from email.MIMEMultipart import MIMEMultipart
    
    def sendsimplemail (warning):
        msg = MIMEText(warning)
        msg['Subject'] = 'python first mail'
        msg['From'] = 'root@localhost'
        try:
            smtp = smtplib.SMTP()
            smtp.connect(r'pop.qq.com')
            smtp.login('1427746783@qq.com', 'passwd')
            smtp.sendmail('1427746783@qq.com', ['1427746783@qq.com'], msg.as_string())
            smtp.close()
        except Exception, e:
            print e
    
    time_str =  time.strftime( "%Y-%m-%d", time.localtime( ) )
    file_name = "./" + time_str + ".log"
    
    while True:
        print_str = "";
        Pro_status = os.popen('netstat -tulnp | grep nginx','r').readlines()
        now_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        try:
            if Pro_status == []:
                os.system('service nginx start')
                new_Pro_status = os.popen('netstat -tulnp | grep nginx','r').readlines()
                str1 = ''.join(new_Pro_status)
                port = str1.split()[3].split(':')[-1]
                if port != '9999':
                    print_str = print_str + "nginx start fail ..."
                else:
                    print_str = print_str + "nginx Program abort. an internal error has occurred" + '
    '
                    print_str = print_str + "nginx start success ..."
    #               sendsimplemail(warning = "This is a warning!!!")
            else:
                print_str = print_str + now_time +"nginx running ..."
            time.sleep(3)
            
            if os.path.exists ( file_name ) == False :
                os.mknod( file_name )
                handle = open ( file_name , "w" )
    #           print ( print_str + "**********1")
                try:
                 handle.write( print_str)
                except:
                 log.error('write backup error:')
                finally:
                 handle.close()
    #            print ( print_str + "**********2")
            else:
                handle = open ( file_name , "a" )
                print ( print_str + "**********3")
                handle.write( print_str + '
    ')
    
        except KeyboardInterrupt:
            sys.exit('
    ')
    

    这个是jkport更新版, 旧版在这里:python监控端口脚本

    新增日志输出功能

    运行模式, 直接守护:nohup ./jkport.py &

    不用像以前那么繁琐。

  • 相关阅读:
    数论学习之乘法逆元
    数论学习之扩展欧几里得
    数论学习之费马与欧拉
    一次函数
    东南西北
    接水问题
    脱水缩合
    背单词
    单词接龙
    字符串,字符数组
  • 原文地址:https://www.cnblogs.com/chenglee/p/7840146.html
Copyright © 2011-2022 走看看