Exemple #1
0
    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()
        boot_time = rdfvalue.RDFDatetime.FromSecondsSinceEpoch(
            psutil.boot_time())
        create_time = rdfvalue.RDFDatetime.FromSecondsSinceEpoch(
            proc.create_time())
        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=create_time,
            boot_time=boot_time)

        response.cpu_samples = self.grr_worker.stats_collector.CpuSamplesBetween(
            start_time=arg.start_time, end_time=arg.end_time)
        response.io_samples = self.grr_worker.stats_collector.IOSamplesBetween(
            start_time=arg.start_time, end_time=arg.end_time)

        self.Send(response)
Exemple #2
0
    def testWellKnownFlowResponsesAreProcessedOnlyOnce(self):
        worker_obj = worker_lib.GRRWorker(token=self.token)

        # Send a message to a WellKnownFlow - ClientStatsAuto.
        client_id = rdf_client.ClientURN("C.1100110011001100")
        self.SendResponse(rdfvalue.SessionID(queue=queues.STATS,
                                             flow_name="Stats"),
                          data=rdf_client.ClientStats(RSS_size=1234),
                          client_id=client_id,
                          well_known=True)

        # Process all messages
        worker_obj.RunOnce()
        worker_obj.thread_pool.Join()

        client = aff4.FACTORY.Open(client_id.Add("stats"), token=self.token)
        stats = client.Get(client.Schema.STATS)
        self.assertEqual(stats.RSS_size, 1234)

        aff4.FACTORY.Delete(client_id.Add("stats"), token=self.token)

        # Process all messages once again - there should be no actual processing
        # done, as all the responses were processed last time.
        worker_obj.RunOnce()
        worker_obj.thread_pool.Join()

        # Check that stats haven't changed as no new responses were processed.
        client = aff4.FACTORY.Open(client_id.Add("stats"), token=self.token)
        self.assertIsNone(client.Get(client.Schema.STATS))
Exemple #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]
Exemple #4
0
    def _testProcessMessagesWellKnown(self):
        worker_obj = worker_lib.GRRWorker(token=self.token)

        # Send a message to a WellKnownFlow - ClientStatsAuto.
        session_id = administrative.GetClientStatsAuto.well_known_session_id
        client_id = rdf_client.ClientURN("C.1100110011001100")
        self.SendResponse(session_id,
                          data=rdf_client.ClientStats(RSS_size=1234),
                          client_id=client_id,
                          well_known=True)

        if data_store.RelationalDBReadEnabled(category="message_handlers"):
            done = threading.Event()

            def handle(l):
                worker_obj._ProcessMessageHandlerRequests(l)
                done.set()

            data_store.REL_DB.RegisterMessageHandler(
                handle, worker_obj.well_known_flow_lease_time, limit=1000)
            self.assertTrue(done.wait(10))

        # Process all messages
        worker_obj.RunOnce()
        worker_obj.thread_pool.Join()

        client = aff4.FACTORY.Open(client_id.Add("stats"), token=self.token)
        stats = client.Get(client.Schema.STATS)
        self.assertEqual(stats.RSS_size, 1234)

        # Make sure no notifications have been sent.
        user = aff4.FACTORY.Open("aff4:/users/%s" % self.token.username,
                                 token=self.token)
        notifications = user.Get(user.Schema.PENDING_NOTIFICATIONS)
        self.assertIsNone(notifications)

        if data_store.RelationalDBReadEnabled(category="message_handlers"):
            data_store.REL_DB.UnregisterMessageHandler()
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 _testProcessMessagesWellKnown(self):
        worker_obj = worker_lib.GRRWorker(token=self.token)

        # Send a message to a WellKnownFlow - ClientStatsAuto.
        session_id = administrative.GetClientStatsAuto.well_known_session_id
        client_id = rdf_client.ClientURN("C.1100110011001100")
        self.SendResponse(session_id,
                          data=rdf_client.ClientStats(RSS_size=1234),
                          client_id=client_id,
                          well_known=True)

        # Process all messages
        worker_obj.RunOnce()
        worker_obj.thread_pool.Join()

        client = aff4.FACTORY.Open(client_id.Add("stats"), token=self.token)
        stats = client.Get(client.Schema.STATS)
        self.assertEqual(stats.RSS_size, 1234)

        # Make sure no notifications have been sent.
        user = aff4.FACTORY.Open("aff4:/users/%s" % self.token.username,
                                 token=self.token)
        notifications = user.Get(user.Schema.PENDING_NOTIFICATIONS)
        self.assertIsNone(notifications)
Exemple #7
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)
Exemple #8
0
 def ProcessMessage(self, message):
     """Processes a stats response from the client."""
     client_stats = rdf_client.ClientStats(message.payload)
     self.ProcessResponse(message.source, client_stats)