/
dex
executable file
·144 lines (129 loc) · 5.65 KB
/
dex
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
#!/usr/bin/env python
################################################################################
#
# Copyright (c) 2012 ObjectLabs Corporation
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
################################################################################
import dargparse
import sys
import traceback
import datetime
from dex import dex
from dargparse import dargparse
from datetime import datetime
PARSER_DEFINITION ={
"prog": "dex",
"description" : "Scans a provided MongoDB log file and uses the provided URI to compare queries found in the logfile to the indexes available in the database, recommending indexes for those queries which are not indexed. Recommended for MongoDB version 2.0.4 or later.",
"args": [
{
"name": "uri",
"type" : "positional",
"help": "database connection URI, including database path, and authentication if required. If authentication is is enabled and multiple databases need to be dexed, this URI should point to the admin database.",
"nargs": 1
},
{
"name": "logfile_path",
"type" : "optional",
"help": "path to a MongoDB log file. If provided, the file will be searched for queries.",
"cmd_arg": [
"-f",
"--file"
],
"nargs": 1,
"default": None
},
{
"name": "use_profile",
"type" : "optional",
"help": "flag to examine the MongoDB system.profile collection. If set, the profile collection will be searched for queries. NOT YET SUPPORTED.",
"cmd_arg": [
"-p",
"--profile"
],
"action": "store_true",
"nargs": 0,
"default": False
},
{
"name": "namespaces",
"type" : "optional",
"help": "a MongoDB namespace (db.collection). Can be provided multiple times. This option creates a filter, and queries not in the provided namespace(s) will not be analyzed. Format: -n ('db.collection' | '*' | 'db.*' | 'collection'). '*.*' and '*.collection' are redundant but also supported. An asterisk is shorthand for 'all'--actual regexes are not supported. Note that -n '*' is equivalent to not providing a -n argument.",
"cmd_arg": [
"-n",
"--namespace"
],
"nargs": 1,
"action": "append",
"default": []
},
{
"name": "verbose",
"type" : "optional",
"help": "enables provision of additional output information.",
"cmd_arg": [
"-v",
"--verbose"
],
"action": "store_true",
"nargs": 0,
"default": False
}
]
}
###############################################################################
# MAIN
###############################################################################
def main(args):
# build the parse object
darg_parser = dargparse.build_parser(PARSER_DEFINITION)
# parse the command line
options = darg_parser.parse_args(args)
namespaces = options.namespaces
md = dex.Dex()
if options.use_profile:
return md.analyze_profile(options.uri,
namespaces,
options.verbose)
if options.logfile_path is None:
print 'Error: Logifle option (-f/--logfile) is currently required.'
return 1
else:
return md.analyze_logfile(options.uri,
options.logfile_path,
namespaces,
options.verbose)
###############################################################################
######################## ####################################
######################## BOOTSTRAP ####################################
######################## ####################################
###############################################################################
if __name__ == '__main__':
try:
# call main with a sub-list starting skipping the
# command itself (which is the first arg)
main(sys.argv[1:])
except (SystemExit, KeyboardInterrupt) , e:
if e.code == 0:
pass
else:
raise
except:
traceback.print_exc()