Beispiel #1
0
    def testCrashHistory(self):
        d = self.db

        client_id = self.InitializeClient()

        ci = rdf_client.ClientCrash(timestamp=12345, crash_message="Crash #1")
        d.WriteClientCrashInfo(client_id, ci)
        ci.crash_message = "Crash #2"
        d.WriteClientCrashInfo(client_id, ci)
        ci.crash_message = "Crash #3"
        d.WriteClientCrashInfo(client_id, ci)

        last_is = d.ReadClientCrashInfo(client_id)
        self.assertIsInstance(last_is, rdf_client.ClientCrash)
        self.assertEqual(last_is.crash_message, "Crash #3")
        self.assertIsInstance(last_is.timestamp, rdfvalue.RDFDatetime)

        hist = d.ReadClientCrashInfoHistory(client_id)
        self.assertEqual(len(hist), 3)
        self.assertEqual([ci.crash_message for ci in hist],
                         ["Crash #3", "Crash #2", "Crash #1"])
        self.assertIsInstance(hist[0].timestamp, rdfvalue.RDFDatetime)
        self.assertGreater(hist[0].timestamp, hist[1].timestamp)
        self.assertGreater(hist[1].timestamp, hist[2].timestamp)

        md = self.db.ReadClientMetadata(client_id)
        self.assertEqual(md.last_crash_timestamp, hist[0].timestamp)

        self.assertIsNone(d.ReadClientCrashInfo("C.0000000000000000"))
        self.assertEqual(d.ReadClientCrashInfoHistory("C.0000000000000000"),
                         [])
Beispiel #2
0
    def HandleMessage(self, message):
        """Handle client messages."""

        crash_details = rdf_client.ClientCrash(
            client_id=self.client_id,
            session_id=message.session_id,
            crash_message="Client killed during transaction",
            timestamp=rdfvalue.RDFDatetime.Now())

        self.flow_id = message.session_id
        # This is normally done by the FrontEnd when a CLIENT_KILLED message is
        # received.
        events.Events.PublishEvent("ClientCrash",
                                   crash_details,
                                   token=self.token)
Beispiel #3
0
    def ProcessMessage(self, message=None):
        """Processes this event."""

        client_id = message.source

        message = message.payload.string

        logging.info(self.logline, client_id, message)

        # Write crash data.
        if data_store.RelationalDBReadEnabled():
            client = data_store.REL_DB.ReadClientSnapshot(client_id)
            client_info = client.startup_info.client_info
        else:
            client = aff4.FACTORY.Open(client_id, token=self.token)
            client_info = client.Get(client.Schema.CLIENT_INFO)

        crash_details = rdf_client.ClientCrash(client_id=client_id,
                                               client_info=client_info,
                                               crash_message=message,
                                               timestamp=long(time.time() *
                                                              1e6),
                                               crash_type="Nanny Message")

        self.WriteAllCrashDetails(client_id, crash_details, token=self.token)

        # Also send email.
        if config.CONFIG["Monitoring.alert_email"]:
            client = aff4.FACTORY.Open(client_id, token=self.token)
            hostname = client.Get(client.Schema.HOSTNAME)
            url = "/clients/%s" % client_id.Basename()

            body = self.__class__.mail_template.render(
                client_id=client_id,
                admin_ui=config.CONFIG["AdminUI.url"],
                hostname=utils.SmartUnicode(hostname),
                signature=config.CONFIG["Email.signature"],
                url=url,
                message=utils.SmartUnicode(message))
            email_alerts.EMAIL_ALERTER.SendEmail(
                config.CONFIG["Monitoring.alert_email"],
                "GRR server",
                self.subject % client_id,
                utils.SmartStr(body),
                is_html=True)
Beispiel #4
0
    def ReceiveMessages(self, client_id, messages):
        """Receives and processes the messages from the source.

    For each message we update the request object, and place the
    response in that request's queue. If the request is complete, we
    send a message to the worker.

    Args:
      client_id: The client which sent the messages.
      messages: A list of GrrMessage RDFValues.
    """
        now = time.time()
        with queue_manager.QueueManager(token=self.token) as manager:
            for session_id, msgs in utils.GroupBy(
                    messages, operator.attrgetter("session_id")).iteritems():

                # Remove and handle messages to WellKnownFlows
                leftover_msgs = self.HandleWellKnownFlows(msgs)

                unprocessed_msgs = []
                for msg in leftover_msgs:
                    if (msg.auth_state == msg.AuthorizationState.AUTHENTICATED
                            or msg.session_id
                            == self.unauth_allowed_session_id):
                        unprocessed_msgs.append(msg)

                if len(unprocessed_msgs) < len(leftover_msgs):
                    logging.info("Dropped %d unauthenticated messages for %s",
                                 len(leftover_msgs) - len(unprocessed_msgs),
                                 client_id)

                if not unprocessed_msgs:
                    continue

                for msg in unprocessed_msgs:
                    manager.QueueResponse(msg)

                for msg in unprocessed_msgs:
                    # Messages for well known flows should notify even though they don't
                    # have a status.
                    if msg.request_id == 0:
                        manager.QueueNotification(session_id=msg.session_id,
                                                  priority=msg.priority)
                        # Those messages are all the same, one notification is enough.
                        break
                    elif msg.type == rdf_flows.GrrMessage.Type.STATUS:
                        # If we receive a status message from the client it means the client
                        # has finished processing this request. We therefore can de-queue it
                        # from the client queue. msg.task_id will raise if the task id is
                        # not set (message originated at the client, there was no request on
                        # the server), so we have to check .HasTaskID() first.
                        if msg.HasTaskID():
                            manager.DeQueueClientRequest(msg)

                        manager.QueueNotification(session_id=msg.session_id,
                                                  priority=msg.priority,
                                                  last_status=msg.request_id)

                        stat = rdf_flows.GrrStatus(msg.payload)
                        if stat.status == rdf_flows.GrrStatus.ReturnedStatus.CLIENT_KILLED:
                            # A client crashed while performing an action, fire an event.
                            crash_details = rdf_client.ClientCrash(
                                client_id=client_id,
                                session_id=session_id,
                                backtrace=stat.backtrace,
                                crash_message=stat.error_message,
                                nanny_status=stat.nanny_status,
                                timestamp=rdfvalue.RDFDatetime.Now())
                            events.Events.PublishEvent("ClientCrash",
                                                       crash_details,
                                                       token=self.token)

        logging.debug("Received %s messages from %s in %s sec", len(messages),
                      client_id,
                      time.time() - now)