Exemplo n.º 1
0
    def __init__(self, home):
        """
        Load configuration object
        """
        AutoConf.autoconf(home)
        self.configuration = AutoConf.get_warden_conf()

        # Setup logger
        # this is the stdout log level
        loglevel = getattr(logging, self.configuration.get('warden', {}).get('loglevel', 'INFO'))
        log.setLevel(loglevel)

        self.startuptime = None
        self.shutdowntime = None
        self.smtp_forwarder_enabled = str(self.configuration.get('smtp_forwarder', {}).get('enabled', '')).lower() in ('true', 't', '1', 'yes', 'y')

        log.info('Initialising Warden..')
        try:
            # initialise Carbon, daemon services are setup here, but the event reactor is not yet run
            self.carbon = CarbonManager()

            # initialise Gentry, this will also perform database manipulation for Sentry
            self.gentry = GentryManager()

            # initialise Diamond, not much is required here
            self.diamond = DiamondManager()


            if self.smtp_forwarder_enabled:
                self.smtpforward = SMTPForwarderManager(dict(self.configuration.get('smtp_forwarder', {})))

        except Exception:
            log.exception("An error occured during initialisation.")
            sys.exit(1)
Exemplo n.º 2
0
    def start(self):
        try:
            self._startup()
            while True:
                time.sleep(5)
                if not self._is_active():
                    log.error("Something caused one of the services to stop!")
                    break
                # need some way to pickup errors at runtime. should check after each sleep whether any of the
                # services have picked up an error

        except KeyboardInterrupt:
            log.info("Keyboard interrupt received.")
            self._shutdown()
        except StartupException:
            log.exception("An error occured during startup.")
            self._shutdown()
        except Exception:
            log.exception("An error occured while running.")
            self._shutdown()
Exemplo n.º 3
0
    def start(self):
        try:
            self._startup()
            while True:
                time.sleep(5)
                if not self._is_active():
                    log.error("Something caused one of the services to stop!")
                    break
                # need some way to pickup errors at runtime. should check after each sleep whether any of the
                # services have picked up an error

        except KeyboardInterrupt:
            log.info("Keyboard interrupt received.")
            self._shutdown()
        except StartupException:
            log.exception("An error occured during startup.")
            self._shutdown()
        except Exception:
            log.exception("An error occured while running.")
            self._shutdown()
Exemplo n.º 4
0
    def __init__(self, home):
        """
        Load configuration object
        """
        AutoConf.autoconf(home)
        self.configuration = AutoConf.get_warden_conf()

        # Setup logger
        # this is the stdout log level
        loglevel = getattr(
            logging,
            self.configuration.get('warden', {}).get('loglevel', 'INFO'))
        log.setLevel(loglevel)

        self.startuptime = None
        self.shutdowntime = None
        self.smtp_forwarder_enabled = str(
            self.configuration.get('smtp_forwarder',
                                   {}).get('enabled',
                                           '')).lower() in ('true', 't', '1',
                                                            'yes', 'y')

        log.info('Initialising Warden..')
        try:
            # initialise Carbon, daemon services are setup here, but the event reactor is not yet run
            self.carbon = CarbonManager()

            # initialise Gentry, this will also perform database manipulation for Sentry
            self.gentry = GentryManager()

            # initialise Diamond, not much is required here
            self.diamond = DiamondManager()

            if self.smtp_forwarder_enabled:
                self.smtpforward = SMTPForwarderManager(
                    dict(self.configuration.get('smtp_forwarder', {})))

        except Exception:
            log.exception("An error occured during initialisation.")
            sys.exit(1)
    def start(self):

        while True:
            for generator_cls in BaseMailGenerator.generator_registry:
                generator = generator_cls()
                mail = generator.create_mail()

                if mail:
                    conn = SMTP()
                    try:
                        log.debug("Connecting..")
                        conn.connect(settings.EMAIL_HOST)
                        conn.set_debuglevel(False)

                        if settings.EMAIL_USE_TLS:
                            log.debug("Starting TLS..")
                            conn.starttls()

                        log.debug("Logging in..")
                        conn.login(settings.EMAIL_USERNAME, settings.EMAIL_PASSWORD)
                        log.debug("Sending mail..")
                        conn.sendmail(mail['From'], mail['To'], mail.as_string())
                        log.debug("Sent.")
                    except smtplib.SMTPRecipientsRefused:
                        log.error("Receipient confused.")
                    except smtplib.SMTPHeloError:
                        log.error("Server didn't respond properly to HELO.")
                    except smtplib.SMTPSenderRefused:
                        log.error("Sender refused.")
                    except smtplib.SMTPDataError:
                        log.error("Unexpected error code.")
                    except Exception as exc:
                        log.exception(exc)
                    finally:
                        if hasattr(conn, 'sock') and conn.sock:
                            conn.quit()

            time.sleep(self.SLEEP_INTERVAL)
