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)
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 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)
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 _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())
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)