def _ResponseToClientsFullInfo(self, response): """Creates a ClientFullInfo object from a database response.""" c_full_info = None prev_cid = None for row in response: (cid, fs, crt, ping, clk, ip, foreman, first, last_client_ts, last_crash_ts, last_startup_ts, client_obj, client_startup_obj, last_startup_obj, label_owner, label_name) = row if cid != prev_cid: if c_full_info: yield mysql_utils.IntToClientID(prev_cid), c_full_info metadata = objects.ClientMetadata( certificate=crt, fleetspeak_enabled=fs, first_seen=mysql_utils.MysqlToRDFDatetime(first), ping=mysql_utils.MysqlToRDFDatetime(ping), clock=mysql_utils.MysqlToRDFDatetime(clk), ip=mysql_utils.StringToRDFProto(rdf_client.NetworkAddress, ip), last_foreman_time=mysql_utils.MysqlToRDFDatetime(foreman), startup_info_timestamp=mysql_utils.MysqlToRDFDatetime( last_startup_ts), last_crash_timestamp=mysql_utils.MysqlToRDFDatetime( last_crash_ts)) if client_obj is not None: l_snapshot = objects.ClientSnapshot.FromSerializedString( client_obj) l_snapshot.timestamp = mysql_utils.MysqlToRDFDatetime( last_client_ts) l_snapshot.startup_info = rdf_client.StartupInfo.FromSerializedString( client_startup_obj) l_snapshot.startup_info.timestamp = l_snapshot.timestamp else: l_snapshot = objects.ClientSnapshot( client_id=mysql_utils.IntToClientID(cid)) if last_startup_obj is not None: startup_info = rdf_client.StartupInfo.FromSerializedString( last_startup_obj) startup_info.timestamp = mysql_utils.MysqlToRDFDatetime( last_startup_ts) else: startup_info = None prev_cid = cid c_full_info = objects.ClientFullInfo( metadata=metadata, labels=[], last_snapshot=l_snapshot, last_startup_info=startup_info) if label_owner and label_name: c_full_info.labels.append( objects.ClientLabel(name=label_name, owner=label_owner)) if c_full_info: yield mysql_utils.IntToClientID(prev_cid), c_full_info
def MultiReadClientMetadata(self, client_ids, cursor=None): """Reads ClientMetadata records for a list of clients.""" ids = [ mysql_utils.ClientIDToInt(client_id) for client_id in client_ids ] query = ( "SELECT client_id, fleetspeak_enabled, certificate, last_ping, " "last_clock, last_ip, last_foreman, first_seen, " "last_crash_timestamp, last_startup_timestamp FROM " "clients WHERE client_id IN ({})").format(", ".join(["%s"] * len(ids))) ret = {} cursor.execute(query, ids) while True: row = cursor.fetchone() if not row: break cid, fs, crt, ping, clk, ip, foreman, first, lct, lst = row ret[mysql_utils.IntToClientID(cid)] = objects.ClientMetadata( certificate=crt, fleetspeak_enabled=fs, first_seen=mysql_utils.MysqlToRDFDatetime(first), ping=mysql_utils.MysqlToRDFDatetime(ping), clock=mysql_utils.MysqlToRDFDatetime(clk), ip=mysql_utils.StringToRDFProto(rdf_client.NetworkAddress, ip), last_foreman_time=mysql_utils.MysqlToRDFDatetime(foreman), startup_info_timestamp=mysql_utils.MysqlToRDFDatetime(lst), last_crash_timestamp=mysql_utils.MysqlToRDFDatetime(lct)) return ret
def MultiReadClientSnapshot(self, client_ids, cursor=None): """Reads the latest client snapshots for a list of clients.""" int_ids = [mysql_utils.ClientIDToInt(cid) for cid in client_ids] query = ( "SELECT h.client_id, h.client_snapshot, h.timestamp, s.startup_info " "FROM clients as c, client_snapshot_history as h, " "client_startup_history as s " "WHERE h.client_id = c.client_id " "AND s.client_id = c.client_id " "AND h.timestamp = c.last_client_timestamp " "AND s.timestamp = c.last_startup_timestamp " "AND c.client_id IN ({})").format(", ".join(["%s"] * len(client_ids))) ret = {cid: None for cid in client_ids} cursor.execute(query, int_ids) while True: row = cursor.fetchone() if not row: break cid, snapshot, timestamp, startup_info = row client_obj = mysql_utils.StringToRDFProto(objects.ClientSnapshot, snapshot) client_obj.startup_info = mysql_utils.StringToRDFProto( rdf_client.StartupInfo, startup_info) client_obj.timestamp = mysql_utils.MysqlToRDFDatetime(timestamp) ret[mysql_utils.IntToClientID(cid)] = client_obj return ret
def MultiReadClientLabels(self, client_ids, cursor=None): """Reads the user labels for a list of clients.""" int_ids = [mysql_utils.ClientIDToInt(cid) for cid in client_ids] query = ("SELECT client_id, owner, label " "FROM client_labels " "WHERE client_id IN ({})").format(", ".join(["%s"] * len(client_ids))) ret = {client_id: [] for client_id in client_ids} cursor.execute(query, int_ids) for client_id, owner, label in cursor.fetchall(): ret[mysql_utils.IntToClientID(client_id)].append( objects.ClientLabel(name=utils.SmartUnicode(label), owner=owner)) for r in ret.values(): r.sort(key=lambda label: (label.owner, label.name)) return ret
def ReadAllAuditEvents(self, cursor=None): """Reads all audit events stored in the database.""" cursor.execute(""" SELECT username, urn, client_id, timestamp, details FROM audit_event ORDER BY timestamp """) result = [] for username, urn, client_id, timestamp, details in cursor.fetchall(): event = rdf_events.AuditEvent.FromSerializedString(details) event.user = username if urn: event.urn = rdfvalue.RDFURN(urn) if client_id is not None: event.client = rdf_client.ClientURN( mysql_utils.IntToClientID(client_id)) event.timestamp = mysql_utils.MysqlToRDFDatetime(timestamp) result.append(event) return result
def ListClientsForKeywords(self, keywords, start_time=None, cursor=None): """Lists the clients associated with keywords.""" keywords = set(keywords) keyword_mapping = {utils.SmartUnicode(kw): kw for kw in keywords} result = {} for kw in keyword_mapping.values(): result[kw] = [] query = ( "SELECT DISTINCT keyword, client_id FROM client_keywords WHERE " "keyword IN ({})".format(",".join(["%s"] * len(keyword_mapping)))) args = keyword_mapping.keys() if start_time: query += " AND timestamp >= %s" args.append(mysql_utils.RDFDatetimeToMysqlString(start_time)) cursor.execute(query, args) for kw, cid in cursor.fetchall(): result[keyword_mapping[kw]].append(mysql_utils.IntToClientID(cid)) return result
def ReadAllClientIDs(self, cursor=None): """Reads client ids for all clients in the database.""" cursor.execute("SELECT client_id FROM clients") return [mysql_utils.IntToClientID(res[0]) for res in cursor.fetchall()]