File: tokufilecheck.py

package info (click to toggle)
mariadb-10.0 10.0.16-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 416,512 kB
  • sloc: cpp: 1,351,103; ansic: 803,086; perl: 59,621; pascal: 32,136; sh: 25,156; yacc: 14,897; xml: 5,194; sql: 4,651; cs: 4,647; makefile: 4,113; python: 2,526; ruby: 2,496; lex: 1,427; asm: 295; awk: 54; php: 22; sed: 16
file content (184 lines) | stat: -rwxr-xr-x 5,178 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python 

import sys
import time
import re
import MySQLdb
import os.path


# run as
# [tcallaghan@mork ~]$ tokufilecheck.py --socket=/tmp/tmc.sock --user=root --datadir=$DB_DIR/data/



def usage():
    print "check for filesystem issues"
    print "[--socket=SOCKET]"
    print "[--port=PORT]"
    print "[--user=USERNAME]"
    print "[--password=PASSWORD]"
    print "[--datadir=<full-path-to-mysql-data-directory>]"
    return 1


# check that each file in our file map exists in the file system
def check_file_map(rs, datadir):
    print "checking that all files in our file map exist in the file system"
    filesExisting = 0
    filesMissing = 0
    
    for t in rs:
        dictName = t[0]
        fileName = t[1]
        if os.path.isfile(os.path.join(datadir, fileName)):
            #print datadir + fileName + " exists"
            filesExisting += 1
        else:
            print "  *** " + datadir + fileName + " is missing"
            filesMissing += 1
    
    print "  %d file(s) found | %d file(s) missing." % (filesExisting, filesMissing)


# check if a .tokudb file exists in our file map
def lookup_tokudb(rs, checkFileName):
    fileExists = False
    
    for t in rs:
        if (t[1] == checkFileName):
            fileExists = True
            break
    
    return fileExists


# check that all .frm files exist
def check_frm(rs, datadir):
    print "checking that all .frm files exist"
    filesExisting = 0
    filesMissing = 0
    
    for t in rs:
        databaseName = t[0]
        tableName = t[1]
        storageEngine = t[2]
        if os.path.isfile(os.path.join(datadir + os.sep + databaseName, tableName + '.frm')):
            #print datadir + fileName + " exists"
            filesExisting += 1
        else:
            print "  *** missing FRM file for table " + tableName + " in database " + databaseName
            filesMissing += 1
    
    print "  %d file(s) found | %d file(s) missing." % (filesExisting, filesMissing)


# check if a .tokudb file exists in our file map
def lookup_frm(rs, tableName, dbName):
    frmExists = False
    
    for t in rs:
        if ((t[0] == dbName) and (t[1] == tableName)):
            frmExists = True
            break
    
    return frmExists


def main():
    host = "localhost"
    socket = None
    port = None
    user = None
    passwd = None
    datadir = "./"

    for a in sys.argv[1:]:
        if a == "-h" or a == "-?" or a == "--help":
            return usage()
        match = re.match("--(.*)=(.*)", a)
        if match:
            exec "%s='%s'" % (match.group(1),match.group(2))
            continue
        return usage()

    connect_parameters = {}
    if socket is not None:
        connect_parameters['unix_socket'] = socket
    if port is not None:
        connect_parameters['host'] = host
        connect_parameters['port'] = int(port)
    if user is not None:
        connect_parameters['user'] = user
    if passwd is not None:
        connect_parameters['passwd'] = passwd
        
    datadir = os.path.normpath(datadir) + os.sep

    try:
        db = MySQLdb.connect(**connect_parameters)
    except:
        print sys.exc_info()
        return 1

    print "connected to MySQL"

    print "retrieving file map"
    try:
        q = 'select * from information_schema.tokudb_file_map'
        c = db.cursor()
        n = c.execute(q)
        rsFileMap = c.fetchall()
        db.commit()
        c.close()
    except MySQLdb.Error, e:
        print "  ** Error %d: %s" % (e.args[0], e.args[1])
        return 2
    except:
        print "db", sys.exc_info()
        return 2

    print "retrieving full table listing"
    try:
        q = "select table_schema, table_name, engine from information_schema.tables where table_schema != 'information_schema'"
        c = db.cursor()
        n = c.execute(q)
        rsTables = c.fetchall()
        db.commit()
        c.close()
    except MySQLdb.Error, e:
        print "  ** Error %d: %s" % (e.args[0], e.args[1])
        return 2
    except:
        print "db", sys.exc_info()
        return 2

    check_file_map(rsFileMap, datadir);
    check_frm(rsTables, datadir);

    print "checking for extra .frm and/or .tokudb files"

    extraFrm = 0
    extraTokudb = 0

    for (path, dirs, files) in os.walk(datadir):
        for file in files:
            if (file.endswith('.frm')):
                tableName = os.path.splitext(file)[0]
                dbName = os.path.basename(os.path.normpath(path))
                if not lookup_frm(rsTables, tableName, dbName):
                    extraFrm += 1
                    print "found .frm file for tableName / dbName = %s / %s" % (tableName, dbName)
            if (file.endswith('.tokudb')):
                # check that it exists in rsFileMap
                checkFileName = "./" + file
                if not lookup_tokudb(rsFileMap, checkFileName):
                    extraTokudb += 1
                    print "  ** found extra .tokudb file %s" % (checkFileName)
                    
    print "  %d unmatched .frm file(s) found" % (extraFrm)
    print "  %d unmatched .tokudb file(s) found" % (extraTokudb)

    return 0

sys.exit(main())