Ejemplo n.º 1
0
def main():
    import argparse
    import ConfigParser
    parser = argparse.ArgumentParser(
        description='Warden configuration file parser')
    parser.add_argument('--config',
                        help="Path to the Warden configuration file.",
                        dest='config',
                        required=True)
    args, unknown = parser.parse_known_args(sys.argv)

    warden_configuration_file = os.path.abspath(os.path.expanduser(
        args.config))

    try:
        with open(warden_configuration_file) as f:
            pass
    except IOError:
        log.error('"%s" Does Not Exist!' % warden_configuration_file)
        sys.exit(1)

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

    carbon_conf = configuration.get('carbon', 'configuration')
    diamond_conf = configuration.get('diamond', 'configuration')
    gentry_settings = configuration.get('gentry', 'gentry_settings_py_path')

    try:
        suser = (configuration.get('gentry', 'super_user'),
                 configuration.get('gentry', 'super_password'),
                 configuration.get('gentry', 'super_email'))
    except:
        suser = None
    ensure(carbon_conf, diamond_conf, gentry_settings, suser, None)
Ejemplo n.º 2
0
def main():
    import argparse
    import ConfigParser
    parser = argparse.ArgumentParser(description='Warden configuration file parser')
    parser.add_argument('--config', help="Path to the Warden configuration file.", dest='config', required=True)
    args, unknown  = parser.parse_known_args(sys.argv)

    warden_configuration_file = os.path.abspath(os.path.expanduser(args.config))

    try:
        with open(warden_configuration_file) as f: pass
    except IOError:
        log.error('"%s" Does Not Exist!' % warden_configuration_file)
        sys.exit(1)

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

    carbon_conf = configuration.get('carbon','configuration')
    diamond_conf = configuration.get('diamond','configuration')
    gentry_settings = configuration.get('gentry', 'gentry_settings_py_path')

    try:
        suser = (configuration.get('gentry', 'super_user'),
            configuration.get('gentry', 'super_password'),
            configuration.get('gentry', 'super_email'))
    except:
        suser = None
    ensure(carbon_conf,diamond_conf,gentry_settings, suser, None)
Ejemplo n.º 3
0
 def stop(self):
     if self.thread.isAlive():
         log.debug("Stopping Diamond..")
         self.thread.stop()
         self.thread.join()
         log.debug("Stopped Diamond.")
     else:
         log.error("Can't stop Diamond if it has not started.")
    def create_mail(self):
        if not settings.EMAIL_TO or settings.EMAIL_TO == '':
            log.error('No receiver email address defined.')
            return None

        if not os.path.isdir(settings.WHISPER_STORAGE_PATH):
            log.error('Invalid whisper storage path specified.')
            return None

        mail = self._setup_mail()
        files = self._attach_files(mail)
        return mail if (files > 0) else None
Ejemplo n.º 5
0
    def stop(self):
        if self.reactor_thread.isAlive():
            log.debug("Stopping Carbon..")

            self.application_service.stopService()

            self.reactor_thread.die()
            self.reactor_thread.join()

            log.debug("Stopped Carbon.")
        else:
            log.error("Can't stop Carbon/Twistd if it has not started.")
Ejemplo n.º 6
0
    def stop(self):
        if self.reactor_thread.isAlive():
            log.debug("Stopping Carbon..")

            self.application_service.stopService()

            self.reactor_thread.die()
            self.reactor_thread.join()

            log.debug("Stopped Carbon.")
        else:
            log.error("Can't stop Carbon/Twistd if it has not started.")
Ejemplo n.º 7
0
def main():
    import argparse
    parser = argparse.ArgumentParser(description='Warden configuration file parser')
    parser.add_argument('--config', help="Path to the Warden configuration file.", dest='config', required=True)
    args, unknown  = parser.parse_known_args(sys.argv)
    warden_configuration_file = os.path.abspath(os.path.expanduser(args.config))
    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)
        sys.exit(1)
    warden_server = WardenServer(warden_configuration_file = warden_configuration_file)
    warden_server.start()
