# Load handlermodules try: logger.debug('Trying to load handlermodules') handlermodules = load_handler_modules( config.get('snmptrapd', 'handlermodules').split(',')) except ModuleLoadError, why: logger.error("Could not load handlermodules %s" % why) sys.exit(1) addresses_text = ", ".join(address_to_string(*addr) for addr in addresses) if opts.daemon: # Daemonize and listen for traps try: logger.debug("Going into daemon mode...") daemon.daemonize(pidfile, stderr=nav.logs.get_logfile_from_logger()) except daemon.DaemonError, 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
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()
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, why: logger.error("Could not load handlermodules %s" %why) sys.exit(1) addresses_text = ", ".join(address_to_string(*addr) for addr in addresses) if opts.daemon: # Daemonize and listen for traps try: logger.debug("Going into daemon mode...") daemon.daemonize(pidfile, stderr=nav.logs.get_logfile_from_logger()) except daemon.DaemonError, 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