Skip to content

Commit b42ced3

Browse files
Print permission mySQL warning (#20090)
* print permission warning and small improvements * changelog * apply linter
1 parent 76c4247 commit b42ced3

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

mysql/changelog.d/20090.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Print permission mySQL warning

mysql/datadog_checks/mysql/databases_data.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def __init__(self, submit_data_function, base_event, logger):
4343
self._total_columns_sent = 0
4444
self.db_to_tables = {} # dbname : {"tables" : []}
4545
self.db_info = {} # name to info
46+
self.any_tables_found = False # Flag to track for permission issues
4647

4748
def set_base_event_data(self, hostname, tags, cloud_metadata, dbms_version, flavor):
4849
self._base_event["host"] = hostname
@@ -55,6 +56,7 @@ def reset(self):
5556
self._total_columns_sent = 0
5657
self._columns_count = 0
5758
self.db_info.clear()
59+
self.any_tables_found = False
5860

5961
def store_db_infos(self, db_infos):
6062
for db_info in db_infos:
@@ -64,6 +66,8 @@ def store(self, db_name, tables, columns_count):
6466
self._columns_count += columns_count
6567
known_tables = self.db_to_tables.setdefault(db_name, [])
6668
known_tables.extend(tables)
69+
if tables:
70+
self.any_tables_found = True
6771

6872
def columns_since_last_submit(self):
6973
return self._columns_count
@@ -282,6 +286,16 @@ def _fetch_for_databases(self, db_infos, cursor):
282286
)
283287
)
284288

289+
# Check if we found databases but no tables across all of them.
290+
# This happens when the datadog user has permissions to see databases
291+
# but lacks SELECT privileges on the tables themselves, which prevents
292+
# the agent from collecting table metadata.
293+
if db_infos and not self._data_submitter.any_tables_found:
294+
self._log.warning(
295+
"No tables were found across any of the {} databases. This may indicate insufficient privileges "
296+
"to view table metadata. The datadog user needs SELECT privileges on the tables.".format(len(db_infos))
297+
)
298+
285299
@tracked_method(agent_check_getter=agent_check_getter)
286300
def _query_db_information(self, cursor):
287301
self._cursor_run(cursor, query=SQL_DATABASES)
@@ -302,13 +316,12 @@ def _get_tables(self, db_name, cursor):
302316
def _get_tables_data(self, table_list, db_name, cursor):
303317

304318
if len(table_list) == 0:
305-
return
319+
return 0, []
320+
306321
table_name_to_table_index = {}
307-
table_names = ""
308322
for i, table in enumerate(table_list):
309323
table_name_to_table_index[table["name"]] = i
310-
table_names += '"' + str(table["name"]) + '",'
311-
table_names = table_names[:-1]
324+
table_names = ','.join(f'"{str(table["name"])}"' for table in table_list)
312325
total_columns_number = self._populate_with_columns_data(
313326
table_name_to_table_index, table_list, table_names, db_name, cursor
314327
)

0 commit comments

Comments
 (0)