Exemplo n.º 6
0
    def _shutdown(self):
        """
        Shutdown in order, some threading may be wrong here, make sure of inidividual .join()
        """
        self.shutdowntime = datetime.datetime.now()

        elapsed = self.shutdowntime - self.startuptime
        log.info('Warden was active for %s' % str(elapsed))

        log.info('Shutting down Warden..')

        if self.configuration.getboolean('smtp_forwarder', 'enabled'):
            try:
                self.smtpforward.stop()
                log.info('4. Graphite SMTP forwarder stopped')
            except Exception:
                log.exception(
                    'An error occured while shutting down Graphite SMTP forwarder'
                )

        try:
            self.gentry.stop()
            log.info('3. Gentry Stopped.')
        except Exception:
            log.exception("An error occured while shutting down Gentry")

        try:
            self.diamond.stop()
            log.info('2. Diamond Stopped.')
        except Exception:
            log.exception("An error occured while shutting down Diamond")

        try:
            self.carbon.stop()
            log.info('1. Carbon Stopped.')
        except Exception:
            log.exception("An error occured while shutting down Carbon")

        log.info('Shut down Warden.')
Exemplo n.º 7
0
    def _shutdown(self):
        """
        Shutdown in order, some threading may be wrong here, make sure of inidividual .join()
        """
        self.shutdowntime = datetime.datetime.now()

        elapsed = self.shutdowntime - self.startuptime
        log.info('Warden was active for %s' % str(elapsed))

        log.info('Shutting down Warden..')

        if self.smtp_forwarder_enabled:
            try:
                self.smtpforward.stop()
                log.info('4. Graphite SMTP forwarder stopped')
            except Exception:
                log.exception('An error occured while shutting down Graphite SMTP forwarder')

        try:
            self.gentry.stop()
            log.info('3. Gentry Stopped.')
        except Exception:
            log.exception("An error occured while shutting down Gentry")

        try:
            self.diamond.stop()
            log.info('2. Diamond Stopped.')
        except Exception:
            log.exception("An error occured while shutting down Diamond")

        try:
            self.carbon.stop()
            log.info('1. Carbon Stopped.')
        except Exception:
            log.exception("An error occured while shutting down Carbon")

        log.info('Shut down Warden.')