Ejemplo n.º 8
0
def setup(
        home,
        super_user,
        project_name
):
    """
    Warden uses values from its default settings file UNLESS explicitely defined
    here in the constructor.
    """
    os.environ['DJANGO_SETTINGS_MODULE'] = '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 not os.path.exists(os.path.dirname(database)):
        os.makedirs(os.path.dirname(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:
        username = super_user[0]
        password = super_user[1]
        email = super_user[2]

        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:

        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.')
    def create_attachment(self, path, name):
        ctype = 'application/octet-stream'
        maintype, subtype = ctype.split('/', 1)

        try:
            fp = open(path, 'rb')
            msg = MIMEBase(maintype, subtype)
            msg.set_payload(fp.read())
            encoders.encode_base64(msg)
            msg.add_header('Content-Disposition', 'attachment', filename=name)
            return msg
        except IOError as e:
            log.error("Error reading contents of %s: %s" % (path, e))

        finally:
            fp.close()

        return None
Ejemplo n.º 10
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()
Ejemplo n.º 11
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()
Ejemplo n.º 12
0
def ensure(carbon_conf,
           diamond_conf,
           gentry_settings,
           super_user=None,
           project_name=None):
    carbon_conf = os.path.abspath(os.path.expanduser(carbon_conf))
    if carbon_conf is not None and not file_exists(carbon_conf):
        log.error('The Carbon configuration "%s" does not exist. Aborting.' %
                  carbon_conf)
        return False

    diamond_conf = os.path.abspath(os.path.expanduser(diamond_conf))
    if diamond_conf is not None and not file_exists(diamond_conf):
        log.error('The Diamond configuration "%s" does not exist. Aborting.' %
                  diamond_conf)
        return False

    gentry_settings = os.path.abspath(os.path.expanduser(gentry_settings))
    if gentry_settings is not None and not file_exists(gentry_settings):
        log.error('The Gentry settings module "%s" does not exist. Aborting.' %
                  gentry_settings)
        return False

    return setup(carbon_conf, diamond_conf, gentry_settings, super_user,
                 project_name)
Ejemplo n.º 13
0
def main():
    import argparse
    parser = argparse.ArgumentParser(description='Warden configuration file parser')
    parser.add_argument('--config', help="Path to the Warden configuration file.", dest='config')
    parser.add_argument('--pid-file', help="PID file for Daemon mode.  This causes Warden to run in Daemon mode", dest='pid_file')
    parser.add_argument('--stop', help='Stop Warden running in Daemon mode', action='store_true', default=False)
    args, unknown  = parser.parse_known_args(sys.argv)
    if not args.config and not args.stop:
        log.error('Warden not being stopped, and no config file specified - aborting')
        sys.exit(1)
    if args.stop and not args.pid_file:
        log.error('Warden cannot stop daemon mode unless the pid-file is specified')
        sys.exit(1)
    if args.stop:
        pid_file = os.path.abspath(os.path.expanduser(args.pid_file))
        if not os.path.exists(pid_file):
            log.error('Warden cannot find pid-file %s',pid_file)
            sys.exit(1)
        pid = int(open(pid_file, 'r').readline())
        log.info('Killing pid %d', pid)
        os.kill(pid, signal.SIGINT)
        # Check if we've managed for 10 seconds
        for i in range(10):
            try:
                os.kill(pid, 0)
                log.info('Waiting for %d to die', pid)
            except OSError:
                log.info('Stop complete')
                return
            time.sleep(1)
        log.warning("Could not end warden process - killing manually")
        os.kill(pid, signal.SIGHUP)
        if os.path.exists(pid_file):
            os.remove(pid_file)
        return
    warden_configuration_file = os.path.abspath(os.path.expanduser(args.config))
    if not os.path.exists(warden_configuration_file):
        log.error('The warden config file specified ("%s") does not exist!' % warden_configuration_file)
        sys.exit(1)
    if args.pid_file:
        pid_file = os.path.abspath(os.path.expanduser(args.pid_file))
        context = daemon.DaemonContext(pidfile=pidlockfile.PIDLockFile(pid_file))
        with context:
            warden_server = WardenServer(warden_configuration_file = warden_configuration_file)
            warden_server.start()
        return
    warden_server = WardenServer(warden_configuration_file = warden_configuration_file)
    warden_server.start()
Ejemplo n.º 14
0
def main():
    import argparse
    parser = argparse.ArgumentParser(description='Warden Server')
    parser.add_argument('home', nargs='?', help="the warden home folder")
    parser.add_argument(
        '--pid-file',
        help=
        "PID file for Daemon mode.  This causes Warden to run in Daemon mode",
        dest='pid_file')
    parser.add_argument('--stop',
                        help='Stop Warden running in Daemon mode',
                        action='store_true',
                        default=False)
    args = parser.parse_args()
    if args.stop and not args.pid_file:
        log.error(
            'Warden cannot stop daemon mode unless the pid-file is specified')
        sys.exit(1)
    if args.stop:
        pid_file = os.path.abspath(os.path.expanduser(args.pid_file))
        if not os.path.exists(pid_file):
            log.error('Warden cannot find pid-file %s', pid_file)
            sys.exit(1)
        pid = int(open(pid_file, 'r').readline())
        log.info('Killing pid %d', pid)
        os.kill(pid, signal.SIGINT)
        # Check if we've managed for 10 seconds
        for i in range(10):
            try:
                os.kill(pid, 0)
                log.info('Waiting for %d to die', pid)
            except OSError:
                log.info('Stop complete')
                return
            time.sleep(1)
        log.warning("Could not end warden process - killing manually")
        os.kill(pid, signal.SIGHUP)
        if os.path.exists(pid_file):
            os.remove(pid_file)
        return
    home = AutoConf.get_home(args.home)
    if not os.path.exists(home):
        log.error('The warden home specified ("%s") does not exist!' % home)
        sys.exit(1)

    if args.pid_file:
        pid_file = os.path.abspath(os.path.expanduser(args.pid_file))
        import daemon
        from lockfile import pidlockfile
        context = daemon.DaemonContext(
            pidfile=pidlockfile.PIDLockFile(pid_file))
        with context:
            warden_server = WardenServer(home)
            warden_server.start()
        return
    warden_server = WardenServer(home)
    warden_server.start()
    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)
Ejemplo n.º 16
0
def ensure(
        carbon_conf,
        diamond_conf,
        gentry_settings,
        super_user = None,
        project_name = None
):
    carbon_conf = os.path.abspath(os.path.expanduser(carbon_conf))
    if carbon_conf is not None and not file_exists(carbon_conf):
        log.error('The Carbon configuration "%s" does not exist. Aborting.' % carbon_conf)
        return False

    diamond_conf = os.path.abspath(os.path.expanduser(diamond_conf))
    if diamond_conf is not None and not file_exists(diamond_conf):
        log.error('The Diamond configuration "%s" does not exist. Aborting.' % diamond_conf)
        return False

    gentry_settings = os.path.abspath(os.path.expanduser(gentry_settings))
    if gentry_settings is not None and not file_exists(gentry_settings):
        log.error('The Gentry settings module "%s" does not exist. Aborting.' % gentry_settings)
        return False

    return setup(carbon_conf, diamond_conf, gentry_settings, super_user, project_name)
Ejemplo n.º 17
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)
Ejemplo n.º 18
0
 def stop(self):
     if self.thread.isAlive():
         self.thread.stop()
         self.thread.join()
     else:
         log.error("Can't stop Gentry if it has not started.")
