def main(): # Verify that subsystem exists, if not insert it into database verifySubsystem() # Initialize and read startupconfig global config config = ConfigParser.ConfigParser() config.read(configfile) # Create parser and define options global opts opts, addresses = parse_args() # When binding to a port < 1024 we need to be root minport = min(port for addr, port in addresses) if minport < 1024: if os.geteuid() != 0: print("Must be root to bind to ports < 1024, exiting") sys.exit(-1) # Check if already running pidfile = nav.buildconf.localstatedir + "/run/snmptrapd.pid" try: daemon.justme(pidfile) except daemon.DaemonError, why: print why sys.exit(-1)
def verify_singleton(): """Verifies that we are the single running navtopology process. If a navtopology process is already running, we exit this process. """ try: daemon.justme(PIDFILE_PATH) except daemon.AlreadyRunningError, error: print >> sys.stderr, "navtopology is already running (%d)" % error.pid sys.exit(1)
def verify_singleton(quiet=False): """Verify that we are the single running logengine process. If a logengine process is already running, we exit this process. """ # Create a pidfile and delete it automagically when the process exits. # Although we're not a daemon, we do want to prevent multiple simultaineous # logengine processes. pidfile = os.path.join(localstatedir, 'run', 'logengine.pid') try: daemon.justme(pidfile) except daemon.AlreadyRunningError, e: if quiet: sys.exit(0) else: print >> sys.stderr, "logengine is already running (%d)" % e.pid sys.exit(1)
def verify_singleton(quiet=False): """Verify that we are the single running logengine process. If a logengine process is already running, we exit this process. """ # Create a pidfile and delete it automagically when the process exits. # Although we're not a daemon, we do want to prevent multiple simultaineous # logengine processes. pidfile = os.path.join(localstatedir, 'run', 'logengine.pid') try: daemon.justme(pidfile) except daemon.AlreadyRunningError as err: if quiet: sys.exit(0) else: print("logengine is already running (%d)" % err.pid, file=sys.stderr) sys.exit(1) daemon.writepidfile(pidfile) atexit.register(daemon.daemonexit, pidfile)
def main(): # Verify that subsystem exists, if not insert it into database verify_subsystem() # Initialize and read startupconfig global config config = ConfigParser.ConfigParser() config.read(configfile) # Create parser and define options opts = parse_args() # When binding to a port < 1024 we need to be root minport = min(port for addr, port in opts.address) if minport < 1024: if os.geteuid() != 0: print("Must be root to bind to ports < 1024, exiting") sys.exit(-1) # Check if already running try: daemon.justme(pidfile) except daemon.DaemonError as why: print(why) sys.exit(-1) # Create SNMP agent object server = agent.TrapListener(*opts.address) server.open() # We have bound to a port and can safely drop privileges runninguser = config.get('snmptrapd', 'user') try: if os.geteuid() == 0: daemon.switchuser(runninguser) except daemon.DaemonError as why: print(why) server.close() sys.exit(-1) global handlermodules nav.logs.init_generic_logging(stderr=True, read_config=True) logger.debug("using %r as SNMP backend", agent.BACKEND) # Load handlermodules try: logger.debug('Trying to load handlermodules') handlermodules = load_handler_modules( config.get('snmptrapd', 'handlermodules').split(',')) except ModuleLoadError as why: logger.error("Could not load handlermodules %s" % why) sys.exit(1) addresses_text = ", ".join( address_to_string(*addr) for addr in opts.address) if opts.daemon: # Daemonize and listen for traps try: logger.debug("Going into daemon mode...") logfile = open(logfile_path, 'a') daemon.daemonize(pidfile, stderr=logfile, stdout=logfile) except daemon.DaemonError as why: logger.error("Could not daemonize: %s", why) server.close() sys.exit(1) # Daemonized; reopen log files nav.logs.reopen_log_files() logger.debug('Daemonization complete; reopened log files.') # Reopen lost db connection # This is a workaround for a double free bug in psycopg 2.0.7 # which is why we don't need to keep the return value getConnection('default') # Reopen log files on SIGHUP logger.debug( 'Adding signal handler for reopening log files on SIGHUP.') signal.signal(signal.SIGHUP, signal_handler) # Exit on SIGTERM signal.signal(signal.SIGTERM, signal_handler) logger.info("Snmptrapd started, listening on %s", addresses_text) try: server.listen(opts.community, trap_handler) except SystemExit: raise except Exception as why: logger.critical("Fatal exception ocurred", exc_info=True) else: # Start listening and exit cleanly if interrupted. try: logger.info("Listening on %s", addresses_text) server.listen(opts.community, trap_handler) except KeyboardInterrupt as why: logger.error("Received keyboardinterrupt, exiting.") server.close()