In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Monitor whether the database can be written. If the logic of your monitoring script is yes, it will show success after writing to the database successfully, otherwise it will show failure and alarm. Then the problem comes, the database really can't be written, your monitoring script write command will also be mysql hang live, has been stuck there, until the end of time, simply can't realize the alarm. Then change the way you think. Wouldn't it be better if you set a timeout?
#!/ usr/bin/env python# -*-coding:utf8-*-import MySQLdbimport reimport smtplibimport jsonfrom email.mime.text import MIMETextimport sysimport timeimport multiprocessingreload(sys)sys.setdefaultencoding('utf8')def mysql_select(sql, pipe): try: conn = MySQLdb.connect(host='xxx.xxx.xxx.xxx',user='xxxx',passwd='xxxx',db='xxxx',port=xxxx,charset='utf8',connect_timeout=10) cursor = conn.cursor() cursor.execute(sql) result = cursor.fetchall() cursor.close() conn.commit() conn.close() pipe.send('successful') except Exception,e: pipe.send("zabbix database exception: %s" % e) def query_with_timeout(sql): pipe_out, pipe_in = multiprocessing.Pipe(False) subproc = multiprocessing.Process(target=mysql_select,args=(sql, pipe_in)) subproc.start() subproc.join(timeout=3) if pipe_out.poll(): ex_c = pipe_out.recv() else: ex_c = "zabbix database cannot be written" subproc.terminate() #raise Exception("Query %r ran for >%r" % (sql, 5)) raise Exception(ex_c)### def se_mail(mail_result): now_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())) sys.setdefaultencoding('utf-8') SUBJECT = "Database Monitoring" TO = "xxxx@ptthink.com" FROM = "ooooo@ptmind.com" msg = MIMEText(""" Zabbix Database Monitoring in China alerting region China host name xxx.xxx.xxx.xxx alarm item zabbix database monitoring alarm level serious alarm state PROBLEM details """ + mail_result + """ occurrence time """ + now_time + """ ""","html","utf-8") msg['Subject'] = SUBJECT msg['From']=FROM msg['To']=TO try: server = smtplib.SMTP('localhost') server.sendmail(FROM, TO, msg.as_string()) server.quit() print "Email sent successfully! " except Exception, e: print "failed: "+str(e) ### if __name__ == '__main__': #Create a monitoring database connection, and whether it can be written, the monitoring table, the following is the creation statement #sql_user_info = """ #CREATE TABLE IF NOT EXISTS db_check_table ( #itemid INT(20), #applicationid INT(20), #hostid INT(20), #name VARCHAR(255), #du_name VARCHAR(255), #item_name VARCHAR(255) #) #""" insert_sql = """insert into db_check_table values ('10211','13564','456789','test-172.5.6.7','cpu','cpu ldie')""" try: query_with_timeout(insert_sql) except Exception,e: mail_result = str(e) if mail_result != "successful" : se_mail(mail_result)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.