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 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()