Example #1
0
 def testSymmetricEqualityForDisjointFields(self):
     # Prevent a regression, where RDFStruct.__eq__ would be True for instances
     # having the same number of fields, but different fields which are set to
     # the default value.
     self.assertNotEqual(rdf_client_stats.IOSample(write_count=6),
                         rdf_client_stats.IOSample(read_bytes=0))
     self.assertNotEqual(rdf_client_stats.IOSample(read_bytes=0),
                         rdf_client_stats.IOSample(write_count=6))
Example #2
0
    def FillClientStats(self, client_id):
        with aff4.FACTORY.Create(client_id.Add("stats"),
                                 aff4_type=aff4_stats.ClientStats,
                                 token=self.token,
                                 mode="rw") as stats_fd:
            for i in range(6):
                with test_lib.FakeTime((i + 1) * 10):
                    timestamp = int((i + 1) * 10 * 1e6)
                    st = rdf_client_stats.ClientStats()

                    sample = rdf_client_stats.CpuSample(timestamp=timestamp,
                                                        user_cpu_time=10 + i,
                                                        system_cpu_time=20 + i,
                                                        cpu_percent=10 + i)
                    st.cpu_samples.Append(sample)

                    sample = rdf_client_stats.IOSample(timestamp=timestamp,
                                                       read_bytes=10 + i,
                                                       write_bytes=10 + i * 2)
                    st.io_samples.Append(sample)

                    stats_fd.AddAttribute(stats_fd.Schema.STATS(st))

                    if data_store.RelationalDBWriteEnabled():
                        data_store.REL_DB.WriteClientStats(
                            client_id=client_id.Basename(), stats=st)
Example #3
0
class MockStatsCollector(client_stats.ClientStatsCollector):
  """Mock stats collector for GetClientStatsActionTest."""

  CPU_SAMPLES = [
      rdf_client_stats.CpuSample(
          timestamp=rdfvalue.RDFDatetime.FromSecondsSinceEpoch(100),
          user_cpu_time=0.1,
          system_cpu_time=0.1,
          cpu_percent=10.0),
      rdf_client_stats.CpuSample(
          timestamp=rdfvalue.RDFDatetime.FromSecondsSinceEpoch(110),
          user_cpu_time=0.1,
          system_cpu_time=0.2,
          cpu_percent=15.0),
      rdf_client_stats.CpuSample(
          timestamp=rdfvalue.RDFDatetime.FromSecondsSinceEpoch(120),
          user_cpu_time=0.1,
          system_cpu_time=0.3,
          cpu_percent=20.0),
  ]

  IO_SAMPLES = [
      rdf_client_stats.IOSample(
          timestamp=rdfvalue.RDFDatetime.FromSecondsSinceEpoch(100),
          read_bytes=100,
          write_bytes=100),
      rdf_client_stats.IOSample(
          timestamp=rdfvalue.RDFDatetime.FromSecondsSinceEpoch(110),
          read_bytes=200,
          write_bytes=200),
      rdf_client_stats.IOSample(
          timestamp=rdfvalue.RDFDatetime.FromSecondsSinceEpoch(120),
          read_bytes=300,
          write_bytes=300),
  ]

  def __init__(self):
    self._cpu_samples = self.CPU_SAMPLES
    self._io_samples = self.IO_SAMPLES
Example #4
0
    def testFromMany(self):
        samples = [
            rdf_client_stats.IOSample(
                timestamp=rdfvalue.RDFDatetime.FromHumanReadable("2001-01-01"),
                read_bytes=0,
                write_bytes=0),
            rdf_client_stats.IOSample(
                timestamp=rdfvalue.RDFDatetime.FromHumanReadable("2002-01-01"),
                read_bytes=512,
                write_bytes=1024),
            rdf_client_stats.IOSample(
                timestamp=rdfvalue.RDFDatetime.FromHumanReadable("2003-01-01"),
                read_bytes=2048,
                write_bytes=4096),
        ]

        expected = rdf_client_stats.IOSample(
            timestamp=rdfvalue.RDFDatetime.FromHumanReadable("2003-01-01"),
            read_bytes=2048,
            write_bytes=4096)

        self.assertEqual(rdf_client_stats.IOSample.FromMany(samples), expected)
            def GetClientStats(self, _):
                """Fake get client stats method."""
                response = rdf_client_stats.ClientStats()
                for i in range(12):
                    sample = rdf_client_stats.CpuSample(timestamp=int(i * 10 *
                                                                      1e6),
                                                        user_cpu_time=10 + i,
                                                        system_cpu_time=20 + i,
                                                        cpu_percent=10 + i)
                    response.cpu_samples.Append(sample)

                    sample = rdf_client_stats.IOSample(timestamp=int(i * 10 *
                                                                     1e6),
                                                       read_bytes=10 + i,
                                                       write_bytes=10 + i)
                    response.io_samples.Append(sample)

                return [response]
