首页 > 代码库 > Python监控服务端口并报警
Python监控服务端口并报警
最近发现公司的测试环境中有个Socket服务的端口总是莫名其妙Down掉,但是服务却正常运行着,看样子是僵死了。。。
虽然是测试环境,但是也不能这样放着不管,于是连夜写了一个简单的监控脚本。因为服务器是Windows的,所以要用到wmi模块。逻辑如下:
1、用wmi模块获取系统中处于停止状态的服务,生成一个字典。
2、判断监控的服务是否存在于字典中,如果存在说明服务已经停止,那么将尝试启动服务,并发送报警邮件。
3、向本地的Socket服务端口发送一个connect,如果捕获到异常将尝试重启服务,并发送报警邮件。
4、每次执行时脚本将会循环执行以上步骤三次,间隔10秒,以确保服务状态正常。
在运行的时候发现了一个问题,Python使用wmi模块来对Windows系统进行操作的时候速度格外的慢,不知道有没有其他的代替方法,哪位如果有更好的方法可以指点一下。
源码如下:
#!/usr/bin/env pythonimport osimport wmiimport timeimport socketimport base64import smtplibimport loggingfrom email.mime.text import MIMETextdef GetSrv(designation): """Get stopped service name and caption, Filtration ‘designation‘ service whether there is ‘Stopped‘. :return: service state """ c = wmi.WMI() ret = dict() for service in c.Win32_Service(): state, caption = service.State, service.Caption if state == ‘Stopped‘: t = ret.get(state, []) t.append(caption) ret[state] = t # If ‘designation‘ service in the ‘Stopped‘, return status is ‘down‘ if designation in ret.get(‘Stopped‘): logging.error(‘Service [%s] is down, try to restart the service. \r\n‘ % designation) return ‘down‘ return Truedef Monitor(sname): """Send the machine IP port 20000 socket request, If capture the abnormal returns the string ‘ex‘. :return: string ‘ex‘ """ 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 ‘ex‘ return Truedef RestartSocket(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. :return: flag or True """ flag = False try: # From GetSrv() to obtain the return value, the return value if run == ‘down‘: ret = os.system(‘sc start "%s"‘ % rstname) if ret != 0: raise Exception(‘[Errno %s]‘ % ret) flag = True elif conn == ‘ex‘: 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 flagdef SendMail(to_list, sub, contents): """Send alarm mail. :return: flag """ mail_server = ‘mail.stmp.com‘ # STMP Server mail_user = ‘YouAccount‘ # Mail account mail_pass = base64.b64decode(‘Password‘) # The encrypted 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 flagdef 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 three times, each time interval to 10 seconds. :return: retValue """ retry = 3 count = 0 retValue = False # Used return to the state of the socket while count < retry: ret = Monitor(sname) if ret != ‘ex‘: # If socket connection is normaol, return retValue retValue =http://www.mamicode.com/ ret return retValue isDown = GetSrv(sname) RestartSocket(rstname=sname, conn=ret, run=isDown) host = socket.gethostname() address = socket.gethostbyname(host) mailto_list = [‘mail@smtp.com‘, ] # Alarm contacts SendMail(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 retValueif __name__ == ‘__main__‘: logging.basicConfig(level=logging.INFO, format=‘%(asctime)s %(levelname)s %(message)s‘, datefmt=‘%Y/%m/%d %H:%M:%S‘, filename=‘D:\\em_logs\\SocketMonitor.log‘, filemode=‘ab‘) name = ‘IM_IMConnectorServerWinService‘ response = main(name) if response: logging.info(‘The [%s] service connection is normal \r\n‘ % name)
Python监控服务端口并报警
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。