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 _get_snmpinfo(): ''' @从数据库osa_snmp表获取snmp配置信息。 ''' try: sql = "select oSnmpPort,oSnmpKey from osa_snmp where id='1'" con = cmdtosql._get_pcon() cur = con.cursor() cur.execute(sql) snmpinfo=cur.fetchone() except Exception as e: log_error("collect.get_snmpinfo(ip):"+str(e)) cmdtosql._exit(con, cur) return snmpinfo
def get_ipinfo(ip): ''' @通过ip从osa_ipinfo中获取ipinfo信息 ''' try: sql = "select * from osa_ipinfo where oIp='"+str(ip)+"'" con = cmdtosql._get_pcon() cur = con.cursor() cur.execute(sql) ipinfo=cur.fetchone() except Exception as e: log_error("collect.get_ipinfo(ip):"+str(e)) cmdtosql._exit(con, cur) return ipinfo
def redis_get_lastdata(itemid): ''' @获取上次插入的结果值 ''' try: sql = " select oMonResult from osa_monitor_record where oItemid ="+str(itemid)+" order by id desc limit 1" con = cmdtosql._get_pcon() cur = con.cursor() cur.execute(sql) itemdata=cur.fetchone() cmdtosql._exit(con, cur) if itemdata == None or itemdata[0]=='' or itemdata[0]=='null': return None else: return eval(itemdata[0]) except Exception as e: log_error("redis_get_lastdata:"+str(e))
def mongodb_get_lastdata(itemid): """ @获取上一次获取的数据中的visitNum字段值 """ try: sql = " select oMonResult from osa_monitor_record where oItemid =" + str(itemid) + " order by id desc limit 1" con = cmdtosql._get_pcon() cur = con.cursor() cur.execute(sql) itemdata = cur.fetchone() cmdtosql._exit(con, cur) if itemdata is None or itemdata[0] == "" or itemdata[0] == "null": return None else: return eval(itemdata[0]) except Exception as e: log_error("mongodb_get_lastdata:" + str(e))
def nginx_get_lastvisit(itemid): ''' @获取上一次获取的数据中的visitNum字段值 ''' try: sql = " select oMonResult from osa_monitor_record where oItemid ="+str(itemid)+" order by id desc limit 1" con = cmdtosql._get_pcon() cur = con.cursor() cur.execute(sql) itemdata=cur.fetchone() cmdtosql._exit(con, cur) if itemdata== None or itemdata[0]=='' or itemdata[0]=='null': return None else: datajson = eval(itemdata[0]) return int(datajson['visitNum']) except Exception as e: log_error('nginx_get_lastvisit:'+str(e))
def get_iplist(): ''' @获取默认采集数据的服务器列表,排除暂停的采集的服务器 @oIsStop = 0 :表示服务器不暂停 ''' try: sql = "select oIp from osa_ipinfo where oIsStop = 0" iplist=[] con = cmdtosql._get_pcon() cur = con.cursor() cur.execute(sql) list = cur.fetchall() except Exception as e: log_error("collect.get_iplist():"+str(e)) for ip in list: iplist.append(ip[0]) cmdtosql._exit(con, cur) return iplist
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 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 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()