python2.6写看门狗并发邮件
来源:互联网 发布:淘宝代购真的假的 编辑:程序博客网 时间:2024/06/07 02:22
top -b 就可以去掉特殊字符。
python写看门狗很合适,永不死,以及方便获取各种信息。
若程序死掉,获取一些信息,然后重启程序,发邮件。
发送的邮件为html格式,比文本更方便阅读。
#!/usr/bin/python2.6# -*- coding: utf-8 -*-import time;import os;import sys;import signal;import socket;# set the default encoding to utf-8# reload sys model to enable the getdefaultencoding method.reload(sys);# using exec to set the encoding, to avoid error in IDE.exec("sys.setdefaultencoding('utf-8')");assert sys.getdefaultencoding().lower() == "utf-8";#TODO: check python version, >=2.6 requires.def handler(signum, frame): print 'usr press ctrl+c, exit'; sys.exit(0)signal.signal(signal.SIGINT, handler)# the command to get and check the My server statuscheck_My_server_command = "/etc/init.d/My-server status";# when server stopped, restart it.start_My_server_command = "/etc/init.d/My-server start";# the interval to check the server.check_My_server_interval_seconds = 5;# sleep seconds when main function exceptionmain_function_exception_sleep_seconds = 10;# email to user listto_user = ["winterserver@126.com", "winterserver@163.com"]# constsMy_server_dir = "/usr/local/My-server";My_server_version_command = "%s/objs/My_server"%(My_server_dir);My_server_conf_file="%s/conf/My_server.conf"%(My_server_dir);My_server_log_prefix = "mylog_server_system";import smtplibfrom email.mime.text import MIMETextdef send_mail(smtp_server, username, password, to_user, subject, content): msg = MIMEText(content, _subtype='html', _charset='gb2312'); msg['Subject'] = subject; msg['From'] = username; msg['To'] = ";".join(to_user); try: smtp = smtplib.SMTP(); smtp.connect(smtp_server); smtp.login(username, password); smtp.sendmail(username, to_user, msg.as_string()); smtp.close(); return 0; except Exception, ex: print "send email failed: %s"%(ex); return 1;if len(sys.argv) <= 3: print """Usage: %s <email_smtp_server> <email_username> <email_password> email_smtp_server the smtp server. eg. smtp.126.com email_username the email user to login to smtp server. eg. mymail@126.com email_password the email user password to login. e.g xxxxxFor example: %s smtp.126.com mymail@126.com xxxxx"""%(sys.argv[0], sys.argv[0]); sys.exit(1);(email_smtp_server, email_username, email_password) = sys.argv[1:];print "email_smtp_server=%s, email_username=%s, email_password=%s"%(email_smtp_server, email_username, email_password);def strip_execute_command_get_output(cmd): output = execute_command_get_output(cmd); output = [x.strip() for x in output]; return output; def execute_command_get_output(cmd): try: pipe = os.popen(cmd); output = pipe.readlines(); pipe.close(); return output; except Exception, ex: print "execute cmd %s failed: %s"%(cmd, ex); return "error";hostname = socket.gethostname();iplist = strip_execute_command_get_output('''ifconfig 2>/dev/null |grep "inet addr"|awk '{print $2}'|awk -F ':' '{print $2}'|grep -v "127.0.0.1"''');sysinfo = "%s %s"%(sys.platform, sys.version.replace("\n", " "));print "hostname=%s, iplist=%s, sysinfo=%s"%(hostname, iplist, sysinfo);def generate_content_then_start_server(): basic_cmd = time.strftime('%Y-%m-%d %X', time.localtime()); basic_info = "%s\n%s\n%s"%(hostname, " ".join(iplist), sysinfo); version_cmd = "%s -v 2>&1"%(My_server_version_command); version_info = execute_command_get_output(version_cmd); ls_cmd = "ls -lh %s 2>&1; ls -lh %s/log 2>&1"%(My_server_dir, My_server_dir); ls_info = execute_command_get_output(ls_cmd); lsbrelease_cmd = "lsb_release -a 2>&1"; lsbrelease_info = execute_command_get_output(lsbrelease_cmd); uname_cmd = "uname -a 2>&1"; uname_info = execute_command_get_output(uname_cmd); ccamr_cmd = "ccamr list 2>&1"; ccamr_info = execute_command_get_output(ccamr_cmd); ps_cmd = "ps aux|grep My|grep -v Myd|grep -v grep 2>&1"; ps_info = execute_command_get_output(ps_cmd); df_cmd = "df -h 2>&1"; df_info = execute_command_get_output(df_cmd); free_cmd = "free -m 2>&1"; free_info = execute_command_get_output(free_cmd); conf_cmd = "cat %s 2>&1"%(My_server_conf_file); conf_info = execute_command_get_output(conf_cmd); top_cmd = "top -c -n 1 -b 2>&1"; top_info = execute_command_get_output(top_cmd); vmstat_cmd = "vmstat 1 3 2>&1"; vmstat_info = execute_command_get_output(vmstat_cmd); iostat_cmd = "iostat 1 3 2>&1"; iostat_info = execute_command_get_output(iostat_cmd); print "dump info success"; # old logs old_logs = strip_execute_command_get_output("(cd %s/log/ && ls |grep %s) 2>&1"%(My_server_dir, My_server_log_prefix)); # start server. start_cmd = "%s 2>&1"%(start_My_server_command); start_info = execute_command_get_output(start_cmd); print "try start server completed"; # get the system start logs newlogs_cmd = "(cd %s/log/ && ls |grep %s"%(My_server_dir, My_server_log_prefix); for i in old_logs: newlogs_cmd = "%s| grep -v %s"%(newlogs_cmd, i); newlogs_cmd = "%s| xargs cat) 2>&1"%(newlogs_cmd); newlogs_info = execute_command_get_output(newlogs_cmd); content = ''' <style> div.cmd{ background-color: #00E000; font-size: 15px; font-weight: bold; padding: 2px 3px 2px 5px; } div.info{ background-color: #CCCCCC; margin-top: 0px; margin-bottom: 10px; margin-left: 10px; font-size: 12px; padding: 2px 3px 2px 5px; } </style> <!-- basic --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- version --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- ls --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- lsbrelease --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- uname --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- ccamr --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- ps --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- df --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- free --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- conf --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- top --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- vmstat --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- iostat --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- start server --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> <!-- server log --> <div> <div class='cmd'>%s</div> <div class='info'>%s</div> </div> '''%(basic_cmd, "<pre>%s</pre>"%"".join(basic_info), version_cmd, "<pre>%s</pre>"%"".join(version_info), ls_cmd, "<pre>%s</pre>"%"".join(ls_info), lsbrelease_cmd, "<pre>%s</pre>"%"".join(lsbrelease_info), uname_cmd, "<pre>%s</pre>"%"".join(uname_info), ccamr_cmd, "<pre>%s</pre>"%"".join(ccamr_info), ps_cmd, "<pre>%s</pre>"%"".join(ps_info), df_cmd, "<pre>%s</pre>"%"".join(df_info), free_cmd, "<pre>%s</pre>"%"".join(free_info), conf_cmd, "<pre>%s</pre>"%"".join(conf_info), top_cmd, "<pre>%s</pre>"%"".join(top_info), vmstat_cmd, "<pre>%s</pre>"%"".join(vmstat_info), iostat_cmd, "<pre>%s</pre>"%"".join(iostat_info), start_cmd, "<pre>%s</pre>"%"".join(start_info), newlogs_cmd, "<pre>%s</pre>"%"".join(newlogs_info) ); #print content; return content;def main(): while True: print "start check server status"; ret = os.system("%s > /dev/null 2>&1"%(check_My_server_command)); if ret != 0: print "server crash, ret=%s"%(ret); subject = "[MyServer] watchdog server %s crash"%(hostname); content = generate_content_then_start_server(); print "email to %s"%(to_user); if 0 == send_mail(email_smtp_server, email_username, email_password, to_user, subject, content): print "email success"; else: print "server is ok"; time.sleep(check_My_server_interval_seconds);while True: try: main(); except Exception, ex: print "main function exception: %s"%(ex); time.sleep(main_function_exception_sleep_seconds);
- python2.6写看门狗并发邮件
- python2.7发送邮件
- python2.7+smtp发邮件
- 汇编写启动代码之关看门狗
- 汇编写启动代码之关看门狗
- Redis2.6 的看门狗
- 看门狗
- 看门狗
- 看门狗
- 看门狗
- 看门狗
- 看门狗
- 看门狗
- 看门狗
- 看门狗
- 看门狗
- 看门狗
- 看门狗
- 剑指offer面试题30最小的k个数
- 找你妹时间2013-05-05(CSDN)
- spring心得6--自动装配知识点讲解及案例分析
- Android多媒体学习一:Android中Image的简单实例
- data mining (foreign blogs)
- python2.6写看门狗并发邮件
- 通过http请求传递xml流和接收xml流的代码示例
- 找啊找啊找GF --动态规划--类似采药
- ZOJ 2158/POJ 1789 - Truck History
- LPCTSTR与CString的类型转换
- Linux内核和驱动学习总结
- C语言-----链表的各项操作总结------双向循环链表
- 何为优秀的设计,何为美?
- 基类和派生类