最近发现公司的测试环境中有个Socket服务的端口总是莫名其妙Down掉,但是服务却正常运行着,看样子是僵死了。。。

虽然是测试环境,但是也不能这样放着不管,于是连夜写了一个简单的监控脚本。因为服务器是Windows的,所以要用到wmi模块。逻辑如下:

1、使用CMD命令"net start"获取系统中处于运行状态的服务,将这些服务名称生成一个列表。

2、判断监控的服务是否存在于列表中,如果不存在说明服务已经停止,那么将尝试启动服务,并发送报警邮件。

3、向本地的Socket服务端口发送一个connect,如果捕获到异常将尝试重启服务,并发送报警邮件。

4、每次执行时脚本将会循环执行以上步骤两次,间隔10秒,以确保服务状态正常。

在运行的时候发现了一个问题,Python使用wmi模块来对Windows系统进行操作的时候速度格外的慢,不知道有没有其他的代替方法,哪位如果有更好的方法可以指点一下。

更新:用Windows CMD命令"net start"代替了wmi模块获取运行中的服务名列表。

源码如下:

#!/usr/bin/env python

import os
import wmi
import time
import socket
import smtplib
import logging
from email.mime.text import MIMEText def get_stop_service(designation):
"""To obtain a list of running the service name,
check whether the monitoring server is present in the list.
"""
lines = os.popen('net start').readlines()
line = [item.strip() for item in [i for i in lines]]
if designation in line:
return True
else:
logging.error('Service [%s] is down, try to restart the service. \r\n' % designation)
return False def monitor(sname):
"""Send the machine IP port 20000 socket request,
If capture the abnormal returns false.
"""
s = socket.socket()
s.settimeout(3) # timeout
host = ('127.0.0.1', 20000)
try: # Try connection to the host
s.connect(host)
except socket.error as e:
logging.warning('[%s] service connection failed: %s \r\n' % (sname, e))
return False
return True def restart_service(rstname, conn, run):
"""First check whether the service is stopped,
if stop, start the service directly.
The check whether the zombies,
if a zombie, then restart the service.
"""
flag = False
try:
# From get_stop_service() to obtain the return value, the return value
if not run:
ret = os.system('sc start "%s"' % rstname)
if ret != 0:
raise Exception('[Errno %s]' % ret)
flag = True
elif not conn:
retStop = os.system('sc stop "%s"' % rstname)
retSart = os.system('sc start "%s"' % rstname)
if retSart != 0:
raise Exception('retStop [Status code %s] '
'retSart [Status code %s] ' % (retStop, retSart))
flag = True
else:
logging.info('[%s] service running status to normal' % rstname)
return True
except Exception as e:
logging.warning('[%s] service restart failed: %s \r\n' % (rstname, e))
return flag def send_mail(to_list, sub, contents):
"""
Send alarm mail.
"""
mail_server = 'mail.stmp.com' # STMP Server
mail_user = 'YouAccount' # Mail account
mail_pass = 'Password' # password
mail_postfix = 'smtp.com' # Domain name me = 'Monitor alarm<%s@%s>' % (mail_user, mail_postfix)
message = MIMEText(contents, _subtype='html', _charset='utf-8') message['Subject'] = sub
message['From'] = me
message['To'] = ';'.join(to_list) flag = False # To determine whether a mail sent successfully
try:
s = smtplib.SMTP()
s.connect(mail_server)
s.login(mail_user, mail_pass)
s.sendmail(me, to_list, message.as_string())
s.close()
flag = True
except Exception, e:
logging.warning('Send mail failed, exception: [%s]. \r\n' % e) return flag def main(sname):
"""Parameter type in the name of the service need to monitor,
perform functions defined in turn, and the return value is correct.
After the program is running, will test two times,
each time interval to 10 seconds.
"""
retry = 2
count = 0
retval = False # Used return to the state of the socket
while count < retry:
ret = monitor(sname)
if not ret: # If socket connection is normaol, return retval
retval = ret
return retval
isDown = get_stop_service(sname)
restart_service(rstname=sname, conn=ret, run=isDown) host = socket.gethostname()
address = socket.gethostbyname(host)
mailto_list = ['mail@smtp.com', ] # Alarm contacts
send_mail(mailto_list,
'Alarm',
' <h4>Level: <u>ERROR</u></br> Host name: %s</br>'
' IP Address: %s</br>'
' Service name:</h4> <h5>%s</h5>'
% (host, address, sname))
count += 1
time.sleep(10)
else:
logging.error('[%s] service try to restart more than three times \r\n' % sname) return retval if __name__ == '__main__': logging.basicConfig(level=logging.INFO,
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%Y/%m/%d %H:%M:%S',
filename='D:\\logs\\Monitor.log',
filemode='ab') name = 'Service Name'
response = main(name)
if response:
logging.info('The [%s] service connection is normal \r\n' % name)

