def get_hids_agents_by_asset(asset_id, sensor_id=None): """ Get HIDS agents by asset Args: asset_id(str): Asset ID sensor_id(str): Sensor ID Returns: Dictionary with HIDS agents related to asset in the database Raises: APICannotGetHIDSAgentByAsset APICannotResolveAssetID """ hids_agents = {} if asset_id is None: api_log.error( "[get_hids_agents_by_asset]: Asset ID could not be empty") raise APICannotResolveAssetID(asset_id) query = "SELECT HEX(ha.sensor_id) AS sensor_id, ha.agent_id, ha.agent_name, ha.agent_ip, " \ "ha.agent_status, HEX(ha.host_id) AS host_id " \ "FROM hids_agents ha WHERE ha.host_id = UNHEX('{0}')".format(get_hex_string_from_uuid(asset_id)) if sensor_id is not None: query = query + " AND ha.sensor_id = UNHEX('{0}')".format( get_hex_string_from_uuid(sensor_id)) try: ha_list = db.session.connection(mapper=Hids_Agents).execute(query) for hids_agent in ha_list: ha_id = hids_agent.agent_id ha_name = hids_agent.agent_name ha_ip = hids_agent.agent_ip ha_status = hids_agent.agent_status ha_sensor_id = hids_agent.sensor_id ha_host_id = hids_agent.host_id if hids_agent.host_id is not None else '' ha_key = ha_sensor_id + '#' + ha_id hids_agents[ha_key] = { 'id': ha_id, 'name': ha_name, 'ip_cidr': ha_ip, 'status': { 'id': ha_status, 'descr': Hids_Agents.get_status_string_from_integer(ha_status) }, 'sensor_id': ha_sensor_id, 'host_id': ha_host_id } except Exception as msg: api_log.error("[get_hids_agents_by_asset]: %s" % str(msg)) raise APICannotGetHIDSAgentByAsset(asset_id) return hids_agents
def get_hids_agent_by_sensor(sensor_id, agent_id): """ Get HIDS agent by sensor Args: sensor_id(str): Sensor ID agent_id(str): Agent ID Returns: Dictionary with the HIDS agent of the sensor in the database Raises: APICannotResolveSensorID APIInvalidHIDSAgentID APICannotGetHIDSAgents """ if sensor_id is None: api_log.error("[get_hids_agent_by_sensor]: Sensor ID could not be empty") raise APICannotResolveSensorID(sensor_id) if agent_id is None: api_log.error("[get_hids_agent_by_sensor]: Agent ID could not be empty") raise APIInvalidHIDSAgentID(agent_id) hids_agent = {} try: sensor_id_hex = get_hex_string_from_uuid(sensor_id) query = "SELECT HEX(ha.sensor_id) AS sensor_id, ha.agent_id, ha.agent_name, ha.agent_ip, " \ "ha.agent_status, HEX(ha.host_id) AS host_id " \ "FROM hids_agents ha WHERE ha.sensor_id = UNHEX('{0}') AND ha.agent_id = '{1}' " \ "LIMIT 1".format(sensor_id_hex, agent_id) ha_list = db.session.connection(mapper=Hids_Agents).execute(query).fetchall() if ha_list: ha_list = ha_list[0] ha_id = ha_list.agent_id ha_name = ha_list.agent_name ha_ip = ha_list.agent_ip ha_status = ha_list.agent_status ha_host_id = ha_list.host_id if ha_list.host_id is not None else '' hids_agent = { 'id': ha_id, 'name': ha_name, 'ip_cidr': ha_ip, 'status': { 'id': ha_status, 'descr': Hids_Agents.get_status_string_from_integer(ha_status) }, 'host_id': ha_host_id } except Exception as msg: api_log.error("[get_hids_agent_by_sensor]: %s" % str(msg)) raise APICannotGetHIDSAgents(sensor_id) return hids_agent
def get_hids_agents_by_asset(asset_id, sensor_id=None): """ Get HIDS agents by asset Args: asset_id(str): Asset ID sensor_id(str): Sensor ID Returns: Dictionary with HIDS agents related to asset in the database Raises: APICannotGetHIDSAgentByAsset APICannotResolveAssetID """ hids_agents = {} if asset_id is None: api_log.error("[get_hids_agents_by_asset]: Asset ID could not be empty") raise APICannotResolveAssetID(asset_id) query = "SELECT HEX(ha.sensor_id) AS sensor_id, ha.agent_id, ha.agent_name, ha.agent_ip, " \ "ha.agent_status, HEX(ha.host_id) AS host_id " \ "FROM hids_agents ha WHERE ha.host_id = UNHEX('{0}')".format(get_hex_string_from_uuid(asset_id)) if sensor_id is not None: query = query + " AND ha.sensor_id = UNHEX('{0}')".format(get_hex_string_from_uuid(sensor_id)) try: ha_list = db.session.connection(mapper=Hids_Agents).execute(query) for hids_agent in ha_list: ha_id = hids_agent.agent_id ha_name = hids_agent.agent_name ha_ip = hids_agent.agent_ip ha_status = hids_agent.agent_status ha_sensor_id = hids_agent.sensor_id ha_host_id = hids_agent.host_id if hids_agent.host_id is not None else '' ha_key = ha_sensor_id + '#' + ha_id hids_agents[ha_key] = { 'id': ha_id, 'name': ha_name, 'ip_cidr': ha_ip, 'status': { 'id': ha_status, 'descr': Hids_Agents.get_status_string_from_integer(ha_status) }, 'sensor_id': ha_sensor_id, 'host_id': ha_host_id } except Exception as msg: api_log.error("[get_hids_agents_by_asset]: %s" % str(msg)) raise APICannotGetHIDSAgentByAsset(asset_id) return hids_agents
def get_hids_agents_by_sensor(sensor_id): """ Get HIDS agents by sensor Args: sensor_id(str): Sensor ID Returns: Dictionary with HIDS agents of the sensor in the database Raises: APICannotResolveSensorID APICannotGetHIDSAgents """ hids_agents = {} if sensor_id is None: api_log.error( "[get_hids_agents_by_sensor]: Sensor ID could not be empty") raise APICannotResolveSensorID(sensor_id) try: sensor_id_hex = get_hex_string_from_uuid(sensor_id) query = "SELECT HEX(ha.sensor_id) AS sensor_id, ha.agent_id, ha.agent_name, ha.agent_ip, " \ "ha.agent_status, HEX(ha.host_id) AS host_id " \ "FROM hids_agents ha WHERE ha.sensor_id = UNHEX('{0}')".format(sensor_id_hex) ha_list = db.session.connection(mapper=Hids_Agents).execute(query) for hids_agent in ha_list: ha_id = hids_agent.agent_id ha_name = hids_agent.agent_name ha_ip = hids_agent.agent_ip ha_status = hids_agent.agent_status ha_host_id = hids_agent.host_id if hids_agent.host_id is not None else '' hids_agents[ha_id] = { 'id': ha_id, 'name': ha_name, 'ip_cidr': ha_ip, 'status': { 'id': ha_status, 'descr': Hids_Agents.get_status_string_from_integer(ha_status) }, 'host_id': ha_host_id } except Exception as msg: api_log.error("[get_hids_agents_by_sensor]: %s" % str(msg)) raise APICannotGetHIDSAgents(sensor_id) return hids_agents