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)
def Run(self, arg): """Returns the client stats.""" if arg is None: arg = rdf_client.GetClientStatsRequest() proc = psutil.Process(os.getpid()) meminfo = proc.memory_info() response = rdf_client.ClientStats( RSS_size=meminfo.rss, VMS_size=meminfo.vms, memory_percent=proc.memory_percent(), bytes_received=stats.STATS.GetMetricValue( "grr_client_received_bytes"), bytes_sent=stats.STATS.GetMetricValue("grr_client_sent_bytes"), create_time=long(proc.create_time() * 1e6), boot_time=long(psutil.boot_time() * 1e6)) samples = self.grr_worker.stats_collector.cpu_samples for (timestamp, user, system, percent) in samples: if arg.start_time < timestamp < arg.end_time: sample = rdf_client.CpuSample(timestamp=timestamp, user_cpu_time=user, system_cpu_time=system, cpu_percent=percent) response.cpu_samples.Append(sample) samples = self.grr_worker.stats_collector.io_samples for (timestamp, read_bytes, write_bytes) in samples: if arg.start_time < timestamp < arg.end_time: sample = rdf_client.IOSample(timestamp=timestamp, read_bytes=read_bytes, write_bytes=write_bytes) response.io_samples.Append(sample) self.Send(response)
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())
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
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]
def FillClientStats( client_id=rdf_client.ClientURN("C.0000000000000001"), token=None): for minute in range(6): stats = rdf_client.ClientStats() for i in range(minute * 60, (minute + 1) * 60): sample = rdf_client.CpuSample(timestamp=int(i * 10 * 1e6), user_cpu_time=10 + i, system_cpu_time=20 + i, cpu_percent=10 + i) stats.cpu_samples.Append(sample) sample = rdf_client.IOSample(timestamp=int(i * 10 * 1e6), read_bytes=10 + i, write_bytes=10 + i * 2) stats.io_samples.Append(sample) message = rdf_flows.GrrMessage(source=client_id, args=stats.SerializeToString()) flow.WellKnownFlow.GetAllWellKnownFlows( token=token)["Stats"].ProcessMessage(message)
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))
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)