Beispiel #1
0
class MockStatsCollector(client_stats.ClientStatsCollector):
    """Mock stats collector for GetClientStatsActionTest."""

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

    IO_SAMPLES = [
        rdf_client.IOSample(
            timestamp=rdfvalue.RDFDatetime.FromSecondsSinceEpoch(100),
            read_bytes=100,
            write_bytes=100),
        rdf_client.IOSample(
            timestamp=rdfvalue.RDFDatetime.FromSecondsSinceEpoch(110),
            read_bytes=200,
            write_bytes=200),
        rdf_client.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
Beispiel #2
0
    def testFromMany(self):
        samples = [
            rdf_client.IOSample(
                timestamp=rdfvalue.RDFDatetime.FromHumanReadable("2001-01-01"),
                read_bytes=0,
                write_bytes=0),
            rdf_client.IOSample(
                timestamp=rdfvalue.RDFDatetime.FromHumanReadable("2002-01-01"),
                read_bytes=512,
                write_bytes=1024),
            rdf_client.IOSample(
                timestamp=rdfvalue.RDFDatetime.FromHumanReadable("2003-01-01"),
                read_bytes=2048,
                write_bytes=4096),
        ]

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

        self.assertEqual(rdf_client.IOSample.FromMany(samples), expected)
Beispiel #3
0
            def GetClientStats(self, _):
                """Fake get client stats method."""
                response = rdf_client.ClientStats()
                for i in range(12):
                    sample = rdf_client.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.IOSample(timestamp=int(i * 10 * 1e6),
                                                 read_bytes=10 + i,
                                                 write_bytes=10 + i)
                    response.io_samples.Append(sample)

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

        sample = rdf_client.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())
Beispiel #5
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.ClientStats()

                    sample = rdf_client.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.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))
Beispiel #6
0
    def testDownsampled(self):
        timestamp = rdfvalue.RDFDatetime.FromHumanReadable

        stats = rdf_client.ClientStats(
            cpu_samples=[
                rdf_client.CpuSample(timestamp=timestamp("2001-01-01 00:00"),
                                     user_cpu_time=2.5,
                                     system_cpu_time=3.2,
                                     cpu_percent=0.5),
                rdf_client.CpuSample(timestamp=timestamp("2001-01-01 00:05"),
                                     user_cpu_time=2.6,
                                     system_cpu_time=4.7,
                                     cpu_percent=0.6),
                rdf_client.CpuSample(timestamp=timestamp("2001-01-01 00:10"),
                                     user_cpu_time=10.0,
                                     system_cpu_time=14.2,
                                     cpu_percent=0.9),
                rdf_client.CpuSample(timestamp=timestamp("2001-01-01 00:12"),
                                     user_cpu_time=12.3,
                                     system_cpu_time=14.9,
                                     cpu_percent=0.1),
                rdf_client.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.IOSample(timestamp=timestamp("2001-01-01 00:00"),
                                    read_count=0,
                                    write_count=0),
                rdf_client.IOSample(timestamp=timestamp("2001-01-01 00:02"),
                                    read_count=3,
                                    write_count=5),
                rdf_client.IOSample(timestamp=timestamp("2001-01-01 00:12"),
                                    read_count=6,
                                    write_count=8),
            ])

        expected = rdf_client.ClientStats(
            cpu_samples=[
                rdf_client.CpuSample(timestamp=timestamp("2001-01-01 00:05"),
                                     user_cpu_time=2.6,
                                     system_cpu_time=4.7,
                                     cpu_percent=0.55),
                rdf_client.CpuSample(timestamp=timestamp("2001-01-01 00:12"),
                                     user_cpu_time=12.3,
                                     system_cpu_time=14.9,
                                     cpu_percent=0.5),
                rdf_client.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.IOSample(timestamp=timestamp("2001-01-01 00:02"),
                                    read_count=3,
                                    write_count=5),
                rdf_client.IOSample(timestamp=timestamp("2001-01-01 00:12"),
                                    read_count=6,
                                    write_count=8),
            ])

        actual = rdf_client.ClientStats.Downsampled(
            stats, interval=rdfvalue.Duration("10m"))

        self.assertEqual(actual, expected)