def insert_collectalarm_exception(isNoticeNow,isNoticeNext,noticeNextUsers,noticeNextTime,ipinfo,alarminfo): ''' @服务器异常通知根据通知方式来更新osa_collect_alarm表 ''' try: isNotice = 0 alarmTime = cmdtosql._get_time(1) ##再添加一个服务器故障时间 startTime = str(ipinfo[5]) #计算服务器故障时间,并写入表osa_collect_alarm #faultTime = time.time()-time.mktime(time.strptime(startTime,"%Y-%m-%d %H:%M:%S")) if isNoticeNow == 0 and isNoticeNext == 0: return False else: if isNoticeNow > 0: isNotice =1 if isNoticeNext > 0: notice_sql = "insert into osa_collect_alarm(oIpid,oAlarmText,oAlarmTime,oAlarmLevel,oAlarmType,oIsNotice,oIsRead,oIsNoticeNext,oNoticeNextTime,oNoticeNextUsers) \ values("+str(ipinfo[0])+",'"+str(alarminfo['text'])+"','"+str(alarmTime)+"','"+str(alarminfo['level'])+"','server',"+str(isNotice)+",0,1,"+str(noticeNextTime)+",'"+str(noticeNextUsers.strip(','))+"')" else: notice_sql = "insert into osa_collect_alarm(oIpid,oAlarmText,oAlarmTime,oAlarmLevel,oAlarmType,oIsNotice,oIsRead) \ values("+str(ipinfo[0])+",'"+str(alarminfo['text'])+"','"+str(alarmTime)+"','"+str(alarminfo['level'])+"','server',"+str(isNotice)+",0)" log_debug("insert_collectalarm_exception()执行sql语句:"+str(notice_sql)) cmdtosql.execsql(notice_sql) except Exception as e: log_error("insert_collectalarm_exception():"+str(e))
def insert_collectdata_exception(ipid,result,replayTime=0): ''' @服务器异常通知,插入osa_collect_data记录 ''' try: result = result.replace("'","") collectTime=cmdtosql._get_time(1) snmp_sql = "insert into osa_collect_data(oIpid,oCollectTime,oCollectData,oReplayTime,oStatus) values("+str(ipid)+",'"+str(collectTime)+"','"+str(result)+"','"+str(replayTime)+"','异常')" log_debug("insert_collectdata_exception()执行sql语句:"+str(snmp_sql)) cmdtosql.execsql(snmp_sql) except Exception as e: log_error("insert_collectdata_exception():"+str(e))
def insert_collectdata_remind(ipid,result,replayTime=0): ''' @服务器恢复通知,插入osa_collect_data记录 ''' try: collectTime=cmdtosql._get_time(1) snmp_sql = "insert into osa_collect_data(oIpid,oCollectTime,oCollectData,oReplayTime,oStatus) values("+str(ipid)+",'"+str(collectTime)+"','"+str(result)+"','"+str(replayTime)+"','正常')" log_debug("insert_collectdata_remind()执行sql语句:"+str(snmp_sql)) cmdtosql.execsql(snmp_sql) except Exception as e: log_debug("insert_collectdata_remind()执行sql语句:"+str(snmp_sql)) log_error("insert_collectdata_remind():"+str(e))
def monitor_get_iteminfos(): ''' @osaMonitor 获取当前需要监控的项目信息 @满足时间条件和 oIsStop = 0 ''' try : time_now = cmdtosql._get_time(1) sql = "select * from osa_monitors where (oNextCheckTime is null or oNextCheckTime <='"+time_now+"') and oIsStop = 0 " log_debug("monitor_get_iteminfos()执行sql语句:"+str(sql)) return cmdtosql.select(sql) except Exception as e: log_error("monitor_get_iteminfos():"+str(e))
def monitor_insert_itemdata(itemid,result='',replayTime=0,status='正常'): ''' @osaMonitor 插入osa_monitor_record记录 @默认数据:恢复正常数据 ''' try: montime=cmdtosql._get_time(1) sql = "insert into osa_monitor_record(oItemid,oMonTime,oMonResult,oReplayTime,oStatus) values("+str(itemid)+",'"+str(montime)+"','"+str(result)+"','"+str(replayTime)+"','"+str(status)+"')" log_debug("monitor_insert_itemdata()执行sql语句:"+str(sql)) cmdtosql.execsql(sql) except Exception as e: log_error("monitor_insert_itemdata():"+str(e))
def insert_itemalarm_remind(isNoticeNow,isNoticeNext,noticeNextUsers,noticeNextTime,iteminfo,type): ''' @osaMonitor 恢复通知根据通知方式来更新osa_monitor_alarm表 ''' try: isNotice = 0 if isNoticeNow == 0 and isNoticeNext == 0 and iteminfo[22] == str(0): return False else: alarmTime = cmdtosql._get_time(1) itemid = iteminfo[0] reason = monitor_get_lastReason(itemid) ntext = '项目恢复正常' if reason == '' or reason == '项目恢复正常': ntext = '不稳定' ##再添加一个服务器故障时间 startTime = str(iteminfo[18]) #计算服务器故障时间,并写入表osa_monitor_alarm if startTime == 'None' or startTime == '': startTime = cmdtosql._get_time(1) faultTime = time.time()-time.mktime(time.strptime(startTime,"%Y-%m-%d %H:%M:%S")) if isNoticeNow > 0: isNotice =1 if isNoticeNext > 0: notice_sql = "insert into osa_monitor_alarm(oItemid,oMonName,oAlarmText,oAlarmTime,oAlarmLevel,oAlarmType,oIsNotice,oIsRead,oFaultTime,oIsNoticeNext,oNoticeNextTime,oNoticeNextUsers) \ values("+str(iteminfo[0])+",'"+str(iteminfo[1])+"','"+str(ntext)+"','"+str(alarmTime)+"','4','"+str(type)+"',"+str(isNotice)+",0,"+str(faultTime)+",1,"+str(noticeNextTime)+",'"+str(noticeNextUsers.strip(','))+"')" else: notice_sql = "insert into osa_monitor_alarm(oItemid,oMonName,oAlarmText,oAlarmTime,oAlarmLevel,oAlarmType,oIsNotice,oIsRead,oFaultTime) \ values("+str(iteminfo[0])+",'"+str(iteminfo[1])+"','"+str(ntext)+"','"+str(alarmTime)+"','4','"+str(type)+"',"+str(isNotice)+",0,"+str(faultTime)+")" log_debug("insert_itemalarm_remind()执行sql语句:"+str(notice_sql)) cmdtosql.execsql(notice_sql) except Exception as e: log_error("insert_itemalarm_remind():"+str(e))
def batchresult(r): ''' @处理批量操作结果 ''' rdict = eval(r) oCmdType = rdict['batchinfo']['command'] oBatchid = rdict['batchinfo']['id'] oClientip = rdict['batchinfo']['clientip'] oRunTime = _get_time(flag=1) oResult = str(rdict['batchresult']) if rdict['batchinfo']['type'] == 'batch': isql = "INSERT INTO `osa_tasknow_result` (`oCmdType`, `oRunTime`, `oTaskNowid`,`oClientip`, `oResult`) VALUES ('" + oCmdType + "', '" + str( oRunTime) + "', " + str(oBatchid) + ", '" + str( oClientip) + "', \"" + oResult + "\")" else: isql = "INSERT INTO `osa_taskplan_result` (`oCmdType`, `oRunTime`, `oTaskPlanid`, `oClientip`, `oResult`) VALUES ('" + oCmdType + "', '" + str( oRunTime) + "', " + str(oBatchid) + ", '" + str( oClientip) + "', \"" + oResult + "\")" try: con = _get_pcon() cur = con.cursor() cur.execute(isql) except Exception as inserror: _exit(con, cur) save_log( 'ERROR', 'insert into db error 1,sql is:' + isql + ",error info:" + str(inserror)) time.sleep(random.randint(0, 10)) try: con = _get_con() cur = con.cursor() cur.execute(isql) except Exception as inserror: save_log( 'ERROR', 'insert into db error 2,sql is:' + isql + ",error info:" + str(inserror)) finally: _exit(con, cur) sys.exit() finally: _exit(con, cur) sys.exit()
def CreateAlarmMsg(oItemName,oItemid,oServerip,oAlarmInfo,oType): ''' 增加新的告警信息:往osa_alarmmsg表里增加新的条目 ''' now = _get_time(1) sql = "INSERT INTO osa_alarmmsg (`oAddTime`, `oItemName`, `oItemid`, `oServerip`, `oAlarmInfo`, `oType`) VALUES ('"+now+"','"+oItemName+"',"+str(oItemid)+",'"+oServerip+"','"+oAlarmInfo+"',"+str(oType)+")" try: con = _get_pcon() cur = con.cursor() cur.execute(sql) except Exception as e: save_log('ERROR','sql INSERT fail! sql:'+sql+',ERROR:'+str(e)) _exit(con, cur) return finally: _exit(con, cur) return
def update_iteminfo_except(isNoticeNow,isNoticeNext,iteminfo): ''' @osaMonitor 服务异常报警更新osa_monitors 记录 ''' try: oNotifiedNum = "" if str(iteminfo[17]) == '1':#表示上次正常,错误时间从现在开始 faultTime = cmdtosql._get_time(1) else:#表示上次异常,错误时间从过去开始 faultTime = iteminfo[18] if isNoticeNow>0 or isNoticeNext >0: oNum = int(iteminfo[19])+1 notiNum = int(iteminfo[20])+1 sql = "update osa_monitors set oStatus='0' ,oFaultTime='"+str(faultTime)+"',oNotifiedNum="+str(oNum)+",oNotiNum="+str(notiNum)+",oIsEmail='1' where id="+str(iteminfo[0]) else: sql = "update osa_monitors set oStatus='0' ,oFaultTime='"+str(faultTime)+"',oIsEmail='1' where id="+str(iteminfo[0]) log_debug("update_iteminfo_except()执行sql语句:"+str(sql)) cmdtosql.update(sql) except Exception as e: log_error("update_iteminfo_except():"+str(e))
def insert_itemalarm_except(isNoticeNow,isNoticeNext,noticeNextUsers,noticeNextTime,iteminfo,alarminfo): ''' @osaMonitor 发生异常时更新osa_monitor_alarm表 ''' try: isNotice = 0 alarmTime = cmdtosql._get_time(1) if isNoticeNow == 0 and isNoticeNext == 0: return False else: if isNoticeNow > 0: isNotice =1 if isNoticeNext > 0: notice_sql = "insert into osa_monitor_alarm(oItemid,oMonName,oAlarmText,oAlarmTime,oAlarmLevel,oAlarmType,oIsNotice,oIsRead,oIsNoticeNext,oNoticeNextTime,oNoticeNextUsers) \ values("+str(iteminfo[0])+",'"+str(iteminfo[1])+"','"+str(alarminfo['text'])+"','"+str(alarmTime)+"','"+str(alarminfo['level'])+"','"+str(alarminfo['type'])+"',"+str(isNotice)+",0,1,"+str(noticeNextTime)+",'"+str(noticeNextUsers.strip(','))+"')" else: notice_sql = "insert into osa_monitor_alarm(oItemid,oMonName,oAlarmText,oAlarmTime,oAlarmLevel,oAlarmType,oIsNotice,oIsRead) \ values("+str(iteminfo[0])+",'"+str(iteminfo[1])+"','"+str(alarminfo['text'])+"','"+str(alarmTime)+"','"+str(alarminfo['level'])+"','"+str(alarminfo['type'])+"',"+str(isNotice)+",0)" log_debug("insert_itemalarm_except()执行sql语句:"+str(notice_sql)) cmdtosql.execsql(notice_sql) except Exception as e: log_error("insert_itemalarm_except():"+str(e))
def _get_monitime(): ''' 获取采集数据入库时间 ''' sql = 'SELECT oMonTime FROM osa_monitor order by id desc limit 1' r = cmdtosql.select(sql) if not r: lasttime = '1979-01-01 00:00:00' else: lasttime = str((r[0])[0]) ltime = time.mktime(time.strptime(lasttime, "%Y-%m-%d %H:%M:%S")) ntime = time.time() if (ntime - ltime) > 600: montime = cmdtosql._get_time(1) #如果当前时间小于数据库的时间 elif (ntime - ltime) <= 0: sys.exit() else: Ltime = time.localtime( ltime + 300 ) montime = time.strftime('%Y-%m-%d %H:%M:%S', Ltime) return montime
def _get_monitime(): ''' 获取采集数据入库时间 ''' sql = 'SELECT oMonTime FROM osa_monitor order by id desc limit 1' r = cmdtosql.select(sql) if not r: lasttime = '1979-01-01 00:00:00' else: lasttime = str((r[0])[0]) ltime = time.mktime(time.strptime(lasttime, "%Y-%m-%d %H:%M:%S")) ntime = time.time() if (ntime - ltime) > 600: montime = cmdtosql._get_time(1) #如果当前时间小于数据库的时间 elif (ntime - ltime) <= 0: sys.exit() else: Ltime = time.localtime(ltime + 300) montime = time.strftime('%Y-%m-%d %H:%M:%S', Ltime) return montime
def batchresult(r): ''' @处理批量操作结果 ''' rdict = eval(r) oCmdType = rdict['batchinfo']['command'] oBatchid = rdict['batchinfo']['id'] oClientip = rdict['batchinfo']['clientip'] oRunTime = _get_time(flag=1) oResult = str(rdict['batchresult']) if rdict['batchinfo']['type'] == 'batch': isql = "INSERT INTO `osa_tasknow_result` (`oCmdType`, `oRunTime`, `oTaskNowid`,`oClientip`, `oResult`) VALUES ('"+oCmdType+"', '"+str(oRunTime)+"', "+str(oBatchid)+", '"+str(oClientip)+"', \""+oResult+"\")" else: isql = "INSERT INTO `osa_taskplan_result` (`oCmdType`, `oRunTime`, `oTaskPlanid`, `oClientip`, `oResult`) VALUES ('"+oCmdType+"', '"+str(oRunTime)+"', "+str(oBatchid)+", '"+str(oClientip)+"', \""+oResult+"\")" try: con = _get_pcon() cur = con.cursor() cur.execute(isql) except Exception as inserror: _exit(con, cur) save_log('ERROR','insert into db error 1,sql is:'+isql+",error info:"+str(inserror)) time.sleep(random.randint(0,10)) try: con = _get_con() cur = con.cursor() cur.execute(isql) except Exception as inserror: save_log('ERROR','insert into db error 2,sql is:'+isql+",error info:"+str(inserror)) finally: _exit(con, cur) sys.exit() finally: _exit(con, cur) sys.exit()
def update_ipinfo_exception(isNoticeNow,isNoticeNext,ipinfo,level): ''' @服务异常报警更新osa_ipinfo 记录 ''' try: if ipinfo[2] == '1':#表示上次正常,错误时间从现在开始 faultTime = cmdtosql._get_time(1) else:#表示上次异常,错误时间从过去开始 faultTime = ipinfo[5] if str(level) == '1': status = '失去响应' elif str(level) == '2': status = '其他异常' if isNoticeNow>0 or isNoticeNext >0: oNum = int(ipinfo[6])+1 notiNum = int(ipinfo[7])+1 sql = "update osa_ipinfo set oIsAlive='0' ,oStatus='"+str(status)+"' ,oFaultTime='"+str(faultTime)+"',oNotifiedNum="+str(oNum)+",oNotiNum="+str(notiNum)+",oIsEmail='1' where id="+str(ipinfo[0]) else: sql = "update osa_ipinfo set oIsAlive='0' ,oStatus='"+str(status)+"' ,oFaultTime='"+str(faultTime)+"' where id="+str(ipinfo[0]) log_debug("update_ipinfo_exception()执行sql语句:"+str(sql)) cmdtosql.update(sql) except Exception as e: log_error("update_ipinfo_exception():"+str(e))
def getoRunCycle(): ''' @获取执行周期,具体执行时间等数据 @oRunNextTime 默认为空 ''' qsql = "SELECT * from `osa_taskplan` WHERE oRunNextTime is null or oRunNextTime = '0000-00-00 00:00:00' or oRunNextTime <= '"+str(cmdtosql._get_time(flag=1))+"'" return cmdtosql.select(qsql)
def chooseoRunCycle(): ''' @根据周期,返回数据列表 ''' rinfo = getoRunCycle() rlist = [] for r in rinfo: if r[7] != None: cmdinfo = '' try: cmdinfo = getoCombinCmd(int(r[0]),r[4]) except Exception as e: save_log('ERROR','getoCombinCmd error:'+str(e)) if cmdinfo: save_log('INFO','TASK CMD:'+str(cmdinfo[0][0])) rlist.append(cmdinfo[0][0]) nexttime = getNextRunTime(r) if nexttime == False or not nexttime: Isql = "INSERT INTO `osa_complantask`(oCmdType,oTaskplanid,oRunCycle,oRunDate,oRunTime) select oCmdType,id,oRunCycle,oRunDate,oRunTime from osa_taskplan" try: con = cmdtosql._get_pcon() cur = con.cursor() cur.execute(Isql) except Exception as Ierror: save_log('ERROR','osa_complantask INSERT ERROR:'+str(Ierror)+',sql is: '+Isql) finally: cmdtosql._exit(con, cur) Dsql = "DELETE from osa_taskplan WHERE id = "+str(r[0]) try: con = cmdtosql._get_pcon() cur = con.cursor() cur.execute(Dsql) except Exception as Ierror: save_log('ERROR','osa_taskplan DELETE ERROR:'+str(Ierror)+',sql is: '+Dsql) finally: cmdtosql._exit(con, cur) else: usql = "UPDATE `osa_taskplan` set oStatus = '运行中',oRunNextTime = '"+nexttime+"',oRunLastTime = '"+str(cmdtosql._get_time(flag=1))+"' WHERE id = "+str(r[0]) cmdtosql.update(usql) else: nexttime = getNextRunTime(r) usql = "UPDATE `osa_taskplan` set oStatus = '运行中',oRunNextTime = '"+nexttime+"',oRunLastTime = '"+str(cmdtosql._get_time(flag=1))+"' WHERE id = "+str(r[0]) cmdtosql.update(usql) return rlist
def GetItemNeedRun(): ''' 检查oNextCheckTime字段,返回需要运行的监控条目 ''' return cmdtosql.select(tablename="osa_alarms",condition="(oNextCheckTime is null or oNextCheckTime <= '"+str(cmdtosql._get_time(flag=1))+"') and oIsAllow=1")
def InsertInto_osa_serverinfo(oMonitorId,oIpOrUrl,oStatus,oNotiNum): ''' 为osa_serverinfo表增加新的条目 ''' sql = "INSERT INTO osa_serverinfo (`oMonitorId`, `oIpOrUrl`, `oStatus`, `oNotiNum`, `oAddTime`) VALUES ("+str(oMonitorId)+",'"+oIpOrUrl+"','"+oStatus+"',"+str(oNotiNum)+",'"+str(_get_time(1))+"')" try: con = _get_pcon() cur = con.cursor() cur.execute(sql) except Exception as e: save_log('ERROR','sql INSERT fail! sql:'+sql+'ERROR:'+str(e)) return return