Exemplo n.º 8
0
    def __init__(
        self,
        warden_configuration_file,
        new_graphite_root=None,  # does the graphite root variable need to be changed
        carbon_config_path=None,  # where are the carbon config files
        diamond_config_path=None,  # where is the diamond config file
        gentry_settings_path=None,  # the name of the gentry settings module
        start_stmp_forwarder=True,
        smtp_forwarder_config_path=None,
    ):
        """
        Load configuration object
        """

        # Otherwise there may be a config argument

        if warden_configuration_file is None:
            log.critical(
                'No Warden configuration file supplied! Please use the "warden_configuration_file" parameter.'
            )
            sys.exit()

        warden_configuration_file = os.path.abspath(
            os.path.expanduser(warden_configuration_file))
        try:
            with open(warden_configuration_file) as f:
                pass
        except IOError:
            log.error(
                'The warden config file specified ("%s") does not exist!' %
                warden_configuration_file)
            raise

        self.configuration = ConfigParser.RawConfigParser()
        self.configuration.read(warden_configuration_file)

        # Setup logger
        # this is the stdout log level
        loglevel = getattr(logging,
                           self.configuration.get('warden', 'loglevel'))
        log.setLevel(loglevel)

        self.startuptime = None
        self.shutdowntime = None

        # pull new config values into configuration object

        if new_graphite_root is not None:
            self.configuration.set('carbon', 'graphite_root',
                                   str(new_graphite_root))

        if carbon_config_path is not None:
            self.configuration.set('carbon', 'configuration',
                                   str(carbon_config_path))

        if diamond_config_path is not None:
            self.configuration.set('diamond', 'configuration',
                                   str(diamond_config_path))

        if gentry_settings_path is not None:
            self.configuration.set('gentry', 'gentry_settings_py_path',
                                   str(gentry_settings_path))

        if start_stmp_forwarder is False:
            self.configuration.set('smtp_forwarder', 'enabled',
                                   str(start_stmp_forwarder))

        if smtp_forwarder_config_path is not None:
            self.configuration.set('smtp_forwarder', 'configuration',
                                   str(smtp_forwarder_config_path))

        log.info('Initialising Warden..')
        try:
            # initialise Carbon, daemon services are setup here, but the event reactor is not yet run
            self.carbon = CarbonManager(
                self.configuration.get('carbon', 'graphite_root'),
                self.configuration.get('carbon', 'configuration'))

            # initialise Gentry, this will also perform database manipulation for Sentry
            self.gentry = GentryManager(
                self.configuration.get('gentry', 'gentry_settings_py_path'))

            # initialise Diamond, not much is required here
            self.diamond = DiamondManager(
                self.configuration.get('diamond', 'diamond_root'),
                self.configuration.get('diamond', 'configuration'),
                getattr(logging, self.configuration.get('diamond',
                                                        'loglevel')))

            if self.configuration.getboolean('smtp_forwarder', 'enabled'):
                self.smtpforward = SMTPForwarderManager(
                    dict(self.configuration.items('smtp_forwarder')))

        except Exception:
            log.exception("An error occured during initialisation.")
            sys.exit(1)
Exemplo n.º 9
0
    def __init__(self,
                 warden_configuration_file,
                 new_graphite_root=None,            # does the graphite root variable need to be changed
                 carbon_config_path=None,           # where are the carbon config files
                 diamond_config_path=None,          # where is the diamond config file
                 gentry_settings_path=None,         # the name of the gentry settings module
                 start_stmp_forwarder=True,
                 smtp_forwarder_config_path=None,
    ):
        """
        Load configuration object
        """

        # Otherwise there may be a config argument
       
        if warden_configuration_file is None:
            log.critical('No Warden configuration file supplied! Please use the "warden_configuration_file" parameter.')
            sys.exit()

        warden_configuration_file = os.path.abspath(os.path.expanduser(warden_configuration_file))
        try:
            with open(warden_configuration_file) as f: pass
        except IOError:
            log.error('The warden config file specified ("%s") does not exist!' % warden_configuration_file)
            raise

        self.configuration = ConfigParser.RawConfigParser()
        self.configuration.read(warden_configuration_file)

        # Setup logger
        # this is the stdout log level
        loglevel = getattr(logging, self.configuration.get('warden','loglevel'))
        log.setLevel(loglevel)

        self.startuptime = None
        self.shutdowntime = None

        # pull new config values into configuration object

        if new_graphite_root is not None:
            self.configuration.set('carbon', 'graphite_root', str(new_graphite_root))

        if carbon_config_path is not None:
            self.configuration.set('carbon', 'configuration', str(carbon_config_path))

        if diamond_config_path is not None:
            self.configuration.set('diamond', 'configuration', str(diamond_config_path))

        if gentry_settings_path is not None:
            self.configuration.set('gentry', 'gentry_settings_py_path', str(gentry_settings_path))

        if start_stmp_forwarder is False:
            self.configuration.set('smtp_forwarder', 'enabled', str(start_stmp_forwarder))

        if smtp_forwarder_config_path is not None:
            self.configuration.set('smtp_forwarder', 'configuration', str(smtp_forwarder_config_path))

        log.info('Initialising Warden..')
        try:
            # initialise Carbon, daemon services are setup here, but the event reactor is not yet run
            self.carbon = CarbonManager(
                self.configuration.get('carbon', 'graphite_root'),
                self.configuration.get('carbon', 'configuration'))

            # initialise Gentry, this will also perform database manipulation for Sentry
            self.gentry = GentryManager(
                self.configuration.get('gentry', 'gentry_settings_py_path'))

            # initialise Diamond, not much is required here
            self.diamond = DiamondManager(
                self.configuration.get('diamond', 'diamond_root'),
                self.configuration.get('diamond', 'configuration'),
                getattr(logging, self.configuration.get('diamond','loglevel')))

            if self.configuration.getboolean('smtp_forwarder', 'enabled'):
                self.smtpforward = SMTPForwarderManager(dict(self.configuration.items('smtp_forwarder')))

        except Exception:
            log.exception("An error occured during initialisation.")
            sys.exit(1)
