python监控端口脚本[jkport1.0.py]
此脚本根据端口判断进程是否存活, 如果有指定的端口就证明进程是没问题的, 如果检测不到端口就是说业务进程已经挂掉了, 此时自动重启程序, 不多说下面请看脚本
创建脚本
我这里模拟的是nginx, 监控端口是9999, 如果您的是8080或者80, 可以自行更改,记得启动程序命令也需要修改喔,
这个脚本是linux中的, 如果是windows中的请看这里Windows server利用批处理脚本判断端口, 启动tomcat原理都是一样的, 换汤不换药.
里面也支持邮件提醒功能, 需要的自行配置.
#!/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('mail@qq.com', 'mail_passwd')
smtp.sendmail('mail@qq.com', ['mail@qq.com'], msg.as_string())
smtp.close()
except Exception, e:
print e while True:
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 'nginx start fail ...'
else:
print 'nginx start success ...'
sendsimplemail(warning = "This is a warning!!!")
else:
print now_time , 'nginx 进程正常运行中 ... '
time.sleep(10)
except KeyboardInterrupt:
sys.exit('\n')
脚本可以放在任意位置,启动方式: 守护进程
我的启动方式是, 守护进程nohup, 然后把打印的结果输入到日志文件中, 我再py脚本中并没有配置输出日志, 我这个加上的是标准输出, 下面是启动方式
nohup python -u jkport.py >>time.log 2>&1 &
看上面已经看出来, 我的脚本名字是jkport.py, 日志名字是time.log, 没5秒把监控到的信息输入进来, 一旦发现程序挂掉, 会自动启动, 如果检测到程序存在, 信息提示正常运行中
如有问题,请自行检测或反馈, 谢谢.
python监控端口脚本[jkport1.0.py]的更多相关文章
- python监控端口脚本[jkport2.0.py]
#!/usr/bin/env python #!coding=utf-8 import os import time import sys import smtplib from email.mime ...
- python扫描端口脚本
# -*- coding:utf8 -*- # # Python: 2.7.8 # Platform: Windows # Authro: wucl # Program: 端口扫描 # History ...
- Python 监控脚本
Python 监控脚本 整体通过psutil模块动态获取资源信息.下为示例图: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time: 2019- ...
- centos 7中监控mysql 数据库脚本(监控端口)
centos 7中监控mysql 数据库脚本(监控端口) 监控mysql数据库的方法如下: 1.监控端口 netstat -nltp |grep 3306 2.监控进程 ps -ef |grep 33 ...
- 【JMeter4.0学习(五)】JMeter对服务器监控测试脚本开发
目录: 下载相关JMeter插件 服务器监控测试脚本开发 附:参考相关文档 本文主要来说一下如何通过JMeter插件来监控服务器CPU.内存.磁盘.网络等相关资源. 一.首先,需要下载相关JMeter ...
- python 监控redis的进程与端口
#!/usr/bin/python # -*- coding:utf-8 -*- import glob,psutil import json,os,datetime import collectio ...
- 【Linux】CentOS下升级Python和Pip版本全自动化py脚本
[Linux]CentOS下升级Python和Pip版本全自动化py脚本 CentOS7.6自带py2.7和py3.6 想要安装其它版本的话就要自己重新下载和编译py其它版本并且配置环境,主要是软链接 ...
- Zabbix如何实现批量监控端口状态
引言 ------------------------------------------------------------------------------------------------- ...
- Python数据库备份脚本
Python数据库备份脚本 #!/usr/bin/env python # author: liudong # -*- coding: utf-8 -*- # filename: db_bak.py ...
随机推荐
- maven冲突问题
通过配置文件解决问题: http://stamen.iteye.com/blog/2030552 1.用命令dependency:tree得到依赖关系 (或者加上Dincludes或者Dexclude ...
- ZOJ 3781 - Paint the Grid Reloaded - [DFS连通块缩点建图+BFS求深度][第11届浙江省赛F题]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 Time Limit: 2 Seconds Me ...
- Oracle Function:COUNT
Description The Oracle/PLSQL COUNT function returns the count of an expression. The COUNT(*) functio ...
- Oracle核心技术之 SQL TRACE
1.SQL TRACE说明: 参数类型 布尔型 缺省值 false 参数类别 动态 取值范围 True|false 2.类型 1)sql trace参数:alter system改变对全局进程影响,如 ...
- pandas介绍及环境部署
pandas介绍 Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的 ...
- Redis经验谈(转)
原文:http://www.programmer.com.cn/14577/ 新浪作为全世界最大的Redis用户,在开发和运维方面有非常多的经验.本文作者来自新浪,希望能为业界提供一些亲身经历,让大家 ...
- kubernetes实战(十一):k8s使用openLDAP统一认证
1.基本概念 为了方便管理和集成jenkins,k8s.harbor.jenkins均使用openLDAP统一认证. 2.部署openLDAP 此处将openLDAP部署在k8s上,openLDAP可 ...
- 【Pyton】【小甲鱼】类和对象:一些相关的BIF(内置函数)
1.issubclass(class,classinfo) 1)一个类被认为是其自身的子类 2)classinfo可以使类对象组成的元祖,只要class与其中任何一个候选类的子类,则返回True. & ...
- MySQL如何开启慢查询
一 简介 开启慢查询日志,可以让MySQL记录下查询超过指定时间的语句,通过定位分析性能的瓶颈,才能更好的优化数据库系统的性能. 二 参数说明 slow_query_log 慢查询开启状态 slo ...
- PAT A+B for Polynomials[简单]
1002 A+B for Polynomials (25)(25 分) This time, you are supposed to find A+B where A and B are two po ...