def selenium(selenium, base_url): """Fixture to initialize the selenium web driver with a NAV session logged in as the admin user. """ from nav.bootstrap import bootstrap_django bootstrap_django(__file__) from nav.web.auth import create_session_cookie selenium.implicitly_wait(10) wait = WebDriverWait(selenium, 10) cookie = create_session_cookie(USERNAME) # visit a non-existent URL just to set the site context for cookies selenium.get('{}/images/400'.format(base_url)) wait.until( EC.text_to_be_present_in_element((By.TAG_NAME, "h1"), "Not found")) print("Cookies after first fetch: {!r}".format(selenium.get_cookies())) selenium.delete_all_cookies() print("Setting session cookie for {}: {!r}".format(USERNAME, cookie)) selenium.add_cookie(cookie) # Cookie modification is also _non-blocking_ in Selenium, so we need to # wait for the cookie to become present in the browser before we continue! wait.until(_session_cookie_is_present(cookie)) print("Cookies after set, before refresh: {!r}".format( selenium.get_cookies())) selenium.refresh() print("Cookies after refresh: {!r}".format(selenium.get_cookies())) yield selenium print("Cookies after test: {!r}".format(selenium.get_cookies()))
def pytest_configure(config): subprocess.check_call([SCRIPT_CREATE_DB]) os.environ['TARGETURL'] = "http://localhost:8000/" start_gunicorn() from nav.bootstrap import bootstrap_django bootstrap_django('pytest') from django.test.utils import setup_test_environment setup_test_environment()
def pytest_configure(config): subprocess.check_call([SCRIPT_CREATE_DB]) os.environ['TARGETURL'] = "http://localhost:8000/" start_gunicorn() # Bootstrap Django config from nav.bootstrap import bootstrap_django bootstrap_django('pytest') # Install custom reactor for Twisted tests from nav.ipdevpoll.epollreactor2 import install install() # Setup test environment for Django from django.test.utils import setup_test_environment setup_test_environment()
# FIXME missing detailed usage import argparse import logging import logging.handlers import os import os.path import signal import socket import sys import time from psycopg2 import InterfaceError from nav.bootstrap import bootstrap_django bootstrap_django(__file__) import nav.buildconf import nav.config import nav.daemon import nav.logs import nav.buildconf import nav.db # These have to be imported after the envrionment is setup from django.db import DatabaseError, connection from nav.alertengine.base import check_alerts from nav.config import NAV_CONFIG # # PATHS
import logging import argparse import time from datetime import datetime from json import JSONDecoder, JSONDecodeError from typing import Generator, Tuple, List import yaml from pyargus.client import Client from pyargus.models import Incident from nav.bootstrap import bootstrap_django from nav.models.fields import INFINITY bootstrap_django("navargus") from nav.models.manage import Netbox, Interface from nav.models.service import Service from nav.models.event import AlertHistory, STATE_START, STATE_STATELESS, STATE_END from nav.logs import init_stderr_logging from nav.config import open_configfile from nav.buildconf import VERSION as _NAV_VERSION from django.urls import reverse from django.db.models import Q _logger = logging.getLogger("navargus") _client = None _config = None # type: Configuration NOT_WHITESPACE = re.compile(r"[^\s]")
# the Free Software Foundation. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # more details. You should have received a copy of the GNU General Public # License along with NAV. If not, see <http://www.gnu.org/licenses/>. # """Script to simulate up/down events from pping""" from __future__ import print_function import argparse import sys from nav.bootstrap import bootstrap_django bootstrap_django() from nav.models.event import EventQueue as Event, Subsystem, EventType from nav.models.manage import Netbox from django.db import transaction DEFAULT_KWARGS = { 'source': Subsystem.objects.get(pk='pping'), 'target': Subsystem.objects.get(pk='eventEngine'), 'event_type': EventType.objects.get(pk='boxState') } @transaction.atomic def main():
# Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. import sys, os # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. sys.path.insert(0, os.path.join(os.path.abspath('..'), 'python')) from nav import buildconf from nav import bootstrap bootstrap.bootstrap_django('doc') # -- General configuration ----------------------------------------------------- # Add any Sphinx extension module names here, as strings. They can be extensions # coming with Sphinx (named 'sphinx.ext.*') or your custom ones. extensions = ['sphinx.ext.autodoc', 'sphinx.ext.doctest', 'sphinx.ext.todo', 'sphinx.ext.ifconfig'] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix of source filenames. source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8'
def main(): bootstrap_django('snmptrapd') # 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()
def main(): bootstrap_django() args = parse_args() # Set config defaults global defaults defaults = { 'username': NAV_CONFIG['NAV_USER'], 'delay': '30', 'delayfactor': '1.5', 'maxdelay': '3600', 'retrylimit': '5', 'retrylimitaction': 'ignore', 'exit_on_permanent_error': 'yes', 'autocancel': '0', 'mailwarnlevel': 'ERROR', 'mailserver': 'localhost', 'mailaddr': NAV_CONFIG['ADMIN_MAIL'], 'fromaddr': NAV_CONFIG['DEFAULT_FROM_EMAIL'], } # Read config file global config config = getconfig(configfile, defaults) # Set variables global delay, failed failed = 0 delay = int(config['main']['delay']) maxdelay = int(config['main']['maxdelay']) delayfactor = float(config['main']['delayfactor']) retrylimit = int(config['main']['retrylimit']) retrylimitaction = config['main']['retrylimitaction'].strip() retryvars = { 'maxdelay': maxdelay, 'delayfactor': delayfactor, 'retrylimit': retrylimit, 'retrylimitaction': retrylimitaction } username = config['main']['username'] autocancel = config['main']['autocancel'] mailwarnlevel = logging.getLevelName(config['main']['mailwarnlevel']) mailserver = config['main']['mailserver'] mailaddr = config['main']['mailaddr'] fromaddr = config['main']['fromaddr'] # Drop privileges if running as root if os.geteuid() == 0: try: nav.daemon.switchuser(username) except nav.daemon.DaemonError as error: print(error, file=sys.stderr) sys.exit( "Run as root or %s. Try `%s --help' for more information." % (username, sys.argv[0])) # Initialize logging nav.logs.init_stderr_logging() if not loginitsmtp(mailwarnlevel, mailaddr, fromaddr, mailserver): sys.exit('Failed to init SMTP logging.') # First log message _logger.info('Starting smsd.') # Set custom loop delay if args.delay: setdelay(args.delay) # Ignore unsent messages if args.cancel: queue = nav.smsd.navdbqueue.NAVDBQueue() ignored_count = queue.cancel() _logger.info("All %d unsent messages ignored.", ignored_count) sys.exit(0) if args.delayfactor: retryvars['delayfactor'] = args.delayfactor if args.maxdelay: retryvars['maxdelay'] = args.maxdelay if args.limit: retryvars['retrylimit'] = args.limit if args.action: retryvars['retrylimitaction'] = args.action # Let the dispatcherhandler take care of our dispatchers try: dh = nav.smsd.dispatcher.DispatcherHandler(config) except PermanentDispatcherError as error: _logger.critical("Dispatcher configuration failed. Exiting. (%s)", error) sys.exit(1) # Send test message (in other words: test the dispatcher) if args.test or args.TEST: text = args.message or "This is a test message from NAV smsd." if args.test: msg = [(0, text, 0)] try: (sms, sent, ignored, smsid) = dh.sendsms(args.test, msg) except DispatcherError as error: _logger.critical("Sending failed. Exiting. (%s)", error) sys.exit(1) _logger.info("SMS sent. Dispatcher returned reference %d.", smsid) elif args.TEST and args.uid: queue = nav.smsd.navdbqueue.NAVDBQueue() rowsinserted = queue.inserttestmsgs(args.uid, args.TEST, text) if rowsinserted: _logger.info("SMS put in queue. %d row(s) inserted.", rowsinserted) else: _logger.info("SMS not put in queue.") sys.exit(0) # Check if already running try: nav.daemon.justme(pidfile) except nav.daemon.DaemonError as error: _logger.error(error) sys.exit(1) # Daemonize if not args.foreground: try: nav.daemon.daemonize(pidfile, stderr=open(logfile, "a")) except nav.daemon.DaemonError as error: _logger.error(error) sys.exit(1) _logger.info('smsd now running in daemon mode') # Reopen log files on SIGHUP _logger.debug( 'Adding signal handler for reopening log files on SIGHUP.') signal.signal(signal.SIGHUP, signalhandler) else: nav.daemon.writepidfile(pidfile) # Exit on SIGTERM/SIGINT signal.signal(signal.SIGTERM, signalhandler) signal.signal(signal.SIGINT, signalhandler) # Initialize queue # NOTE: If we're initalizing a queue with a DB connection before # daemonizing we've experienced that the daemon dies silently upon trying # to use the DB connection after becoming a daemon queue = nav.smsd.navdbqueue.NAVDBQueue() # Automatically cancel unsent messages older than a given interval if autocancel != '0': ignored_count = queue.cancel(autocancel) _logger.info("%d unsent messages older than '%s' autocanceled.", ignored_count, autocancel) # Loop forever while True: _logger.debug("Starting loop.") # Queue: Get users with unsent messages users = queue.getusers('N') _logger.info("Found %d user(s) with unsent messages.", len(users)) # Loop over cell numbers for user in users: # Queue: Get unsent messages for a user ordered by severity desc msgs = queue.getusermsgs(user, 'N') _logger.info("Found %d unsent message(s) for %s.", len(msgs), user) # Dispatcher: Format and send SMS try: (sms, sent, ignored, smsid) = dh.sendsms(user, msgs) except PermanentDispatcherError as error: _logger.critical("Sending failed permanently. Exiting. (%s)", error) sys.exit(1) except DispatcherError as error: try: # Dispatching failed. Backing off. backoff(delay, error, retryvars) break # End this run except: _logger.exception("") raise except Exception as error: _logger.exception("Unknown exception: %s", error) _logger.info("SMS sent to %s.", user) if failed: resetdelay() failed = 0 _logger.debug("Resetting delay and number of failed runs.") for msgid in sent: queue.setsentstatus(msgid, 'Y', smsid) for msgid in ignored: queue.setsentstatus(msgid, 'I', smsid) _logger.info("%d messages were sent and %d ignored.", len(sent), len(ignored)) # Sleep a bit before the next run _logger.debug("Sleeping for %d seconds.", delay) time.sleep(delay)
# more details. You should have received a copy of the GNU General Public # License along with NAV. If not, see <http://www.gnu.org/licenses/>. # """This is not really a Sphinx extension, but a tool to autogenerate the reference documentation for NAV's event- and alert type hierarchy. It should only be necessary to re-run this in the event of changes to the supported event/alert types, or in the templates used to generate this doc. """ import os from jinja2 import FileSystemLoader, Environment # Jinja is a sub-dependency of Sphinx from nav.bootstrap import bootstrap_django bootstrap_django(__name__) from nav.models.event import EventType, AlertType def main(): env = Environment(loader=FileSystemLoader(os.path.dirname(__file__))) template = env.get_template('alerttypes.rst.j2') types = [(eventtype, AlertType.objects.filter(event_type=eventtype)) for eventtype in EventType.objects.all()] print(template.render(types=types)) if __name__ == '__main__': main()