def WriteClientSnapshot(self, client, cursor=None): """Write new client snapshot.""" startup_info = client.startup_info client.startup_info = None insert_history_query = ( "INSERT INTO client_snapshot_history(client_id, timestamp, " "client_snapshot) VALUES (%s, %s, %s)") insert_startup_query = ( "INSERT INTO client_startup_history(client_id, timestamp, " "startup_info) VALUES(%s, %s, %s)") update_query = ("UPDATE clients SET last_client_timestamp=%s, " "last_startup_timestamp=%s " "WHERE client_id = %s") int_id = _ClientIDToInt(client.client_id) timestamp = datetime.datetime.utcnow() try: cursor.execute(insert_history_query, (int_id, timestamp, client.SerializeToString())) cursor.execute(insert_startup_query, (int_id, timestamp, startup_info.SerializeToString())) cursor.execute(update_query, (timestamp, timestamp, int_id)) except MySQLdb.IntegrityError as e: raise db_module.UnknownClientError(e) finally: client.startup_info = startup_info
def WriteClientCrashInfo(self, client_id, crash_info): if client_id not in self.metadatas: raise db.UnknownClientError(client_id) ts = rdfvalue.RDFDatetime.Now() self.metadatas[client_id]["last_crash_timestamp"] = ts history = self.crash_history.setdefault(client_id, {}) history[ts] = crash_info.SerializeToString()
def WriteClientStartupInfo(self, client_id, startup_info): if client_id not in self.metadatas: raise db.UnknownClientError(client_id) ts = rdfvalue.RDFDatetime.Now() self.metadatas[client_id]["startup_info_timestamp"] = ts history = self.startup_history.setdefault(client_id, {}) history[ts] = startup_info.SerializeToString()
def AddClientLabels(self, client_id, owner, labels): if client_id not in self.metadatas: raise db.UnknownClientError(client_id) labelset = self.labels.setdefault(client_id, {}).setdefault(owner, set()) for l in labels: labelset.add(utils.SmartUnicode(l))
def AddClientKeywords(self, client_id, keywords): if client_id not in self.metadatas: raise db.UnknownClientError(client_id) keywords = [utils.SmartStr(k) for k in keywords] for k in keywords: self.keywords.setdefault(k, {}) self.keywords[k][client_id] = rdfvalue.RDFDatetime.Now()
def AddClientLabels(self, client_id, owner, labels, cursor=None): """Attaches a list of user labels to a client.""" cid = _ClientIDToInt(client_id) try: for label in labels: cursor.execute( "INSERT IGNORE INTO client_labels (client_id, owner, label) " "VALUES (%s, %s, %s)", [cid, owner, utils.SmartUnicode(label)]) except MySQLdb.IntegrityError as e: raise db_module.UnknownClientError(e)
def MultiWritePathHistory(self, client_id, stat_entries, hash_entries): """Writes a collection of hash and stat entries observed for given paths.""" if client_id not in self.metadatas: raise db.UnknownClientError(client_id) for path_info, stat_entry in stat_entries.iteritems(): path_record = self._GetPathRecord(client_id, path_info) path_record.AddStatEntry(stat_entry, path_info.timestamp) for path_info, hash_entry in hash_entries.iteritems(): path_record = self._GetPathRecord(client_id, path_info) path_record.AddHashEntry(hash_entry, path_info.timestamp)
def AddClientKeywords(self, client_id, keywords, cursor=None): """Associates the provided keywords with the client.""" cid = _ClientIDToInt(client_id) now = datetime.datetime.utcnow() try: for kw in keywords: cursor.execute( "INSERT INTO client_keywords (client_id, keyword, timestamp) " "VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE timestamp=%s", [cid, utils.SmartUnicode(kw), now, now]) except MySQLdb.IntegrityError as e: raise db_module.UnknownClientError(e)
def WriteClientCrashInfo(self, client_id, crash_info, cursor=None): """Writes a new client crash record.""" cid = _ClientIDToInt(client_id) now = _RDFDatetimeToMysqlString(rdfvalue.RDFDatetime.Now()) try: cursor.execute( "INSERT INTO client_crash_history (client_id, timestamp, crash_info) " "VALUES (%s, %s, %s)", [cid, now, crash_info.SerializeToString()]) cursor.execute( "UPDATE clients SET last_crash_timestamp = %s WHERE client_id=%s", [now, cid]) except MySQLdb.IntegrityError as e: raise db_module.UnknownClientError(e)
def _WritePathInfo(self, client_id, path_info, ancestor): """Writes a single path info record for given client.""" if client_id not in self.metadatas: raise db.UnknownClientError(client_id) path_record = self._GetPathRecord(client_id, path_info) if not ancestor: path_record.AddPathHistory(path_info) else: path_record.AddPathInfo(path_info) parent_path_info = path_info.GetParent() if parent_path_info is not None: parent_path_record = self._GetPathRecord(client_id, parent_path_info) parent_path_record.AddChild(path_info)
def WriteClientSnapshotHistory(self, clients): if clients[0].client_id not in self.metadatas: raise db.UnknownClientError(clients[0].client_id) for client in clients: startup_info = client.startup_info client.startup_info = None snapshots = self.clients.setdefault(client.client_id, {}) snapshots[client.timestamp] = client.SerializeToString() startup_infos = self.startup_history.setdefault( client.client_id, {}) startup_infos[client.timestamp] = startup_info.SerializeToString() client.startup_info = startup_info
def WriteClientStartupInfo(self, client_id, startup_info, cursor=None): """Writes a new client startup record.""" cid = mysql_utils.ClientIDToInt(client_id) now = mysql_utils.RDFDatetimeToMysqlString(rdfvalue.RDFDatetime.Now()) try: cursor.execute( "INSERT INTO client_startup_history " "(client_id, timestamp, startup_info) " "VALUES (%s, %s, %s)", [cid, now, startup_info.SerializeToString()]) cursor.execute( "UPDATE clients SET last_startup_timestamp = %s WHERE client_id=%s", [now, cid]) except MySQLdb.IntegrityError as e: raise db.UnknownClientError(client_id, cause=e)
def WriteClientSnapshot(self, client): """Writes new client snapshot.""" client_id = client.client_id if client_id not in self.metadatas: raise db.UnknownClientError(client_id) startup_info = client.startup_info client.startup_info = None ts = rdfvalue.RDFDatetime.Now() history = self.clients.setdefault(client_id, {}) history[ts] = client.SerializeToString() history = self.startup_history.setdefault(client_id, {}) history[ts] = startup_info.SerializeToString() client.startup_info = startup_info
def WriteClientSnapshotHistory(self, clients, cursor=None): """Writes the full history for a particular client.""" cid = mysql_utils.ClientIDToInt(clients[0].client_id) latest_timestamp = None for client in clients: startup_info = client.startup_info client.startup_info = None timestamp = mysql_utils.RDFDatetimeToMysqlString(client.timestamp) latest_timestamp = max(latest_timestamp, client.timestamp) try: cursor.execute( "INSERT INTO client_snapshot_history " "(client_id, timestamp, client_snapshot) " "VALUES (%s, %s, %s)", [cid, timestamp, client.SerializeToString()]) cursor.execute( "INSERT INTO client_startup_history " "(client_id, timestamp, startup_info) " "VALUES (%s, %s, %s)", [cid, timestamp, startup_info.SerializeToString()]) except MySQLdb.IntegrityError as e: raise db.UnknownClientError(clients[0].client_id, cause=e) finally: client.startup_info = startup_info latest_timestamp_str = mysql_utils.RDFDatetimeToMysqlString( latest_timestamp) cursor.execute( "UPDATE clients SET last_client_timestamp=%s " "WHERE client_id = %s AND " "(last_client_timestamp IS NULL OR last_client_timestamp < %s)", [latest_timestamp_str, cid, latest_timestamp_str]) cursor.execute( "UPDATE clients SET last_startup_timestamp=%s " "WHERE client_id = %s AND " "(last_startup_timestamp IS NULL OR last_startup_timestamp < %s)", [latest_timestamp_str, cid, latest_timestamp_str])
def WriteClientMessages(self, messages, cursor=None): """Writes messages that should go to the client to the db.""" query = ("INSERT IGNORE INTO client_messages " "(client_id, message_id, timestamp, message) " "VALUES %s ON DUPLICATE KEY UPDATE " "timestamp=VALUES(timestamp), message=VALUES(message)") now = mysql_utils.RDFDatetimeToMysqlString(rdfvalue.RDFDatetime.Now()) value_templates = [] args = [] for m in messages: client_id_int = mysql_utils.ClientIDToInt(m.queue.Split()[0]) args.extend([client_id_int, m.task_id, now, m.SerializeToString()]) value_templates.append("(%s, %s, %s, %s)") query %= ",".join(value_templates) try: cursor.execute(query, args) except MySQLdb.IntegrityError as e: raise db.UnknownClientError(cause=e)
def _WritePathInfo(self, client_id, path_info, ancestor): """Writes a single path info record for given client.""" if client_id not in self.metadatas: raise db.UnknownClientError(client_id) idx = (client_id, path_info.path_type) path_infos = self.path_info_map_by_client_id.setdefault(idx, {}) path_children = self.path_child_map_by_client_id.setdefault(idx, {}) path_info = path_info.Copy() if not ancestor: path_info.last_path_history_timestamp = rdfvalue.RDFDatetime.Now() path_id = path_info.GetPathID() if path_id in path_infos: path_infos[path_id].UpdateFrom(path_info) else: path_infos[path_id] = path_info parent_path_info = path_info.GetParent() if parent_path_info is not None: parent_path_id = parent_path_info.GetPathID() path_children.setdefault(parent_path_id, set()).add(path_id)