예제 #1
0
    def test_parse_complex(self):
        config = NginxConfig(complex_config)

        context.log.info(config.index)
        context.log.info(config.tree)
        context.log.info(config.files)
        context.log.info(config.checksum())

        # error logs
        assert_that(config.error_logs, has_length(0))

        # access logs
        assert_that(config.access_logs, has_length(0))

        # log formats
        assert_that(config.log_formats, has_length(0))

        # stub status url
        assert_that(config.stub_status, has_length(0))
예제 #2
0
    def collect(self):
        try:
            config = NginxConfig(self.object.conf_path, binary=self.object.bin_path, prefix=self.object.prefix)

            for error in config.parser_errors:
                self.eventd.event(level=WARNING, message=error)

            files_mtimes, total_size = {}, 0
            for file_name, file_info in config.files.iteritems():
                files_mtimes[file_name] = file_info['mtime']
                total_size += file_info['size']

            if files_mtimes != self.previous_mtimes:
                checksum = config.checksum()

                # Send event for parsing nginx config.
                # Use config.parser.filename to account for default value defined in NginxConfigParser.
                self.eventd.event(
                    level=INFO,
                    message='nginx config parsed, read from %s' % config.parser.filename,
                )

                # run upload
                if self.object.upload_config:
                    self.upload(config, checksum)

                # config changed, so we need to restart the object
                if self.previous_checksum:
                    self.object.need_restart = True
                # otherwise run test
                else:
                    # run test
                    if self.object.run_config_test and total_size < 10*1024*1024:  # 10 MB
                        run_time = config.run_test()

                        # Send event for testing nginx config.
                        if config.test_errors:
                            self.eventd.event(level=WARNING, message='nginx config test failed')
                        else:
                            self.eventd.event(level=INFO, message='nginx config tested ok')

                        for error in config.test_errors:
                            self.eventd.event(level=CRITICAL, message=error)

                        if run_time > context.app_config['containers']['nginx']['max_test_duration']:
                            context.app_config['containers']['nginx']['run_test'] = False
                            context.app_config.mark_unchangeable('run_test')
                            self.eventd.event(
                                level=WARNING,
                                message='%s -t -c %s took %s seconds, disabled until agent restart' % (
                                    config.binary, config.filename, run_time
                                )
                            )
                            self.object.run_config_test = False

                self.previous_checksum = checksum
                self.previous_mtimes = copy.copy(files_mtimes)
        except Exception, e:
            exception_name = e.__class__.__name__
            context.log.error('failed to collect due to %s' % exception_name)
            context.log.debug('additional info:', exc_info=True)

            self.eventd.event(
                level=INFO,
                message='nginx config parser failed, path %s' % self.object.conf_path,
                onetime=True
            )