def gen_alert_sqlserver(server_id, role_switch, db_name): if g_alert != "1": return -1 mysql_conn = '' try: mysql_conn = mysql.ConnectMysql() except Exception as e: logger.error(e) sql = """SELECT a.server_id, b.tags, a.host, a.port, b.send_mail, b.send_mail_to_list, b.send_sms, b.send_sms_to_list, b.send_wx, 'sqlserver' AS db_type FROM sqlserver_status a, db_cfg_sqlserver b WHERE a.server_id = b.id and a.server_id = %s """ % (server_id) result = mysql.GetMultiValue(mysql_conn, sql) if result <> 0: for line in result: server_id = line[0] tags = line[1] host = line[2] port = line[3] send_mail = line[4] send_mail_to_list = line[5] send_sms = line[6] send_sms_to_list = line[7] send_wx = line[8] db_type = line[9] if send_mail_to_list is None or send_mail_to_list.strip() == '': send_mail_to_list = mail_to_list_common if send_sms_to_list is None or send_sms_to_list.strip() == '': send_sms_to_list = sms_to_list_common create_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) if role_switch == 1: msg = "database mirror of %s has been switched" % (db_name) send_mail = update_send_mail_status(server_id, db_type, 'role_switch', send_mail, send_mail_max_count) send_sms = update_send_sms_status(server_id, db_type, 'role_switch', send_sms, send_sms_max_count) send_wx = update_send_wx_status(server_id, db_type, 'role_switch', send_wx) add_alert(server_id, tags, host, port, create_time, db_type, 'role_switch', 'Principal', 'warning', msg, send_mail, send_mail_to_list, send_sms, send_sms_to_list, send_wx) else: pass
def get_option(key): try: mysql_conn = mysql.ConnectMysql() sql = "select value from options where name='%s'; " % (key) option_value = mysql.GetSingleValue(mysql_conn, sql) return option_value except Exception, e: print 'traceback.print_exc():' traceback.print_exc()
def update_send_sms_status(server, db_type, alert_item, send_sms, send_sms_max_count): try: mysql_conn = mysql.ConnectMysql() sql = "" if db_type == "os": sql = "select count(1) from alerts_temp where ip='%s' and db_type='%s' and alert_item='%s' and alert_type='sms' and create_time > date_add(sysdate(), interval -%s second);" % ( server, db_type, alert_item, send_sms_sleep_time) else: sql = "select count(1) from alerts_temp where server_id=%s and db_type='%s' and alert_item='%s' and alert_type='sms' and create_time > date_add(sysdate(), interval -%s second);" % ( server, db_type, alert_item, send_sms_sleep_time) alert_count = mysql.GetSingleValue(mysql_conn, sql) if int(alert_count) > 0: send_sms = 0 else: send_sms = send_sms return send_sms except Exception, e: print 'traceback.print_exc():' traceback.print_exc()
def add_alert(server_id, tags, db_host, db_port, create_time, db_type, alert_item, alert_value, level, message, send_mail, send_mail_to_list, send_sms, send_sms_to_list): try: mysql_conn = mysql.ConnectMysql() if db_type == 'os': count_str = "select id from alerts where host='%s' and alert_item='%s';" % ( db_host, alert_item) alert_count = mysql.GetSingleValue(mysql_conn, count_str) if int(alert_count) > 0: sql = "insert into alerts_his select *,DATE_FORMAT(sysdate(),'%%Y%%m%%d%%H%%i%%s') from alerts where host='%s' and alert_item='%s';" % ( db_host, alert_item) try: mysql.ExecuteSQL(mysql_conn, sql) except Exception, e: print "Move alert to history: " + str(e) sql = "delete from alerts where host='%s' and alert_item='%s' ;" % ( db_host, alert_item) mysql.ExecuteSQL(mysql_conn, sql) else:
sys.exit(2) for opt, arg in opts: if opt == '-p': pri_id = arg elif opt == '-s': sta_id = arg elif opt == '-g': group_id = arg ########################################################################### # connect to mysql mysql_conn = '' try: mysql_conn = mysql.ConnectMysql() except Exception as e: logger.error(e) sys.exit(2) # get infomation from mysql db_name = "" p_host = "" p_port = "" p_username = "" p_password = "" s_host = "" s_port = ""