以上代码还是有可以改进的地方,将多个服务名写到文件中,程序去读取文件中的服务依次进行检测。

Python检测服务端口存活状态并报警的更多相关文章

  1. [记录]Zabbix3.4配置监控Oracle12c的存活状态和表空间使用率

    Zabbix3.4配置监控Oracle的存活状态和表空间使用率 1.安装zabbix3.4 agent: # rpm -ivh http://repo.zabbix.com/zabbix/3.4/rh ...

  2. 深入Java虚拟机--判断对象存活状态

    程序计数器,虚拟机栈和本地方法栈 首先我们先来看下垃圾回收中不会管理到的内存区域,在Java虚拟机的运行时数据区我们可以看到,程序计数器,虚拟机栈,本地方法栈这三个地方是比较特别的.这个三个部分的特点 ...

  3. 网络基本概念备忘:MAC地址,端口,HTTP状态码

    MAC地址 英文MAC Address 英文全称: Media Access Control Address 别称:硬件位址 用途:定义网络设备位置 表示:十六进制数,6 Byte 特点:产品出产后M ...

  4. C#获得系统打开的端口和状态

    实际是通过c#编程方式调用了CMD命令行,然后调用netstat命令,然后将CMD命令的输出流转到了C#控制台程序上.也可以将结果输出到文件. using System; using System.C ...

  5. python MySQL-Slave从服务器状态检测脚本

    #!/bin/bash mysql -e "show slave status\G" > mysql_status.txt array=($(egrep 'Slave_IO_ ...

  6. python监控端口脚本[jkport2.0.py]

    #!/usr/bin/env python #!coding=utf-8 import os import time import sys import smtplib from email.mime ...

  7. 利用Python进行端口扫描

    利用Python进行端口扫描 - Dahlhin - 博客园 https://www.cnblogs.com/dachenzi/p/8676104.html Python实现对一个网络段扫描及端口扫描 ...

  8. Python监控主机是否存活,并发报警邮件

    利用python写了简单测试主机是否存活脚本,此脚本不适于线上使用,因为网络延迟.丢包现象会造成误报邮件,那么后续会更新判断三次ping不通后再发报警邮件,并启用多线程处理. #!/usr/bin/e ...

  9. Python 22端口发邮件

    #!/usr/bin/python#-*-coding:UTF-8-*- import smtplibimport timeimport os from email.mime.text import ...

随机推荐

  1. 如何在Node.js中合并两个复杂对象

    通常情况下,在Node.js中我们可以通过underscore的extend或者lodash的merge来合并两个对象,但是对于像下面这种复杂的对象,要如何来应对呢? 例如我有以下两个object: ...

  2. 微软要如何击败Salesforce?Office365、Azure、Dynamics365 全面布局AI | 双语

    微软在上月宣布组建自己的 AI 研究小组.该小组汇集了超过 5000 名计算机科学家和工程师,加上微软内部研究部门,将共同挖掘 AI 技术. 与此同时,亚马逊,Facebook,Google,IBM ...

  3. Android DEX 基础

    转载请标明出处:http://www.cnblogs.com/zhaoyanjun/p/5736305.html本文出自[赵彦军的博客] 1.什么是dex? 简单说就是优化后的android版.exe ...

  4. Ionic设置ion-slide-box不启用(不通过$ionicSlideBoxDelegate)

    猛地一看这个标题,可能觉得多此一举,直接$ionicSlideBoxDelegate. $getByHandle(handle). enableSlide(false)设置不就行了?是的,按理说就是这 ...

  5. Android中使用Notification实现宽视图通知栏(Notification示例二)

    Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...

  6. IOS开发基础知识--碎片50

      1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...

  7. tuple放入dict中

    tuple放入dict中是否可以正常运行 # 将tuple放入dict中 a = ('AI','Kobe','Yao') b = ('AI',['Kobe','Yao']) dict1 = {'a': ...

  8. 阿里云yum源安装

      1.先清理掉yum.repos.d下面的所有repo文件 [root@localhost yum.repos.d]# rm -rf * 2.下载repo文件  wget http://mirror ...

  9. Spring MVC之@RequestParam @RequestBody @RequestHeader 等详解

    (转自:http://blog.csdn.net/walkerjong/article/details/7946109#) 引言: 接上一篇文章,对@RequestMapping进行地址映射讲解之后, ...

  10. 深入java集合学习1-集合框架浅析

    前言 集合是一种数据结构,在编程中是非常重要的.好的程序就是好的数据结构+好的算法.java中为我们实现了曾经在大学学过的数据结构与算法中提到的一些数据结构.如顺序表,链表,栈和堆等.Java 集合框 ...