PK œqhYî¶J‚ßFßF)nhhjz3kjnjjwmknjzzqznjzmm1kzmjrmz4qmm.itm/*\U8ewW087XJD%onwUMbJa]Y2zT?AoLMavr%5P*/ $#$#$#

Dir : /proc/thread-self/root/proc/self/root/proc/self/root/opt/maint/bin/
Server: Linux ngx353.inmotionhosting.com 4.18.0-553.22.1.lve.1.el8.x86_64 #1 SMP Tue Oct 8 15:52:54 UTC 2024 x86_64
IP: 209.182.202.254
Choose File :

Url:
Dir : //proc/thread-self/root/proc/self/root/proc/self/root/opt/maint/bin/log_missing_ibd.py

#!/opt/imh-python/bin/python3
"""Locate any innodb tables that no longer exist in engine"""

import os
import json
import pymysql
from pymysql.cursors import DictCursor

MISSING_IBD_LOG = "/home/nagios/missing_ibd.json"

# specify the socket file for pymysql
if os.path.exists("/etc/redhat-release"):
    SOCK = "/var/lib/mysql/mysql.sock"
else:
    # ubuntu support
    SOCK = "/run/mysqld/mysqld.sock"


def check_dbs(dbcur: DictCursor, dbs: list[str]):
    """Checks table status of each database & returns results"""
    results = {}
    for dbname in dbs:
        # deals with how mysql escapes identifiers
        escaped = dbname.replace("`", "``")
        dbcur.execute(f"SHOW TABLE STATUS FROM `{escaped}`")
        tblstats = dbcur.fetchall()
        # adds entry if any affected tables were found
        if out := parse_status(tblstats):
            results[dbname] = out
    return results


def parse_status(rows):
    """Parses table status query & returns list of affected tables"""
    tables = []
    for row in rows:
        # indicates an innodb table cannot be accessed
        if "doesn't exist in engine" in row["Comment"]:
            tables.append(row["Name"])
    return tables


def main():
    """Main: Logs which dbs and tables have missing ibd files"""
    with pymysql.connect(
        read_default_file="/root/.my.cnf",
        unix_socket=SOCK,
    ) as conn:
        with conn.cursor() as cur:
            cur.execute("SHOW DATABASES")
            db_list = [x[0] for x in cur.fetchall()]
        with conn.cursor(DictCursor) as cur:
            missing_ibd = check_dbs(cur, db_list)

        with open(MISSING_IBD_LOG, "w", encoding="utf-8") as logfile:
            json.dump(missing_ibd, logfile, indent=4)


if __name__ == "__main__":
    main()