Beispiel #1
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,
                                             "StatsStore",
                                             mode="w",
                                             token=self.token) as stats_store:
                        stats_store.WriteStats(process_id="worker_1",
                                               sync=True)

        self.Check(
            "GET", "/api/stats/store/WORKER/metrics/sample_counter?"
            "start=42000000&end=3600000000")
        self.Check(
            "GET", "/api/stats/store/WORKER/metrics/sample_counter?"
            "start=42000000&end=3600000000&rate=1m")

        self.Check(
            "GET", "/api/stats/store/WORKER/metrics/sample_gauge_value?"
            "start=42000000&end=3600000000")

        self.Check(
            "GET", "/api/stats/store/WORKER/metrics/sample_event?"
            "start=42000000&end=3600000000")
        self.Check(
            "GET", "/api/stats/store/WORKER/metrics/sample_event?"
            "start=42000000&end=3600000000&"
            "distribution_handling_mode=DH_COUNT")
Beispiel #2
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(
        "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)

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

    config.CONFIG.Set("Datastore.implementation",
                      fake_data_store.FakeDataStore.__name__)
    config.CONFIG.Set("Blobstore.implementation",
                      memory_stream_bs.MemoryStreamBlobstore.__name__)

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

    data_store.DataStoreInit().Run()
    aff4.AFF4InitHook().Run()

    if not INIT_RAN:
        log.ServerLoggingStartupInit()

    registry.TestInit()

    INIT_RAN = True
Beispiel #3
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, "StatsStore", mode="w", token=self.token) as stats_store:
        stats_store.WriteStats(process_id="worker_1", sync=True)

    self.Check("GET", "/api/stats/store/WORKER/metadata")
Beispiel #4
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
Beispiel #5
0
def main(unused_argv):
    """Main."""
    # Change the startup sequence in order to set the database path, if needed.
    startup.AddConfigContext()
    startup.ConfigInit()

    if flags.FLAGS.path:
        config_lib.CONFIG.Set("Datastore.location", flags.FLAGS.path)

    startup.ServerLoggingStartupInit()
    stats.STATS = stats.StatsCollector()

    # We avoid starting some hooks because they add unneeded things
    # to the data store.
    do_not_start = set(
        ["ConfigurationViewInitHook", "FileStoreInit", "GRRAFF4Init"])
    registry.Init(skip_set=do_not_start)

    Start(data_store.DB, port=flags.FLAGS.port, is_master=flags.FLAGS.master)
  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"))
Beispiel #7
0
def Init():
    """Run all required startup routines and initialization hooks."""
    global INIT_RAN
    if INIT_RAN:
        return

    stats.STATS = stats.StatsCollector()

    # 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:
        AddConfigContext()
        ConfigInit()
    except config_lib.Error:
        syslog_logger.exception("Died during config initialization")
        raise

    ServerLoggingStartupInit()
    registry.Init()

    if config_lib.CONFIG["Server.username"] != "root":
        try:
            os.setuid(
                pwd.getpwnam(config_lib.CONFIG["Server.username"]).pw_uid)
        except (KeyError, OSError):
            logging.exception("Unable to switch to user %s",
                              config_lib.CONFIG["Server.username"])
            raise

    INIT_RAN = True
Beispiel #8
0
def TestInit():
  """Only used in tests and will rerun all the hooks to create a clean state."""
  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.CONFIG["Test.config"]
  flags.FLAGS.secondary_configs = [
      os.path.join(config_lib.CONFIG["Test.data_dir"], "grr_test.yaml")]

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

  AddConfigContext()
  ConfigInit()

  # Tests additionally add a test configuration file.
  ServerLoggingStartupInit()
  registry.TestInit()
  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"))
Beispiel #10
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()