Exemplo n.º 10
0
def setup(
        carbon_conf,
        diamond_conf,
        gentry_settings,
        super_user,
        project_name
):
    """
    Warden uses values from its default settings file UNLESS explicitely defined
    here in the constructor.
    """
    # GENTRY

    sentry_key = base64.b64encode(os.urandom(40))

    # write key into settings file
    try:
        new_lines = []
        with open(gentry_settings) as f:
            old_lines = f.readlines()
            for line in old_lines:
                if line.startswith('SENTRY_KEY'):
                    nline = 'SENTRY_KEY=\'' + str(sentry_key) + '\'\n'
                    log.info( 'Rewriting "%s" -> "%s"' % (line.strip(), nline.strip()))
                else:
                    nline = line
                new_lines.append(nline)
        if len(new_lines) > 0:
            log.info('Writing new Sentry_key into settings module "%s"' % gentry_settings)
            with open(gentry_settings, 'wb') as f:
                f.writelines(new_lines)
                f.flush()
                f.close()
    except IOError:
        log.exception('Could not write gentry_settings module: "%s"' % gentry_settings)

    if gentry_settings is None:
        os.environ['DJANGO_SETTINGS_MODULE'] = 'gentry.settings'
    else:
        n = 'j5_warden_gentry_settings'
        os.environ['DJANGO_SETTINGS_MODULE'] = n
        if not sys.modules.has_key(n):
            imp.load_source(n, os.path.abspath(os.path.expanduser(gentry_settings)))

    log.info ('$DJANGO_SETTINGS_MODULE = %s' % os.environ['DJANGO_SETTINGS_MODULE'])
    from django.conf import settings as gsetts

    database = gsetts.DATABASES['default']['NAME']
    if file_exists(database):
        os.remove(database)
    management.execute_from_command_line(['manage.py', 'syncdb','--noinput'])
    management.execute_from_command_line(['manage.py', 'migrate', '--noinput'])

    # add a super user
    if super_user is not None:
        username = super_user[0]
        password = super_user[1]
        email = super_user[2]
    else:
        username, password, email = '', '', ''
        log.info('Creating new Superuser for Sentry:')
        while True:
            username = raw_input('Enter username: '******' ' in username: continue
            password = raw_input('Enter password: '******' ' in password: continue
            email = raw_input('Enter email: ').strip()
            if len(email) == 0 or ' ' in email or '@' not in email: continue
            break

    from sentry.models import User
    try:
        auser = User.objects.using('default').get(username=username)
    except User.DoesNotExist:
        auser = User.objects.db_manager('default').create_superuser(username, email, password)
        log.info('Added Sentry superuser "%s" with password like "%s%s"' % (username, password[:3], '*'*(len(password)-3)))
    else:
        log.error('Username "%s" is already taken.' % username)

    if project_name is None:
        yesno = raw_input('Would you like to create a new project for Sentry? (yes/no): ' )
        if yesno == 'yes' or yesno == 'y':
            while True:
                project_name = raw_input('Enter Project Name: ').strip()
                if len(project_name) == 0: continue
                break

    if project_name is not None:

        project_slug = project_name.lower().replace(' ','_')
        try:
            # add a project
            from sentry.models import Project, Team
            team = Team.objects.create(name=project_name + ' Team', slug=project_slug + '_team', owner=auser)
            project = Project.objects.create(name=project_name, slug=project_slug, owner=auser, team=team)
            key = project.key_set.filter(user=auser)[0]
            dsn = "http://%s:%s@localhost:%s/%s" % (key.public_key, key.secret_key, gsetts.SENTRY_WEB_PORT, key.project_id)
            log.info('Added "%s" project to Sentry with dsn: %s' % (project_name, dsn))

        except Exception:
            log.error('Failed to create project.')
Exemplo n.º 11
0
def setup(carbon_conf, diamond_conf, gentry_settings, super_user,
          project_name):
    """
    Warden uses values from its default settings file UNLESS explicitely defined
    here in the constructor.
    """
    # GENTRY

    sentry_key = base64.b64encode(os.urandom(40))

    # write key into settings file
    try:
        new_lines = []
        with open(gentry_settings) as f:
            old_lines = f.readlines()
            for line in old_lines:
                if line.startswith('SENTRY_KEY'):
                    nline = 'SENTRY_KEY=\'' + str(sentry_key) + '\'\n'
                    log.info('Rewriting "%s" -> "%s"' %
                             (line.strip(), nline.strip()))
                else:
                    nline = line
                new_lines.append(nline)
        if len(new_lines) > 0:
            log.info('Writing new Sentry_key into settings module "%s"' %
                     gentry_settings)
            with open(gentry_settings, 'wb') as f:
                f.writelines(new_lines)
                f.flush()
                f.close()
    except IOError:
        log.exception('Could not write gentry_settings module: "%s"' %
                      gentry_settings)

    if gentry_settings is None:
        os.environ['DJANGO_SETTINGS_MODULE'] = 'gentry.settings'
    else:
        n = 'j5_warden_gentry_settings'
        os.environ['DJANGO_SETTINGS_MODULE'] = n
        if not sys.modules.has_key(n):
            imp.load_source(
                n, os.path.abspath(os.path.expanduser(gentry_settings)))

    log.info('$DJANGO_SETTINGS_MODULE = %s' %
             os.environ['DJANGO_SETTINGS_MODULE'])
    from django.conf import settings as gsetts

    database = gsetts.DATABASES['default']['NAME']
    if file_exists(database):
        os.remove(database)
    management.execute_from_command_line(['manage.py', 'syncdb', '--noinput'])
    management.execute_from_command_line(['manage.py', 'migrate', '--noinput'])

    # add a super user
    if super_user is not None:
        username = super_user[0]
        password = super_user[1]
        email = super_user[2]
    else:
        username, password, email = '', '', ''
        log.info('Creating new Superuser for Sentry:')
        while True:
            username = raw_input('Enter username: '******' ' in username: continue
            password = raw_input('Enter password: '******' ' in password: continue
            email = raw_input('Enter email: ').strip()
            if len(email) == 0 or ' ' in email or '@' not in email: continue
            break

    from sentry.models import User
    try:
        auser = User.objects.using('default').get(username=username)
    except User.DoesNotExist:
        auser = User.objects.db_manager('default').create_superuser(
            username, email, password)
        log.info('Added Sentry superuser "%s" with password like "%s%s"' %
                 (username, password[:3], '*' * (len(password) - 3)))
    else:
        log.error('Username "%s" is already taken.' % username)

    if project_name is None:
        yesno = raw_input(
            'Would you like to create a new project for Sentry? (yes/no): ')
        if yesno == 'yes' or yesno == 'y':
            while True:
                project_name = raw_input('Enter Project Name: ').strip()
                if len(project_name) == 0: continue
                break

    if project_name is not None:

        project_slug = project_name.lower().replace(' ', '_')
        try:
            # add a project
            from sentry.models import Project, Team
            team = Team.objects.create(name=project_name + ' Team',
                                       slug=project_slug + '_team',
                                       owner=auser)
            project = Project.objects.create(name=project_name,
                                             slug=project_slug,
                                             owner=auser,
                                             team=team)
            key = project.key_set.filter(user=auser)[0]
            dsn = "http://%s:%s@localhost:%s/%s" % (
                key.public_key, key.secret_key, gsetts.SENTRY_WEB_PORT,
                key.project_id)
            log.info('Added "%s" project to Sentry with dsn: %s' %
                     (project_name, dsn))

        except Exception:
            log.error('Failed to create project.')
Exemplo n.º 12
0
        def run(self):
            self.running = True

            self.configuration = self.load_config()

            self.SLEEP_TIME = int(self.configuration['send_interval'])
            self.last_poll_time = time.time()

            log.debug('SMTP dispatch will occur in %s' % str(self.prettiertime(self.SLEEP_TIME)))

            while self.running:
                if (time.time()-self.last_poll_time) < self.SLEEP_TIME:
                    time.sleep(1)
                    continue
                                                    # this overrides the value in the gentry_settings_module

                conn = SMTP()
                try:
                    log.debug('Connecting...')
                    conn.connect(self.configuration['email_host'])
                    conn.set_debuglevel(False)

                    if self.configuration['email_use_tls']:
                        conn.starttls()

                    log.debug('Logging in..')
                    conn.login(self.configuration['email_username'], self.configuration['email_password'])
                    max_mail_size = int(conn.esmtp_features['size'])

                    for generator_cls in BaseMailGenerator.generator_registry:
                        generator = generator_cls(self.configuration, max_mail_size)
                        mails = generator.get_mail_list()

                        for mail in mails:
                            if mail:

                                bytes = len(mail.as_string())
                                if bytes < 1024:
                                    sizestr = str(bytes) + "b"
                                elif bytes < 1048576:
                                    sizestr = "%.2f Kb" % (bytes/1024.0)
                                else:
                                    sizestr = "%.2f Mb" % ((bytes/1024.0)/1024.0)

                                log.debug('%s: Sending mail to: %s Size: %s' % (generator.__class__.__name__, mail['To'],sizestr))

                                start_time = time.time()
                                conn.sendmail(mail['From'], mail['To'], mail.as_string())
                                log.debug('Sent mail in %d seconds.' % (time.time()-start_time))

                    self.last_poll_time = time.time()

                    self.configuration = self.load_config()
                    self.SLEEP_TIME = int(self.configuration['send_interval'])

                    log.debug('Next SMTP dispatch will occur in %s' % str(self.prettiertime(self.SLEEP_TIME)))

                except smtplib.SMTPRecipientsRefused:
                    log.error('STMPRecipientsRefused')
                except smtplib.SMTPHeloError:
                    log.error('SMTPHeloError')
                except smtplib.SMTPSenderRefused:
                    log.exception('SMTPSenderRefused')
                except smtplib.SMTPDataError:
                    log.error('SMTPDataError')
                except Exception:
                    log.exception('An exception occured when sending mail')
                finally:
                    # Did it fail to send
                    if time.time() - self.last_poll_time > self.SLEEP_TIME:
                        self.last_poll_time = time.time() + (60 * 10) - self.SLEEP_TIME
                        log.debug('Next SMTP dispatch will occur in %s' % str(self.prettiertime(60*10)))

                    if hasattr(conn, 'sock') and conn.sock:
                        conn.quit()