def _checkStatus(self, agentip): ''' ' 检查当前状态,包括主机是否可达,告警缓存文件是否处理 ''' logger.info('enter function _checkStatus') cmdfile = None try: if agentip: status = EsdkKeyDefines.NAGIOS_UNKNOWN info = EsdkKeyDefines.STATUS_UNKNOWN_STR logger.info('begin handle warnning file agentip[%s]' % (agentip)) hostStatus = EsdkUtils.checkHosts(self._configdata.HostCheckCmd, agentip) if hostStatus[0] == EsdkKeyDefines.NAGIOS_OK: logger.info("host[%s] is okay" % (agentip)) warningfiles = os.listdir(self._configdata.Cachepath) if warningfiles: status, info = self._checkWarningCacheFiles(agentip, warningfiles) logger.info("agentip[%s],the status[%s],the alarm [%s]" % (agentip, status, info)) else: status = EsdkKeyDefines.NAGIOS_OK info = EsdkKeyDefines.STATUS_OK_STR logger.info("agentip[%s],there is no alarm" % (agentip)) ''' ' 根据告警文件打印消息 ''' print info sys.exit(status) except Exception, err: print str(err) logger.error("checkStatusTimer exception err[%s]" % (str(err))) if cmdfile: cmdfile.close()
def __parseTrapMsg(self, varBinds): """ ' 解析trap信息 ' params: ' varBinds: 当前trap的绑定值 """ if varBinds: for oid, val in varBinds: matcher = re.search(EsdkKeyDefines.OID_VALUE_FORMAT, val.prettyPrint().strip()) if matcher: valSecStr = matcher.group(0) resList = re.split("=", valSecStr.replace("\n", " ").strip(), 1) resType = resList[0] resValue = resList[1] # #修复建议 # if EsdkKeyDefines.HWISMREPORTINGALARMRESTOREADVICEOID==oid.prettyPrint(): # if EsdkKeyDefines.STRING_TYPE==resType: # if resValue.startswith('0x'): # self.__alarmRestoreAdvice=EsdkUtils.hex2char(resValue[2:]) # #故障告警标题 # elif EsdkKeyDefines.HWISMREPORTINGALARMFAULTTITLEOID==oid.prettyPrint(): # if EsdkKeyDefines.STRING_TYPE==resType: # self.__alarmFaultTitle=resValue # 告警级别 if EsdkKeyDefines.HWISMREPORTINGALARMFAULTLEVELOID == oid.prettyPrint(): if EsdkKeyDefines.INTEGER_TYPE == resType: self.__alarmFaultLevel = resValue # 告警ID elif EsdkKeyDefines.HWISMREPORTINGALARMLOCATIONALARMID == oid.prettyPrint(): if EsdkKeyDefines.BIG_COUNTER_TYPE == resType: self.__alarmID = "0x" + EsdkUtils.dec2hex(resValue) # 获取告警时间,以服务端接收到的告警时间为准 elif EsdkKeyDefines.HWISMREPORTINGALARMFAULTTIMEOID == oid.prettyPrint(): # 获取当前系统的时间 self.__alarmFaultTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 详细信息 elif EsdkKeyDefines.HWISMREPORTINGALARMADDITIONINFOOID == oid.prettyPrint(): if EsdkKeyDefines.STRING_TYPE == resType: self.__alarmAdditionInfo = resValue # 告警类型 elif EsdkKeyDefines.HWISMREPORTINGALARMFAULTCATEGORYOID == oid.prettyPrint(): if EsdkKeyDefines.INTEGER_TYPE == resType: self.__alarmFaultCategory = resValue
def handleWarningFiles(self, trapdata): """ ' 处理所有告警文件并汇总,告警状态为最高值,告警信息逐行叠加 ' params: ' trapdata: 当前trap数据实例 ' return tuple: 由该类所有告警中的最高级别和所有告警信息叠加组成的元组 ' raise PluginException: 当操作文件异常或其他数据异常时抛出 """ logger.info("begin handle warnning files") statuslst = [] info = EsdkKeyDefines.ALARM_TABLE_HEAD_FORMAT try: warningfiles = os.listdir(self._filecachepath) if warningfiles is None or len(warningfiles) < 1: return (EsdkKeyDefines.STATUS_OK_INT, EsdkKeyDefines.STATUS_OK_STR) file = None for filename in warningfiles: agentip = filename.split(EsdkKeyDefines.CMD_FILE_NAME_SEP)[0] if not re.match(EsdkKeyDefines.IP_ADDRESS_FORMAT, agentip) is None and agentip == trapdata.AgentIp: file = open(self._constructFullFilename(filename), "r") firstRead = True for fileline in file.readlines(): fileline = fileline.strip(EsdkKeyDefines.NEWLINE_CHARACTER) if firstRead: firstRead = False statuslst.append(fileline.split(EsdkKeyDefines.CMD_FILE_CONTENT_SEP)[0]) info = info + fileline.split(EsdkKeyDefines.CMD_FILE_CONTENT_SEP)[1] else: info = info + fileline file.close() info = info + "</table>" if statuslst: statuslst.sort() logger.info("alarm level [%s],alarm info [%s]" % (statuslst[0], info)) return (EsdkUtils.covertStatus(statuslst[0]), info) else: logger.info("has no warnning") return (EsdkKeyDefines.NAGIOS_OK, EsdkKeyDefines.STATUS_OK_STR) except IOError, err: raise PluginException("Read warning file error. Cause: \n" + str(err)) if not file is None: file.close()
def _checkWarningCacheFiles(self, hostip, warningfiles): ''' ' 检查告警缓存文件 ' params: ' hostip: 受管主机IP ' warningfiles: 告警缓存文件 ' return: ' (status, info): 指定主机的状态和信息 ''' cachefile = None statuslst = [] info = EsdkKeyDefines.ALARM_TABLE_HEAD_FORMAT try: for filename in warningfiles: filenameseq = filename.split(EsdkKeyDefines.CMD_FILE_NAME_SEP) if len(filenameseq) < 2: continue agentip = filenameseq[0] if agentip == hostip: cachefile = open(self._configdata.Cachepath + os.path.sep + filename, 'r') firstRead = True for fileline in cachefile.readlines(): fileline = fileline.strip(EsdkKeyDefines.NEWLINE_CHARACTER) if firstRead: firstRead = False statuslst.append(int(fileline.split(EsdkKeyDefines.CMD_FILE_CONTENT_SEP)[0])) info += fileline.split(EsdkKeyDefines.CMD_FILE_CONTENT_SEP)[1] else: info += fileline info += '</table>' if statuslst: statuslst.sort() logger.info('alarm level [%s],alarm info [%s]' % (statuslst[0], info)) return (EsdkUtils.covertStatus(statuslst[0]), info) else: logger.info('has no warnning') return (EsdkKeyDefines.NAGIOS_OK, EsdkKeyDefines.STATUS_OK_STR) except Exception, err: print str(err) if not cachefile is None: cachefile.close()
def main(): cmdList=['checklun','checksys','checkcontroller'] MSG_USAGE = '''\n %prog checklun -d LOCAL_ADDRESS [-p SEND_PORT] [-c READ_COMMUNITY]\ \nor\ \n %prog checksys -d LOCAL_ADDRESS [-p SEND_PORT] [-c READ_COMMUNITY]\ \n %prog uninstall ''' optParser = OptionParser(MSG_USAGE) optParser.add_option("-d", "--local_address", action="store" , dest="local_address" , help="Local IP address (mandatory). " + "example:192.168.1.1") optParser.add_option("-c", "--read_community", action="store" , dest="read_community" , help="read community (mandatory). " + "example:public") optParser.add_option("-p", "--listen_port", action="store" , dest="listen_port" , default='10062' , help="Listening port number. Default value: 10062") optParser.add_option("-l", "--server_list", action="store" , dest="server_list" , help="Server list. " + "Format: 'serip:sername:user:passwd:community:snmpversion'. " + "community:public. " + "snmpversion:[v1|v2c|v3]. " + "\r\nexample: 192.168.1.1:huawei-host:user:passwd:public:v3") optParser.add_option("-a", "--auto_run", action="store_true" , dest="auto_run" , help="Automatically installs the Huawei nagios plug-in.") options, args = optParser.parse_args() if len(args) == 1: if checkCmd(args[0],cmdList): if options.local_address is not None: checkip(options.local_address) address = re.sub(r'[/|\\]*$', '', options.local_address) community=re.sub(r'[/|\\]*$', '', options.read_community) if platform.system() == "Linux": hostStatus=EsdkUtils.checkHosts(EsdkKeyDefines.HOST_CHECK_CMD_KEY,address) if hostStatus[0] != EsdkKeyDefines.STATUS_OK_INT: print 'the host[%s] can not arrived'%address logger.info('the host[%s] can not arrived'%address) sys.exit(EsdkKeyDefines.NAGIOS_UNKNOWN) authData={} authData[EsdkKeyDefines.HOST_CFG_KEY_GETCOMMUNITY]=community authData[EsdkKeyDefines.HOST_CFG_KEY_IP]=address authData[EsdkKeyDefines.HOST_CFG_KEY_SNMPVERSION]='v2c' msg='action[%s],ip[%s],community[%s],version[%s]' if 'checklun'==args[0]: msg=msg%('checklun',address,community,'v2c'); logger.info(msg) lunHandle(authData) elif 'checksys'==args[0]: msg=msg%('checksys',address,community,'v2c'); logger.info(msg) sysHandle(authData) elif 'checkcontroller'==args[0]: msg=msg%('checkcontroller',address,community,'v2c'); logger.info(msg) controllerHandle(authData) else: optParser.print_help() logger.info('the cmd format is not right') else: optParser.print_help()