Exemplo n.º 1
0
def TestInit():
    """Only used in tests and will rerun all the hooks to create a clean state."""
    global INIT_RAN

    if stats.STATS is None:
        stats.STATS = stats.StatsCollector()

    # Tests use both the server template grr_server.yaml as a primary config file
    # (this file does not contain all required options, e.g. private keys), and
    # additional configuration in test_data/grr_test.yaml which contains typical
    # values for a complete installation.
    flags.FLAGS.config = config_lib.Resource().Filter(
        "install_data/etc/grr-server.yaml")

    flags.FLAGS.secondary_configs.append(config_lib.Resource().Filter(
        "grr_response_test/test_data/grr_test.yaml@grr-response-test"))

    # This config contains non-public settings that should be applied during
    # tests.
    extra_test_config = config.CONFIG["Test.additional_test_config"]
    if os.path.exists(extra_test_config):
        flags.FLAGS.secondary_configs.append(extra_test_config)

    # Tests additionally add a test configuration file.
    config_lib.SetPlatformArchContext()
    config_lib.ParseConfigCommandLine()

    # We are running a test so let the config system know that.
    config.CONFIG.AddContext(contexts.TEST_CONTEXT,
                             "Context applied when we run tests.")

    test_ds = flags.FLAGS.test_data_store
    if test_ds is None:
        test_ds = fake_data_store.FakeDataStore.__name__

    if not INIT_RAN:
        config.CONFIG.Set("Datastore.implementation", test_ds)
        config.CONFIG.Set("Blobstore.implementation",
                          memory_stream_bs.MemoryStreamBlobstore.__name__)

        server_logging.ServerLoggingStartupInit()
        server_logging.SetTestVerbosity()

    registry.TestInit()

    db = data_store.DB.SetupTestDB()
    if db:
        data_store.DB = db
    data_store.DB.Initialize()
    aff4.AFF4InitHook().Run()

    INIT_RAN = True
Exemplo n.º 2
0
def ClientInit():
    """Run all startup routines for the client."""
    if stats.STATS is None:
        stats.STATS = stats.StatsCollector()

    config_lib.SetPlatformArchContext()
    config_lib.ParseConfigCommandLine()

    client_logging.LogInit()
    registry.Init()

    if not config.CONFIG.ContextApplied(contexts.CLIENT_BUILD_CONTEXT):
        config.CONFIG.Persist("Client.labels")
        config.CONFIG.Persist("Client.proxy_servers")
        config.CONFIG.Persist("Client.tempdir_roots")
Exemplo n.º 3
0
def Init():
    """Run all required startup routines and initialization hooks."""
    global INIT_RAN
    if INIT_RAN:
        return

    # Set up a temporary syslog handler so we have somewhere to log problems
    # with ConfigInit() which needs to happen before we can start our create our
    # proper logging setup.
    syslog_logger = logging.getLogger("TempLogger")
    if os.path.exists("/dev/log"):
        handler = logging.handlers.SysLogHandler(address="/dev/log")
    else:
        handler = logging.handlers.SysLogHandler()
    syslog_logger.addHandler(handler)

    try:
        config_lib.SetPlatformArchContext()
        config_lib.ParseConfigCommandLine()
    except config_lib.Error:
        syslog_logger.exception("Died during config initialization")
        raise

    if hasattr(registry_init, "stats"):
        logging.debug("Using local stats collector.")
        stats.STATS = registry_init.stats.StatsCollector()
    else:
        logging.debug("Using default stats collector.")
        stats.STATS = stats.StatsCollector()

    server_logging.ServerLoggingStartupInit()

    registry.Init()

    # Exempt config updater from this check because it is the one responsible for
    # setting the variable.
    if not config.CONFIG.ContextApplied("ConfigUpdater Context"):
        if not config.CONFIG.Get("Server.initialized"):
            raise RuntimeError(
                "Config not initialized, run \"grr_config_updater"
                " initialize\". If the server is already configured,"
                " add \"Server.initialized: True\" to your config.")

    INIT_RAN = True
Exemplo n.º 4
0
    def Run(self):
        stats_collector = stats.StatsCollector()

        stats_collector.RegisterCounterMetric(
            "sample_counter", docstring="Sample counter metric.")

        stats_collector.RegisterGaugeMetric("sample_gauge_value",
                                            str,
                                            docstring="Sample gauge metric.")

        stats_collector.RegisterEventMetric("sample_event",
                                            docstring="Sample event metric.")

        with utils.Stubber(stats, "STATS", stats_collector):
            with aff4.FACTORY.Create(None,
                                     aff4_stats_store.StatsStore,
                                     mode="w",
                                     token=self.token) as stats_store:
                stats_store.WriteStats(process_id="worker_1")

        self.Check("ListStatsStoreMetricsMetadata",
                   args=stats_plugin.ApiListStatsStoreMetricsMetadataArgs(
                       component="WORKER"))
Exemplo n.º 5
0
    def Run(self):
        stats_collector = stats.StatsCollector()

        stats_collector.RegisterCounterMetric(
            "sample_counter", docstring="Sample counter metric.")

        stats_collector.RegisterGaugeMetric("sample_gauge_value",
                                            float,
                                            docstring="Sample gauge metric.")

        stats_collector.RegisterEventMetric("sample_event",
                                            docstring="Sample event metric.")

        with utils.Stubber(stats, "STATS", stats_collector):
            for i in range(10):
                with test_lib.FakeTime(42 + i * 60):
                    stats_collector.IncrementCounter("sample_counter")
                    stats_collector.SetGaugeValue("sample_gauge_value",
                                                  i * 0.5)
                    stats_collector.RecordEvent("sample_event", 0.42 + 0.5 * i)

                    with aff4.FACTORY.Create(None,
                                             aff4_stats_store.StatsStore,
                                             mode="w",
                                             token=self.token) as stats_store:
                        stats_store.WriteStats(process_id="worker_1")

        self.Check("GetStatsStoreMetric",
                   args=stats_plugin.ApiGetStatsStoreMetricArgs(
                       component="WORKER",
                       metric_name="sample_counter",
                       start=42000000,
                       end=3600000000))
        self.Check("GetStatsStoreMetric",
                   args=stats_plugin.ApiGetStatsStoreMetricArgs(
                       component="WORKER",
                       metric_name="sample_counter",
                       start=42000000,
                       end=3600000000,
                       rate="1m"))

        self.Check("GetStatsStoreMetric",
                   args=stats_plugin.ApiGetStatsStoreMetricArgs(
                       component="WORKER",
                       metric_name="sample_gauge_value",
                       start=42000000,
                       end=3600000000))

        self.Check("GetStatsStoreMetric",
                   args=stats_plugin.ApiGetStatsStoreMetricArgs(
                       component="WORKER",
                       metric_name="sample_event",
                       start=42000000,
                       end=3600000000))
        self.Check("GetStatsStoreMetric",
                   args=stats_plugin.ApiGetStatsStoreMetricArgs(
                       component="WORKER",
                       metric_name="sample_event",
                       start=42000000,
                       end=3600000000,
                       distribution_handling_mode="DH_COUNT"))
Exemplo n.º 6
0
 def setUp(self):  # pylint: disable=invalid-name
     super(StatsTestMixin, self).setUp()
     self._stats_patcher = mock.patch.object(stats, "STATS",
                                             stats.StatsCollector())
     self._stats_patcher.start()