示例#1
0
    def Run(self, arg):
        """Returns the client stats."""
        if arg is None:
            arg = rdf_client_action.GetClientStatsRequest()

        proc = psutil.Process(os.getpid())
        meminfo = proc.memory_info()
        boot_time = rdfvalue.RDFDatetime.FromSecondsSinceEpoch(
            psutil.boot_time())
        create_time = rdfvalue.RDFDatetime.FromSecondsSinceEpoch(
            proc.create_time())
        response = rdf_client_stats.ClientStats(
            RSS_size=meminfo.rss,
            VMS_size=meminfo.vms,
            memory_percent=proc.memory_percent(),
            bytes_received=communicator.GRR_CLIENT_RECEIVED_BYTES.GetValue(),
            bytes_sent=communicator.GRR_CLIENT_SENT_BYTES.GetValue(),
            create_time=create_time,
            boot_time=boot_time)

        response.cpu_samples = self.grr_worker.stats_collector.CpuSamplesBetween(
            start_time=arg.start_time, end_time=arg.end_time)
        response.io_samples = self.grr_worker.stats_collector.IOSamplesBetween(
            start_time=arg.start_time, end_time=arg.end_time)

        self.Send(response)
示例#2
0
  def testReturnsAllDataByDefault(self):
    """Checks that stats collection works."""
    communicator.GRR_CLIENT_RECEIVED_BYTES.Increment(1566)
    communicator.GRR_CLIENT_SENT_BYTES.Increment(2000)

    results = self.RunAction(
        admin.GetClientStats,
        grr_worker=MockClientWorker(),
        arg=rdf_client_action.GetClientStatsRequest())

    response = results[0]
    self.assertEqual(response.bytes_received, 1566)
    self.assertEqual(response.bytes_sent, 2000)

    self.assertLen(response.cpu_samples, 3)
    for i in range(3):
      self.assertEqual(response.cpu_samples[i].timestamp,
                       rdfvalue.RDFDatetime.FromSecondsSinceEpoch(100 + i * 10))
      self.assertAlmostEqual(response.cpu_samples[i].user_cpu_time, 0.1)
      self.assertAlmostEqual(response.cpu_samples[i].system_cpu_time,
                             0.1 * (i + 1))
      self.assertAlmostEqual(response.cpu_samples[i].cpu_percent, 10.0 + 5 * i)

    self.assertLen(response.io_samples, 3)
    for i in range(3):
      self.assertEqual(response.io_samples[i].timestamp,
                       rdfvalue.RDFDatetime.FromSecondsSinceEpoch(100 + i * 10))
      self.assertEqual(response.io_samples[i].read_bytes, 100 * (i + 1))
      self.assertEqual(response.io_samples[i].write_bytes, 100 * (i + 1))

    self.assertEqual(response.boot_time, 100 * 1e6)
示例#3
0
  def testFiltersDataPointsByEndTime(self):
    end_time = rdfvalue.RDFDatetime.FromSecondsSinceEpoch(102)
    results = self.RunAction(
        admin.GetClientStats,
        grr_worker=MockClientWorker(),
        arg=rdf_client_action.GetClientStatsRequest(end_time=end_time))

    response = results[0]
    self.assertLen(response.cpu_samples, 1)
    self.assertEqual(response.cpu_samples[0].timestamp,
                     rdfvalue.RDFDatetime.FromSecondsSinceEpoch(100))

    self.assertLen(response.io_samples, 1)
    self.assertEqual(response.io_samples[0].timestamp,
                     rdfvalue.RDFDatetime.FromSecondsSinceEpoch(100))
示例#4
0
    def _Send(self):
        if not self._ShouldSend():
            return

        # TODO(hanuszczak): We shouldn't manually create action instances. Instead,
        # we should refactor action code to some other function and make the action
        # class use that function. Then here we should use that function as well.
        #
        # Also, it looks like there is a very weird dependency triangle: the worker
        # creates stat collector (which requires a worker), then the stats action
        # requires a worker and uses stat collector internally. But this action is
        # spawned by the stat collector. What...?
        action = admin.GetClientStatsAuto(grr_worker=self._worker)
        request = rdf_client_action.GetClientStatsRequest(
            start_time=self._last_send_time)
        action.Run(request)

        self._should_send = False
        self._last_send_time = rdfvalue.RDFDatetime.Now()
示例#5
0
    def testReturnsAllDataByDefault(self):
        """Checks that stats collection works."""

        stats.STATS.RegisterCounterMetric("grr_client_received_bytes")
        stats.STATS.IncrementCounter("grr_client_received_bytes", 1566)

        stats.STATS.RegisterCounterMetric("grr_client_sent_bytes")
        stats.STATS.IncrementCounter("grr_client_sent_bytes", 2000)

        results = self.RunAction(admin.GetClientStats,
                                 grr_worker=MockClientWorker(),
                                 arg=rdf_client_action.GetClientStatsRequest())

        response = results[0]
        self.assertEqual(response.bytes_received, 1566)
        self.assertEqual(response.bytes_sent, 2000)

        self.assertEqual(len(response.cpu_samples), 3)
        for i in range(3):
            self.assertEqual(
                response.cpu_samples[i].timestamp,
                rdfvalue.RDFDatetime.FromSecondsSinceEpoch(100 + i * 10))
            self.assertAlmostEqual(response.cpu_samples[i].user_cpu_time, 0.1)
            self.assertAlmostEqual(response.cpu_samples[i].system_cpu_time,
                                   0.1 * (i + 1))
            self.assertAlmostEqual(response.cpu_samples[i].cpu_percent,
                                   10.0 + 5 * i)

        self.assertEqual(len(response.io_samples), 3)
        for i in range(3):
            self.assertEqual(
                response.io_samples[i].timestamp,
                rdfvalue.RDFDatetime.FromSecondsSinceEpoch(100 + i * 10))
            self.assertEqual(response.io_samples[i].read_bytes, 100 * (i + 1))
            self.assertEqual(response.io_samples[i].write_bytes, 100 * (i + 1))

        self.assertEqual(response.boot_time, 100 * 1e6)