def _run(self): try: verInfo = kkbLib.getPythonVer() if verInfo not in kkbConfig.pythonVer: verStr = '/'.join(kkbConfig.pythonVer) kkbLib.info('当前pyhon版本还没兼容,程序执行中止,目前支持python版本:' + verStr) sys.exit(1) print('Starting master process') kkbLib.info('Starting master process') masterProcess = MasterProcess() monitor = AgentProcessMonitor(masterProcess) monitor.setName('AgentProcessMonitor') monitor.setDaemon(True) monitor.start() print('Started master process') kkbLib.info('Started master process') while True: try: time.sleep(5) masterProcess.ping() except Exception: kkbLib.error(traceback.format_exc()) except KeyboardInterrupt: masterProcess.stop() except Exception: kkbLib.error(traceback.format_exc())
def stop(self): # Get the pid from the pidfile try: pid = kkbLib.readPid('master') except IOError: pid = None if not pid: message = "The process does not exist, the operation aborts" kkbLib.printout(message) kkbLib.info(message) return # not an error in a restart # Try killing the daemon process try: while 1: os.kill(pid, SIGTERM) time.sleep(0.1) except OSError: kkbLib.rmPid('agent') kkbLib.rmPid('master') kkbLib.printout('Successful process closes') kkbLib.info('Successful process closes')
def run(self): self.objList = {} try: if 'agent' in self.plugConf and 'agentTime' in self.plugConf['agent']: self.post_interval = self.plugConf['agent']['agentTime'] if 'agent' in self.plugConf and 'pluginTime' in self.plugConf['agent']: self.pluginTime = self.plugConf['agent']['pluginTime'] if 'agent' in self.plugConf and 'post_time' in self.plugConf['agent']: self.post_time = self.plugConf['agent']['post_time'] else: kkbLib.error('none set post time!') if 'error' in self.plugConf: kkbLib.error(self.plugConf['error']) if 'plugin' in self.plugConf: for taskId in self.plugConf['plugin']: plug = self.plugConf['plugin'][taskId + ''] if plug['status'] == '1': # self.startOne(plug) kkbLib.info(str(plug)) else: kkbLib.info('none plugin info!!') cur_time = 0 while self.running: if ((cur_time + 10) < self.post_interval): time.sleep(self.post_interval - cur_time) else: kkbLib.error('post data out time!!') time.sleep(180) cur_time = time.time() # command = self.postData() # self.doneCommand(command) kkbLib.info('do commands...') cur_time = time.time() - cur_time #cur_time = int(cur_time) except Exception: kkbLib.error(traceback.format_exc())
def start(self): """ Start the daemon """ # Check for a pidfile to see if the daemon already runs try: pid = kkbLib.readPid('master') except IOError: pid = 0 if pid > 0: isRun = kkbLib.checkPidLinux(pid) if isRun: kkbLib.printout('Program has been started, the process ID:' + str(pid)) sys.exit(1) # Start the daemon kkbLib.info('starting daemon...') self._daemonize() kkbLib.info('started daemon.') kkbLib.info('starting run...') self._run()
setproctitle(pname) agentPid = os.getpid() try: pid = kkbLib.readPid('agent') except IOError: pid = 0 if pid > 0: isRun = kkbLib.checkPidLinux(pid) if isRun: kkbLib.printout('Program has been started, the process PID:' + str(pid)) sys.exit(1) kkbLib.writePid(agentPid, 'agent') kkbLib.info('Starting agent process') kkbLib.printout('Starting agent process') parentPid = None try: if len(sys.argv) > 1: parentPid = sys.argv[1] except Exception: kkbLib.error(traceback.format_exc()) try: agentProcess = AgentProcess() agentProcess.setName('AgentProcess') agentProcess.start() monitor = MasterProcessMonitor(agentProcess) monitor.setName('MasterProcessMonitor')