Example #6
0
    def _CollectIOUsage(self):
        # Not supported on MacOS.
        try:
            io_counters = self._process.io_counters()
        except (AttributeError, NotImplementedError, psutil.Error):
            return

        sample = rdf_client_stats.IOSample(
            timestamp=rdfvalue.RDFDatetime.Now(),
            read_bytes=io_counters.read_bytes,
            write_bytes=io_counters.write_bytes,
            read_count=io_counters.read_count,
            write_count=io_counters.write_count)

        self._io_samples.append(sample)
        self._io_samples = self.IOSamplesBetween(
            start_time=rdfvalue.RDFDatetime.Now() - self.KEEP_DURATION,
            end_time=rdfvalue.RDFDatetime.Now())
Example #7
0
  def FillClientStats(self, client_id):
    stats = []
    for i in range(6):
      timestamp = int((i + 1) * 10 * 1e6)
      st = rdf_client_stats.ClientStats()

      sample = rdf_client_stats.CpuSample(
          timestamp=timestamp,
          user_cpu_time=10 + i,
          system_cpu_time=20 + i,
          cpu_percent=10 + i)
      st.cpu_samples.Append(sample)

      sample = rdf_client_stats.IOSample(
          timestamp=timestamp, read_bytes=10 + i, write_bytes=10 + i * 2)
      st.io_samples.Append(sample)

      stats.append(st)

    for st in stats:
      with test_lib.FakeTime(st.cpu_samples[0].timestamp):
        data_store.REL_DB.WriteClientStats(client_id=client_id, stats=st)
Example #8
0
    def testDownsampled(self):
        timestamp = rdfvalue.RDFDatetime.FromHumanReadable

        stats = rdf_client_stats.ClientStats(
            cpu_samples=[
                rdf_client_stats.CpuSample(
                    timestamp=timestamp("2001-01-01 00:00"),
                    user_cpu_time=2.5,
                    system_cpu_time=3.2,
                    cpu_percent=0.5),
                rdf_client_stats.CpuSample(
                    timestamp=timestamp("2001-01-01 00:05"),
                    user_cpu_time=2.6,
                    system_cpu_time=4.7,
                    cpu_percent=0.6),
                rdf_client_stats.CpuSample(
                    timestamp=timestamp("2001-01-01 00:10"),
                    user_cpu_time=10.0,
                    system_cpu_time=14.2,
                    cpu_percent=0.9),
                rdf_client_stats.CpuSample(
                    timestamp=timestamp("2001-01-01 00:12"),
                    user_cpu_time=12.3,
                    system_cpu_time=14.9,
                    cpu_percent=0.1),
                rdf_client_stats.CpuSample(
                    timestamp=timestamp("2001-01-01 00:21"),
                    user_cpu_time=16.1,
                    system_cpu_time=22.3,
                    cpu_percent=0.4)
            ],
            io_samples=[
                rdf_client_stats.IOSample(
                    timestamp=timestamp("2001-01-01 00:00"),
                    read_count=0,
                    write_count=0),
                rdf_client_stats.IOSample(
                    timestamp=timestamp("2001-01-01 00:02"),
                    read_count=3,
                    write_count=5),
                rdf_client_stats.IOSample(
                    timestamp=timestamp("2001-01-01 00:12"),
                    read_count=6,
                    write_count=8),
            ])

        expected = rdf_client_stats.ClientStats(
            cpu_samples=[
                rdf_client_stats.CpuSample(
                    timestamp=timestamp("2001-01-01 00:05"),
                    user_cpu_time=2.6,
                    system_cpu_time=4.7,
                    cpu_percent=0.55),
                rdf_client_stats.CpuSample(
                    timestamp=timestamp("2001-01-01 00:12"),
                    user_cpu_time=12.3,
                    system_cpu_time=14.9,
                    cpu_percent=0.5),
                rdf_client_stats.CpuSample(
                    timestamp=timestamp("2001-01-01 00:21"),
                    user_cpu_time=16.1,
                    system_cpu_time=22.3,
                    cpu_percent=0.4),
            ],
            io_samples=[
                rdf_client_stats.IOSample(
                    timestamp=timestamp("2001-01-01 00:02"),
                    read_count=3,
                    write_count=5),
                rdf_client_stats.IOSample(
                    timestamp=timestamp("2001-01-01 00:12"),
                    read_count=6,
                    write_count=8),
            ])

        actual = rdf_client_stats.ClientStats.Downsampled(
            stats, interval=rdfvalue.Duration.From(10, rdfvalue.MINUTES))

        self.assertEqual(actual, expected)