Ejemplo n.º 19
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)
Ejemplo n.º 20
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.')
Ejemplo n.º 21
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()
Ejemplo n.º 22
0
            log.error('Failed to create project.')


if __name__ == '__main__':
    import argparse
    import ConfigParser
    parser = argparse.ArgumentParser(description='Warden configuration file parser')
    parser.add_argument('--config', help="Path to the Warden configuration file.", dest='config', required=True)
    args, unknown  = parser.parse_known_args(sys.argv)

    warden_configuration_file = os.path.abspath(os.path.expanduser(args.config))

    try:
        with open(warden_configuration_file) as f: pass
    except IOError:
        log.error('"%s" Does Not Exist!' % warden_configuration_file)
        sys.exit(1)

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

    carbon_conf = configuration.get('carbon','configuration')
    diamond_conf = configuration.get('diamond','configuration')
    gentry_settings = configuration.get('gentry', 'gentry_settings_py_path')

    try:
        suser = (configuration.get('gentry', 'super_user'),
            configuration.get('gentry', 'super_password'),
            configuration.get('gentry', 'super_email'))
    except:
        suser = None
Ejemplo n.º 23
0
 def stop(self):
     if self.thread.isAlive():
         self.thread.stop()
         self.thread.join()
     else:
         log.error("Can't stop Gentry if it has not started.")
Ejemplo n.º 24
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.')