Exemple #1
0
    def testFromMany(self):
        samples = [
            rdf_client.CpuSample(
                timestamp=rdfvalue.RDFDatetime.FromHumanReadable("2001-01-01"),
                cpu_percent=0.2,
                user_cpu_time=0.1,
                system_cpu_time=0.5),
            rdf_client.CpuSample(
                timestamp=rdfvalue.RDFDatetime.FromHumanReadable("2001-02-01"),
                cpu_percent=0.1,
                user_cpu_time=2.5,
                system_cpu_time=1.2),
            rdf_client.CpuSample(
                timestamp=rdfvalue.RDFDatetime.FromHumanReadable("2001-03-01"),
                cpu_percent=0.6,
                user_cpu_time=3.4,
                system_cpu_time=2.4),
        ]

        expected = rdf_client.CpuSample(
            timestamp=rdfvalue.RDFDatetime.FromHumanReadable("2001-03-01"),
            cpu_percent=0.3,
            user_cpu_time=3.4,
            system_cpu_time=2.4)

        self.assertEqual(rdf_client.CpuSample.FromMany(samples), expected)
Exemple #2
0
    def _CollectCpuUsage(self):
        cpu_times = self._process.cpu_times()
        cpu_percent = self._process.cpu_percent()

        sample = rdf_client.CpuSample(timestamp=rdfvalue.RDFDatetime.Now(),
                                      user_cpu_time=cpu_times.user,
                                      system_cpu_time=cpu_times.system,
                                      cpu_percent=cpu_percent)

        self._cpu_samples.append(sample)
        self._cpu_samples = self.CpuSamplesBetween(
            start_time=rdfvalue.RDFDatetime.Now() - self.KEEP_DURATION,
            end_time=rdfvalue.RDFDatetime.Now())
Exemple #3
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
Exemple #4
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]
Exemple #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))
Exemple #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)